diff --git a/bin/index.js b/bin/index.js index 291880f..100cc13 100644 --- a/bin/index.js +++ b/bin/index.js @@ -7,6 +7,6 @@ var minimist = require('minimist'); var argv = minimist(process.argv.slice(2)); -var options = Object.assign({}, { separator: '', adjectives: 2 }, argv); +var options = Object.assign({}, { separator: '', adjectives: 2, animals: 1 }, argv); console.log(gfynonce(options)); diff --git a/definitions/constants.js b/definitions/constants.js new file mode 100644 index 0000000..784ef0d --- /dev/null +++ b/definitions/constants.js @@ -0,0 +1,6 @@ +export const DEFAULT_OPTIONS = { + separator: "", + adjectives: 2, + animals: 1, + }; + \ No newline at end of file diff --git a/src/index.js b/src/index.js index 5e2022a..1b73b56 100644 --- a/src/index.js +++ b/src/index.js @@ -1,5 +1,6 @@ import ANIMALS from '../data/animals.json'; import ADJECTIVES from '../data/adjectives.json'; +import { DEFAULT_OPTIONS } from '../definitions/constants'; function shuffleArray(arr) { let array = [...arr]; @@ -16,26 +17,28 @@ const randomIndex = array => Math.floor(Math.random() * array.length); const getRandomArraySliceStart = (array, size) => Math.max(0, randomIndex(array) - size); const getRandomArraySlice = (array, size, start = getRandomArraySliceStart(array, size)) => shuffleArray(array).slice(start, start + size); -const defaultOptions = { - separator: '', - adjectives: 2, -}; - -export default function gfynonce(options = {}) { - options = Object.assign({}, defaultOptions, options); - - // set some ground rules to prevent it from breaking - if (options.adjectives <= 0) { - options.adjectives = 1; - } else if (options.adjectives >= ADJECTIVES.length) { - options.adjectives = ADJECTIVES.length; - } +// set some ground rules to prevent it from breaking +const safeSize = (size, max) => { + if (size <= 0) return 1; - const randomAdjectives = getRandomArraySlice(ADJECTIVES, options.adjectives); - // TO-DO: More complicated tests are required to allow more than one animal - const randomAnimals = getRandomArraySlice(ANIMALS, 1); + if (size >= max) return max; - const pieces = [...randomAdjectives, ...randomAnimals]; + return size; +}; - return pieces.join(options.separator); -} \ No newline at end of file +export default function gfynonce(options = {}) { + options = Object.assign({}, DEFAULT_OPTIONS, options); + + return ( + getRandomArraySlice(ANIMALS, safeSize(options.animals, ANIMALS.length)) + .map((animal) => [ + ...getRandomArraySlice( + ADJECTIVES, + safeSize(options.adjectives, ADJECTIVES.length) + ), + animal, + ].join(options.separator)) + // TODO - maybe a custom phrase separator + .join("\n") + ); +} diff --git a/test/index.js b/test/index.js index c9afe76..b9a3f0c 100644 --- a/test/index.js +++ b/test/index.js @@ -142,6 +142,26 @@ describe('gfynonce', () => { return false; }); }); + it("should generate a nonce with default adjectives and 2 animals", () => { + const adjectives = 2; + const animals = 2; + const separator = "."; + + expect(gfynonce({ animals, separator, adjectives })).to.satisfy( + (result) => { + const phrases = result.split("\n"); + + return ( + phrases.length === 2 && + phrases.every((phrase) => { + const pieces = phrase.split(separator).slice(0, adjectives); + + return uniq(pieces).length === adjectives; + }) + ); + } + ); + }); }); }); }); \ No newline at end of file