Created
May 30, 2013 12:58
-
-
Save alucky0707/5677635 to your computer and use it in GitHub Desktop.
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
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