@@ -8,68 +8,88 @@ function that can be used to evaluate the expression on a json payload.
88** Kind** : global class
99
1010* [ JsonFormula] ( #JsonFormula )
11- * [ new JsonFormula([ customFunctions] , [ stringToNumber] , [ language ] , [ debug] )] ( #new_JsonFormula_new )
12- * [ .search(json, [ globals] )] ( #JsonFormula+search ) ⇒ <code >\* </code >
13- * [ .run(ast, json, globals)] ( #JsonFormula+run ) ⇒ <code >\* </code >
14- * [ .compile(expression, [ allowedGlobalNames] , [ debug ] )] ( #JsonFormula+compile )
11+ * [ new JsonFormula([ customFunctions] , [ stringToNumber] , [ debug] )] ( #new_JsonFormula_new )
12+ * [ .search(expression, json, [ globals] , [ language ] )] ( #JsonFormula+search ) ⇒ <code >\* </code >
13+ * [ .run(ast, json, [ language ] , globals)] ( #JsonFormula+run ) ⇒ <code >\* </code >
14+ * [ .compile(expression, [ allowedGlobalNames] )] ( #JsonFormula+compile )
1515
1616<a name =" new_JsonFormula_new " ></a >
1717
18- ### new JsonFormula([ customFunctions] , [ stringToNumber] , [ language ] , [ debug] )
18+ ### new JsonFormula([ customFunctions] , [ stringToNumber] , [ debug] )
1919
2020| Param | Type | Default | Description |
2121| --- | --- | --- | --- |
2222| [ customFunctions] | <code >object</code > | <code >{}</code > | custom functions needed by a hosting application. |
2323| [ stringToNumber] | <code >function</code > | <code >' ; null' ; </code > | A function that converts string values to numbers. Can be used to convert currencies/dates to numbers |
24- | [ language] | <code >string</code > | <code >" ; en-US" ; </code > | |
2524| [ debug] | <code >array</code > | <code >[ ] </code > | will be populated with any errors/warnings |
2625
2726<a name =" JsonFormula+search " ></a >
2827
29- ### jsonFormula.search(json, [ globals] ) ⇒ <code >\* </code >
28+ ### jsonFormula.search(expression, json, [ globals] , [ language ] ) ⇒ <code >\* </code >
3029Evaluates the JsonFormula on a particular json payload and return the result
3130
3231** Kind** : instance method of [ <code >JsonFormula</code >] ( #JsonFormula )
3332** Returns** : <code >\* </code > - the result of the expression being evaluated
3433
3534| Param | Type | Default | Description |
3635| --- | --- | --- | --- |
36+ | expression | <code >string</code > | | the json-formula expression to evaluate |
3737| json | <code >object</code > \| <code >array</code > | | the json data on which the expression needs to be evaluated |
3838| [ globals] | <code >object</code > | <code >{}</code > | global objects that can be accessed via custom functions. |
39-
39+ | [ language] | <code >string</code > | <code >" ; en-US" ; </code > | BCP-47 language tag |
40+
41+ ** Example**
42+ ``` js
43+ const jf = new JsonFormula ();
44+ const result = jf .search (' toDate(d) | day(@) & "/" & month(@)' , {d: " 2025-11-12" });
45+ // returns "12/11"
46+ ```
4047<a name =" JsonFormula+run " ></a >
4148
42- ### jsonFormula.run(ast, json, globals) ⇒ <code >\* </code >
43- Execute a previously compiled expression against a json object and return the result
49+ ### jsonFormula.run(ast, json, [ language] , globals) ⇒ <code >\* </code >
50+ Execute a previously compiled expression against a json object and return the result.
51+ Use this method for better performance when you will evaluate the same expression
52+ multiple times with different data.
4453
4554** Kind** : instance method of [ <code >JsonFormula</code >] ( #JsonFormula )
4655** Returns** : <code >\* </code > - the result of the expression being evaluated
4756
48- | Param | Type | Description |
49- | --- | --- | --- |
50- | ast | <code >object</code > | The abstract syntax tree returned from compile() |
51- | json | <code >object</code > \| <code >array</code > | the json data on which the expression needs to be evaluated |
52- | globals | <code >\* </code > | set of objects available in global scope |
53-
57+ | Param | Type | Default | Description |
58+ | --- | --- | --- | --- |
59+ | ast | <code >object</code > | | The abstract syntax tree returned from compile() |
60+ | json | <code >object</code > \| <code >array</code > | | the json data on which the expression needs to be evaluated |
61+ | [ language] | <code >string</code > | <code >" ; en-US" ; </code > | BCP-47 language tag |
62+ | globals | <code >\* </code > | | set of objects available in global scope |
63+
64+ ** Example**
65+ ``` js
66+ const globals = {
67+ $days: [' Mon' , ' Tue' , ' Wed' , ' Thu' , ' Fri' , ' Sat' , ' Sun' ]
68+ };
69+ const jf = new JsonFormula ();
70+ const ast = jf .compile (' value($days, num)' , [' $days' ]); // compile the expression once
71+ const result1 = jf .run (ast, {num: 2 }, ' en-US' , globals); // returns "Wed"
72+ const result2 = jf .run (ast, {num: 3 }, ' en-US' , globals); // returns "Thu"
73+ ```
5474<a name =" JsonFormula+compile " ></a >
5575
56- ### jsonFormula.compile(expression, [ allowedGlobalNames] , [ debug ] )
76+ ### jsonFormula.compile(expression, [ allowedGlobalNames] )
5777Creates a compiled expression that can be executed later on with some data.
5878
5979** Kind** : instance method of [ <code >JsonFormula</code >] ( #JsonFormula )
6080
6181| Param | Type | Default | Description |
6282| --- | --- | --- | --- |
6383| expression | <code >string</code > | | the expression to evaluate |
64- | [ allowedGlobalNames] | <code >Array.< ; string> ; </code > | <code >[ ] </code > | A list of names of the global variables being used in the expression. |
65- | [ debug] | <code >array</code > | <code >[ ] </code > | will be populated with any errors/warnings |
84+ | [ allowedGlobalNames] | <code >Array.< ; string> ; </code > | <code >[ ] </code > | An array of names of the global variables being used in the expression. |
6685
6786<a name =" jsonFormula " ></a >
6887
69- ## jsonFormula(json, globals, expression, [ customFunctions] , [ stringToNumber] , [ language ] , [ debug ] ) ⇒ <code >\* </code >
88+ ## jsonFormula(json, globals, expression, [ customFunctions] , [ stringToNumber] , [ debug ] , [ language ] ) ⇒ <code >\* </code >
7089Compile and execute a json-formula expression.
7190If executing the same expression multiple times, it is more efficient to create a
72- class instance of {JsonFormula} and call the search method multiple times.
91+ class instance of JsonFormula and call the search() method or the compile()/run() methods
92+ multiple times.
7393
7494** Kind** : global function
7595** Returns** : <code >\* </code > - the result of the expression being evaluated
@@ -81,6 +101,74 @@ class instance of {JsonFormula} and call the search method multiple times.
81101| expression | <code >string</code > | | the expression to evaluate |
82102| [ customFunctions] | <code >object</code > | <code >{}</code > | custom functions needed by a hosting application. |
83103| [ stringToNumber] | <code >function</code > | <code >' ; null' ; </code > | A function that converts string values to numbers. Can be used to convert currencies/dates to numbers |
84- | [ language] | <code >string</code > | <code >" ; en-US" ; </code > | |
85104| [ debug] | <code >array</code > | <code >[ ] </code > | will be populated with any errors/warnings |
105+ | [ language] | <code >string</code > | <code >" ; en-US" ; </code > | BCP-47 language tag |
106+
107+ <a name =" CustomFunctionDefinition " ></a >
86108
109+ ## CustomFunctionDefinition : <code >object</code >
110+ ** Kind** : global typedef
111+ ** Properties**
112+
113+ | Name | Type | Description |
114+ | --- | --- | --- |
115+ | _ func | <code >function</code > | The function implementation |
116+ | [ _ signature] | <code >array</code > | Function signature metadata |
117+
118+ ** Example**
119+ ``` js
120+ // simple custom functions definition
121+ const customFunctions = {
122+ true_fn: {
123+ _func : () => true ,
124+ _signature: [],
125+ },
126+ false_fn: {
127+ _func : () => false ,
128+ _signature: [],
129+ },
130+ };
131+
132+ ```
133+ ** Example**
134+ ``` js
135+ // custom function with a signature for its parameters
136+ const customFunctions = {
137+ padEnd: {
138+ _func : args => {
139+ const src = args[0 ];
140+ const length = args[1 ];
141+ const padChar = args[2 ];
142+ if (Array .isArray (src)) return src .map (s => s .padEnd (length, padChar));
143+ return src .padEnd (length, padChar);
144+ },
145+ _signature: [
146+ { types: [TYPE_STRING , TYPE_ARRAY_STRING ] },
147+ { types: [TYPE_NUMBER ] },
148+ { types: [TYPE_STRING ] },
149+ ],
150+ }
151+ }
152+ // May also register functions is via the `register()` or `registerWithParams()` methods. e.g.
153+
154+ const regFormula = ` ${ register (" ${fn_name}" , & ${code})` ;
155+ // Run the registration formula after which, the registered function may be called
156+ this.search(regFormula, {});
157+ ` ` `
158+ < a name= " globals" >< / a>
159+
160+ ## globals : < code> object< / code>
161+ An object where each key ** MUST ** begin with a ` $` character, representing global variables
162+ that can be accessed inside a json- formula expression.
163+ The value of each key can be of any data type supported by json.
164+
165+ ** Kind** : global typedef
166+ ** Example**
167+ ` ` ` js
168+ const globals = {
169+ $num: 42,
170+ $arr: [1, 2, 3],
171+ $days: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
172+ };
173+ jsonFormula({}, globals, '$arr * $num') // returns [42, 84, 126]
174+ ` ` `
0 commit comments