Comparing version 0.1.5 to 0.1.6
@@ -6,3 +6,3 @@ { | ||
"description": "A data structure that combines a hash and an array for CRUD operations by object keys or index.", | ||
"version": "0.1.5", | ||
"version": "0.1.6", | ||
"main": "index.js", | ||
@@ -9,0 +9,0 @@ "url": "https://github.com/joshjung/hash-array", |
@@ -41,2 +41,5 @@ var JClass = require('jclass'); | ||
}, | ||
addAll: function (arr) { | ||
this.add.apply(this, arr); | ||
}, | ||
addMap: function(key, obj) { | ||
@@ -64,2 +67,33 @@ this._map[key] = obj; | ||
}, | ||
getUniqueRandomIntegers: function (count, min, max) { | ||
var res = [], map = {}; | ||
count = Math.min(max - min, count); | ||
while (res.length < count) | ||
{ | ||
var r = Math.floor(min + (Math.random() * (max + 1))); | ||
if (map[r]) continue; | ||
map[r] = true; | ||
res.push(r); | ||
} | ||
return res; | ||
}, | ||
sample: function (count, keys) { | ||
// http://en.wikipedia.org/wiki/Image_(mathematics) | ||
var image = this.all, | ||
ixs = {}, | ||
res = []; | ||
if (keys) | ||
image = this.getAll(keys); | ||
var rand = this.getUniqueRandomIntegers(count, 0, image.length - 1); | ||
for (var i = 0; i < rand.length; i++) | ||
res.push(image[rand[i]]); | ||
return res; | ||
}, | ||
has: function(key) { | ||
@@ -66,0 +100,0 @@ return this._map.hasOwnProperty(key); |
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
20786
501