|
1 | 1 | ## JSONata Function Library
|
2 |
| -The following is a proposed function library for use within JSONata expressions. |
3 |
| -This is work in progress. Some of these functions have been implemented, and those that have not are marked accordingly. |
| 2 | +The documentation for JSONata and its function library has now moved to http://docs.jsonata.org. |
4 | 3 |
|
5 |
| - |
6 |
| -### String functions |
7 |
| -#### `$string(arg)` |
8 |
| - |
9 |
| -Casts the `arg` parameter to a string using the following casting rules |
10 |
| - - Strings are unchanged |
11 |
| - - Functions are converted to an empty string |
12 |
| - - Numeric infinity and NaN throw an error because they cannot be represented as a JSON number |
13 |
| - - All other values are converted to a JSON string using the [JSON.stringify](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify) function |
14 |
| - |
15 |
| -####`$length(str)` |
16 |
| - |
17 |
| -Returns the number of characters in the string `str`. An error is thrown if `str` is not a string. |
18 |
| - |
19 |
| -####`$substring(str, start[, length])` |
20 |
| - |
21 |
| -Returns a string containing the characters in the first parameter `str` |
22 |
| -starting at position `start` (zero-offset). If `length` is specified, then |
23 |
| -the substring will contain maximum `length` characters. If `start` is negative |
24 |
| -then it indicates the number of characters from the end of `str`. |
25 |
| -See [substr](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substr) for full definition. |
26 |
| - |
27 |
| -####`$substringBefore(str, chars)` |
28 |
| - |
29 |
| -Returns the substring before the first occurrence of the character sequence `chars` in `str`. |
30 |
| -If `str` does not contain `chars`, then it returns `str`. |
31 |
| - |
32 |
| -####`$substringAfter(str, chars)` |
33 |
| - |
34 |
| -Returns the substring after the first occurrence of the character sequence `chars` in `str`. |
35 |
| -If `str` does not contain `chars`, then it returns `str`. |
36 |
| - |
37 |
| - |
38 |
| -#### `$uppercase(str)` |
39 |
| - |
40 |
| -Returns a string with all the characters of `str` converted to uppercase. |
41 |
| - |
42 |
| - |
43 |
| -#### `$lowercase(str)` |
44 |
| - |
45 |
| -Returns a string with all the characters of `str` converted to lowercase. |
46 |
| - |
47 |
| - |
48 |
| -#### `$split(str, separator, limit)` |
49 |
| - |
50 |
| -Splits the `str` parameter into an array of substrings. It is an error if `str` is not a string. |
51 |
| - |
52 |
| -The optional `separator` parameter |
53 |
| -specifies the characters within the `str` about which it should be split. If `separator` is |
54 |
| -not specified, then the empty string is assumed, and `str` will be split into an array of single |
55 |
| -characters. It is an error if `separator` is not a string. |
56 |
| - |
57 |
| -The optional `limit` parameter is a number that specifies the maximum number of substrings to |
58 |
| -include in the resultant array. Any additional substrings are discarded. If `limit` is not |
59 |
| -specified, then `str` is fully split with no limit to the size of the resultant array. |
60 |
| -It is an error if `limit` is not a non-negative number. |
61 |
| - |
62 |
| -#### `$join(array[, separator])` |
63 |
| - |
64 |
| -Joins an array of component strings into a single concatenated string with each component string |
65 |
| -separated by the optional `separator` parameter. |
66 |
| - |
67 |
| -It is an error if the input array contains an item which isn't a string. |
68 |
| - |
69 |
| -If `separator` is not specified, then it is assumed to be the empty string, i.e. no separator |
70 |
| -between the component strings. It is an error if `separator` is not a string. |
71 |
| - |
72 |
| -#### `$format(pattern, ...)` - to be implemented |
73 |
| - |
74 |
| - [printf style formatting](https://en.wikipedia.org/wiki/Printf_format_string#Format_placeholder_specification). Other formatting schemes are available. |
75 |
| - |
76 |
| -### Numerical functions |
77 |
| - |
78 |
| -#### `$number(arg)` |
79 |
| - |
80 |
| -Casts the `arg` parameter to a number using the following casting rules |
81 |
| - - Numbers are unchanged |
82 |
| - - Strings that contain a sequence of characters that represent a legal JSON number are converted to that number |
83 |
| - - All other values cause an error to be thrown. |
84 |
| - |
85 |
| -#### `$abs(number)` - to be implemented |
86 |
| -#### `$round(number)` - to be implemented |
87 |
| - |
88 |
| -Rounds up to the nearest integer |
89 |
| - |
90 |
| -#### `$roundHalfToEven(number)` - to be implemented |
91 |
| - |
92 |
| - [Round half to even](https://en.wikipedia.org/wiki/Rounding#Round_half_to_even) Commonly used in financial calculations. |
93 |
| - |
94 |
| -#### `$power(base, exponent)` - to be implemented |
95 |
| - |
96 |
| -### Numeric aggregation functions |
97 |
| - |
98 |
| -#### `$sum(array)` |
99 |
| - |
100 |
| -Returns the arithmetic sum of an array of numbers. |
101 |
| -It is an error if the input array contains an item which isn't a number. |
102 |
| - |
103 |
| -#### `$max(array)` |
104 |
| - |
105 |
| -Returns the maximum number in an array of numbers. |
106 |
| -It is an error if the input array contains an item which isn't a number. |
107 |
| - |
108 |
| -#### `$min(array)` |
109 |
| - |
110 |
| -Returns the minimum number in an array of numbers. |
111 |
| -It is an error if the input array contains an item which isn't a number. |
112 |
| - |
113 |
| -#### `$average(array)` |
114 |
| - |
115 |
| -Returns the mean value of an array of numbers. |
116 |
| -It is an error if the input array contains an item which isn't a number. |
117 |
| - |
118 |
| -### Boolean functions |
119 |
| - |
120 |
| -#### `$boolean(arg)` |
121 |
| - |
122 |
| -Casts the argument to a Boolean using the following rules: |
123 |
| - |
124 |
| -| Argument type | Result | |
125 |
| -| ------------- | ------ | |
126 |
| -| Boolean | unchanged | |
127 |
| -| string: empty | `false`| |
128 |
| -| string: non-empty | `true` | |
129 |
| -| number: 0 | `false`| |
130 |
| -| number: non-zero | `true` | |
131 |
| -| null | `false`| |
132 |
| -| array: empty | `false` | |
133 |
| -| array: contains a member that casts to `true` | `true` | |
134 |
| -| array: all members cast to `false` | `false` | |
135 |
| -| object: empty | `false` | |
136 |
| -| object: non-empty | `true` | |
137 |
| -| function | `false` | |
138 |
| - |
139 |
| - |
140 |
| -#### `$not(arg)` |
141 |
| - |
142 |
| -Returns Boolean NOT on the argument. `arg` is first cast to a boolean |
143 |
| - |
144 |
| -#### `$exists(arg)` |
145 |
| - |
146 |
| -Returns Boolean `true` if the arg expression evaluates to a value, or |
147 |
| -`false` if the expression does not match anything (e.g. a path to a non-existent |
148 |
| -field reference). |
149 |
| - |
150 |
| -### Date/Time functions |
151 |
| -tbd |
152 |
| - |
153 |
| -### Array functions |
154 |
| - |
155 |
| -#### `$count(array)` |
156 |
| - |
157 |
| -Returns the number of items in the array |
158 |
| - |
159 |
| -#### `$append(array, array)` |
160 |
| - |
161 |
| -Appends two arrays |
162 |
| - |
163 |
| -#### `$flatten(array)` - to be implemented |
164 |
| - |
165 |
| -Flattens nested array into flat array |
166 |
| - |
167 |
| -#### `$range(start, end, increment)` - to be implemented |
168 |
| - |
169 |
| -Generates an array of numbers starting with `start`, not exceeding `end`, in increments of `increment` (defaults to 1) |
170 |
| - |
171 |
| -### Object functions |
172 |
| - |
173 |
| -#### `$keys(object)` |
174 |
| - |
175 |
| -Returns an array containing the keys in the object. If the argument is an array of objects, |
176 |
| -then the array returned contains a de-duplicated list of all the keys in all of the objects. |
177 |
| - |
178 |
| -#### `$lookup(object, key)` |
179 |
| - |
180 |
| -Returns the value associated with `key` in `object`. If the first argument is an array of objects, |
181 |
| -then all of the objects in the array are searched, and the values associated with all occurrences |
182 |
| -of `key` are returned. |
183 |
| - |
184 |
| -#### `$merge(object, object)` - to be implemented |
185 |
| - |
186 |
| -Returns an object containing the union of the two `object` parameters. If an entry in the second object |
187 |
| -has the same key as an entry in the first, then the value will be overridden by the second. |
188 |
| - |
189 |
| -#### `$spread(object)` |
190 |
| - |
191 |
| -Splits an `object` containing key/value pairs into an array of objects, each of which has a single |
192 |
| -key/value pair from the input `object`. If the parameter is an array of objects, then the resultant |
193 |
| -array contains an object for every key/value pair in every object in the supplied array. |
194 |
| - |
195 |
| -### Higher-order functions |
196 |
| - |
197 |
| -#### `$map` |
198 |
| - |
199 |
| -#### `$reduce` |
| 4 | +The docs are open-source on GitHub Pages: https://github.com/jsonata-js/jsonata-js.github.io |
0 commit comments