Comparing version 0.1.7 to 0.1.8
@@ -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.7", | ||
"version": "0.1.8", | ||
"main": "index.js", | ||
@@ -9,0 +9,0 @@ "url": "https://github.com/joshjung/hash-array", |
@@ -11,2 +11,4 @@ var JClass = require('jclass'); | ||
this.isHashArray = true; | ||
if (callback) { | ||
@@ -63,2 +65,7 @@ callback('construct'); | ||
getAll: function(keys) { | ||
keys = keys instanceof Array ? keys : [keys]; | ||
if (keys[0] == '*') | ||
return this.all; | ||
var res = new HashArray(this.keyFields); | ||
@@ -208,5 +215,8 @@ for (var key in keys) | ||
}, | ||
sum: function(keys, key) { | ||
sum: function(keys, key, weightKey) { | ||
var ret = 0; | ||
this.forEachDeep(keys, key, function (value) { | ||
if (weightKey !== undefined) | ||
value *= self.find(item, weightKey); | ||
ret += value; | ||
@@ -236,2 +246,16 @@ }); | ||
return weightKey !== undefined ? ret : ret / tot; | ||
}, | ||
filter: function (keys, callbackOrKey) { | ||
var self = this; | ||
var callback = (typeof(callbackOrKey) == 'function') ? callbackOrKey : defaultCallback; | ||
var ha = new HashArray(this.keyFields); | ||
ha.addAll(this.getAll(keys).filter(callback)); | ||
return ha; | ||
function defaultCallback(item) { | ||
var val = self.find(item, callbackOrKey); | ||
return val !== undefined && val !== false; | ||
} | ||
} | ||
@@ -238,0 +262,0 @@ }); |
@@ -534,2 +534,34 @@ var assert = require('assert'), | ||
}); | ||
describe('filter(keys, callback) should work and return new HashArray', function() { | ||
var ha = new HashArray(['type']); | ||
var a = {type: 'airplane', data: {speed: 100, weight: 0.1, mobile: true}}, | ||
b = {type: 'airplane', data: {speed: 50, weight: 0.2, mobile: true}}, | ||
c = {type: 'airplane', data: {speed: 25, weight: 0.2, mobile: false}}; | ||
d = {type: 'boat', data: {speed: 10, weight: 0.2, mobile: true}}; | ||
e = {type: 'boat', data: {speed: 5, weight: 0.3, mobile: true}}; | ||
ha.add(a, b, c, d, e); | ||
it('should return a new HashArray', function() { | ||
assert.equal(ha.filter('*', function (item) { | ||
return item.data.speed == 100; | ||
}).isHashArray, true); | ||
}); | ||
it('should return a new HashArray with the right length of items', function() { | ||
assert.equal(ha.filter('*', function (item) { | ||
return item.data.speed == 100; | ||
}).all.length, 1); | ||
}); | ||
it('should work with a key for the callback', function() { | ||
assert.equal(ha.filter('airplane', ['data', 'mobile']).all.length, 2); | ||
}); | ||
it('should work with a key for the callback for a non-existent key', function() { | ||
assert.equal(ha.filter('airplane', 'does not exist').all.length, 0); | ||
}); | ||
}); | ||
}); |
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
28163
688