Skip to content

Instantly share code, notes, and snippets.

@alucky0707
Created May 30, 2013 12:58
Show Gist options
  • Save alucky0707/5677635 to your computer and use it in GitHub Desktop.
Save alucky0707/5677635 to your computer and use it in GitHub Desktop.
module.exports = (function() {
/* constructor */
function Search(arr){
if(!(this instanceof Search)) return new Search(arr);
this._arr = arr;
this._createTable();
}
var
/* member */
proto = Search.prototype = {
_arr: null,
_table: null,
};
/* private */
proto._createTable = function() {
this._table = this._arr.reduce(function(m, a, i) {
m[a] = (m[a] || []).concat(i);
return m;
}, {});
};
/* public */
proto.has = function(a) {
return a in this._table;
};
proto.index = function(a) {
return this._table[a] || [];
};
proto.search = function(a) {
return this.index(a).reduce(function(m, i) {
m.push(this._arr[i]);
return m;
}, []);
};
return Search;
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment