Comparing version 1.0.1 to 1.1.0
30
index.js
@@ -150,2 +150,32 @@ let fs = require('fs'); | ||
getOne ({ gender = Genders.Any, output = OutputFormat.Set, exclude = new Set(), delimiter = ' ', format = StringFormat.NoFormat } = {}) { | ||
let self = this; | ||
if (gender !== Genders.Any) { | ||
if (!self.keys.includes(gender)) { | ||
return [`Vocabulary does not contain arrays of ${gender} gender, supported only ${self.keys}`, null]; | ||
} | ||
} | ||
let [err, proExclude] = NVRNG.arrOrSetToSet(exclude); | ||
if (err) { | ||
return [err, null]; | ||
} | ||
let formatter = NVRNG.getStringFormatter(format); | ||
if (formatter === null) { | ||
return [`Unsupported output string format "${format}". Use [${Object.keys(StringFormat)}]`, null]; | ||
} | ||
let limitter = self.limit, | ||
out; | ||
do { | ||
let p = []; | ||
for (let space of self.spaces) { | ||
let arr = self.getArrOfGender(self.rset[space], gender); | ||
let index = NVRNG.randIntFromZero(arr.length); | ||
p.push(formatter(arr[index])); | ||
} | ||
out = p.join(delimiter); | ||
} while (limitter-->0 && | ||
proExclude.has(out)); | ||
return [null, out]; | ||
} | ||
getSet (size, { gender = Genders.Any, output = OutputFormat.Set, include = new Set(), exclude = new Set(), delimiter = ' ', format = StringFormat.NoFormat } = {}) { | ||
@@ -152,0 +182,0 @@ let self = this; |
{ | ||
"name": "nvrng", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"description": "Not very random temporary name generator", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -41,2 +41,3 @@ [![Codecov Coverage](https://img.shields.io/codecov/c/github/ssypachev/nvrng/master.svg?style=flat-square)](https://codecov.io/gh/ssypachev/nvrng/) | ||
Generate set of random names. Returns array. First element is error (`null` if ok), second is a set of names; | ||
`gender` - gender, possible values are 'n', 'f', 'm', or 'a'. You can also use Enum `Genders` with values | ||
@@ -79,2 +80,6 @@ | ||
## `getOne({ gender = Genders.Any, exclude, delimiter = ' ', format = StringFormat.NoFormat }) => [err, string]` | ||
Returns one string. Same options as in `getSet`. | ||
## Vocabulary | ||
@@ -109,2 +114,29 @@ Vocabulary is a json object which contains one or more objects ("spaces") with | ||
## Examples | ||
```js | ||
let { NVRNG, OutputFormat, Genders, StringFormat } = require('../index.js'); | ||
let gen = new NVRNG(); | ||
gen.upload(__dirname + '/../examples/simplest4.json'); | ||
let [err, set] = gen.getSet(3, { delimiter: '-', format: StringFormat.Capitalize }); | ||
console.log(err); | ||
set.forEach(item => console.log(item)); | ||
let newname; | ||
[err, newname] = gen.getOne({ delimiter: '-', format: StringFormat.Capitalize }); | ||
console.log(newname); | ||
``` | ||
Output is | ||
```js | ||
null | ||
Dark-blue-Mouse-With-Notebook | ||
Purple-Dog-With-Gun | ||
Green-Elephant-With-Gun | ||
Purple-Dog-With-Car | ||
``` | ||
## Testing | ||
@@ -111,0 +143,0 @@ Clone repository and run |
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
11274
206
164