Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions lib/data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[
{
"text":"The only people who never fail are those who never try.",
"author":"Ilka Chase"
},
{
"text":"Failure is just another way to learn how to do something right.",
"author":"Marian Wright Edelman"
},
{
"text":"I failed my way to success.",
"author":"Thomas Edison"
}
]
54 changes: 27 additions & 27 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
/**
*
* @author Vinit Shahdeo <https://github.com/vinitshahdeo>
* Library entry for inspirational-quotes
* Contributed by Basuki
* Original author: Vinit Shahdeo <https://github.com/vinitshahdeo>
*/
const arr = require('../data/data.json'),
const arr = require('../data/data.json');

// utility to generate a random number
randomInt = (min, max) => Math.floor(Math.random() * (max - min + 1) + min),
index = randomInt(0, arr.length), // random index to be used

/**
*
* Kept `getRandomQuote` method to ensure the backward compatibility as
* All the version <= `v1.0.8` has this method
*
* TO BE DEPRECATED SOON
*/
getRandomQuote = () => arr[index].text,
// utility to generate a random integer between min and max (inclusive)
const randomInt = (min, max) => Math.floor(Math.random() * (max - min + 1) + min);

/**
*
* By default, { author: true },
* To hide author, pass options as { author: false }
*/
getQuote = (options) => {
!options && (options = {author: true}); // default option
return {
text: arr[index].text,
...(options && options.author ? { author: arr[index].from } : {})
};
};
/**
* Kept `getRandomQuote` method to ensure backward compatibility.
* Returns the text of a random quote.
* NOTE: choose a new random index on each call (previous behaviour chose it once at module load).
*/
function getRandomQuote() {
const index = randomInt(0, arr.length - 1);
return arr[index].text;
}

/**
* getQuote(options)
* By default returns both text and author. To hide author, pass { author: false }.
*/
function getQuote(options) {
if (!options) options = { author: true };
const index = randomInt(0, arr.length - 1);
const quote = { text: arr[index].text };
if (options && options.author) quote.author = arr[index].from;
return quote;
}

module.exports = {
getRandomQuote,
Expand Down