From a9807c949b9f006c91961fa00e9cc3379f1d7466 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samy=20Pess=C3=A9?= Date: Wed, 2 Nov 2016 15:52:10 +0100 Subject: [PATCH] Add method to return all keys in a node --- src/models/node.js | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/src/models/node.js b/src/models/node.js index f7661211f..55227c32b 100644 --- a/src/models/node.js +++ b/src/models/node.js @@ -20,6 +20,22 @@ import { List, Set } from 'immutable' const Node = { + /** + * Return a set of all keys in the node. + * + * @return {Set} keys + */ + + getKeys() { + const keys = [] + + this.filterDescendants(desc => { + keys.push(desc.key) + }) + + return Set(keys) + }, + /** * Assert that a node has a child by `key` and return it. * @@ -1044,11 +1060,7 @@ const Node = { */ insertNode(index, node) { - let keys = new Set([ this.key ]) - - this.findDescendant((desc) => { - keys = keys.add(desc.key) - }) + let keys = this.getKeys() if (keys.contains(node.key)) { node = node.regenerateKey()