@@ -14,6 +14,21 @@ This fork extends the original specs, adding the following functionality;
1414* [ Root value access] ( #root-value-access )
1515* [ Number literals] ( #number-literals )
1616
17+ Additionally, it adds the following functions:
18+
19+ * [ if] ( #if ) - Conditional expression
20+ * [ range] ( #range ) - Generate a range of numbers or prefixed strings
21+ * [ to_object] ( #to_object ) - Convert an array of key-value pairs into an object
22+ * [ json_serialize] ( #json_serialize ) - Serialize a JSON value to a string
23+ * [ json_parse] ( #json_parse ) - Parse a JSON string into a JSON object
24+ * [ sha256] ( #sha256 ) - Calculate the SHA-256 hash of a string
25+ * [ sha512] ( #sha512 ) - Calculate the SHA-512 hash of a string
26+ * [ uuid] ( #uuid ) - Generate a UUID
27+ * [ regex_test] ( #regex_test ) - Test if a string matches a regular expression
28+ * [ regex_match] ( #regex_match ) - Return the first match of a regular expression in a string
29+ * [ regex_match_all] ( #regex_match_all ) - Return all matches of a regular expression in a string
30+ * [ regex_replace] ( #regex_replace ) - Replace parts of a string matching a regular expression with a replacement string
31+
1732## INSTALLATION
1833
1934```
@@ -164,7 +179,7 @@ Numbers in the root scope are treated as number literals. This means that you do
164179need to quote numbers with backticks.
165180
166181` ` ` javascript
167- search ([{" bar" : 1 }, {" bar" : 10 }]} , ' [?bar==10]' )
182+ search ([{" bar" : 1 }, {" bar" : 10 }], ' [?bar==10]' )
168183
169184// OUTPUTS;
170185// [{"bar": 10}]
@@ -174,9 +189,200 @@ You can also use numbers in arithmetic operations
174189
175190` ` `
176191search ({}, ' 16 + 26' ); // 42
192+ ` ` `
193+
194+ ## Additional Functions
195+
196+ ### ` if `
197+ **Syntax**:
198+ ` ` ` jmespath
199+ if (condition, thenValue, elseValue? )
200+ ` ` `
201+
202+ **Description**:
203+ Returns ` thenValue` if ` condition` is true, otherwise returns ` elseValue` . If ` elseValue` is not provided, it defaults to ` null ` .
204+
205+ **Example**:
206+ ` ` ` jmespath
207+ if (@ > 10 , " large" , " small" )
208+ ` ` `
209+
210+ ### ` range`
211+ **Syntax**:
212+ ` ` ` jmespath
213+ range (start, end, prefix? )
214+ ` ` `
215+
216+ **Description**:
217+ Generates an array of numbers or prefixed strings from ` start` to ` end - 1 ` . If ` prefix` is provided, each number is prefixed.
218+
219+ **Example**:
220+ ` ` ` jmespath
221+ range (1 , 5 ) // [1, 2, 3, 4]
222+ range (1 , 5 , ' item_' ) // ["item_1", "item_2", "item_3", "item_4"]
223+ ` ` `
224+
225+ ### ` to_object`
226+ **Syntax**:
227+ ` ` ` jmespath
228+ to_object (entries)
229+ ` ` `
230+
231+ **Description**:
232+ Converts an array of key-value pairs into an object.
233+
234+ **Example**:
235+ ` ` ` jmespath
236+ to_object ([[' key1' , ' value1' ], [' key2' , ' value2' ]])
237+ // { "key1": "value1", "key2": "value2" }
238+
239+ [ ' value1' , ' value2' ] | to_object (zip (range (1 , length (@) + 1 , ' key' ), @))
240+ // { "key1": "value1", "key2": "value2" }
241+ ` ` `
242+
243+ ### ` json_serialize`
244+ **Syntax**:
245+ ` ` ` jmespath
246+ json_serialize (value)
247+ ` ` `
248+
249+ _Uses a [deterministic version of JSON.stringify](https://www.npmjs.com/package/fast-json-stable-stringify) to serialize the value._
250+
251+ **Description**:
252+ Serializes a JSON value to a string.
253+
254+ **Example**:
255+ ` ` ` jmespath
256+ json_serialize ({ key: ' value' })
257+ // "{\"key\":\"value\"}"
258+ ` ` `
259+
260+ ### ` json_parse`
261+ **Syntax**:
262+ ` ` ` jmespath
263+ json_parse (string)
264+ ` ` `
265+
266+ **Description**:
267+ Parses a JSON string into a JSON object.
268+
269+ **Example**:
270+ ` ` ` jmespath
271+ json_parse (" {\" key\" :\" value\" }" )
272+ // { "key": "value" }
273+ ` ` `
274+
275+ ### ` sha256`
276+ **Syntax**:
277+ ` ` ` jmespath
278+ sha256 (string)
279+ ` ` `
280+
281+ **Description**:
282+ Calculates the SHA-256 hash of a string and returns it as a hexadecimal string.
283+
284+ **Example**:
285+ ` ` ` jmespath
286+ sha256 (' hello' )
287+ // "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824"
288+ ` ` `
289+
290+ ### ` sha512`
291+ **Syntax**:
292+ ` ` ` jmespath
293+ sha512 (string)
294+ ` ` `
295+
296+ **Description**:
297+ Calculates the SHA-512 hash of a string and returns it as a hexadecimal string.
298+
299+ **Example**:
300+ ` ` ` jmespath
301+ sha512 (' hello' )
302+ // "9b71d224bd62f3785d96d46ad3ea3d73319b0c44e59b202205c5d235a0a6caa5a3b36f8c0ab9d45df9215bf07d4d1552c0b1f8bd2671c8a7a3d126f457d79d72"
303+ ` ` `
304+
305+ ### ` uuid`
306+ **Syntax**:
307+ ` ` ` jmespath
308+ uuid (name? , namespace? )
309+ ` ` `
310+
311+ **Description**:
312+ Generates a UUID. If ` name` and (optionally) ` namespace` are provided, generates a version 5 UUID; otherwise, generates a version 4 UUID.
313+
314+ **Example**:
315+ ` ` ` jmespath
316+ uuid () // Random v4 UUID
317+ uuid (' example' ) // v5 UUID
318+ uuid (' example' , ' 6ba7b810-9dad-11d1-80b4-00c04fd430c8' ) // v5 UUID with namespace
319+ ` ` `
320+
321+ ` name` must be a string. Use ` json_serialize ()` to convert a JSON object to a string.
322+
323+ ### ` regex_test`
324+ **Syntax**:
325+ ` ` ` jmespath
326+ regex_test (regex, string)
327+ ` ` `
328+
329+ **Description**:
330+ Tests if a string matches a given regular expression.
331+
332+ **Example**:
333+ ` ` ` jmespath
334+ regex_test (' /^hello/' , ' hello world' ) // true
335+
336+ regex_test (' /^hello/' , ' HELLO world' ) // false
337+ regex_test (' /^hello/i' , ' HELLO world' ) // true
338+ ` ` `
339+
340+ ### ` regex_match`
341+ **Syntax**:
342+ ` ` ` jmespath
343+ regex_match (regex, string)
344+ ` ` `
345+
346+ **Description**:
347+ Returns the first match of a regular expression in a string as an array.
348+
349+ **Example**:
350+ ` ` ` jmespath
351+ regex_match (' /hello (\\ w+)/' , ' hello world' )
352+ // ["hello world", "world"]
353+
354+ regex_match (' /\\ w+/g' , ' hello world' )
355+ // ["hello", "world"]
356+ ` ` `
357+
358+ ### ` regex_match_all`
359+ **Syntax**:
360+ ` ` ` jmespath
361+ regex_match_all (regex, string)
362+ ` ` `
363+
364+ **Description**:
365+ Returns all matches of a regular expression in a string as an array of arrays.
366+
367+ **Example**:
368+ ` ` ` jmespath
369+ regex_match_all (' /(\\ w+)=(\d +)/g' , ' foo=24 bar=99' )
370+ // [["foo=24", "foo", "24"], ["bar=99", "bar", "99"]]
371+ ` ` `
372+
373+ ### ` regex_replace`
374+ **Syntax**:
375+ ` ` ` jmespath
376+ regex_replace (regex, replacement, string)
377+ ` ` `
378+
379+ **Description**:
380+ Replaces parts of a string matching a regular expression with a replacement string.
177381
178- // With the original specs we'd need to do
179- search ({}, ' `16` + `26`' );
382+ **Example**:
383+ ` ` ` jmespath
384+ regex_replace (' /world/' , ' universe' , ' hello world' )
385+ // "hello universe"
180386` ` `
181387
182388## More Resources
0 commit comments