File tree Expand file tree Collapse file tree 2 files changed +45
-0
lines changed Expand file tree Collapse file tree 2 files changed +45
-0
lines changed Original file line number Diff line number Diff line change @@ -506,6 +506,27 @@ class SymbolTree {
506506 return index ;
507507 }
508508
509+ /**
510+ * Calculate the number of children.
511+ *
512+ * `O(n)`<br>
513+ * `O(1)` (cached)
514+ *
515+ * @method childrenCount
516+ * @memberOf module:symbol-tree#
517+ * @param parent
518+ * @return {Number }
519+ */
520+ childrenCount ( parent ) {
521+ const parentNode = this . _node ( parent ) ;
522+
523+ if ( ! parentNode . last ) {
524+ return 0 ;
525+ }
526+
527+ return this . index ( parentNode . last ) + 1 ;
528+ }
529+
509530 /**
510531 * Remove the object from this tree.
511532 * Has no effect if already removed.
Original file line number Diff line number Diff line change @@ -960,3 +960,27 @@ test('cached index', function(t) {
960960
961961 t . end ( ) ;
962962} ) ;
963+
964+ test ( 'children count' , function ( t ) {
965+ // no need to test the caching since we already tested for that in childrenCount
966+ const tree = new SymbolTree ( ) ;
967+ const a = { } ;
968+ const aa = { } ;
969+ const ab = { } ;
970+ const aba = { } ;
971+ const ac = { } ;
972+ const b = { } ;
973+
974+ tree . insertLast ( aa , a ) ;
975+ tree . insertLast ( ab , a ) ;
976+ tree . insertLast ( aba , ab ) ;
977+ tree . insertLast ( ac , a ) ;
978+ tree . insertAfter ( b , a ) ;
979+
980+ t . equal ( 3 , tree . childrenCount ( a ) , 'foo' ) ;
981+ t . equal ( 0 , tree . childrenCount ( aa ) ) ;
982+ t . equal ( 1 , tree . childrenCount ( ab ) ) ;
983+ t . equal ( 0 , tree . childrenCount ( b ) ) ;
984+
985+ t . end ( ) ;
986+ } ) ;
You can’t perform that action at this time.
0 commit comments