Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding possibility to use more animals #3

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
2 changes: 1 addition & 1 deletion bin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
6 changes: 6 additions & 0 deletions definitions/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export const DEFAULT_OPTIONS = {
separator: "",
adjectives: 2,
animals: 1,
};

43 changes: 23 additions & 20 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -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];
Expand All @@ -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);
}
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")
);
}
20 changes: 20 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
})
);
}
);
});
});
});
});