Skip to content

Commit

Permalink
fix: direct access for helpers, better instrumentation
Browse files Browse the repository at this point in the history
  • Loading branch information
Tobiah committed Oct 31, 2019
1 parent acf9f86 commit 5b36588
Show file tree
Hide file tree
Showing 4 changed files with 545 additions and 164 deletions.
94 changes: 65 additions & 29 deletions lib/Generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,27 +46,22 @@ const getRandomEntry = (array) => array[Math.floor(Math.random() * array.length)

const toTitleCase = (string, includeSpaces) => string.toLowerCase()
.split(' ')
.map((word) => word.replace(word[0], word[0].toUpperCase())).join(includeSpaces ? ' ' : '');
.map((word) => word.replace(word[0], word[0].toUpperCase()))
.join(includeSpaces ? ' ' : '');


/**
* Get a unique noun.
* @param {string[]} tokens array of existing tokens
* @param {string} [race='random'] race type to use
* @param {string} [type='random'] noun type to use
* @param {boolean} [spaces=true}] whether or not to include spaces
* @returns {string} the unique noun result
*/
const getNoun = (tokens, {
race = 'random', type = 'random', spaces = true, iterations = 0,
}) => {
const entry = toTitleCase(getRandomEntry(data[race][type]), spaces);
if (tokens.includes(entry) && (iterations < data[race][type].length)) {
return getNoun(tokens, {
race, type, spaces, iterations: iterations + 1,
});
const evalRace = (race) => {
const r = race.toLowerCase() === 'random' ? races[Math.floor(Math.random() * races.length)] : race;
if (!races.includes(r.toLowerCase())) {
throw new Error(`Invalid race '${r}' provided.\nValid races: ${races.map((innRace) => `'${innRace}'`).join(', ')}`);
}
return entry;
return r;
};
const evalType = (type) => {
const t = type.toLowerCase() === 'random' ? types[Math.floor(Math.random() * types.length)] : type;
if (!types.includes(t.toLowerCase())) {
throw new Error(`Invalid type '${t}' provided.\nValid types: ${types.map((iType) => `'${iType}'`).join(', ')}`);
}
return t;
};

/**
Expand Down Expand Up @@ -95,26 +90,67 @@ class Generator {
make({
race = 'random', nouns = 1, type = 'random', spaces = true, includeRace = false, adjective = false, debug = false,
} = { race: 'random', nouns: 1, type: 'random' }) {
const raceRes = race.toLowerCase() === 'random' ? races[Math.floor(Math.random() * races.length)] : race;
if (!races.includes(raceRes.toLowerCase())) {
throw new Error(`Invalid race '${type}' provided.\nValid races: ${races.map((innRace) => `'${innRace}'`).join(', ')}`);
}

const typeRes = type.toLowerCase() === 'random' ? types[Math.floor(Math.random() * types.length)] : type;
if (!types.includes(typeRes.toLowerCase())) {
throw new Error(`Invalid type '${typeRes}' provided.\nValid types: ${types.map((iType) => `'${iType}'`).join(', ')}`);
}
const raceRes = evalRace(race);
const typeRes = evalType(type);

const tokens = [];
if (adjective) tokens.push(toTitleCase(getRandomEntry(data[raceRes].adjectives), spaces));
if (includeRace) tokens.push(toTitleCase(raceRes, spaces));
for (let index = 0; index < nouns; index += 1) {
tokens.push(getNoun(tokens, { race: raceRes, type: typeRes, spaces }));
tokens.push(this.getNoun(tokens, { race: raceRes, type: typeRes, spaces }));
}
const name = tokens.join(spaces ? ' ' : '');
if (debug) this.logger.debug(name);
return name;
}

/**
* Get a random entry
* @returns {string} a random entry`
*/
get random() {
return this.getNoun();
}

/**
* Get another string that's not already included in the tokens
* @param {string[]} tokens tokens to exclude
* @returns {string} new unique token
*/
another(tokens) {
return this.getNoun(tokens);
}

/**
* Get a unique noun.
* @param {string[]} tokens array of existing tokens
* @param {string} [race='random'] race type to use
* @param {string} [type='random'] noun type to use
* @param {boolean} [spaces=true}] whether or not to include spaces
* @returns {string} the unique noun result
*/
// eslint-disable-next-line class-methods-use-this
getNoun(tokens = [], {
race = 'random',
type = 'random',
spaces = true,
iterations = 0,
} = {
race: 'random',
type: 'random',
spaces: true,
iterations: 0,
}) {
const raceR = evalRace(race);
const typeR = evalType(type);
const entry = toTitleCase(getRandomEntry(data[raceR][typeR]), spaces);
if (tokens.includes(entry) && (iterations < data[raceR][typeR].length)) {
return this.getNoun(tokens, {
race, type, spaces, iterations: iterations + 1,
});
}
return entry;
}
}

module.exports = Generator;
Loading

0 comments on commit 5b36588

Please sign in to comment.