Skip to content

Instantly share code, notes, and snippets.

@jcready
Created May 4, 2014 02:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jcready/98e5bae921f1fe7eba84 to your computer and use it in GitHub Desktop.
Save jcready/98e5bae921f1fe7eba84 to your computer and use it in GitHub Desktop.
function MasterStore() {
this.datastore = {};
}
MasterStore.prototype.has = function(key, value){
if (value == null) return this.datastore.hasOwnProperty(key);
if (Array.isArray(value)) {
for (var i = 0; i < value.length; i++) {
if (this.datastore[key].indexOf(value[i]) < 0) {
return false;
}
}
return true;
}
return this.datastore[key].indexOf(value) >= 0;
};
MasterStore.prototype.add = function (key, value) {
if (value == null) return this;
var valueIsArray = Array.isArray(value);
if (this.datastore.hasOwnProperty(key)) {
// if value is an array and the datastore[key] is also an array
if (valueIsArray) {
// concatenate the value array to the datastore[key] array
this.datastore[key] = this.datastore[key].concat(value);
} else if (this.datastore[key].indexOf(value) >= 0) {
//if present then do nothing
} else {
// otherwise push value onto the datastore[key] array
this.datastore[key].push(value);
}
} else {
this.datastore[key] = valueIsArray ? value : [value];
}
return this;
}
MasterStore.prototype.remove = function (key, value) {
if (value == null) {
// if no value present then remove the whole array from the datastore
delete this.datastore[key];
return this;
}
var valueIsArray = Array.isArray(value);
if (this.datastore.hasOwnProperty(key)) {
if (Array.isArray(value)) {
// should we keep splicing the array or just create a new one
if (value.length < 20) {
// if we don't have a lot of values to remove, just splice
for (var i = 0; i < value.length; i++) {
var index = this.datastore[key].indexOf(value[i]);
if (index >= 0) this.datastore[key].splice(index, 1);
}
} else {
// otherwise it's probably faster to just create a new array
var store = this.datastore[key], tmp = [];
for (var i = 0; i < store.length; i++) {
var index = value.indexOf(store[i]);
if (index < 0) {
// if datastore[key][i] isn't in the value array
// push it into our new array
tmp.push(store[i]);
}
}
this.datastore[key] = tmp;
}
} else {
var index = this.datastore[key].indexOf(value);
if (index >= 0) this.datastore[key].splice(index, 1);
}
}
return this;
}
var a = new MasterStore;
a.add('days', 'monday');
console.log(a.datastore.days); //>> ['monday']
// You can also chain because add() and remove() return this
console.log(a.add('days', 'tuesday').datastore.days); //>> ['monday', 'tuesday']
a.add('days', 'wednesday').remove('days', 'tuesday');
console.log(a.datastore.days); //>> ['monday', 'wednesday']
// You can add entire arrays at a time
a.add('months', ['january', 'february', 'march']);
a.add('months', ['april', 'may', 'june']);
console.log(a.datastore.months); //>> ['january', 'february', 'march', 'april', 'may', 'june']
// You can remove individual values from the datastore[key] array
a.remove('months', 'february');
console.log(a.datastore.months); //>> ['january', 'march', 'april', 'may', 'june']
// You can remove multiple values from the datastore[key] array
a.remove('months', ['january', 'april', 'june']);
console.log(a.datastore.months); //>> ['march', 'may']
// You can also entirely remove the datastore[key] array
// (this is mostly for convince as you can do: "delete datastore[key];" instead)
a.remove('months');
console.log(a.datastore.months); //>> undefined
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment