From 0f562feff59167be3431c04cb1aa65a678eba400 Mon Sep 17 00:00:00 2001 From: Gervwyk Date: Thu, 7 Jan 2021 12:09:09 +0200 Subject: [PATCH] feat: Add _uuid operator to NodeParser. --- .pnp.js | 3 ++- packages/operators/package.json | 3 ++- packages/operators/src/node/index.js | 2 ++ packages/operators/src/node/uuid.js | 23 +++++++++++++++++++ .../operators/test/node/NodeParser.test.js | 8 +++++++ packages/operators/test/node/uuid.test.js | 13 +++++++++++ packages/operators/test/web/WebParser.test.js | 6 ----- yarn.lock | 1 + 8 files changed, 51 insertions(+), 8 deletions(-) create mode 100644 packages/operators/src/node/uuid.js create mode 100644 packages/operators/test/node/uuid.test.js diff --git a/.pnp.js b/.pnp.js index 77bb4c107..e14dce134 100755 --- a/.pnp.js +++ b/.pnp.js @@ -4660,7 +4660,8 @@ function $$SETUP_STATE(hydrateRuntimeState, basePath) { ["babel-jest", "virtual:caddf51df4928b33a437ca87b8f5ddfb6205ebd6d8231f74d4ee7223f3866e6f815b221aa1e2bd33e98915f701e95bae72a93d2288b49a34a6246bdbc2a4a132#npm:26.6.3"], ["jest", "npm:26.6.3"], ["js-yaml", "npm:3.14.1"], - ["mingo", "npm:3.1.0"] + ["mingo", "npm:3.1.0"], + ["uuid", "npm:8.3.2"] ], "linkType": "SOFT", }] diff --git a/packages/operators/package.json b/packages/operators/package.json index 44d879796..65d99523f 100644 --- a/packages/operators/package.json +++ b/packages/operators/package.json @@ -41,7 +41,8 @@ "@lowdefy/helpers": "3.1.1", "@lowdefy/nunjucks": "3.1.1", "js-yaml": "3.14.1", - "mingo": "3.1.0" + "mingo": "3.1.0", + "uuid": "8.3.2" }, "devDependencies": { "@babel/cli": "7.12.10", diff --git a/packages/operators/src/node/index.js b/packages/operators/src/node/index.js index 7d7f1f181..f4752755f 100644 --- a/packages/operators/src/node/index.js +++ b/packages/operators/src/node/index.js @@ -17,9 +17,11 @@ import _secret from './secret'; import _base64_decode from './base64_decode'; import _base64_encode from './base64_encode'; +import _uuid from './uuid'; export default { _base64_decode, _base64_encode, _secret, + _uuid, }; diff --git a/packages/operators/src/node/uuid.js b/packages/operators/src/node/uuid.js new file mode 100644 index 000000000..9ec230983 --- /dev/null +++ b/packages/operators/src/node/uuid.js @@ -0,0 +1,23 @@ +/* + Copyright 2020 Lowdefy, Inc + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +import { v4 as uuidv4 } from 'uuid'; + +function _uuid() { + return uuidv4(); +} + +export default _uuid; diff --git a/packages/operators/test/node/NodeParser.test.js b/packages/operators/test/node/NodeParser.test.js index 16f3510dd..c9043c563 100644 --- a/packages/operators/test/node/NodeParser.test.js +++ b/packages/operators/test/node/NodeParser.test.js @@ -190,3 +190,11 @@ test('parse _random operator', () => { expect(res.errors).toMatchInlineSnapshot(`Array []`); Math.random = mathRandomFn; }); + +test('parse _uuid operator', () => { + const input = { a: { _uuid: true } }; + const parser = new NodeParser({ state }); + const res = parser.parse({ input, args, location: 'locationId' }); + expect(res.output.a.length).toEqual(36); + expect(res.errors).toMatchInlineSnapshot(`Array []`); +}); diff --git a/packages/operators/test/node/uuid.test.js b/packages/operators/test/node/uuid.test.js new file mode 100644 index 000000000..e2851c647 --- /dev/null +++ b/packages/operators/test/node/uuid.test.js @@ -0,0 +1,13 @@ +import { v4 as uuidv4 } from 'uuid'; +import uuid from '../../src/node/uuid'; + +jest.mock('uuid', () => { + return { + v4: jest.fn(() => 'ABC'), + }; +}); + +test('_uuid', () => { + expect(uuid({ params: true, location: 'locationId' })).toEqual('ABC'); + expect(uuidv4).toHaveBeenCalled(); +}); diff --git a/packages/operators/test/web/WebParser.test.js b/packages/operators/test/web/WebParser.test.js index 64cbad38e..90eb0783b 100644 --- a/packages/operators/test/web/WebParser.test.js +++ b/packages/operators/test/web/WebParser.test.js @@ -30,12 +30,6 @@ const context = { menuId: 'm_2', }, ], - mutations: { - not_loaded: { loading: true, response: 'fail' }, - string: { loading: false, response: 'mutation String' }, - number: { loading: false, response: 500 }, - arr: { loading: false, response: [{ a: 'mutation a1' }, { a: 'mutation a2' }] }, - }, requests: { not_loaded: { loading: true, response: 'fail' }, string: { loading: false, response: 'request String' }, diff --git a/yarn.lock b/yarn.lock index e40b35906..6704a7331 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3244,6 +3244,7 @@ __metadata: jest: 26.6.3 js-yaml: 3.14.1 mingo: 3.1.0 + uuid: 8.3.2 languageName: unknown linkType: soft