Skip to content

Commit

Permalink
Refactor with more idiomatic JavaScript options function parameter,…
Browse files Browse the repository at this point in the history
… rather than individual parameters.

That this will facilitate future extensions which anticipate a much greater option set.
  • Loading branch information
stemail23 committed Oct 25, 2018
1 parent 6e33d7f commit 8e59558
Showing 1 changed file with 25 additions and 21 deletions.
46 changes: 25 additions & 21 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,25 @@ const adjectiveLengths = findLengths(adjectives);
nouns.sort((a, b) => a.length === b.length ? a.localeCompare(b) : a.length - b.length);
const nounLengths = findLengths(nouns);

function codenameParticles(seed, adjectiveCount, maxItemChars, hashAlgorithm) {
// Minimum length of 3 is required
maxItemChars = maxItemChars > 0 ? Math.max(3, maxItemChars) : 0;
if (maxItemChars > 9) { maxItemChars = 0; }
function codenameParticles(options) {
if (!options || typeof options !== 'object') {
options = {};
}
options.maxItemChars = options.maxItemChars > 0 ? Math.max(3, options.maxItemChars) : 0;
if (options.maxItemChars > 9) { options.maxItemChars = 0; }
options.adjectiveCount = options.adjectiveCount || 1;
options.seed = (options.seed || '').toString();
options.hashAlgorithm = options.hashAlgorithm || 'md5';

// Prepare codename word lists and calculate size of codename space
const useNouns = maxItemChars > 0 ? nouns.slice(0, nounLengths[maxItemChars]) : nouns;
const useAdjectives = maxItemChars > 0 ? adjectives.slice(0, adjectiveLengths[maxItemChars]) : adjectives;
// Prepare codename word lists and calculate size of codename space
const useNouns = options.maxItemChars > 0 ? nouns.slice(0, nounLengths[options.maxItemChars]) : nouns;
const useAdjectives = options.maxItemChars > 0 ? adjectives.slice(0, adjectiveLengths[options.maxItemChars]) : adjectives;
const particles = [useNouns];
for (let i = 0; i < adjectiveCount; i++) { particles.push(useAdjectives); }

const totalWords = bigInt(useAdjectives.length).pow(adjectiveCount).multiply(bigInt(useNouns.length));

// Convert seed to string
seed = (seed || '').toString();
hashAlgorithm = hashAlgorithm || 'md5';
for (let i = 0; i < options.adjectiveCount; i++) { particles.push(useAdjectives); }
const totalWords = bigInt(useAdjectives.length).pow(options.adjectiveCount).multiply(bigInt(useNouns.length));

const hash = crypto.createHash(hashAlgorithm);
hash.update(seed);
const hash = crypto.createHash(options.hashAlgorithm);
hash.update(options.seed);
const hashDigest = hash.digest('hex');
const objHash = bigInt(hashDigest, 16).multiply('36413321723440003717');

Expand All @@ -55,15 +55,19 @@ function codenameParticles(seed, adjectiveCount, maxItemChars, hashAlgorithm) {
return codenameParticles;
}

function codenamize(seed, adjectiveCount, maxItemChars, separator, capitalize, hashAlgorithm) {
let particles = codenameParticles(seed, adjectiveCount, maxItemChars, hashAlgorithm);
function codenamize(options) {
if (!options || typeof options !== 'object') {
options = {};
}
options.separator = options.separator || '-';

let particles = codenameParticles(options);

separator = separator || '';
if (capitalize) {
if (options.capitalize) {
particles = particles.map(particle => particle.charAt(0).toUpperCase() + particle.substring(1));
}

return particles.join(separator);
return particles.join(options.separator);
}

module.exports = codenamize;

0 comments on commit 8e59558

Please sign in to comment.