Last active
December 16, 2015 09:09
-
-
Save varl/5410754 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
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; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See https://gist.github.com/varl/5403312 for the tests for this class.