From fc2211721aa3bb60ca20259c5c87d6082b7b6c24 Mon Sep 17 00:00:00 2001 From: smhasanmansur Date: Thu, 9 Jun 2016 21:26:44 +0600 Subject: [PATCH 1/2] treeLength method --- lib/nested_set.js | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/lib/nested_set.js b/lib/nested_set.js index ef06acb..d1eec35 100644 --- a/lib/nested_set.js +++ b/lib/nested_set.js @@ -412,6 +412,26 @@ var NestedSetPlugin = function(schema, options) { callback(err, nodes.length); }); }); + + // Returns the length of the subtree under this object + schema.method('treeLength', function(callback) { + var self = this; + self.selfAndDescendants(function(err, nodes) { + var levels = []; + nodes.forEach(function (node) { + node.level(function (err, value) { + levels.push(value); + if (levels.length == nodes.length) { + var min = levels[0]; + var max = Math.max.apply(null, levels); + var length = (max - min) + 1; + callback(err, length); + } + }); + }); + }); + }); + }; module.exports = exports = NestedSetPlugin; From ec18f142f11daa4115cce6a87ca3a26254b966a6 Mon Sep 17 00:00:00 2001 From: smhasanmansur Date: Sat, 11 Jun 2016 11:55:57 +0600 Subject: [PATCH 2/2] min value for treeLength --- lib/nested_set.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/nested_set.js b/lib/nested_set.js index d1eec35..5776cd3 100644 --- a/lib/nested_set.js +++ b/lib/nested_set.js @@ -422,7 +422,7 @@ var NestedSetPlugin = function(schema, options) { node.level(function (err, value) { levels.push(value); if (levels.length == nodes.length) { - var min = levels[0]; + var min = Math.min.apply(null, levels); var max = Math.max.apply(null, levels); var length = (max - min) + 1; callback(err, length);