Comparing version 1.3.0 to 1.4.0
110
index.js
@@ -10,2 +10,4 @@ let fs = require('fs'); | ||
let genderValues = new Set(Object.values(Genders)); | ||
let OutputFormat = { | ||
@@ -82,2 +84,21 @@ Set: "set", | ||
} | ||
/** | ||
Key set must be non-empty | ||
If have 'a' key, then 'a' must be single array of space | ||
*/ | ||
static validateKeys (spacename, keys) { | ||
if (keys.length === 0) { | ||
return `Bad amount of keys for space "${spacename}". Supported keys are [${Object.values(Genders)}]`; | ||
} | ||
for (let key of keys) { | ||
if (!genderValues.has(key)) { | ||
return `Bad key "${key}" found in space "${spacename}". Supported keys are [${Object.values(Genders)}]`; | ||
} | ||
if (key === 'a' && keys.length > 1) { | ||
return `Wrong keys found in space "${spacename}". Key "a" must be single key of the space`; | ||
} | ||
} | ||
return null; | ||
} | ||
@@ -90,4 +111,25 @@ upload (filename, { shuffle = true } = {}) { | ||
return err; | ||
} | ||
let allKeySets = []; | ||
for (let [spacename, space] of Object.entries(self.rset)) { | ||
let keys = Object.keys(space); | ||
let err = NVRNG.validateKeys(spacename, keys); | ||
if (err) { | ||
return err; | ||
} | ||
allKeySets.push(keys.sort()); | ||
} | ||
if (shuffle) { | ||
self.spaces = Object.keys(self.rset); | ||
self.spaceLength = self.spaces.length; | ||
/* | ||
for (let i = 0; i < self.spaceLength - 1; i += 1) { | ||
if (!(allKeySets[i].length === 1 && allKeySets[i][0] === Genders.Any) && | ||
(JSON.stringify(allKeySets[i]) !== JSON.stringify(allKeySets[i+1]))) { | ||
return `Key sets [${allKeySets[i]}] of "${self.spaces[i]}" and [${allKeySets[i+1]}] of "${self.spaces[i+1]}" are not equal`; | ||
} | ||
} | ||
*/ | ||
if (shuffle) { | ||
for (let [key, space] of Object.entries(self.rset)) { | ||
@@ -99,16 +141,20 @@ for (let [key, arr] of Object.entries(space)) { | ||
} | ||
let allKeySets = []; | ||
for (let [key, space] of Object.entries(self.rset)) { | ||
allKeySets.push(Object.keys(space).sort()); | ||
for (let i = 0; i < self.spaceLength - 1; i += 1) { | ||
if (allKeySets[i].length === 1 && allKeySets[i][0] === Genders.Any) { | ||
allKeySets[i] = allKeySets[i + 1]; | ||
} | ||
} | ||
self.spaces = Object.keys(self.rset); | ||
self.spaceLength = self.spaces.length; | ||
for (let i = 0; i < self.spaceLength - 1; i += 1) { | ||
if (JSON.stringify(allKeySets[i]) !== JSON.stringify(allKeySets[i+1])) { | ||
return `Key sets [${allKeySets[i]}] of "${self.spaces[i]}" and [${allKeySets[i+1]}] of "${self.spaces[i+1]}" are not equal`; | ||
for (let i = self.spaceLength - 1; i > 0; i -= 1) { | ||
if (allKeySets[i].length === 1 && allKeySets[i][0] === Genders.Any) { | ||
allKeySets[i] = allKeySets[i - 1]; | ||
} else if (JSON.stringify(allKeySets[i]) !== JSON.stringify(allKeySets[i-1])) { | ||
return `Key sets [${allKeySets[i]}] and [${allKeySets[i-1]}] are not equal`; | ||
} | ||
} | ||
self.keys = JSON.parse(JSON.stringify(allKeySets[0])); | ||
self.keys = JSON.parse(JSON.stringify(allKeySets[0])); | ||
if (self.keys[0] === 'a') { | ||
self.keys = ['f', 'm', 'n']; | ||
} | ||
allKeySets = []; | ||
@@ -119,3 +165,7 @@ return null; | ||
getArrOfGender (space, gender) { | ||
let self = this; | ||
let self = this, | ||
keys = Object.keys(space); | ||
if (keys[0] === 'a') { | ||
return [gender, space['a']]; | ||
} | ||
if (gender === Genders.Any) { | ||
@@ -159,16 +209,16 @@ let proGender = self.keys[ NVRNG.randIntFromZero(self.keys.length) ]; | ||
} | ||
static getJoiner (noJoin) { | ||
if (noJoin === true) { | ||
return (data, delimiter) => { | ||
return [data.join(delimiter), data]; | ||
} | ||
} else { | ||
return (data, delimiter) => { | ||
let tstr = data.join(delimiter); | ||
return [tstr, tstr]; | ||
} | ||
} | ||
} | ||
static getJoiner (noJoin) { | ||
if (noJoin === true) { | ||
return (data, delimiter) => { | ||
return [data.join(delimiter), data]; | ||
} | ||
} else { | ||
return (data, delimiter) => { | ||
let tstr = data.join(delimiter); | ||
return [tstr, tstr]; | ||
} | ||
} | ||
} | ||
getOne ({ gender = Genders.Any, output = OutputFormat.Set, exclude = new Set(), delimiter = ' ', format = StringFormat.NoFormat, noJoin = false } = {}) { | ||
@@ -191,6 +241,6 @@ let self = this; | ||
out, tstr, | ||
joiner = NVRNG.getJoiner(noJoin); | ||
joiner = NVRNG.getJoiner(noJoin); | ||
do { | ||
let p = [], arr; | ||
let proGender = gender; | ||
let proGender = gender; | ||
for (let space of self.spaces) { | ||
@@ -201,3 +251,3 @@ [proGender, arr] = self.getArrOfGender(self.rset[space], proGender); | ||
} | ||
[tstr, out] = joiner(p, delimiter); | ||
[tstr, out] = joiner(p, delimiter); | ||
} while (limitter-->0 && | ||
@@ -232,4 +282,4 @@ proExclude.has(tstr)); | ||
limitter = self.limit, | ||
tstr, tmp, | ||
joiner = NVRNG.getJoiner(noJoin); | ||
tstr, tmp, | ||
joiner = NVRNG.getJoiner(noJoin); | ||
while (out.size < size && limitter-->0) { | ||
@@ -236,0 +286,0 @@ let p = [], arr; |
{ | ||
"name": "nvrng", | ||
"version": "1.3.0", | ||
"version": "1.4.0", | ||
"description": "Not very random temporary name generator", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -8,3 +8,3 @@ [![Codecov Coverage](https://img.shields.io/codecov/c/github/ssypachev/nvrng/master.svg?style=flat-square)](https://codecov.io/gh/ssypachev/nvrng/) | ||
`NVRNG` generates array of random readable names, like 'Silver Raccoon' or 'Purple Snake'. | ||
`NVRNG` generates array of random readable names, like 'Silver Raccoon', 'Purple Snake' or sets of real names like 'Aaron Smith', etc. | ||
`NVRNG` can create sets for different genders (like 'Lemon Stallion' or 'Marble Actress'). | ||
@@ -30,2 +30,8 @@ `NVRNG` uses vocabulary in `json` format. | ||
[EN Colored Animals](https://github.com/ssypachev/nvrng/blob/master/vocabularies/en_colored_animals.json) | ||
[RU Colored Animals](https://github.com/ssypachev/nvrng/blob/master/vocabularies/ru_colored_animals.json) | ||
[EN Common Names](https://github.com/ssypachev/nvrng/blob/master/vocabularies/en_common_names.json) | ||
Please, make pull request if found any errors or if you have any other vocabulary to suggest. | ||
@@ -120,3 +126,3 @@ | ||
const filename = __dirname + '/../examples/gender.json'; | ||
err = gen.upload(filename); | ||
err = gen.upload(filename); | ||
console.log(gen.getKeys()); //[ 'f', 'm', 'n' ] | ||
@@ -154,2 +160,8 @@ ``` | ||
Number of "spaces" is not limited. If one array can be used for different genders (like adjective for male and female animals in english, or second name for both male and female first name) | ||
then it can be substituted with one "a" array, see [EN Common Names](https://github.com/ssypachev/nvrng/blob/master/vocabularies/en_common_names.json). Array must contain at least one word. | ||
NOTE: if "a" is used, then "space" must not have any other keys. Also, all keys from all different "spaces" must match, except for an "a" key. If | ||
vocabulary has any errors then `upload` method will return error with description of the problem. | ||
## Examples | ||
@@ -156,0 +168,0 @@ ```js |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
28451
273
217