You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/syntax.md
+119Lines changed: 119 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -134,7 +134,13 @@ Besides the "operator" functions, there are several pre-defined functions. You c
134
134
| count(a) | Returns the number of items in an array. |
135
135
| map(f, a) | Array map: Pass each element of `a` the function `f`, and return an array of the results. |
136
136
| fold(f, y, a) | Array fold: Fold/reduce array `a` into a single value, `y` by setting `y = f(y, x, index)` for each element `x` of the array. |
137
+
| reduce(f, y, a) | Alias for `fold`. Reduces array `a` into a single value using function `f` starting with accumulator `y`. |
137
138
| filter(f, a) | Array filter: Return an array containing only the values from `a` where `f(x, index)` is `true`. |
139
+
| find(f, a) | Returns the first element in array `a` where `f(x, index)` is `true`, or `undefined` if not found. |
140
+
| some(f, a) | Returns `true` if at least one element in array `a` satisfies `f(x, index)`, `false` otherwise. |
141
+
| every(f, a) | Returns `true` if all elements in array `a` satisfy `f(x, index)`. Returns `true` for empty arrays. |
142
+
| unique(a) | Returns a new array with duplicate values removed from array `a`. |
143
+
| distinct(a) | Alias for `unique`. Returns a new array with duplicate values removed. |
138
144
| indexOf(x, a) | Return the first index of string or array `a` matching the value `x`, or `-1` if not found. |
139
145
| join(sep, a) | Concatenate the elements of `a`, separated by `sep`. |
140
146
| naturalSort(arr) | Sorts an array of strings using natural sort order (alphanumeric-aware). For example, `["file10", "file2", "file1"]` becomes `["file1", "file2", "file10"]`. |
@@ -146,6 +152,19 @@ Besides the "operator" functions, there are several pre-defined functions. You c
146
152
| if(c, a, b) | Function form of c ? a : b. Note: This always evaluates both `a` and `b`, regardless of whether `c` is `true` or not. Use `c ? a : b` instead if there are side effects, or if evaluating the branches could be expensive. |
147
153
| coalesce(a, b, ...) | Returns the first non-null and non-empty string value from the arguments. Numbers and booleans (including 0 and false) are considered valid values. |
148
154
155
+
### Type Checking Functions
156
+
157
+
| Function | Description |
158
+
|:------------- |:----------- |
159
+
| isArray(v) | Returns `true` if `v` is an array, `false` otherwise. |
160
+
| isObject(v) | Returns `true` if `v` is an object (excluding null and arrays), `false` otherwise. |
161
+
| isNumber(v) | Returns `true` if `v` is a number, `false` otherwise. |
162
+
| isString(v) | Returns `true` if `v` is a string, `false` otherwise. |
163
+
| isBoolean(v) | Returns `true` if `v` is a boolean, `false` otherwise. |
164
+
| isNull(v) | Returns `true` if `v` is null, `false` otherwise. |
165
+
| isUndefined(v)| Returns `true` if `v` is undefined, `false` otherwise. |
166
+
| isFunction(v) | Returns `true` if `v` is a function, `false` otherwise. |
167
+
149
168
## String Functions
150
169
151
170
The parser includes comprehensive string manipulation capabilities.
> **Note:** Arrow functions share the same `fndef` operator flag as traditional function definitions. If function definitions are disabled via parser options, arrow functions will also be disabled.
405
424
425
+
### Examples of New Array Functions
426
+
427
+
The new array utility functions provide additional ways to work with arrays:
some(isString, [1, 2, "hello", 3]) // true (has at least one string)
508
+
every(isNumber, [1, 2, 3, 4]) // true (all are numbers)
509
+
every(isNumber, [1, "a", 3]) // false (not all numbers)
510
+
```
511
+
512
+
**Practical examples:**
513
+
514
+
```js
515
+
// Count how many strings are in an array
516
+
count(filter(isString, [1, "a", 2, "b", 3])) // 2
517
+
518
+
// Get the first number in a mixed array
519
+
find(isNumber, ["a", "b", 3, "c", 5]) // 3
520
+
521
+
// Check if any value is null or undefined
522
+
some(x=>isNull(x) or isUndefined(x), data) // true/false
523
+
```
524
+
406
525
## Custom JavaScript Functions
407
526
408
527
If you need additional functions that aren't supported out of the box, you can easily add them in your own code. Instances of the `Parser` class have a property called `functions` that's simply an object with all the functions that are in scope. You can add, replace, or delete any of the properties to customize what's available in the expressions. For example:
0 commit comments