Created
December 17, 2013 16:47
-
-
Save zbabtkis/8008126 to your computer and use it in GitHub Desktop.
Revisions
-
Zachary Babtkis created this gist
Dec 17, 2013 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,41 @@ var DB = {}; DB.collections = {}; var Collection = function(Constructor, IndexInterface) { var d = [] , i = new IndexInterface; return { add: function(data) { d.push(new Constructor(data)); i.index(d.length - 1, data); }, get: function(query) { return d[i.atIndex(query)]; } }; }; var IdIndexer = function() { this.indexes = {}; }; IdIndexer.prototype.atIndex = function(query, col) { if(typeof query === 'number' ? query : query.id) { return this.indexes[query || query.id]; } else { atIndex(query); } }; IdIndexer.prototype.index = function(ind, data) { if(data.id) { this.indexes[data.id] = ind; } else { throw new Error("No id to index for data", data); } };