From 90b7c2bc4762351e70da710237d26e9c7abcf354 Mon Sep 17 00:00:00 2001 From: seancannon Date: Thu, 30 Mar 2023 11:21:40 -0700 Subject: [PATCH] 0.7.7 - add isUuid predicate to Strings --- README.md | 11 ++++++++++- lib/predicates/strings.js | 5 ++++- package.json | 2 +- spec/predicates/stringsSpec.js | 12 ++++++++++++ 4 files changed, 27 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 2258d49..0a95a3a 100644 --- a/README.md +++ b/README.md @@ -45,7 +45,6 @@ if (prr.objectSatisfies(userType, user)) { const validateUserOrThrow = V.isObjectOf(userType); validateUserOrThrow(userType, user); - ``` ## Strings @@ -197,6 +196,16 @@ expect(prr.isJSON('{}')).toBe(true); expect(prr.isJSON('{"foo":"bar"}')).toBe(true); ``` +--- +### isUuid +String → Boolean +``` +expect(prr.isUuid('foo')).toBe(false); +expect(prr.isUuid(123)).toBe(false); +expect(prr.isUuid('cd17c371-9468-963b-f3e0bf05e70e')).toBe(false); +expect(prr.isUuid('cd17c371-9468-4baa-963b-f3e0bf05e70e')).toBe(true); +``` + ## Numbers diff --git a/lib/predicates/strings.js b/lib/predicates/strings.js index dcfebdb..aa28bb0 100644 --- a/lib/predicates/strings.js +++ b/lib/predicates/strings.js @@ -38,6 +38,8 @@ const isTimestamp = R.test(/(\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d\.\d+([ const isMySqlTimestamp = R.test(/^([1-2][0-9]{3})-([0-1][0-9])-([0-3][0-9])(?:( [0-2][0-9]):([0-5][0-9]):([0-5][0-9]))?$/); +const isUuid = R.test(/^[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}$/gi); + const isJSON = (str) => { try { JSON.parse(str); @@ -63,5 +65,6 @@ module.exports = { isTimestamp, isMySqlTimestamp, isEmail, - isJSON + isJSON, + isUuid }; diff --git a/package.json b/package.json index bb86930..22923bd 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "prettycats", - "version": "0.7.6", + "version": "0.7.7", "description": "Helpful, common, and curried predicates library built on Ramda.", "main": "dist/prettycats.js", "dependencies": { diff --git a/spec/predicates/stringsSpec.js b/spec/predicates/stringsSpec.js index 595f56d..5f83569 100644 --- a/spec/predicates/stringsSpec.js +++ b/spec/predicates/stringsSpec.js @@ -219,5 +219,17 @@ describe('string predicates', () => { expect(strings.isJSON('{foo:"bar"}')).toBe(false); }); }); + + describe('isUuid', () => { + it('passes when given a correctly formatted UUID', () => { + expect(strings.isUuid('cd17c371-9468-4baa-963b-f3e0bf05e70e')).toBe(true); + }); + it('fails when given a value not of type String', () => { + expect(strings.isUuid(123)).toBe(false); + }); + it('fails when given an invalid UUID string', () => { + expect(strings.isUuid('cd17c371-9468-963b-f3e0bf05e70e')).toBe(false); + }); + }); });