Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

chance-generators

Package Overview
Dependencies
Maintainers
1
Versions
59
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

chance-generators - npm Package Compare versions

Comparing version 1.9.0 to 1.10.0

.nvmrc

33

lib/chance-generators.js

@@ -71,2 +71,25 @@ /*global define*/

function installMapFunction (generator) {
generator.map = function (f) {
var lastValue, lastMappedValue
var mapGenerator = generatorFunction(generator.name + '.map', [], function () {
lastValue = generator()
lastMappedValue = f(lastValue)
return lastMappedValue
})
mapGenerator.shrink = function (value) {
if (value === lastMappedValue) {
return generator.shrink(lastValue).map(f)
} else {
return generator
}
}
installMapFunction(mapGenerator)
return mapGenerator
}
}
var overrides = {

@@ -229,2 +252,4 @@ n: function (generator, count) {

installMapFunction(g)
return g

@@ -247,5 +272,11 @@ }

that.identity = generatorFunction('identity', [], function (data) {
return generatorFunction('identity', [data], function () {
var identityGenerator = generatorFunction('identity', [data], function () {
return data
})
identityGenerator.map = function (fn) {
return that.identity(fn(data))
}
return identityGenerator
})

@@ -252,0 +283,0 @@

15

package.json
{
"name": "chance-generators",
"version": "1.9.0",
"version": "1.10.0",
"description": "Random generators based on changejs",

@@ -9,3 +9,5 @@ "main": "lib/chance-generators.js",

"lint": "standard",
"test": "mocha"
"test": "mocha ./documentation/**/*.md ./test/**/*.js && npm run lint",
"generate-site": "generate-site --require ./bootstrap-unexpected-markdown.js",
"update-examples": "generate-site --require ./bootstrap-unexpected-markdown.js --update-examples"
},

@@ -25,4 +27,11 @@ "keywords": [

"standard": "5.4.1",
"unexpected": "10.5.1"
"unexpected": "10.5.1",
"unexpected-documentation-site-generator": "4.2.0",
"unexpected-markdown": "1.6.1"
},
"standard": {
"globals": [
"beforeEach"
]
}
}
# chance-generators
This module is thin wrapper around the excellent [chance](http://chancejs.com/)
library for doing composable generators.
## Installation
```
npm install --save chance-generators
```
## Usage
```js
var g = require('./lib/chance-generators')(42)
var emails = g.email({ domain: g.pick(['example.com', 'mail.me']) })
expect(emails(), 'to equal', 'hiapli@example.com')
expect(emails(), 'to equal', 'cuval@mail.me')
expect(emails(), 'to equal', 'ozi@example.com')
```
When you call any of the functions documented on
[www.chancejs.com](http://www.chancejs.com/)
with one of more arguments a new generator function is returned instead of just
generating the result:
```js
var positiveIntegers = g.integer({ min: 0 })
expect(positiveIntegers, 'to be a function')
expect(positiveIntegers(), 'to be a number')
```
If you call a generator without any arguments it will produce a new value.
```js
expect(g.integer(), 'to be a number')
```
Any generators passed as parameters to other generators will be called to
produce a random value:
```js
var g = require('./lib/chance-generators')(42)
var smallStrings = g.string({ length: g.integer({ min: 3, max: 9 }) })
expect(smallStrings(), 'to equal', '(n25S')
expect(smallStrings(), 'to equal', 'GlheH#y')
expect(smallStrings(), 'to equal', '0Wbe)19')
```
The only exception to this rule is the `n` and `unique` functions, they don't
dereference the generator given as the first argument:
```js
var g = require('./lib/chance-generators')(42)
var stringArrays = g.n(g.string, g.integer({ min: 0, max: 10 }))
expect(stringArrays(), 'to equal', [
'(n25SSlGlheH#ySk0', 'be)19*pan]nTwTM', 'FbvMT', 'kdv[BrHg6To'
])
expect(stringArrays(), 'to equal', [
'[RId@SYmHea(*', 'P7CwbhrYrGYjTK9cm^Ct', 'X3xFMpO', 'nc)!5H*D%&S1&y'
])
expect(stringArrays(), 'to equal', [])
```
## Additional generators
### identity
Always generates the given value.
```js
var g = require('./lib/chance-generators')(42)
expect(g.identity(42), 'when called', 'to equal', 42)
```
### array
Same as `n` but defaults to generate arrays between 0-50 items when no count is
supplied.
```js
var g = require('./lib/chance-generators')(42)
expect(
chance.array(chance.natural({ max: 10 })),
'when called',
'to equal', [
8, 10, 2, 8, 8, 6, 6, 1, 4,
1, 1, 0, 5, 9, 3, 6, 1, 7, 7
]
)
```
### shape
Generates values of the given shape, if the structure contains generators they
will be unwrapped.
```js
expect(chance.shape({
constant: 42,
point: {
x: chance.integer,
y: chance.integer
}
}), 'when called', 'to satisfy', {
constant: 42,
point: {
x: expect.it('to be a number'),
y: expect.it('to be a number')
}
})
```
## MIT License
Copyright (c) 2016 Sune Simonsen <sune@we-knowhow.dk>
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Read [the documentation](https://sunesimonsen.github.io/chance-generators/).
/*global describe, it*/
var Chance = require('../lib/chance-generators')
var chance = new Chance(42)
var expect = require('unexpected')

@@ -19,2 +18,8 @@

describe('chance-generators', function () {
var chance
beforeEach(() => {
chance = new Chance(42)
})
describe('constructor', () => {

@@ -52,2 +57,22 @@ describe('given a seed', () => {

})
describe('map', () => {
it('returns a new generator where the generated values are mapped with the given function', () => {
expect(
chance.integer({ min: -10, max: 10 }).map(v => v + 10),
'when called',
'to be within', 0, 20
)
})
describe('shrink', () => {
it('returns a new generator where the input is shrunken with with regards to the original generator', () => {
var generator = chance.integer({ min: -10, max: 10 }).map(v => v + 10)
while (generator.shrink) {
generator = generator.shrink(generator())
}
expect(generator, 'when called', 'to equal', 10)
})
})
})
})

@@ -65,2 +90,28 @@

})
describe('map', () => {
it('returns a new generator where the generated values are mapped with the given function', () => {
var generator = chance.string.map(s => s.toUpperCase()).map(s => s.replace(/[^A-Z]/g, '-'))
expect(
generator,
'when called',
'to match', /[A-Z-]*/
)
})
describe('shrink', () => {
it('returns a new generator where the input is shrunken with with regards to the original generator', () => {
var generator = chance.string({ length: chance.natural({ max: 10 }) })
.map(s => s.toUpperCase())
.map(s => s.replace(/[^A-Z]/g, '-'))
while (generator.shrink) {
generator = generator.shrink(generator())
}
expect(generator, 'when called', 'to equal', '')
})
})
})
})

@@ -101,3 +152,3 @@

describe('shrink', () => {
it('result a new generator that work on the provided data', () => {
it('returns a new generator that work on the provided data', () => {
var generator = chance.n(chance.string, chance.integer({ min: 2, max: 4 }))

@@ -150,10 +201,8 @@ for (var i = 0; i < 3; i += 1) {

describe('shrink', () => {
it('result a new generator that work on the provided data', () => {
it('returns a new generator that work on the provided data', () => {
var generator = chance.array(chance.string)
for (var i = 0; i < 3; i += 1) {
var generatedValue = generator()
generator = generator.shrink(generatedValue)
expect(generator, 'when called', 'to have items satisfying',
'to be contained by', generatedValue)
while (generator.shrink) {
generator = generator.shrink(generator())
}
expect(generator, 'when called', 'to be empty')
})

@@ -255,3 +304,3 @@ })

describe('shrink', () => {
it('result a new generator that work on the provided data', () => {
it('returns a new generator that work on the provided data', () => {
var generator = chance.shape({

@@ -274,3 +323,33 @@ constant: 42,

})
describe('map', () => {
it('returns a new generator where the generated values are mapped with the given function', () => {
var generator = chance.shape({
x: chance.integer,
y: chance.integer
}).map(coordinate => `${coordinate.x},${coordinate.y}`)
expect(
generator,
'when called',
'to match', /\d+,\d+/
)
})
describe('shrink', () => {
it('returns a new generator where the input is shrunken with with regards to the original generator', () => {
var generator = chance.shape({
x: chance.integer({ min: -20, max: 20 }),
y: chance.integer({ min: -20, max: 20 })
}).map(coordinate => `${coordinate.x},${coordinate.y}`)
for (var i = 0; i < 10; i += 1) {
generator = generator.shrink(generator())
}
expect(generator, 'when called', 'to equal', '0,0')
})
})
})
})
})

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc