Created
November 13, 2008 16:04
-
-
Save tomocchino/24473 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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