Created
February 21, 2014 15:22
-
-
Save stu43005/9136118 to your computer and use it in GitHub Desktop.
This file contains 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
/** | |
* Specifications | |
* http://wiki.ecmascript.org/doku.php?id=harmony:simple_maps_and_sets | |
*/ | |
(function () { | |
"use strict"; | |
/** A non-stupid alternative to Array.prototype.indexOf */ | |
function indexOfIdentical(keys, key) { | |
var i; | |
for (i = 0; i < keys.length; i += 1) { | |
if (keys[i] === key) { | |
return i; | |
} | |
} | |
return -1; | |
} | |
var Map = window.Map = function (iterable) { | |
var i; | |
iterable = iterable || []; | |
this.keysName = []; | |
this.valsName = []; | |
for (i = 0; i < iterable.length; i += 1) { | |
this.set(iterable[i][0], iterable[i][1]); | |
} | |
}; | |
Map.prototype.get = function (key, defaultValue) { | |
var i = indexOfIdentical(this.keysName, key); | |
defaultValue = defaultValue || undefined; | |
return i < 0 ? defaultValue : this.valsName[i]; | |
}; | |
Map.prototype.set = function (key, value) { | |
var i = indexOfIdentical(this.keysName, key); | |
if (i < 0) { | |
i = this.keysName.length; | |
} | |
this.keysName[i] = key; | |
this.valsName[i] = value; | |
}; | |
Map.prototype.has = function (key) { | |
return indexOfIdentical(this.keysName, key) >= 0; | |
}; | |
Map.prototype.delete_ = function (key) { | |
var i = indexOfIdentical(this.keysName, key); | |
if (i < 0) { | |
return false; | |
} | |
this.keysName.splice(i, 1); | |
this.valsName.splice(i, 1); | |
return true; | |
}; | |
Map.prototype.clear = function () { | |
this.keysName = []; | |
this.valsName = []; | |
}; | |
Map.prototype.items = function () { | |
var i, array = []; | |
for (i = 0; i < this.keysName.length; i += 1) { | |
array.push([this.keysName[i], this.valsName[i]]); | |
} | |
return array; | |
}; | |
Map.prototype.keys = function () { | |
var i, array = []; | |
for (i = 0; i < this.keysName.length; i += 1) { | |
array.push(this.keysName[i]); | |
} | |
return array; | |
}; | |
Map.prototype.values = function () { | |
var i, array = []; | |
for (i = 0; i < this.keysName.length; i += 1) { | |
array.push(this.valsName[i]); | |
} | |
return array; | |
}; | |
Object.defineProperty(Map.prototype, "iterator", { | |
configurable: true, | |
writable: true, | |
value: Map.prototype.items | |
}); | |
}()); | |
(function () { | |
"use strict"; | |
var Set = window.Set = function (iterable) { | |
var i; | |
iterable = iterable || []; | |
this.mapName = new window.Map(); | |
for (i = 0; i < iterable.length; i += 1) { | |
this.add(iterable[i]); | |
} | |
}; | |
Set.prototype.add = function (key) { | |
this.mapName.set(key, true); | |
}; | |
Set.prototype.has = function (key) { | |
return this.mapName.has(key); | |
}; | |
Set.prototype.delete_ = function (key) { | |
return this.mapName.delete_(key); | |
}; | |
Set.prototype.values = function () { | |
return this.mapName.keys(); | |
}; | |
Object.defineProperty(Set.prototype, "iterator", { | |
configurable: true, | |
writable: true, | |
value: Set.prototype.values | |
}); | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment