From 523094bdc85cfa98729d2cc2d204d3bdedc649fd Mon Sep 17 00:00:00 2001 From: Alexey Date: Thu, 6 Aug 2020 18:00:13 +0300 Subject: [PATCH] add comparator for indexOf --- package.json | 2 +- src/merkleTree.js | 10 ++++++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 94a03cf..cdd8ee0 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "fixed-merkle-tree", - "version": "0.3.1", + "version": "0.3.2", "description": "Fixed depth merkle tree implementation with sequential inserts", "main": "src/merkleTree.js", "scripts": { diff --git a/src/merkleTree.js b/src/merkleTree.js index c8cd4d9..e493ba2 100644 --- a/src/merkleTree.js +++ b/src/merkleTree.js @@ -134,10 +134,16 @@ class MerkleTree { /** * Find an element in the tree * @param element An element to find + * @param comparator A function that checks leaf value equality * @returns {number} Index if element is found, otherwise -1 */ - indexOf(element) { - return this._layers[0].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) + } } /**