Skip to content

Instantly share code, notes, and snippets.

@varl
Last active December 16, 2015 09:09
Show Gist options
  • Save varl/5410754 to your computer and use it in GitHub Desktop.
Save varl/5410754 to your computer and use it in GitHub Desktop.
function Indexer (object, separator) {
this.separator = separator || /\|/;
this.list = typeof object === "string" ? object.split('\n') : [];
}
/***
* Returns true if the given key is found.
*/
Indexer.prototype.containsKey = function (key) {
this.key = key;
return this.list.filter(findKey, this).length !== 0;
};
/***
* Returns value if the given key is found.
*/
Indexer.prototype.getValue = function (key) {
if (!this.containsKey(key)) {
console.log("Did not find key: " +key);
return null;
} else {
console.log("Found key: " +key);
}
this.key = key;
var result = this.list.filter(findKey, this)[0].split(this.separator)[1];
console.log("Got item: " +result);
return result;
};
/***
* Helper filter function
*/
var findKey = function(element, index, array) {
return this.key === element.split(this.separator)[0];
};
module.exports = Indexer;
@varl
Copy link
Author

varl commented Apr 18, 2013

See https://gist.github.com/varl/5403312 for the tests for this class.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment