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

weighted

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

weighted - npm Package Compare versions

Comparing version 0.2.2 to 0.3.0

examples/simple.js

16

lib/weighted.js

@@ -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)
})
})
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