Comparing version 1.4.0 to 1.4.1
123
index.js
@@ -83,22 +83,22 @@ let fs = require('fs'); | ||
} | ||
/** | ||
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; | ||
} | ||
/** | ||
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; | ||
} | ||
upload (filename, { shuffle = true } = {}) { | ||
@@ -110,10 +110,10 @@ let self = this; | ||
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; | ||
} | ||
let keys = Object.keys(space); | ||
let err = NVRNG.validateKeys(spacename, keys); | ||
if (err) { | ||
return err; | ||
} | ||
allKeySets.push(keys.sort()); | ||
@@ -124,12 +124,3 @@ } | ||
/* | ||
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) { | ||
if (shuffle) { | ||
for (let [key, space] of Object.entries(self.rset)) { | ||
@@ -142,11 +133,11 @@ for (let [key, arr] of Object.entries(space)) { | ||
for (let i = 0; i < self.spaceLength - 1; i += 1) { | ||
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]; | ||
} | ||
allKeySets[i] = allKeySets[i + 1]; | ||
} | ||
} | ||
for (let i = self.spaceLength - 1; i > 0; i -= 1) { | ||
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])) { | ||
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`; | ||
@@ -156,6 +147,6 @@ } | ||
self.keys = JSON.parse(JSON.stringify(allKeySets[0])); | ||
if (self.keys[0] === 'a') { | ||
self.keys = ['f', 'm', 'n']; | ||
} | ||
self.keys = JSON.parse(JSON.stringify(allKeySets[0])); | ||
if (self.keys[0] === 'a') { | ||
self.keys = ['f', 'm', 'n']; | ||
} | ||
allKeySets = []; | ||
@@ -167,6 +158,6 @@ return null; | ||
let self = this, | ||
keys = Object.keys(space); | ||
if (keys[0] === 'a') { | ||
return [gender, space['a']]; | ||
} | ||
keys = Object.keys(space); | ||
if (keys[0] === 'a') { | ||
return [gender, space['a']]; | ||
} | ||
if (gender === Genders.Any) { | ||
@@ -212,3 +203,3 @@ let proGender = self.keys[ NVRNG.randIntFromZero(self.keys.length) ]; | ||
static getJoiner (noJoin) { | ||
if (noJoin === true) { | ||
if (!!noJoin === true) { | ||
return (data, delimiter) => { | ||
@@ -240,11 +231,11 @@ return [data.join(delimiter), data]; | ||
} | ||
let limitter = self.limit, | ||
out, tstr, | ||
joiner = NVRNG.getJoiner(noJoin); | ||
let limitter = self.limit, | ||
proGender = gender, | ||
joiner = NVRNG.getJoiner(noJoin), | ||
out, tstr, p, arr, index; | ||
do { | ||
let p = [], arr; | ||
let proGender = gender; | ||
p = []; | ||
for (let space of self.spaces) { | ||
[proGender, arr] = self.getArrOfGender(self.rset[space], proGender); | ||
let index = NVRNG.randIntFromZero(arr.length); | ||
index = NVRNG.randIntFromZero(arr.length); | ||
p.push(formatter(arr[index])); | ||
@@ -259,2 +250,6 @@ } | ||
getSet (size, { gender = Genders.Any, output = OutputFormat.Set, include = new Set(), exclude = new Set(), delimiter = ' ', format = StringFormat.NoFormat, noJoin = false } = {}) { | ||
size = +size; | ||
if (isNaN(size) || size < 0) { | ||
return [`Bad "size" parameter`, null]; | ||
} | ||
let self = this; | ||
@@ -281,9 +276,9 @@ if (gender !== Genders.Any) { | ||
} | ||
let adder = proExclude.size > 0 ? addToSetAndExclude : addToSet, | ||
limitter = self.limit, | ||
tstr, tmp, | ||
joiner = NVRNG.getJoiner(noJoin); | ||
while (out.size < size && limitter-->0) { | ||
let p = [], arr; | ||
let proGender = gender; | ||
let adder = proExclude.size > 0 ? addToSetAndExclude : addToSet, | ||
limitter = self.limit, | ||
joiner = NVRNG.getJoiner(noJoin), | ||
proGender = gender, | ||
tstr, tmp, p, arr; | ||
do { | ||
p = []; | ||
for (let space of self.spaces) { | ||
@@ -296,3 +291,3 @@ [proGender, arr] = self.getArrOfGender(self.rset[space], proGender); | ||
adder(out, tmp, tstr, proExclude); | ||
} | ||
} while (out.size < size && limitter-->0); | ||
switch (output) { | ||
@@ -299,0 +294,0 @@ case OutputFormat.Set: case OutputFormat.Array: case OutputFormat.Object: |
{ | ||
"name": "nvrng", | ||
"version": "1.4.0", | ||
"version": "1.4.1", | ||
"description": "Not very random temporary name generator", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
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
28378
269