From 14bb0026e06ebc150093d6dfd6bab8b44960fcb8 Mon Sep 17 00:00:00 2001 From: mikhail Date: Wed, 30 Dec 2020 01:00:51 +0000 Subject: [PATCH] Update 'index.js' --- index.js | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index ab07355..2d7ced0 100644 --- a/index.js +++ b/index.js @@ -23,7 +23,7 @@ return shuffle(numbers); } - function random() { + function populate() { if (!adjectives.length) { adjectives = shuffle(lists.adjectives.slice(0)); } @@ -33,13 +33,32 @@ if (!numbers.length) { numbers = shuffle(genNumbers()); } + } + function random() { + populate(); return adjectives.pop() + '-' + animals.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.hri = exports.humanReadableIds;