Skip to content

Commit

Permalink
0.7.7 - add isUuid predicate to Strings
Browse files Browse the repository at this point in the history
  • Loading branch information
SeanCannon committed Mar 30, 2023
1 parent 597d3bf commit 90b7c2b
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 3 deletions.
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ if (prr.objectSatisfies(userType, user)) {
const validateUserOrThrow = V.isObjectOf(userType);

validateUserOrThrow(userType, user);

```

## Strings
Expand Down Expand Up @@ -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

Expand Down
5 changes: 4 additions & 1 deletion lib/predicates/strings.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -63,5 +65,6 @@ module.exports = {
isTimestamp,
isMySqlTimestamp,
isEmail,
isJSON
isJSON,
isUuid
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
12 changes: 12 additions & 0 deletions spec/predicates/stringsSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
});

0 comments on commit 90b7c2b

Please sign in to comment.