Comparing version 0.0.5 to 0.0.6
@@ -5,3 +5,3 @@ { | ||
"description": "A data structure that combines a hash and an array for CRUD operations by object keys or index.", | ||
"version": "0.0.5", | ||
"version": "0.0.6", | ||
"main": "index.js", | ||
@@ -8,0 +8,0 @@ "url": "https://github.com/joshjung/hash-array", |
@@ -30,6 +30,7 @@ var HashArray = function(keyFields, callback) { | ||
if (this._map[inst]) { | ||
throw Error('HashArray key ' + obj[key] + ' already exists'); | ||
} | ||
this._map[inst] = obj; | ||
if (this._map[inst].indexOf(obj) != -1) { | ||
continue; | ||
} | ||
this._map[inst].push(obj); | ||
} else this._map[inst] = [obj]; | ||
} | ||
@@ -54,3 +55,3 @@ } | ||
get: function(key) { | ||
return this._map[key]; | ||
return (!(this._map[key] instanceof Array) || this._map[key].length != 1) ? this._map[key] : this._map[key][0]; | ||
}, | ||
@@ -60,2 +61,5 @@ has: function(key) { | ||
}, | ||
hasMultiple: function(key) { | ||
return this._map[key] instanceof Array; | ||
}, | ||
removeByKey: function() { | ||
@@ -65,11 +69,21 @@ var removed = []; | ||
var key = arguments[i]; | ||
var item = this._map[key]; | ||
if (item) { | ||
removed.push(item); | ||
for (var ix in this.keyFields) { | ||
var key2 = this.find(item, this.keyFields[ix]); | ||
if (key2) | ||
delete this._map[key2]; | ||
var items = this._map[key]; | ||
if (items) { | ||
removed = removed.concat(items); | ||
for (var j in items) { | ||
var item = items[j]; | ||
for (var ix in this.keyFields) { | ||
var key2 = this.find(item, this.keyFields[ix]); | ||
if (key2 && this._map[key2]) { | ||
var ix = this._map[key2].indexOf(item); | ||
if (ix != -1) | ||
this._map[key2].splice(ix, 1); | ||
if (this._map[key2].length == 0) | ||
delete this._map[key2]; | ||
} | ||
} | ||
this._list.splice(this._list.indexOf(item), 1); | ||
} | ||
this._list.splice(this._list.indexOf(item), 1); | ||
} | ||
@@ -85,10 +99,16 @@ delete this._map[key]; | ||
for (var i = 0; i < arguments.length; i++) { | ||
var item = arguments[i]; | ||
for (var ix in this.keyFields) { | ||
var key = this.find(arguments[i], this.keyFields[ix]); | ||
var key = this.find(item, this.keyFields[ix]); | ||
if (key) { | ||
delete this._map[key]; | ||
var ix = this._map[key].indexOf(item); | ||
if (ix != -1) | ||
this._map[key].splice(ix, 1); | ||
if (this._map[key].length == 0) | ||
delete this._map[key]; | ||
} | ||
} | ||
this._list.splice(this._list.indexOf(arguments[i]), 1); | ||
this._list.splice(this._list.indexOf(item), 1); | ||
} | ||
@@ -95,0 +115,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
14155
367