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.0.1 to 1.1.0

152

lib/chance-generators.js

@@ -12,2 +12,16 @@ /*global define*/

})(this, function (Chance) {
// Fix that pick provided a count of zero or one does not return an array
var originalPick = Chance.prototype.pick
Chance.prototype.pick = function (array, count) {
if (count === 0) {
return []
}
if (count === 1) {
return [originalPick.call(this, array, count)]
}
return originalPick.call(this, array, count)
}
function ExtendedChance (seed) {

@@ -24,20 +38,21 @@ if (!(this instanceof ExtendedChance)) {

function unwrap (v) {
return v && v.isGenerator ? v() : v
}
function deepUnwrap (v) {
if (Array.isArray(v)) {
return v.map(deepUnwrap)
return v.map(unwrap)
} else if (v && typeof v === 'object') {
return Object.keys(v).reduce(function (result, key) {
result[key] = deepUnwrap(v[key])
result[key] = unwrap(v[key])
return result
}, {})
} else {
return unwrap(v)
return v && v.isGenerator ? v() : v
}
}
function markGenerator (f) {
function generatorFunction (name, args, f) {
f.isGenerator = true
f.generatorName = name
f.args = args
f.toString = function () {
return name
}
return f

@@ -48,13 +63,103 @@ }

n: function (generator, count) {
return markGenerator(function () {
return chance.n(generator, unwrap(count))
})
return createGenerator('n', [generator, count], [generator])
},
unique: function (generator, count, options) {
return markGenerator(function () {
return chance.unique(generator, unwrap(count))
})
return createGenerator('unique', [generator, count], [generator])
}
}
function minMaxShrinker (generator, data) {
var currentLimits = generator.args[0] || {}
var limits
if (data < 0) {
limits = {
min: data,
max: Math.min(0, currentLimits.max || 0)
}
} else {
limits = {
min: Math.max(0, currentLimits.min || 0),
max: data
}
}
return createGenerator(generator.generatorName, [limits])
}
var shrinkers = {
n: function (generator, data) {
if (data.length === 0) {
return that.identity(data)
}
var count = generator.args[1]
if (count && count.shrink) {
count = count.shrink(data.length)
} else {
count = data.length
}
return createGenerator('pick', [data, count])
},
pick: function (generator, data) {
if (data.length === 0) {
return that.identity(data)
}
var count = generator.args[1]
if (count && count.shrink) {
count = count.shrink(data.length)
} else if (typeof count !== 'number') {
count = that.integer({ min: 0, max: data.length })
}
return createGenerator('pick', [data, count])
},
unique: function (generator, data) {
var count = generator.args[1]
if (count && count.shrink) {
count = count.shrink(data.length)
} else {
count = data.length
}
return createGenerator('pick', [data, count])
},
integer: minMaxShrinker,
natural: minMaxShrinker,
floating: minMaxShrinker
}
function createGenerator (name, args, omitUnwap) {
var omitUnwrapIndex = {}
omitUnwap && args.forEach(function (arg, i) {
if (omitUnwap.indexOf(arg) !== -1) {
omitUnwrapIndex[i] = true
}
})
var g = generatorFunction(name, args, function () {
if (arguments.length === 0) {
return chance[name].apply(chance, args.map(function (arg, i) {
return omitUnwrapIndex[i] ? arg : unwrap(arg)
}))
} else {
return createGenerator(
name,
Array.prototype.slice.call(arguments)
)
}
})
var shrinker = shrinkers[name]
if (shrinker) {
g.shrink = function (data) {
return shrinker(g, data)
}
}
return g
}
Object.keys(Chance.prototype).forEach(function (key) {

@@ -64,14 +169,5 @@ var property = chance[key]

if (overrides[key]) {
that[key] = markGenerator(overrides[key])
that[key] = generatorFunction(key, [], overrides[key])
} else {
that[key] = markGenerator(function () {
if (arguments.length === 0) {
return property.apply(chance, arguments)
} else {
var args = Array.prototype.slice.call(arguments)
return markGenerator(function () {
return property.apply(chance, args.map(deepUnwrap))
})
}
})
that[key] = createGenerator(key, [])
}

@@ -82,2 +178,8 @@ } else {

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

@@ -84,0 +186,0 @@

2

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

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

@@ -13,3 +13,3 @@ /*global describe, it*/

expect(arr, 'to have items satisfying', function (item) {
expect(item, 'not to be contained by', seen)
expect(seen.indexOf(item), 'to be', -1)
seen.push(item)

@@ -46,2 +46,8 @@ })

})
describe('toString', () => {
it('returns the name of the generator', () => {
expect(chance.integer({ max: 10 }).toString(), 'to be', 'integer')
})
})
})

@@ -92,2 +98,14 @@

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

@@ -94,0 +112,0 @@

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