@travi/any
Advanced tools
Comparing version 1.0.0 to 1.0.1
@@ -5,4 +5,4 @@ # API Reference | ||
Produces a simple object with a random number of key/value pairs. Each key is a random [word](#word), and each value is | ||
a random [string](#string) | ||
Produces a simple object with a random number of key/value pairs. Each key is a | ||
random [word](#word), and each value is a random [string](#string) | ||
@@ -21,9 +21,10 @@ ### Example | ||
### Parameters | ||
### `objectWithKeys` parameters | ||
* `keys` (required): list of keys to be used for the produced object's keys | ||
* `options`: object to provide configuration options | ||
* `factory`: factory function to be used to create each of the values for the produced object. defaults to | ||
[`string`](#string), if not provided | ||
* `factory`: factory function to be used to create each of the values for the | ||
produced object. defaults to [`string`](#string), if not provided | ||
### Examples | ||
### `objectWithKeys` examples | ||
@@ -46,6 +47,7 @@ ```js | ||
### Parameters | ||
### `fromList` parameters | ||
* `list` (required): list of items to choose from | ||
### Example | ||
### `fromList` example | ||
@@ -62,10 +64,12 @@ ```js | ||
### Parameters | ||
### `listOf` parameters | ||
* `factory` (required): the factory function for producing the items for the list. | ||
* `options`: object to provide configuration options | ||
* `size`: length of list that should be produced. defaults to a random number between `1` and `20`, if | ||
not provided. | ||
* `uniqueOn`: property on the produced objects that needs to be unique within the resulting list | ||
* `size`: length of list that should be produced. defaults to a random number | ||
between `1` and `20`, if not provided. | ||
* `uniqueOn`: property on the produced objects that needs to be unique within | ||
the resulting list | ||
### Examples | ||
### `listOf` examples | ||
@@ -92,11 +96,13 @@ ```js | ||
Direct usage of [`string()` from chance.js](http://chancejs.com/#string). Options are passed directly to the chance | ||
method so refer to its documentation for what is available. | ||
Direct usage of [`string()` from chance.js](http://chancejs.com/#string). | ||
Options are passed directly to the chance method so refer to its documentation | ||
for what is available. | ||
### Note | ||
Be aware that `string()` can include special characters that may not be suitable for some use cases. If they would | ||
cause issues, you may prefer [`word()`](#word) instead. | ||
Be aware that `string()` can include special characters that may not be suitable | ||
for some use cases. If they would cause issues, you may prefer [`word()`](#word) | ||
instead. | ||
### Example | ||
### `string` example | ||
@@ -111,6 +117,7 @@ ```js | ||
An almost direct usage of [`word()` from chance.js](http://chancejs.com/#word). No options are passed to the chance | ||
method because I have not had a need for anything but the default version yet. | ||
An almost direct usage of [`word()` from chance.js](http://chancejs.com/#word). | ||
No options are passed to the chance method because I have not had a need for | ||
anything but the default version yet. | ||
### Example | ||
### `word` example | ||
@@ -125,6 +132,8 @@ ```js | ||
Produces a random integer > 0. Direct usage of [`natural()` from chance.js](http://chancejs.com/#natural). Options are | ||
passed directly to the chance method so refer to its documentation for what is available. | ||
Produces a random integer > 0. Direct usage of | ||
[`natural()` from chance.js](http://chancejs.com/#natural). Options are passed | ||
directly to the chance method so refer to its documentation for what is | ||
available. | ||
### Example | ||
### `integer` example | ||
@@ -139,6 +148,7 @@ ```js | ||
Direct usage of [`floating()` from chance.js](http://chancejs.com/#floating). Options are passed directly to the chance | ||
method so refer to its documentation for what is available. | ||
Direct usage of [`floating()` from chance.js](http://chancejs.com/#floating). | ||
Options are passed directly to the chance method so refer to its documentation | ||
for what is available. | ||
### Example | ||
### `float` example | ||
@@ -155,3 +165,3 @@ ```js | ||
### Example | ||
### `boolean` example | ||
@@ -166,6 +176,7 @@ ```js | ||
Direct usage of [`url()` from chance.js](http://chancejs.com/#url). Options are passed directly to the chance | ||
method so refer to its documentation for what is available. | ||
Direct usage of [`url()` from chance.js](http://chancejs.com/#url). Options are | ||
passed directly to the chance method so refer to its documentation for what is | ||
available. | ||
### Example | ||
### `url` example | ||
@@ -180,6 +191,7 @@ ```js | ||
An almost direct usage of [`email()` from chance.js](http://chancejs.com/#email). No options are passed to the chance | ||
method because I have not had a need for anything but the default version yet. | ||
An almost direct usage of [`email()` from chance.js](http://chancejs.com/#email). | ||
No options are passed to the chance method because I have not had a need for | ||
anything but the default version yet. | ||
### Example | ||
### `email` example | ||
@@ -194,7 +206,8 @@ ```js | ||
An almost direct usage of [`date()` from chance.js](http://chancejs.com/#date). No options passed to the `any` method | ||
are passed to the `chance` method, but the `{string: true}` option is passed because I have only had a use for the string | ||
form of random dates so far. | ||
An almost direct usage of [`date()` from chance.js](http://chancejs.com/#date). | ||
No options passed to the `any` method are passed to the `chance` method, but | ||
the `{string: true}` option is passed because I have only had a use for the | ||
string form of random dates so far. | ||
### Example | ||
### `date` example | ||
@@ -201,0 +214,0 @@ ```js |
@@ -13,97 +13,97 @@ 'use strict'; | ||
var integer = function integer(options) { | ||
return chance.natural(options); | ||
return chance.natural(options); | ||
}; | ||
var float = function float(options) { | ||
return chance.floating(options); | ||
return chance.floating(options); | ||
}; | ||
var string = function string(options) { | ||
return chance.string(options); | ||
return chance.string(options); | ||
}; | ||
var word = function word() { | ||
return chance.word(); | ||
return chance.word(); | ||
}; | ||
var url = function url(options) { | ||
return chance.url(options); | ||
return chance.url(options); | ||
}; | ||
var boolean = function boolean() { | ||
return chance.bool(); | ||
return chance.bool(); | ||
}; | ||
var email = function email() { | ||
return chance.email(); | ||
return chance.email(); | ||
}; | ||
var date = function date() { | ||
return chance.date({ string: true }); | ||
return chance.date({ string: true }); | ||
}; | ||
function simpleObject() { | ||
var object = {}, | ||
size = integer(DEFAULT_SIZE_RANGE); | ||
var object = {}, | ||
size = integer(DEFAULT_SIZE_RANGE); | ||
for (var i = 0; i < size; i += 1) { | ||
object[word()] = string(); | ||
} | ||
for (var i = 0; i < size; i += 1) { | ||
object[word()] = string(); | ||
} | ||
return object; | ||
return object; | ||
} | ||
function objectWithKeys(keys) { | ||
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
var object = {}; | ||
var object = {}; | ||
keys.forEach(function (key) { | ||
if (options.factory) { | ||
object[key] = options.factory(); | ||
} else { | ||
object[key] = string(); | ||
} | ||
}); | ||
keys.forEach(function (key) { | ||
if (options.factory) { | ||
object[key] = options.factory(); | ||
} else { | ||
object[key] = string(); | ||
} | ||
}); | ||
return object; | ||
return object; | ||
} | ||
function listOf(factory) { | ||
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
var listSize = options.size || integer(Object.assign({}, DEFAULT_SIZE_RANGE, options)); | ||
var listSize = options.size || integer(Object.assign({}, DEFAULT_SIZE_RANGE, options)); | ||
if (options.uniqueOn) { | ||
var uniqueValues = {}; | ||
if (options.uniqueOn) { | ||
var uniqueValues = {}; | ||
while (Object.keys(uniqueValues).length < listSize) { | ||
var item = factory(); | ||
uniqueValues[item[options.uniqueOn]] = item; | ||
} | ||
while (Object.keys(uniqueValues).length < listSize) { | ||
var item = factory(); | ||
uniqueValues[item[options.uniqueOn]] = item; | ||
} | ||
return _.values(uniqueValues); | ||
} else { | ||
var list = []; | ||
return _.values(uniqueValues); | ||
} | ||
for (var i = 0; i < listSize; i += 1) { | ||
list.push(factory()); | ||
} | ||
var list = []; | ||
return list; | ||
} | ||
for (var i = 0; i < listSize; i += 1) { | ||
list.push(factory()); | ||
} | ||
return list; | ||
} | ||
function fromList(list) { | ||
return list[integer({ min: 0, max: list.length - 1 })]; | ||
return list[integer({ min: 0, max: list.length - 1 })]; | ||
} | ||
var any = { | ||
string: string, | ||
word: word, | ||
integer: integer, | ||
float: float, | ||
boolean: boolean, | ||
url: url, | ||
email: email, | ||
date: date, | ||
simpleObject: simpleObject, | ||
objectWithKeys: objectWithKeys, | ||
listOf: listOf, | ||
fromList: fromList | ||
string: string, | ||
word: word, | ||
integer: integer, | ||
float: float, | ||
boolean: boolean, | ||
url: url, | ||
email: email, | ||
date: date, | ||
simpleObject: simpleObject, | ||
objectWithKeys: objectWithKeys, | ||
listOf: listOf, | ||
fromList: fromList | ||
}; | ||
module.exports = any; |
@@ -9,97 +9,97 @@ import _ from 'lodash'; | ||
var integer = function integer(options) { | ||
return chance.natural(options); | ||
return chance.natural(options); | ||
}; | ||
var float = function float(options) { | ||
return chance.floating(options); | ||
return chance.floating(options); | ||
}; | ||
var string = function string(options) { | ||
return chance.string(options); | ||
return chance.string(options); | ||
}; | ||
var word = function word() { | ||
return chance.word(); | ||
return chance.word(); | ||
}; | ||
var url = function url(options) { | ||
return chance.url(options); | ||
return chance.url(options); | ||
}; | ||
var boolean = function boolean() { | ||
return chance.bool(); | ||
return chance.bool(); | ||
}; | ||
var email = function email() { | ||
return chance.email(); | ||
return chance.email(); | ||
}; | ||
var date = function date() { | ||
return chance.date({ string: true }); | ||
return chance.date({ string: true }); | ||
}; | ||
function simpleObject() { | ||
var object = {}, | ||
size = integer(DEFAULT_SIZE_RANGE); | ||
var object = {}, | ||
size = integer(DEFAULT_SIZE_RANGE); | ||
for (var i = 0; i < size; i += 1) { | ||
object[word()] = string(); | ||
} | ||
for (var i = 0; i < size; i += 1) { | ||
object[word()] = string(); | ||
} | ||
return object; | ||
return object; | ||
} | ||
function objectWithKeys(keys) { | ||
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
var object = {}; | ||
var object = {}; | ||
keys.forEach(function (key) { | ||
if (options.factory) { | ||
object[key] = options.factory(); | ||
} else { | ||
object[key] = string(); | ||
} | ||
}); | ||
keys.forEach(function (key) { | ||
if (options.factory) { | ||
object[key] = options.factory(); | ||
} else { | ||
object[key] = string(); | ||
} | ||
}); | ||
return object; | ||
return object; | ||
} | ||
function listOf(factory) { | ||
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
var listSize = options.size || integer(Object.assign({}, DEFAULT_SIZE_RANGE, options)); | ||
var listSize = options.size || integer(Object.assign({}, DEFAULT_SIZE_RANGE, options)); | ||
if (options.uniqueOn) { | ||
var uniqueValues = {}; | ||
if (options.uniqueOn) { | ||
var uniqueValues = {}; | ||
while (Object.keys(uniqueValues).length < listSize) { | ||
var item = factory(); | ||
uniqueValues[item[options.uniqueOn]] = item; | ||
} | ||
while (Object.keys(uniqueValues).length < listSize) { | ||
var item = factory(); | ||
uniqueValues[item[options.uniqueOn]] = item; | ||
} | ||
return _.values(uniqueValues); | ||
} else { | ||
var list = []; | ||
return _.values(uniqueValues); | ||
} | ||
for (var i = 0; i < listSize; i += 1) { | ||
list.push(factory()); | ||
} | ||
var list = []; | ||
return list; | ||
} | ||
for (var i = 0; i < listSize; i += 1) { | ||
list.push(factory()); | ||
} | ||
return list; | ||
} | ||
function fromList(list) { | ||
return list[integer({ min: 0, max: list.length - 1 })]; | ||
return list[integer({ min: 0, max: list.length - 1 })]; | ||
} | ||
var any = { | ||
string: string, | ||
word: word, | ||
integer: integer, | ||
float: float, | ||
boolean: boolean, | ||
url: url, | ||
email: email, | ||
date: date, | ||
simpleObject: simpleObject, | ||
objectWithKeys: objectWithKeys, | ||
listOf: listOf, | ||
fromList: fromList | ||
string: string, | ||
word: word, | ||
integer: integer, | ||
float: float, | ||
boolean: boolean, | ||
url: url, | ||
email: email, | ||
date: date, | ||
simpleObject: simpleObject, | ||
objectWithKeys: objectWithKeys, | ||
listOf: listOf, | ||
fromList: fromList | ||
}; | ||
export default any; |
{ | ||
"name": "@travi/any", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "random data generator for when test data is insignificant", | ||
@@ -8,4 +8,10 @@ "main": "lib/any.cjs.js", | ||
"scripts": { | ||
"clean": "rimraf coverage/ lib/", | ||
"build": "rollup -c", | ||
"test": "grunt", | ||
"lint:js": "eslint .", | ||
"lint:md": "globstar --node -- markdownlint **/*.md", | ||
"tests:unit": "mocha --recursive test/unit/", | ||
"test": "run-s clean lint:* coverage", | ||
"coverage": "nyc run-s tests:unit", | ||
"report-coverage": "nyc report --reporter=text-lcov | coveralls", | ||
"preversion": "npm test", | ||
@@ -36,6 +42,6 @@ "prepublish": "npm run build", | ||
"chance": "1.0.4", | ||
"lodash": "4.17.2" | ||
"lodash": "4.17.3" | ||
}, | ||
"devDependencies": { | ||
"@travi/eslint-config-travi": "0.3.0", | ||
"@travi/eslint-config-travi": "1.0.0-beta.2", | ||
"babel-cli": "6.18.0", | ||
@@ -48,20 +54,19 @@ "babel-preset-es2015-node": "6.1.1", | ||
"cz-conventional-changelog": "1.2.0", | ||
"formatio": "1.2.0", | ||
"globstar": "1.0.0", | ||
"greenkeeper-postpublish": "1.0.1", | ||
"grunt": "1.0.1", | ||
"grunt-cli": "1.2.0", | ||
"grunt-contrib-clean": "1.0.0", | ||
"grunt-eslint": "19.0.0", | ||
"grunt-mocha-istanbul": "5.0.2", | ||
"husky": "0.12.0-2", | ||
"husky": "0.12.0", | ||
"istanbul": "1.0.0-alpha.2", | ||
"load-grunt-config": "0.19.2", | ||
"markdownlint-cli": "0.2.0", | ||
"mocha": "3.2.0", | ||
"npm-run-all": "3.1.2", | ||
"nyc": "10.0.0", | ||
"proxyquire": "1.7.10", | ||
"referee": "1.2.0", | ||
"referee-sinon": "1.0.3", | ||
"rollup": "0.37.0", | ||
"rimraf": "2.5.4", | ||
"rollup": "0.38.0", | ||
"rollup-plugin-babel": "2.7.1", | ||
"semantic-release": "6.3.2", | ||
"sinon": "2.0.0-pre.4", | ||
"time-grunt": "1.4.0", | ||
"validate-commit-msg": "2.8.2" | ||
@@ -68,0 +73,0 @@ }, |
@@ -11,24 +11,29 @@ # Random data generator | ||
Random data generator for when test data is insignificant. Tailoring data too | ||
closely for the domain can end up being confusing in tests because it distracts | ||
from the behavior that is actually important in the test. This is why I prefer | ||
to make it very clear when test data is insignificant by using a data generator. | ||
This library captures most of the patterns I use frequently. | ||
Random data generator for when test data is insignificant. Tailoring data too closely for the domain can end | ||
up being confusing in tests because it distracts from the behavior that is actually important in the test. This | ||
is why I prefer to make it very clear when test data is insignificant by using a data generator. This library | ||
captures most of the patterns I use frequently. | ||
## Based on Chance.js | ||
To save myself from needing to maintain the actual data generators, this library leverages [Chance.js](http://chancejs.com/) | ||
for generating the data. So why not just use Chance.js directly? | ||
To save myself from needing to maintain the actual data generators, this library | ||
leverages [Chance.js](http://chancejs.com/) for generating the data. So why not | ||
just use Chance.js directly? | ||
* I find it annoying that an instance of Chance has to be created. Rather than initialize an instance in every | ||
module, I wrap a single instance for reuse throughout the tests. | ||
* I can set my default options to align with my typical conventions rather than having to repeatedly configure | ||
Chance.js's very flexible API | ||
* Lots of my tests need objects to be generated, but the structure of those objects typically does not matter. | ||
Chance.js does not have a [simple-object generator](docs/API.md#simpleobject), so I've included that on my own. | ||
* Many of my tests need [lists](docs/API.md#listof) generated. Like objects, Chance.js does not generate lists, so I've included this | ||
on my own. | ||
* I find it annoying that an instance of Chance has to be created. Rather than | ||
initialize an instance in every module, I wrap a single instance for reuse | ||
throughout the tests. | ||
* I can set my default options to align with my typical conventions rather than | ||
having to repeatedly configure Chance.js's very flexible API | ||
* Lots of my tests need objects to be generated, but the structure of those | ||
objects typically does not matter. Chance.js does not have a | ||
[simple-object generator](docs/API.md#simpleobject), so I've included that on | ||
my own. | ||
* Many of my tests need [lists](docs/API.md#listof) generated. Like objects, | ||
Chance.js does not generate lists, so I've included this on my own. | ||
## Install | ||
``` | ||
```bash | ||
$ npm install @travi/any --save-dev | ||
@@ -38,2 +43,3 @@ ``` | ||
## Documentation | ||
* [API](docs/API.md) |
@@ -0,16 +1,17 @@ | ||
/* eslint import/no-extraneous-dependencies: ["error", {"devDependencies": true}] */ | ||
import babel from 'rollup-plugin-babel'; | ||
export default { | ||
entry: 'any.js', | ||
plugins: [ | ||
babel({ | ||
babelrc: false, | ||
exclude: ['./node_modules/**'], | ||
presets: ['es2015-rollup'] | ||
}) | ||
], | ||
targets: [ | ||
{dest: 'lib/any.cjs.js', format: 'cjs'}, | ||
{dest: 'lib/any.es.js', format: 'es'} | ||
] | ||
entry: 'any.js', | ||
plugins: [ | ||
babel({ | ||
babelrc: false, | ||
exclude: ['./node_modules/**'], | ||
presets: ['es2015-rollup'] | ||
}) | ||
], | ||
targets: [ | ||
{dest: 'lib/any.cjs.js', format: 'cjs'}, | ||
{dest: 'lib/any.es.js', format: 'es'} | ||
] | ||
}; |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
14927
26
11
44
0
184
+ Addedlodash@4.17.3(transitive)
- Removedlodash@4.17.2(transitive)
Updatedlodash@4.17.3