chance-generators
Advanced tools
Comparing version 2.3.0 to 2.4.0
@@ -120,3 +120,16 @@ /*global define*/ | ||
unique: function (generator, count, options) { | ||
return createGenerator('unique', [generator, count], [generator]) | ||
var uniqueGenerator = generatorFunction('unique', [generator, count], function () { | ||
const comparator = options && options.comparator | ||
return comparator | ||
? chance.unique(() => generator(), unwrap(count), { comparator: comparator }) | ||
: chance.unique(generator, unwrap(count)) | ||
}) | ||
installMapFunction(uniqueGenerator) | ||
uniqueGenerator.shrink = function (data) { | ||
return shrinkers.unique(uniqueGenerator, data) | ||
} | ||
return uniqueGenerator | ||
} | ||
@@ -231,5 +244,11 @@ } | ||
var dataGenerator = generator.args[0] | ||
var minCount = 0 | ||
return that.unique(dataGenerator, count) | ||
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 }) | ||
}, | ||
@@ -236,0 +255,0 @@ shape: function (generator, data) { |
{ | ||
"name": "chance-generators", | ||
"version": "2.3.0", | ||
"version": "2.4.0", | ||
"description": "Random generators based on changejs", | ||
@@ -5,0 +5,0 @@ "main": "lib/chance-generators.js", |
@@ -508,2 +508,13 @@ /*global describe, it*/ | ||
describe('given a generator and a number', () => { | ||
it('generates arrays with unique values from that generator', () => { | ||
const arr = [42, 13, 666, 3.14] | ||
const generator = chance.unique(chance.pick(arr), 2) | ||
expect([generator(), generator(), generator(), generator()], 'to satisfy', [ | ||
[ 13, 3.14 ], | ||
[ 3.14, 42 ], | ||
[ 666, 3.14 ], | ||
[ 666, 42 ] | ||
]) | ||
}) | ||
it('returns a new generator that returns the specified number of unique items generated by the given generator', () => { | ||
@@ -518,3 +529,36 @@ const arr = [42, 'foo', { wat: 'taw' }] | ||
describe('when given a comparator function', () => { | ||
it('uses the comparator function to decide if items are equal', () => { | ||
const persons = chance.pickone([ | ||
'Sune', 'Andreas', 'Gustav', 'Alex', 'Peter', | ||
'Sune', 'Andreas', 'Gustav', 'Alex', 'Peter', | ||
'Sune', 'Andreas', 'Gustav', 'Alex', 'Peter', | ||
'Sune', 'Andreas', 'Gustav', 'Alex', 'Peter', | ||
'Sune', 'Andreas', 'Gustav', 'Alex', 'Peter' | ||
].map(name => ({ name }))) | ||
expect(chance.unique(persons, 5, { | ||
comparator: (arr, person) => arr.some(p => p.name === person.name) | ||
}), 'when called', 'to satisfy', [ | ||
{ name: 'Peter' }, | ||
{ name: 'Alex' }, | ||
{ name: 'Andreas' }, | ||
{ name: 'Gustav' }, | ||
{ name: 'Sune' } | ||
]) | ||
}) | ||
}) | ||
describe('shrink', () => { | ||
it('generates smaller sub-sets of the given data', () => { | ||
const generator = chance.unique(chance.state, chance.natural({ max: 10 })) | ||
const shrunkenGenerator = generator.shrink(['SC', 'WV', 'FL']) | ||
expect([shrunkenGenerator(), shrunkenGenerator(), shrunkenGenerator()], 'to satisfy', [ | ||
['SC'], | ||
['SC', 'WV', 'FL'], | ||
['SC', 'WV'] | ||
]) | ||
}) | ||
it('returns a new generator generating smaller unique sets', () => { | ||
@@ -521,0 +565,0 @@ let generator = chance.unique(chance.state, chance.natural({ max: 10 })) |
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
48325
1048