Skip to content

Instantly share code, notes, and snippets.

@goatslacker
Created September 14, 2011 23:07
Show Gist options
  • Save goatslacker/1218072 to your computer and use it in GitHub Desktop.
Save goatslacker/1218072 to your computer and use it in GitHub Desktop.
cutesy little Object storage with Array like functionality
var List = function () {
this.h = {};
this.i = [];
};
List.prototype.set = function (key, val, unshift) {
var op = unshift ? "unshift" : "push";
this.h[key] = val;
Array.prototype[op].call(this.i, key);
};
List.prototype.get = function (key) {
return this.h[key];
};
List.prototype.split = function (by) {
return this.join("").split(by);
};
List.prototype.join = function (by) {
var str = "";
this.i.forEach(function (key) {
str += this.h[key] + by;
}.bind(this));
return str;
};
List.prototype.pop = function () {
var key = this.i.pop();
var val = this.h[key];
delete this.h[key];
return val;
};
List.prototype.shift = function () {
var key = this.i.shift();
var val = this.h[key];
delete this.h[key];
return val;
};
List.prototype.push = function (val) {
this.set(this.i.length, val); // TODO generate random chars?
};
List.prototype.unshift = function () {
this.set(this.i.length, val, true); // TODO generate random chars?
};
module.exports = List;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment