Allow custom format for random values #3

Open
Ghost wants to merge 2 commits from (deleted):master into master
1 changed files with 20 additions and 1 deletions
Showing only changes of commit 14bb0026e0 - Show all commits

View File

@ -23,7 +23,7 @@
return shuffle(numbers); return shuffle(numbers);
} }
function random() { function populate() {
if (!adjectives.length) { if (!adjectives.length) {
adjectives = shuffle(lists.adjectives.slice(0)); adjectives = shuffle(lists.adjectives.slice(0));
} }
@ -33,13 +33,32 @@
if (!numbers.length) { if (!numbers.length) {
numbers = shuffle(genNumbers()); numbers = shuffle(genNumbers());
} }
}
function random() {
populate();
return adjectives.pop() return adjectives.pop()
+ '-' + animals.pop() + '-' + animals.pop()
+ '-' + numbers.pop() + '-' + numbers.pop()
; ;
} }
// Accepts a format as a stirng of 'a' for adjective, 'n' for noun, and '#' for number
function get(format) {
populate();
hri = new Array();
for (var i = 0; i < format.length; i++) {
switch (format[i]) {
case 'a': hri.push(adjectives.pop()); break;
case 'n': hri.push(animals.pop()); break;
case '#': hri.push(adjectives.pop()); break;
default:
throw 'Unexpected value ' + format[i] + '. Expected a,n,#';
}
return hri.join('-');
}
exports.humanReadableIds = { random: random }; exports.humanReadableIds = { random: random };
exports.hri = exports.humanReadableIds; exports.hri = exports.humanReadableIds;