Skip to content

Instantly share code, notes, and snippets.

@bingeboy
Created October 31, 2013 17:05
Show Gist options
  • Save bingeboy/7253287 to your computer and use it in GitHub Desktop.
Save bingeboy/7253287 to your computer and use it in GitHub Desktop.
var memoize = {
keys:{},
set: function(key, value) { // set a key, value pair, return value
this.keys[key] = value;
return value;
},
clear: function( ) {
this.keys = {}; // set in memoize keys, and return result
return this.key;
},
remove: function(key) {
this.keys[key] = key;
delete this.keys[key];
},
get: function(key, value, callback) { // get a key
//return key if it exists
if(this.keys[key]) {
console.log(this.keys[key]);
return this.keys[key];
}
//if no key and callback is present
if (typeof callback === "function") {
var that = this;
value(function(response){
console.log("received response");
that.set(key, response);
callback(response); //callback exe
});
}
//if no key or callback
else if(typeof value === "function") {
var valueResult = value.call(this, key); // store result of finder function
if(valueResult) { // if result is not null
return this.set(key, valueResult); // set in memoize keys, and return result
} else{
//set value
}
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment