add comparator for indexOf

This commit is contained in:
Alexey 2020-08-06 18:00:13 +03:00
parent bb55160b17
commit 523094bdc8
2 changed files with 9 additions and 3 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "fixed-merkle-tree", "name": "fixed-merkle-tree",
"version": "0.3.1", "version": "0.3.2",
"description": "Fixed depth merkle tree implementation with sequential inserts", "description": "Fixed depth merkle tree implementation with sequential inserts",
"main": "src/merkleTree.js", "main": "src/merkleTree.js",
"scripts": { "scripts": {

View File

@ -134,11 +134,17 @@ class MerkleTree {
/** /**
* Find an element in the tree * Find an element in the tree
* @param element An element to find * @param element An element to find
* @param comparator A function that checks leaf value equality
* @returns {number} Index if element is found, otherwise -1 * @returns {number} Index if element is found, otherwise -1
*/ */
indexOf(element) { indexOf(element, comparator) {
if (comparator) {
const result = this._layers[0].findIndex((el) => comparator(element, el))
return result === undefined ? -1 : result
} else {
return this._layers[0].indexOf(element) return this._layers[0].indexOf(element)
} }
}
/** /**
* Returns a copy of non-zero tree elements * Returns a copy of non-zero tree elements