Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add function $uuid() #731

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/jsonata.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [12.x, 14.x, 16.x, 18.x, 20.x, 22.x]
node-version: [20.x, 22.x]
steps:
- name: Checkout
uses: actions/checkout@v3
Expand Down
9 changes: 9 additions & 0 deletions docs/string-functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -329,3 +329,12 @@ Decodes a Uniform Resource Locator (URL) previously created by encodeUrl.
__Examples__

- `$decodeUrl("https://mozilla.org/?x=%D1%88%D0%B5%D0%BB%D0%BB%D1%8B")` => `"https://mozilla.org/?x=шеллы"`

## `$uuid()`
__Signature:__ `$uuid()`

Generates a random universally unique identifer in accordance with the UUIDv4 RFC (https://datatracker.ietf.org/doc/html/rfc9562#name-uuid-version-4)

__Examples__

- `$uuid()` => `"721ae209-7d07-4cda-a1ff-1df536cfc79c"`
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,6 @@
"uglify-es": "^3.3.10"
},
"engines": {
"node": ">= 8"
"node": ">= 16"
}
}
11 changes: 10 additions & 1 deletion src/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,15 @@ const functions = (() => {
return atob(str);
}

/**
* Generate a UUID Version 4 (random)
* See https://datatracker.ietf.org/doc/html/rfc9562#name-uuid-version-4
* @returns {String} UUIDv4
*/
function uuid() {
return crypto.randomUUID();
}

/**
* Encode a string into a component for a url
* @param {String} str - String to encode
Expand Down Expand Up @@ -2064,7 +2073,7 @@ const functions = (() => {
boolean, not,
map, zip, filter, single, foldLeft, sift,
keys, lookup, append, exists, spread, merge, reverse, each, error, assert, type, sort, shuffle, distinct,
base64encode, base64decode, encodeUrlComponent, encodeUrl, decodeUrlComponent, decodeUrl
base64encode, base64decode, encodeUrlComponent, encodeUrl, decodeUrlComponent, decodeUrl, uuid
};
})();

Expand Down
1 change: 1 addition & 0 deletions src/jsonata.js
Original file line number Diff line number Diff line change
Expand Up @@ -1929,6 +1929,7 @@ var jsonata = (function() {
staticFrame.bind('encodeUrl', defineFunction(fn.encodeUrl, '<s-:s>'));
staticFrame.bind('decodeUrlComponent', defineFunction(fn.decodeUrlComponent, '<s-:s>'));
staticFrame.bind('decodeUrl', defineFunction(fn.decodeUrl, '<s-:s>'));
staticFrame.bind('uuid', defineFunction(fn.uuid, '<:s>'));
staticFrame.bind('eval', defineFunction(functionEval, '<sx?:x>'));
staticFrame.bind('toMillis', defineFunction(datetime.toMillis, '<s-s?:n>'));
staticFrame.bind('fromMillis', defineFunction(datetime.fromMillis, '<n-s?s?:s>'));
Expand Down
6 changes: 6 additions & 0 deletions test/test-suite/groups/function-uuid/case000.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"expr": "$length($uuid())",
"data": null,
"bindings": {},
"result": 36
}
6 changes: 6 additions & 0 deletions test/test-suite/groups/function-uuid/case001.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"expr": "$match($uuid(), /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/).index",
"data": null,
"bindings": {},
"result": 0
}
Loading