Skip to content

Commit

Permalink
0.7.5 - add isMySqlTimestamp predicate to Strings
Browse files Browse the repository at this point in the history
  • Loading branch information
SeanCannon committed Nov 3, 2021
1 parent 35b33c8 commit 97267e1
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
3 changes: 3 additions & 0 deletions lib/predicates/strings.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ const isNumericString = str => isString(str) && !isNaN(parseInt(str, 10));

const isTimestamp = R.test(/(\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d\.\d+([+-][0-2]\d:[0-5]\d|Z))|(\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d([+-][0-2]\d:[0-5]\d|Z))|(\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d([+-][0-2]\d:[0-5]\d|Z))/);

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 isJSON = (str) => {
try {
JSON.parse(str);
Expand All @@ -59,6 +61,7 @@ module.exports = {
stringIsOneOf,
isNumericString,
isTimestamp,
isMySqlTimestamp,
isEmail,
isJSON
};
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.4",
"version": "0.7.5",
"description": "Helpful, common, and curried predicates library built on Ramda.",
"main": "dist/prettycats.js",
"dependencies": {
Expand Down
15 changes: 15 additions & 0 deletions spec/predicates/stringsSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,21 @@ describe('string predicates', () => {
});
});

describe('isMySqlTimestamp', () => {
it('passes when given supported variations of a MySQL timestamp', () => {
expect(strings.isMySqlTimestamp('2012-06-22 05:40:06')).toBe(true);
expect(strings.isMySqlTimestamp('2021-11-03 11:15:59')).toBe(true);
});
it('fails when given unsupported variations of datetime strings', () => {
expect(strings.isMySqlTimestamp('Thu Aug 24 2017')).toBe(false);
expect(strings.isMySqlTimestamp('Thu Aug 24 2017 19:20:30 GMT-0700 (PDT)')).toBe(false);
});
it('fails when given a type other than string', () => {
expect(strings.isMySqlTimestamp({ foo : 'bar' })).toBe(false);
expect(strings.isMySqlTimestamp(2017)).toBe(false);
});
});

describe('isJSON', () => {
it('passes when given correctly formatted JSON', () => {
expect(strings.isJSON('{}')).toBe(true);
Expand Down

0 comments on commit 97267e1

Please sign in to comment.