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

chance-generators

Package Overview
Dependencies
Maintainers
2
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 2.5.1 to 2.6.0

81

lib/chance-generators.js

@@ -24,2 +24,14 @@ /*global define*/

function getMin(value) {
if (typeof value === 'number') {
return value
}
if (value && value.isGenerator && value.args[0] && value.args[0].min) {
return value.args[0].min
}
return 0
}
function unwrap (v) {

@@ -153,2 +165,21 @@ if (Array.isArray(v)) {

return uniqueGenerator
},
weighted: function (data, weights) {
var generator = generatorFunction('weighted', [data, weights], function () {
generator.lastValue = chance.weighted(data, weights)
generator.lastUnwrappedValue = unwrap(generator.lastValue)
return generator.lastUnwrappedValue
})
installMapFunction(generator)
generator.shrink = function (data) {
return shrinkers.weighted(generator, data)
}
generator.expand = function (data) {
return expanders.weighted(generator, data)
}
return generator
}

@@ -192,10 +223,4 @@ }

var minLength = 0
var minLength = getMin(length)
if (typeof length === 'number') {
minLength = length
} else if (length && length.isGenerator && length.args[0] && length.args[0].min) {
minLength = length.args[0].min
}
var dataGenerator = generator.args[0]

@@ -258,16 +283,4 @@ if (dataGenerator.shrink) {

var count = generator.args[1]
if (count && count.shrink) {
count = count.shrink(data.length)
} else {
count = data.length
}
var minCount = getMin(count)
var minCount = 0
if (typeof count === 'number') {
minCount = count
} else if (count && count.isGenerator && count.args[0] && count.args[0].min) {
minCount = count.args[0].min
}
return that.arraySplicer(data, { min: minCount })

@@ -298,3 +311,2 @@ },

var options = generator.args[0] || {}
var length = options.length

@@ -305,14 +317,15 @@ if (data.length === 0) {

var minLength = 0
var length = options.length
var minLength = getMin(length)
if (typeof length === 'number') {
minLength = length
} else if (length && length.min) {
minLength = options.length.min
} else if (length && length.isGenerator && length.args[0] && length.args[0].min) {
minLength = length.args[0].min
}
return that.stringSplicer(data, { min: minLength })
},
weighted: function (generator, data) {
var shrinkable = generator.lastUnwrappedValue === data && generator.lastValue.shrink
if (shrinkable) {
return generator.lastValue.shrink(data)
} else {
return that.constant(data)
}
},
integer: minMaxShrinker,

@@ -400,2 +413,10 @@ natural: minMaxShrinker,

return that.shape(newShape)
},
weighted: function (generator, data) {
var expandable = generator.lastUnwrappedValue === data && generator.lastValue.expand
if (expandable) {
return generator.lastValue.expand(data)
} else {
return that.constant(data)
}
}

@@ -402,0 +423,0 @@ }

{
"name": "chance-generators",
"version": "2.5.1",
"version": "2.6.0",
"description": "Random generators based on changejs",

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

@@ -654,2 +654,78 @@ /*global describe, it*/

describe('weighted', () => {
describe('given an array of values and weights', () => {
it('generates values picks values from the array based on the weights', () => {
const arr = [42, chance.string, 666, chance.character]
const weights = [10, 20, 1, 20]
const generator = chance.weighted(arr, weights)
expect([generator(), generator(), generator(), generator()], 'to satisfy', [
'(n25SSlGlheH#ySk0', 'b', 42, '1'
])
})
})
describe('shrink', () => {
describe('when the picked value is a generator', () => {
it('shrinks the value with regards to the picked generator', () => {
const arr = [chance.string, chance.natural({ min: 10, max: 20 })]
const weights = [10, 20]
const generator = chance.weighted(arr, weights)
const value = generator()
const shrunkenGenerator = generator.shrink(value)
expect([value, shrunkenGenerator(), shrunkenGenerator(), shrunkenGenerator()], 'to satisfy', [
18, 18, 11, 16
])
})
})
describe('when the picked value is not a generator', () => {
it('does not shrink the value', () => {
const arr = [0, 1]
const weights = [10, 20]
const generator = chance.weighted(arr, weights)
const value = generator()
const shrunkenGenerator = generator.shrink(value)
expect([value, shrunkenGenerator(), shrunkenGenerator(), shrunkenGenerator()], 'to satisfy', [
1, 1, 1, 1
])
})
})
})
describe('expand', () => {
describe('when the picked value is a generator', () => {
it('expands the value with regards to the picked generator', () => {
const arr = [chance.natural({ min: 10, max: 20 }), chance.string]
const weights = [10, 20]
const generator = chance.weighted(arr, weights)
const value = generator()
const expandedGenerator = generator.expand(value)
expect([value, expandedGenerator(), expandedGenerator(), expandedGenerator()], 'to satisfy', [
'(n25SSlGlheH#ySk0',
'(n25SSlGnheH#ySk0',
'(n25SSlGlheH#ySk0',
'(n25SSlGlheH#ySkF'
])
})
})
describe('when the picked value is not a generator', () => {
it('does not expand the value', () => {
const arr = [0, 1]
const weights = [10, 20]
const generator = chance.weighted(arr, weights)
const value = generator()
const expandedGenerator = generator.expand(value)
expect([value, expandedGenerator(), expandedGenerator(), expandedGenerator()], 'to satisfy', [
1, 1, 1, 1
])
})
})
})
})
describe('constant', () => {

@@ -656,0 +732,0 @@ it('generate the given value', () => {

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