Comparing version 0.1.6 to 0.1.7
@@ -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.6", | ||
"version": "0.1.7", | ||
"main": "index.js", | ||
@@ -9,0 +9,0 @@ "url": "https://github.com/joshjung/hash-array", |
@@ -42,3 +42,8 @@ var JClass = require('jclass'); | ||
addAll: function (arr) { | ||
this.add.apply(this, arr); | ||
if (arr.length < 100) | ||
this.add.apply(this, arr); | ||
else { | ||
for (var i = 0; i < arr.length; i++) | ||
this.add(arr[i]); | ||
} | ||
}, | ||
@@ -70,3 +75,3 @@ addMap: function(key, obj) { | ||
count = Math.min(max - min, count); | ||
count = Math.min(Math.max(max - min, 1), count); | ||
@@ -181,2 +186,19 @@ while (res.length < count) | ||
}, | ||
forEach: function(keys, callback) { | ||
keys = keys instanceof Array ? keys : [keys]; | ||
var objs = this.getAll(keys); | ||
objs.forEach(callback); | ||
}, | ||
forEachDeep: function(keys, key, callback) { | ||
keys = keys instanceof Array ? keys : [keys]; | ||
var self = this, | ||
objs = this.getAll(keys); | ||
objs.forEach(function (item) { | ||
callback(self.find(item, key), item); | ||
}); | ||
}, | ||
clone: function(callback) { | ||
@@ -186,3 +208,31 @@ var n = new HashArray(this.keyFields.concat(), callback ? callback : this.callback); | ||
return n; | ||
} | ||
}, | ||
sum: function(keys, key) { | ||
var ret = 0; | ||
this.forEachDeep(keys, key, function (value) { | ||
ret += value; | ||
}); | ||
return ret; | ||
}, | ||
average: function(keys, key, weightKey) { | ||
var ret = 0, | ||
tot = 0, | ||
weightsTotal = 0, | ||
self = this; | ||
if (weightKey !== undefined) | ||
this.forEachDeep(keys, weightKey, function (value) { | ||
weightsTotal += value; | ||
}) | ||
this.forEachDeep(keys, key, function (value, item) { | ||
if (weightKey !== undefined) | ||
value *= (self.find(item, weightKey) / weightsTotal); | ||
ret += value; | ||
tot++; | ||
}); | ||
return weightKey !== undefined ? ret : ret / tot; | ||
} | ||
}); | ||
@@ -189,0 +239,0 @@ |
@@ -392,2 +392,144 @@ var assert = require('assert'), | ||
}); | ||
describe('forEach(keys, callback) should work', function() { | ||
var ha = new HashArray(['type']); | ||
var a = {type: 'airplane', data: {speed: 100, weight: 10000}}, | ||
b = {type: 'airplane', data: {speed: 50, weight: 20000}}, | ||
c = {type: 'airplane', data: {speed: 25, weight: 50000}}; | ||
d = {type: 'boat', data: {speed: 10, weight: 100000}}; | ||
e = {type: 'boat', data: {speed: 5, weight: 200000}}; | ||
ha.add(a, b, c, d, e); | ||
it('should work.', function() { | ||
var s = 0; | ||
ha.forEach('airplane', function (airplane) { | ||
s += airplane.data.speed; | ||
}); | ||
assert.equal(s, 175); | ||
}); | ||
it('should work (speed test boats).', function() { | ||
var s = 0; | ||
ha.forEach(['boat'], function (item) { | ||
s += item.data.speed; | ||
}); | ||
assert.equal(s, 15); | ||
}); | ||
it('should work (speed test all).', function() { | ||
var s = 0; | ||
ha.forEach(['airplane', 'boat'], function (item) { | ||
s += item.data.speed; | ||
}); | ||
assert.equal(s, 190); | ||
}); | ||
}); | ||
describe('forEachDeep(keys, key, callback) should work', function() { | ||
var ha = new HashArray(['type']); | ||
var a = {type: 'airplane', data: {speed: 100, weight: 10000}}, | ||
b = {type: 'airplane', data: {speed: 50, weight: 20000}}, | ||
c = {type: 'airplane', data: {speed: 25, weight: 50000}}; | ||
d = {type: 'boat', data: {speed: 10, weight: 100000}}; | ||
e = {type: 'boat', data: {speed: 5, weight: 200000}}; | ||
ha.add(a, b, c, d, e); | ||
it('should work (speed test airplanes).', function() { | ||
var s = 0; | ||
ha.forEachDeep('airplane', ['data', 'speed'], function (speed) { | ||
s += speed; | ||
}); | ||
assert.equal(s, 175); | ||
}); | ||
it('should work (speed test boats).', function() { | ||
var s = 0; | ||
ha.forEachDeep(['boat'], ['data', 'speed'], function (speed) { | ||
s += speed; | ||
}); | ||
assert.equal(s, 15); | ||
}); | ||
it('should work (speed test all).', function() { | ||
var s = 0; | ||
ha.forEachDeep(['airplane', 'boat'], ['data', 'speed'], function (speed) { | ||
s += speed; | ||
}); | ||
assert.equal(s, 190); | ||
}); | ||
}); | ||
describe('sum(keys, key) should work', function() { | ||
var ha = new HashArray(['type']); | ||
var a = {type: 'airplane', data: {speed: 100, weight: 10000}}, | ||
b = {type: 'airplane', data: {speed: 50, weight: 20000}}, | ||
c = {type: 'airplane', data: {speed: 25, weight: 50000}}; | ||
d = {type: 'boat', data: {speed: 10, weight: 100000}}; | ||
e = {type: 'boat', data: {speed: 5, weight: 200000}}; | ||
ha.add(a, b, c, d, e); | ||
it('should work (speed test airplanes).', function() { | ||
assert.equal(ha.sum('airplane', ['data', 'speed']), 175); | ||
}); | ||
it('should work (speed test boats).', function() { | ||
assert.equal(ha.sum(['boat'], ['data', 'speed']), 15); | ||
}); | ||
it('should work (speed test all).', function() { | ||
assert.equal(ha.sum(['airplane', 'boat'], ['data', 'speed']), 190); | ||
}); | ||
}); | ||
describe('average(keys, key, weight) should work', function() { | ||
var ha = new HashArray(['type']); | ||
var a = {type: 'airplane', data: {speed: 100, weight: 0.1}}, | ||
b = {type: 'airplane', data: {speed: 50, weight: 0.2}}, | ||
c = {type: 'airplane', data: {speed: 25, weight: 0.2}}; | ||
d = {type: 'boat', data: {speed: 10, weight: 0.2}}; | ||
e = {type: 'boat', data: {speed: 5, weight: 0.3}}; | ||
ha.add(a, b, c, d, e); | ||
it('should work (speed test airplanes).', function() { | ||
assert.equal(ha.average('airplane', ['data', 'speed']), 175 / 3); | ||
}); | ||
it('should work (speed test boats).', function() { | ||
assert.equal(ha.average(['boat'], ['data', 'speed']), 15 / 2); | ||
}); | ||
it('should work (speed test all).', function() { | ||
assert.equal(ha.average(['airplane', 'boat'], ['data', 'speed']), 190 / 5); | ||
}); | ||
it('should work with weighted average == 1.0.', function() { | ||
assert.equal(ha.average(['airplane', 'boat'], ['data', 'speed'], ['data', 'weight']), 28.5); | ||
}); | ||
it('should work with weighted average != 1.0.', function() { | ||
a.data.weight = 1.1; | ||
assert.equal(ha.average(['airplane', 'boat'], ['data', 'speed'], ['data', 'weight']), 64.25); | ||
}); | ||
}); | ||
}); |
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
26260
646