From 3959dc20ec476841fc2f2b7feddcc616b8485d19 Mon Sep 17 00:00:00 2001 From: Ramesh Nair Date: Thu, 22 Nov 2018 17:44:29 +0000 Subject: [PATCH] 1.7.0 - uuid support --- package.json | 2 +- src/index.js | 1 + src/uuid.js | 15 +++++++++++++++ src/uuid.test.js | 29 +++++++++++++++++++++++++++++ 4 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 src/uuid.js create mode 100644 src/uuid.test.js diff --git a/package.json b/package.json index c11bfb1..db936f0 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@wearekickback/shared", - "version": "1.6.0", + "version": "1.7.0", "description": "Shared utils between client and server", "main": "dist/index.js", "module": "src/index.js", diff --git a/src/index.js b/src/index.js index b12c7c3..f51d2d2 100644 --- a/src/index.js +++ b/src/index.js @@ -7,3 +7,4 @@ export * from './strings' export * from './legal' export * from './participants' export * from './social' +export * from './uuid' diff --git a/src/uuid.js b/src/uuid.js new file mode 100644 index 0000000..9fba2a2 --- /dev/null +++ b/src/uuid.js @@ -0,0 +1,15 @@ +import { isUUID as checkIsUUID } from 'validator' + +export const isUUID = str => { + if (typeof str !== 'string') { + return false + } + + return checkIsUUID(str) +} + +export const assertUUID = str => { + if (!isUUID(str)) { + throw new Error(`Invalid UUID: ${str}`) + } +} diff --git a/src/uuid.test.js b/src/uuid.test.js new file mode 100644 index 0000000..55929ed --- /dev/null +++ b/src/uuid.test.js @@ -0,0 +1,29 @@ +import { + isUUID, + assertUUID, +} from './' + + + +describe('uuid', () => { + it('checks and assertions', () => { + const tests = [ + [ 1024, false ], + [ '', false ], + [ 'str', false ], + [ 'a21c09cc-f1db-4086-9bd7-e568e23fe160', true ], + ] + + expect.assertions(tests.length * 2) + + tests.forEach(([ input, expected ]) => { + expect(isUUID(input)).toEqual(expected) + + if (expected) { + expect(() => assertUUID(input)).not.toThrow() + } else { + expect(() => assertUUID(input)).toThrow() + } + }) + }) +})