From 7e4207a165ff96f0bc9178209096495574d71b6f Mon Sep 17 00:00:00 2001 From: Lucasmick1 Date: Mon, 12 Jun 2023 16:54:36 -0300 Subject: [PATCH 1/3] Adding possibility to use more animals --- bin/index.js | 2 +- definitions/constants.js | 6 ++++++ src/index.js | 45 ++++++++++++++++++++++------------------ test/index.js | 20 ++++++++++++++++++ 4 files changed, 52 insertions(+), 21 deletions(-) create mode 100644 definitions/constants.js 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..97eedfd 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,30 @@ 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 safeLength = (length, max) => { + if (length <= 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 (length >= max) return max; - const pieces = [...randomAdjectives, ...randomAnimals]; + return length; +}; - 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, safeLength(options.animals, ANIMALS.length)) + .map((animal) => { + return [ + ...getRandomArraySlice( + ADJECTIVES, + safeLength(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 From f8caa17fc6949e3ff375564240bfa14fa2f6c7b2 Mon Sep 17 00:00:00 2001 From: Lucasmick1 Date: Mon, 12 Jun 2023 17:46:25 -0300 Subject: [PATCH 2/3] Using implicit return --- src/index.js | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/index.js b/src/index.js index 97eedfd..7208fe3 100644 --- a/src/index.js +++ b/src/index.js @@ -31,15 +31,13 @@ export default function gfynonce(options = {}) { return ( getRandomArraySlice(ANIMALS, safeLength(options.animals, ANIMALS.length)) - .map((animal) => { - return [ + .map((animal) => [ ...getRandomArraySlice( ADJECTIVES, safeLength(options.adjectives, ADJECTIVES.length) ), animal, - ].join(options.separator); - }) + ].join(options.separator)) // TODO - maybe a custom phrase separator .join("\n") ); From 5bf38c14c75af91a9776d79f2ce62cb106eb2b54 Mon Sep 17 00:00:00 2001 From: Lucasmick1 Date: Mon, 12 Jun 2023 17:48:30 -0300 Subject: [PATCH 3/3] Renaming function --- src/index.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/index.js b/src/index.js index 7208fe3..1b73b56 100644 --- a/src/index.js +++ b/src/index.js @@ -18,23 +18,23 @@ const getRandomArraySliceStart = (array, size) => Math.max(0, randomIndex(array) const getRandomArraySlice = (array, size, start = getRandomArraySliceStart(array, size)) => shuffleArray(array).slice(start, start + size); // set some ground rules to prevent it from breaking -const safeLength = (length, max) => { - if (length <= 0) return 1; +const safeSize = (size, max) => { + if (size <= 0) return 1; - if (length >= max) return max; + if (size >= max) return max; - return length; + return size; }; export default function gfynonce(options = {}) { options = Object.assign({}, DEFAULT_OPTIONS, options); return ( - getRandomArraySlice(ANIMALS, safeLength(options.animals, ANIMALS.length)) + getRandomArraySlice(ANIMALS, safeSize(options.animals, ANIMALS.length)) .map((animal) => [ ...getRandomArraySlice( ADJECTIVES, - safeLength(options.adjectives, ADJECTIVES.length) + safeSize(options.adjectives, ADJECTIVES.length) ), animal, ].join(options.separator))