Skip to content

Instantly share code, notes, and snippets.

@subtleGradient
Forked from tomocchino/gist:24473
Created November 13, 2008 16:04
Show Gist options
  • Save subtleGradient/24474 to your computer and use it in GitHub Desktop.
Save subtleGradient/24474 to your computer and use it in GitHub Desktop.
var Dictionary = new Native({
name: 'Dictionary',
initialize: function(){
this.keys = [];
this.values = [];
}
});
Dictionary.implement({
getKey: function(value){
var idx = this.values.indexOf(value);
return (idx > -1) ? this.keys[idx] : null;
},
getValue: function(key){
var idx = this.keys.indexOf(key);
return (idx > -1) ? this.values[idx] : null;
},
forEach: function(fn){
for (var i = 0, l = this.keys.length; i < l; i++){
fn.call(this, this.values[i], this.keys[i], this);
}
},
set: function(key, value, noforce){
if (!noforce) this.erase(key);
else if (this.getValue(key)) return this;
this.keys.push(key);
this.values.push(value);
return this;
},
include: function(key, value){
return this.set(key, value, true);
},
erase: function(key){
var kIdx = this.keys.indexOf(key);
if (kIdx > -1){
this.values.splice(kIdx, 1);
this.keys.splice(kIdx, 1);
}
return this;
}
});
Dictionary.alias('getValue', 'get');
Dictionary.alias('forEach', 'each');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment