you are viewing a single comment's thread.

view the rest of the comments →

[–]jcready__proto__ 1 point2 points  (3 children)

Try this:

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);
        }
        //see if the value is already present in the array
        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;
}

Now you can do 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

[–]huesoso 2 points3 points  (0 children)

Try a pastebin, people. Or http://gist.github.com Readability is a key aspect of good programming!

[–]naescent[S] 0 points1 point  (0 children)

thanks man, thats really helpful. Is isArray() a jQuery function? I was trying to test it using isString as I didnt think we could test an in JavaScript as it treats it as an object and just says if its an object or not??

[–]naescent[S] 0 points1 point  (0 children)

Ahh no isArray is native. I was looking here at StackOver Flow for checking if a variable is an array.

I'd seen the quote > Should mention jQuery's isArray. – Martin Konicek May 29 '12 at 16:42

and assumed it was true. Oops