Created
July 30, 2013 04:57
-
-
Save romeoh/6110339 to your computer and use it in GitHub Desktop.
Map()
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
/* map */ | |
Mp.Map = function() { | |
this.array = []; | |
} | |
Mp.Map.prototype = { | |
put: function(key, value){ | |
var index = this.getIndex(key); | |
if (index > -1) { | |
this.array[index] = {key: key, value: value}; | |
} else { | |
this.array.push({key: key, value: value}); | |
} | |
}, | |
get: function(key) { | |
var index = this.getIndex(key); | |
if (index > -1) { | |
return this.array[index].value; | |
} else { | |
return ''; | |
} | |
}, | |
getAtIndex: function(index){ | |
return this.array[index].value; | |
}, | |
remove: function(value){ | |
var index = this.getIndexOfValue(value); | |
this.removeByIndex(index); | |
}, | |
removeByKey: function(key){ | |
var index = this.getIndex(key); | |
this.removeByIndex(index); | |
}, | |
removeByIndex: function(index){ | |
this.array.splice(index, 1); | |
}, | |
clear: function(){ | |
this.array.length = 0; | |
}, | |
all: function(){ | |
return this.array; | |
}, | |
getIndex: function(key){ | |
var index = -1; | |
for (var i=0; i<this.array.length; i++) { | |
var item = this.array[i]; | |
if (item.key == key) index = i; | |
} | |
return index; | |
}, | |
getIndexOfValue: function(value){ | |
var index = -1; | |
for (var i=0; i<this.array.length; i++) { | |
var item = this.array[i]; | |
if (item.value == value) index = i; | |
} | |
return index; | |
}, | |
getKeyOfValue: function(value){ | |
var key = ''; | |
for (var i=0; i<this.array.length; i++) { | |
var item = this.array[i]; | |
if (item.value == value) key = item.key; | |
} | |
return key; | |
}, | |
contains: function(value){ | |
var index = this.getIndexOfValue(value); | |
return index > -1; | |
}, | |
containsKey: function(key){ | |
var index = this.getIndex(key); | |
return index > -1; | |
}, | |
size: function(){ | |
return this.array.length; | |
} | |
} | |
Mp.global = new Mp.Map(); | |
// onInitPage | |
Mp.onReady = function(fn) { | |
Mp.global.put('ready', fn) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment