chance-generators
Advanced tools
Comparing version 2.2.0 to 2.3.0
@@ -157,7 +157,10 @@ /*global define*/ | ||
var count = generator.args[1] | ||
if (count && count.shrink) { | ||
count = count.shrink(data.length) | ||
} else { | ||
count = data.length | ||
var length = generator.args[1] | ||
var minLength = 0 | ||
if (typeof length === 'number') { | ||
minLength = length | ||
} else if (length && length.isGenerator && length.args[0] && length.args[0].min) { | ||
minLength = length.args[0].min | ||
} | ||
@@ -167,5 +170,5 @@ | ||
if (dataGenerator.shrink) { | ||
return that.pickset(data.map(dataGenerator.shrink), count) | ||
return that.arraySplicer(data.map(dataGenerator.shrink), { min: minLength }) | ||
} else { | ||
return that.pickset(data, count) | ||
return that.arraySplicer(data, { min: minLength }) | ||
} | ||
@@ -269,14 +272,3 @@ }, | ||
return that.natural({ min: minLength, max: data.length }).map(function (n) { | ||
var result = new Array(data.length) | ||
var likelihood = n / data.length * 100 | ||
for (var i = 0, j = 0; i < data.length; i += 1) { | ||
if (chance.bool({ likelihood: likelihood })) { | ||
result[j++] = data[i] | ||
} | ||
} | ||
return result.join('') | ||
}) | ||
return that.stringSplicer(data, { min: minLength }) | ||
}, | ||
@@ -355,2 +347,29 @@ integer: minMaxShrinker, | ||
that.stringSplicer = generatorFunction('stringSplicer', [], function (text, options) { | ||
if (typeof text !== 'string') { | ||
throw new Error('The stringSplicer requires a string as the first argument') | ||
} | ||
var min = (options || {}).min || 0 | ||
var g = generatorFunction('stringSplicer', [text, options], function () { | ||
var from = chance.natural({ max: text.length }) | ||
var length = chance.natural({ max: text.length - min }) | ||
return text.slice(0, from) + text.slice(from + length) | ||
}) | ||
g.shrink = function (data) { | ||
if (data.length === min) { | ||
return that.constant(data) | ||
} | ||
return that.stringSplicer(data, options) | ||
} | ||
installMapFunction(g) | ||
return g | ||
}) | ||
that.array = generatorFunction('array', [], function (generator, count) { | ||
@@ -364,2 +383,47 @@ if (typeof count === 'undefined') { | ||
that.arraySplicer = generatorFunction('arraySplicer', [], function (array, options) { | ||
if (!Array.isArray(array)) { | ||
throw new Error('The arraySplicer requires an array as the first argument') | ||
} | ||
var min = (options || {}).min || 0 | ||
var g = generatorFunction('arraySplicer', [array, options], function () { | ||
var from = chance.natural({ max: array.length }) | ||
var length = chance.natural({ max: array.length - min }) | ||
g.lastValue = array.slice() | ||
g.lastValue.splice(from, length) | ||
g.lastUnwrappedValue = unwrap(g.lastValue) | ||
return g.lastUnwrappedValue | ||
}) | ||
g.shrink = function (data) { | ||
var shrinkableData = (g.lastValue || []).some(function (g) { | ||
return g && g.shrink | ||
}) | ||
if (!shrinkableData && data.length === min) { | ||
return that.constant(data) | ||
} | ||
if (shrinkableData && data.length < 10 && g.lastUnwrappedValue === data) { | ||
data = g.lastValue.map(function (g, i) { | ||
return g && g.shrink | ||
? g.shrink(data[i]) | ||
: data[i] | ||
}) | ||
} else { | ||
data = g.lastValue | ||
} | ||
return that.arraySplicer(data, options) | ||
} | ||
installMapFunction(g) | ||
return g | ||
}) | ||
that.sequence = generatorFunction('sequence', [], function (fn, count) { | ||
@@ -366,0 +430,0 @@ count = typeof count === 'undefined' |
{ | ||
"name": "chance-generators", | ||
"version": "2.2.0", | ||
"version": "2.3.0", | ||
"description": "Random generators based on changejs", | ||
@@ -5,0 +5,0 @@ "main": "lib/chance-generators.js", |
@@ -113,3 +113,3 @@ /*global describe, it*/ | ||
], 'to satisfy', [ | ||
'oa', 'fo', '', 'ab', 'foobrbaz' | ||
'foo', 'foobarbaz', 'foobarb', 'fooba', 'frbaz' | ||
]) | ||
@@ -156,2 +156,80 @@ }) | ||
describe('stringSplicer', () => { | ||
it('throw when not given a string', () => { | ||
expect(() => { | ||
chance.stringSplicer() | ||
}, 'to throw', 'The stringSplicer requires a string as the first argument') | ||
expect(() => { | ||
chance.stringSplicer({}) | ||
}, 'to throw', 'The stringSplicer requires a string as the first argument') | ||
}) | ||
it('produces a string by splicing out regions from the given string', () => { | ||
const generator = chance.stringSplicer('foobarbaz') | ||
expect([ | ||
generator(), | ||
generator(), | ||
generator(), | ||
generator(), | ||
generator(), | ||
generator(), | ||
generator() | ||
], 'to equal', [ | ||
'foo', 'foobarbaz', 'foobarb', 'fooba', 'frbaz', 'foobarbaz', 'arbaz' | ||
]) | ||
}) | ||
it('shrinks towards the empty string', () => { | ||
let generator = chance.stringSplicer('foobarbaz') | ||
while (generator.shrink) { | ||
generator = generator.shrink(generator()) | ||
} | ||
expect(generator, 'when called', 'to be empty') | ||
}) | ||
describe('when given a min', () => { | ||
it('shrinks towards a string of the min length', () => { | ||
let generator = chance.stringSplicer('foobarbaz', { min: 3 }) | ||
while (generator.shrink) { | ||
generator = generator.shrink(generator()) | ||
} | ||
expect(generator, 'when called', 'to have length', 3) | ||
}) | ||
it('produces a string by splicing out regions from the given string, while preserving the min constraint', () => { | ||
const generator = chance.stringSplicer('foobarbaz', { min: 6 }) | ||
expect([ | ||
generator(), | ||
generator(), | ||
generator(), | ||
generator(), | ||
generator(), | ||
generator(), | ||
generator() | ||
], 'to equal', [ | ||
'foobaz', 'foobarbaz', 'foobarb', 'foobaaz', 'fobarbaz', 'foobarbaz', 'oobarbaz' | ||
]) | ||
}) | ||
}) | ||
it('supports the map method', () => { | ||
const generator = chance.stringSplicer('foobarbaz').map(s => '>' + s) | ||
expect([ | ||
generator(), | ||
generator(), | ||
generator(), | ||
generator(), | ||
generator(), | ||
generator(), | ||
generator() | ||
], 'to equal', [ | ||
'>foo', '>foobarbaz', '>foobarb', '>fooba', '>frbaz', '>foobarbaz', '>arbaz' | ||
]) | ||
}) | ||
}) | ||
describe('n', () => { | ||
@@ -246,2 +324,111 @@ describe('given a generator function', () => { | ||
describe('arraySplicer', () => { | ||
it('throw when not given an array', () => { | ||
expect(() => { | ||
chance.arraySplicer() | ||
}, 'to throw', 'The arraySplicer requires an array as the first argument') | ||
expect(() => { | ||
chance.arraySplicer({}) | ||
}, 'to throw', 'The arraySplicer requires an array as the first argument') | ||
}) | ||
it('produces a array by splicing out regions from the given array', () => { | ||
const generator = chance.arraySplicer('foobarbaz'.split('')) | ||
expect([ | ||
generator(), | ||
generator(), | ||
generator(), | ||
generator(), | ||
generator(), | ||
generator(), | ||
generator() | ||
], 'to equal', [ | ||
[ 'f', 'o', 'o' ], | ||
[ 'f', 'o', 'o', 'b', 'a', 'r', 'b', 'a', 'z' ], | ||
[ 'f', 'o', 'o', 'b', 'a', 'r', 'b' ], | ||
[ 'f', 'o', 'o', 'b', 'a' ], | ||
[ 'f', 'r', 'b', 'a', 'z' ], | ||
[ 'f', 'o', 'o', 'b', 'a', 'r', 'b', 'a', 'z' ], | ||
[ 'a', 'r', 'b', 'a', 'z' ] | ||
]) | ||
}) | ||
describe('when given items that are generators', () => { | ||
it('unwraps the items', () => { | ||
const valueGenerator = chance.natural({ max: 10 }) | ||
const generator = chance.arraySplicer([valueGenerator, valueGenerator, 'foo', 42]) | ||
expect([ | ||
generator(), | ||
generator(), | ||
generator(), | ||
generator(), | ||
generator(), | ||
generator(), | ||
generator() | ||
], 'to equal', [ | ||
[ 10 ], | ||
[ 42 ], | ||
[ 6, 1, 'foo' ], | ||
[ 1, 0, 'foo', 42 ], | ||
[ 3, 6 ], [ 42 ], | ||
[ 0, 10, 'foo', 42 ] | ||
]) | ||
}) | ||
it('shrinks the item generators', () => { | ||
const valueGenerator = chance.natural({ min: 2, max: 10 }) | ||
let generator = chance.arraySplicer([valueGenerator, valueGenerator, 'foo', 42], { min: 2 }) | ||
while (generator.shrink) { | ||
generator = generator.shrink(generator()) | ||
} | ||
expect(generator(), 'to equal', [ 2, 42 ]) | ||
}) | ||
}) | ||
it('shrinks towards the empty array', () => { | ||
let generator = chance.arraySplicer('foobarbaz'.split('')) | ||
while (generator.shrink) { | ||
generator = generator.shrink(generator()) | ||
} | ||
expect(generator, 'when called', 'to be empty') | ||
}) | ||
describe('when given a min', () => { | ||
it('shrinks towards an array of the min length', () => { | ||
let generator = chance.arraySplicer('foobarbaz'.split(''), { min: 3 }) | ||
while (generator.shrink) { | ||
generator = generator.shrink(generator()) | ||
} | ||
expect(generator, 'when called', 'to have length', 3) | ||
}) | ||
it('produces a string by splicing out regions from the given array, while preserving the min constraint', () => { | ||
const generator = chance.arraySplicer('foobarbaz'.split(''), { min: 6 }) | ||
expect([ | ||
generator(), | ||
generator(), | ||
generator(), | ||
generator(), | ||
generator(), | ||
generator(), | ||
generator() | ||
], 'to equal', [ | ||
[ 'f', 'o', 'o', 'b', 'a', 'z' ], | ||
[ 'f', 'o', 'o', 'b', 'a', 'r', 'b', 'a', 'z' ], | ||
[ 'f', 'o', 'o', 'b', 'a', 'r', 'b' ], | ||
[ 'f', 'o', 'o', 'b', 'a', 'a', 'z' ], | ||
[ 'f', 'o', 'b', 'a', 'r', 'b', 'a', 'z' ], | ||
[ 'f', 'o', 'o', 'b', 'a', 'r', 'b', 'a', 'z' ], | ||
[ 'o', 'o', 'b', 'a', 'r', 'b', 'a', 'z' ] | ||
]) | ||
}) | ||
}) | ||
}) | ||
describe('shuffle', () => { | ||
@@ -248,0 +435,0 @@ describe('given an array', () => { |
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
46063
994