diff --git a/.github/workflows/jsonata.yml b/.github/workflows/jsonata.yml index 76048c5..f59466f 100644 --- a/.github/workflows/jsonata.yml +++ b/.github/workflows/jsonata.yml @@ -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: [16.x, 18.x, 20.x, 22.x] steps: - name: Checkout uses: actions/checkout@v3 diff --git a/docs/string-functions.md b/docs/string-functions.md index c4638a9..8bcf055 100644 --- a/docs/string-functions.md +++ b/docs/string-functions.md @@ -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"` diff --git a/package.json b/package.json index e3abe6e..f8bbdf4 100644 --- a/package.json +++ b/package.json @@ -57,6 +57,6 @@ "uglify-es": "^3.3.10" }, "engines": { - "node": ">= 8" + "node": ">= 16" } } diff --git a/src/functions.js b/src/functions.js index c3fff7a..5b4102e 100644 --- a/src/functions.js +++ b/src/functions.js @@ -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 @@ -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 }; })(); diff --git a/src/jsonata.js b/src/jsonata.js index 5807736..0bc0d29 100644 --- a/src/jsonata.js +++ b/src/jsonata.js @@ -1929,6 +1929,7 @@ var jsonata = (function() { staticFrame.bind('encodeUrl', defineFunction(fn.encodeUrl, '')); staticFrame.bind('decodeUrlComponent', defineFunction(fn.decodeUrlComponent, '')); staticFrame.bind('decodeUrl', defineFunction(fn.decodeUrl, '')); + staticFrame.bind('uuid', defineFunction(fn.uuid, '<:s>')); staticFrame.bind('eval', defineFunction(functionEval, '')); staticFrame.bind('toMillis', defineFunction(datetime.toMillis, '')); staticFrame.bind('fromMillis', defineFunction(datetime.fromMillis, '')); diff --git a/test/test-suite/groups/function-uuid/case000.json b/test/test-suite/groups/function-uuid/case000.json new file mode 100644 index 0000000..cecf58d --- /dev/null +++ b/test/test-suite/groups/function-uuid/case000.json @@ -0,0 +1,6 @@ +{ + "expr": "$length($uuid())", + "data": null, + "bindings": {}, + "result": 36 +} \ No newline at end of file diff --git a/test/test-suite/groups/function-uuid/case001.json b/test/test-suite/groups/function-uuid/case001.json new file mode 100644 index 0000000..9966f65 --- /dev/null +++ b/test/test-suite/groups/function-uuid/case001.json @@ -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 +} \ No newline at end of file