add comparator for indexOf
This commit is contained in:
parent
bb55160b17
commit
523094bdc8
@ -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": {
|
||||||
|
@ -134,10 +134,16 @@ 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) {
|
||||||
return this._layers[0].indexOf(element)
|
if (comparator) {
|
||||||
|
const result = this._layers[0].findIndex((el) => comparator(element, el))
|
||||||
|
return result === undefined ? -1 : result
|
||||||
|
} else {
|
||||||
|
return this._layers[0].indexOf(element)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
Reference in New Issue
Block a user