Skip to content

Commit a3ca9f7

Browse files
Add function $uuid()
Generates a random UUID (version 4) Signed-off-by: Andrew Coleman <[email protected]>
1 parent 09dba37 commit a3ca9f7

File tree

7 files changed

+34
-3
lines changed

7 files changed

+34
-3
lines changed

.github/workflows/jsonata.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ jobs:
99
runs-on: ubuntu-latest
1010
strategy:
1111
matrix:
12-
node-version: [12.x, 14.x, 16.x, 18.x, 20.x, 22.x]
12+
node-version: [20.x, 22.x]
1313
steps:
1414
- name: Checkout
1515
uses: actions/checkout@v3

docs/string-functions.md

+9
Original file line numberDiff line numberDiff line change
@@ -329,3 +329,12 @@ Decodes a Uniform Resource Locator (URL) previously created by encodeUrl.
329329
__Examples__
330330

331331
- `$decodeUrl("https://mozilla.org/?x=%D1%88%D0%B5%D0%BB%D0%BB%D1%8B")` => `"https://mozilla.org/?x=шеллы"`
332+
333+
## `$uuid()`
334+
__Signature:__ `$uuid()`
335+
336+
Generates a random universally unique identifer in accordance with the UUIDv4 RFC (https://datatracker.ietf.org/doc/html/rfc9562#name-uuid-version-4)
337+
338+
__Examples__
339+
340+
- `$uuid()` => `"721ae209-7d07-4cda-a1ff-1df536cfc79c"`

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,6 @@
5757
"uglify-es": "^3.3.10"
5858
},
5959
"engines": {
60-
"node": ">= 8"
60+
"node": ">= 16"
6161
}
6262
}

src/functions.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,15 @@ const functions = (() => {
587587
return atob(str);
588588
}
589589

590+
/**
591+
* Generate a UUID Version 4 (random)
592+
* See https://datatracker.ietf.org/doc/html/rfc9562#name-uuid-version-4
593+
* @returns {String} UUIDv4
594+
*/
595+
function uuid() {
596+
return crypto.randomUUID();
597+
}
598+
590599
/**
591600
* Encode a string into a component for a url
592601
* @param {String} str - String to encode
@@ -2064,7 +2073,7 @@ const functions = (() => {
20642073
boolean, not,
20652074
map, zip, filter, single, foldLeft, sift,
20662075
keys, lookup, append, exists, spread, merge, reverse, each, error, assert, type, sort, shuffle, distinct,
2067-
base64encode, base64decode, encodeUrlComponent, encodeUrl, decodeUrlComponent, decodeUrl
2076+
base64encode, base64decode, encodeUrlComponent, encodeUrl, decodeUrlComponent, decodeUrl, uuid
20682077
};
20692078
})();
20702079

src/jsonata.js

+1
Original file line numberDiff line numberDiff line change
@@ -1929,6 +1929,7 @@ var jsonata = (function() {
19291929
staticFrame.bind('encodeUrl', defineFunction(fn.encodeUrl, '<s-:s>'));
19301930
staticFrame.bind('decodeUrlComponent', defineFunction(fn.decodeUrlComponent, '<s-:s>'));
19311931
staticFrame.bind('decodeUrl', defineFunction(fn.decodeUrl, '<s-:s>'));
1932+
staticFrame.bind('uuid', defineFunction(fn.uuid, '<:s>'));
19321933
staticFrame.bind('eval', defineFunction(functionEval, '<sx?:x>'));
19331934
staticFrame.bind('toMillis', defineFunction(datetime.toMillis, '<s-s?:n>'));
19341935
staticFrame.bind('fromMillis', defineFunction(datetime.fromMillis, '<n-s?s?:s>'));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"expr": "$length($uuid())",
3+
"data": null,
4+
"bindings": {},
5+
"result": 36
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"expr": "$match($uuid(), /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/).index",
3+
"data": null,
4+
"bindings": {},
5+
"result": 0
6+
}

0 commit comments

Comments
 (0)