From 14bb0026e06ebc150093d6dfd6bab8b44960fcb8 Mon Sep 17 00:00:00 2001 From: mikhail Date: Wed, 30 Dec 2020 01:00:51 +0000 Subject: [PATCH 1/2] 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; -- 2.38.5 From 9e447b2a119c269f36beffcc6af1635122f37682 Mon Sep 17 00:00:00 2001 From: Mikhail Simin Date: Sat, 2 Jan 2021 09:13:54 -0800 Subject: [PATCH 2/2] Add docs --- README.md | 3 +++ index.js | 27 +++++++++++++++++---------- tests/run-in-node.js | 4 ++++ 3 files changed, 24 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index e3f58a3..4e14e0a 100644 --- a/README.md +++ b/README.md @@ -55,6 +55,9 @@ var i; for (i = 0; i < 100; i += 1) { console.log(hri.random()); } + +// Use custom pattern of number-noun-adjective +console.log(hri.get('#na')); ``` ### bower / browser diff --git a/index.js b/index.js index 2d7ced0..4595a64 100644 --- a/index.js +++ b/index.js @@ -7,11 +7,11 @@ , animals = [] , adjectives = [] , numbers = [] - ; + ; function genNumbers() { var i = 2 - ; + ; numbers = []; numbers.push(0); @@ -46,20 +46,27 @@ // 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; + let hri = []; + for (let item of format) { + switch (item) { + case 'a': + hri.push(adjectives.pop()); + break; + case 'n': + hri.push(animals.pop()); + break; + case '#': + hri.push(numbers.pop()); + break; default: - throw 'Unexpected value ' + format[i] + '. Expected a,n,#'; + throw 'Unexpected value ' + item + '. Expected a,n,#'; } + } return hri.join('-'); } - exports.humanReadableIds = { random: random }; + exports.humanReadableIds = {random: random, get: get}; exports.hri = exports.humanReadableIds; }('undefined' !== typeof exports && exports || new Function('return this')())); diff --git a/tests/run-in-node.js b/tests/run-in-node.js index a8eb4c1..8ef6063 100644 --- a/tests/run-in-node.js +++ b/tests/run-in-node.js @@ -9,4 +9,8 @@ for (i = 0; i < 100; i += 1) { console.log(hri.random()); } + + for (i = 0; i < 10; i += 1) { + console.log(hri.get('#na')); + } }('undefined' !== typeof exports && exports || new Function('return this')())); -- 2.38.5