@@ -48,7 +48,11 @@ import { arityCheck, typeCheck } from './utils.js';
48
48
* ```
49
49
*/
50
50
class Functions {
51
+ /**
52
+ * A set of all the custom functions available in this and all child classes.
53
+ */
51
54
public methods : Set < string > = new Set ( ) ;
55
+
52
56
/**
53
57
* Get the absolute value of the provided number.
54
58
*
@@ -528,14 +532,32 @@ class Functions {
528
532
return Object . values ( arg ) ;
529
533
}
530
534
535
+ /**
536
+ * Lazily introspects the methods of the class instance and all child classes
537
+ * to get the names of the methods that correspond to JMESPath functions.
538
+ *
539
+ * This method is used to get the names of the custom functions that are available
540
+ * in the class instance and all child classes. The names of the functions are used
541
+ * to create the custom function map that is passed to the JMESPath search function.
542
+ *
543
+ * The method traverses the inheritance chain going from the leaf class to the root class
544
+ * and stops when it reaches the `Functions` class, which is the root class.
545
+ *
546
+ * In doing so, it collects the names of the methods that start with `func` and adds them
547
+ * to the `methods` set. Finally, when the recursion collects back to the current instance,
548
+ * it adds the collected methods to the `this.methods` set so that they can be accessed later.
549
+ *
550
+ * @param scope The scope of the class instance to introspect
551
+ */
531
552
public introspectMethods ( scope ?: Functions ) : Set < string > {
532
553
const prototype = Object . getPrototypeOf ( this ) ;
533
- const ownName = prototype . constructor . name ;
534
554
const methods = new Set < string > ( ) ;
535
- if ( ownName !== ' Functions' ) {
555
+ if ( this instanceof Functions ) {
536
556
for ( const method of prototype . introspectMethods ( scope ) ) {
537
557
methods . add ( method ) ;
538
558
}
559
+ } else {
560
+ return methods ;
539
561
}
540
562
541
563
// This block is executed for every class in the inheritance chain
0 commit comments