Comparing version 0.1.4 to 0.1.5
@@ -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.4", | ||
"version": "0.1.5", | ||
"main": "index.js", | ||
@@ -9,0 +9,0 @@ "url": "https://github.com/joshjung/hash-array", |
@@ -51,2 +51,27 @@ hash-array | ||
** Retreiving Multiples of a Single Key (getAsArray) ** | ||
var ha = new HashArray(['firstName', 'lastName']); | ||
var person1 = {firstName: 'Bill', lastName: 'William'}, | ||
person2 = {firstName: 'Bob', lastName: 'William'}; | ||
ha.add(person1, person2); | ||
console.log(ha.getAsArray('William')); // [person1, person2] | ||
** Retrieving Sets by Multiple Keys (getAll) ** | ||
var ha = new HashArray(['firstName', 'lastName']); | ||
var person1 = {firstName: 'Victor', lastName: 'Victor'}, | ||
person2 = {firstName: 'Victor', lastName: 'Manning'}, | ||
person3 = {firstName: 'Manning', lastName: 'Victor'}; | ||
person4 = {firstName: 'John', lastName: 'Smith'}; | ||
ha.add(person1, person2, person3, person4); | ||
console.log(ha.getAll(['Victor', 'Smith'])); // [person1, person2, person3, person4] | ||
console.log(ha.getAll(['John', 'Smith'])); // [person4] | ||
**Multi-level Keys** | ||
@@ -53,0 +78,0 @@ |
@@ -17,3 +17,4 @@ var JClass = require('jclass'); | ||
for (var i = 0; i < arguments.length; i++) { | ||
var obj = arguments[i]; | ||
var obj = arguments[i], | ||
needsDupCheck = false; | ||
for (var key in this.keyFields) { | ||
@@ -26,3 +27,4 @@ key = this.keyFields[key]; | ||
// Cannot add the same item twice | ||
return; | ||
needsDupCheck = true; | ||
continue; | ||
} | ||
@@ -34,3 +36,4 @@ this._map[inst].push(obj); | ||
this._list.push(obj); | ||
if (!needsDupCheck || this._list.indexOf(obj) == -1) | ||
this._list.push(obj); | ||
} | ||
@@ -53,2 +56,12 @@ if (this.callback) { | ||
}, | ||
getAll: function(keys) { | ||
var res = new HashArray(this.keyFields); | ||
for (var key in keys) | ||
res.add.apply(res, this.getAsArray(keys[key])); | ||
return res.all; | ||
}, | ||
getAsArray: function(key) { | ||
return this._map[key] || []; | ||
}, | ||
has: function(key) { | ||
@@ -55,0 +68,0 @@ return this._map.hasOwnProperty(key); |
@@ -47,2 +47,25 @@ var assert = require('assert'), | ||
describe('add(items) should work with 2 item and duplicate keys', function() { | ||
var ha = new HashArray(['key1', 'key2']); | ||
var item1 = { | ||
key1: 'whatever', | ||
key2: 'whatever' | ||
}; | ||
var item2 = { | ||
key1: 'whatever', | ||
key2: 'whatever' | ||
}; | ||
ha.add(item1, item2); | ||
it('Should have a 2 items.', function() { | ||
assert.equal(ha.all.length, 2); | ||
}); | ||
it('Should map "whatever" to both items in proper order.', function() { | ||
assert.equal(ha.getAsArray('whatever')[0], item1); | ||
assert.equal(ha.getAsArray('whatever')[1], item2); | ||
}); | ||
}); | ||
describe('add(items) should not allow addition of same item twice.', function() { | ||
@@ -354,2 +377,18 @@ var ha = new HashArray(['key']); | ||
}); | ||
describe('getAll(keys) should work', function() { | ||
var ha = new HashArray(['firstName', 'lastName']); | ||
var person1 = {firstName: 'Victor', lastName: 'Victor'}, | ||
person2 = {firstName: 'Victor', lastName: 'Manning'}, | ||
person3 = {firstName: 'Manning', lastName: 'Victor'}; | ||
person4 = {firstName: 'John', lastName: 'Smith'}; | ||
ha.add(person1, person2, person3, person4); | ||
it('Should retrieve only items for the keys requested without duplicates.', function() { | ||
assert.equal(ha.getAll(['Victor', 'Smith']).length, 4); | ||
assert.equal(ha.getAll(['John', 'Smith']).length, 1); | ||
}); | ||
}); | ||
}); |
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
20031
474
163