Comparing version 0.2.2 to 0.3.0
@@ -34,3 +34,3 @@ function getTotal(weights) { | ||
if (set.length !== weights.length) { | ||
throw new Error('Different number of options & weights.') | ||
throw new TypeError('Different number of options & weights.') | ||
} | ||
@@ -50,3 +50,3 @@ | ||
throw new Error('All weights do not add up to >= 1 as expected.') | ||
throw new RangeError('All weights do not add up to >= 1 as expected.') | ||
} | ||
@@ -75,2 +75,8 @@ | ||
if (Array.isArray(set)) { | ||
if (weights == null) { | ||
weights = set.map(function () { | ||
return 1 | ||
}) | ||
} | ||
if (Array.isArray(weights)) { | ||
@@ -81,6 +87,6 @@ if (set.length === weights.length) { | ||
throw new Error('Set and Weights are different sizes.') | ||
throw new TypeError('Set and Weights are different sizes.') | ||
} | ||
throw new Error('Set is an Array, and Weights is not.') | ||
throw new TypeError('Set is an Array, and Weights is not.') | ||
} | ||
@@ -92,3 +98,3 @@ | ||
throw new Error('Set is not an Object, nor is it an Array.') | ||
throw new TypeError('Set is not an Object, nor is it an Array.') | ||
} | ||
@@ -95,0 +101,0 @@ |
{ | ||
"name": "weighted", | ||
"version": "0.2.2", | ||
"version": "0.3.0", | ||
"description": "A dead-simple module for picking a random item with weights.", | ||
@@ -10,3 +10,3 @@ "main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1", | ||
"test": "mocha test", | ||
"bench": "survey bench/weighted.js" | ||
@@ -29,4 +29,6 @@ }, | ||
"devDependencies": { | ||
"chai": "~3.0.0", | ||
"mocha": "~2.2.5", | ||
"survey": "~0.3.2" | ||
} | ||
} |
var weighted = require('../') | ||
, optionsArr = ['Wake Up', 'Snooze'] | ||
, weightsArr = [0.25, 0.75] | ||
, optionsObj = { | ||
'Wake Up': 0.25 | ||
, 'Snooze': 0.75 | ||
} | ||
var expect = require('chai').expect | ||
var OPTIONS_ARR_5 = [1, 2, 3, 4, 5] | ||
var WEIGHTS_ARR_5 = [0.2, 0.4, 0.1, 0.2, 0.1] | ||
var WEIGHTS_ARR_4 = [0.2, 0.4, 0.2, 0.2] | ||
var OPTIONS_OBJ = OPTIONS_ARR_5.reduce(function (obj, key, index) { | ||
obj[key] = WEIGHTS_ARR_5[index] | ||
return obj | ||
}, {}) | ||
console.log(weighted.select(optionsArr, weightsArr)) | ||
console.log(weighted.select(optionsObj)) | ||
describe('Weighted', function () { | ||
it('should export a Function', function () { | ||
expect(weighted).to.be.a('function') | ||
}) | ||
it('should export as .select()', function () { | ||
expect(weighted.select).to.be.a('function') | ||
expect(weighted.select).to.equal(weighted) | ||
}) | ||
describe('Array, Array => Value', function () { | ||
it('should return a value', function () { | ||
expect(weighted.select(OPTIONS_ARR_5, WEIGHTS_ARR_5)).to.be.a('number') | ||
}) | ||
it('should accept Array, Null', function () { | ||
expect(weighted.select(OPTIONS_ARR_5)).to.be.a('number') | ||
}) | ||
it('should reject Array[m], Array[n] where m != n', function () { | ||
expect(function () { | ||
weighted.select(OPTIONS_ARR_5, WEIGHTS_ARR_4) | ||
}).to.throw(TypeError) | ||
}) | ||
it('should reject Array, Object', function () { | ||
expect(function () { | ||
weighted.select(OPTIONS_ARR_5, OPTIONS_OBJ) | ||
}).to.throw(TypeError) | ||
}) | ||
it('should reject Array, Primitive', function () { | ||
expect(function () { | ||
weighted.select(OPTIONS_ARR_5, 42) | ||
}).to.throw(TypeError) | ||
}) | ||
}) | ||
describe('Object => Value', function () { | ||
it('should return a value', function () { | ||
expect(weighted.select(OPTIONS_OBJ)).to.be.a('string') | ||
}) | ||
}) | ||
it('should reject other calling forms', function () { | ||
expect(function () { | ||
weighted.select(1, 2, 3, 4, 5) | ||
}).to.throw(TypeError) | ||
}) | ||
}) |
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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
11350
9
211
2
0
3