Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Date and time #26

Merged
merged 13 commits into from
May 20, 2020
Merged
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"test": "run-s build test:*",
"test:unit": "nyc --silent ava",
"watch": "run-s clean build:main && run-p \"build:main -- -w\" \"test:unit -- --watch\"",
"watch-tsc": "tsc --watch",
"cov": "run-s build test:unit cov:html && opn coverage/index.html",
"cov:html": "nyc report --reporter=html",
"cov:send": "nyc report --reporteyarnr=lcov > coverage.lcov && codecov",
Expand Down
7 changes: 5 additions & 2 deletions src/lib/dataInference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,11 @@ export function isFormatDateValid(
): boolean {
if(!inferIfStringIsNumber(value[0])) return false

// this will match yyyy-mm-dd and also yyyy-m-d
const regDate = /^([1-9][0-9]{3})\-(0?[1-9]|1[012])\-(0?[1-9]|[12][0-9]|3[01])$/;
// this will match date (and time) formats:
// date formats: YYYY-MM-DD or YYYY-M-D
// time formats: HH:mm:ss
// separator between date and time can be ` ` or `, `
const regDate = /^([1-9][0-9]{3})\-(0?[1-9]|1[012])\-(0?[1-9]|[12][0-9]|3[01])(,?[\s]([0-1][0-9]|[2][0-3]):([0-5][0-9])(:([0-5][0-9]))?)?$/;
// TODO: add other regex to accept also other date formats
ilariaventurini marked this conversation as resolved.
Show resolved Hide resolved

const isFormatDateValid = regDate.test(value.toString());
Expand Down
9 changes: 8 additions & 1 deletion src/lib/test/dataInference.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,17 @@ test('isDateValid', t => {
t.is(isFormatDateValid('2018-02-29'), true); // this day doesn't exist but we check only the date format
t.is(isFormatDateValid('2017-02-30'), true); // this day doesn't exist but we check only the date format
t.is(isFormatDateValid('2020-04-31'), true); // this day doesn't exist but we check only the date format
t.is(isFormatDateValid('2019-01-15 13:12:29'), true);
t.is(isFormatDateValid('2019-01-15, 13:12:29'), true);
t.is(isFormatDateValid('2019-01-15, 00:00:00'), true);
t.is(isFormatDateValid('2019-01-15, 23:59:59'), true);
t.is(isFormatDateValid('17-02-2019', rightParser), true);
t.is(isFormatDateValid('17-02-2019', wrongParser), true); // this shouldn't be right but we assume that if the user has written a parser, then the dates are in the correct format
t.is(isFormatDateValid('17-02-2019', defaultParser), true); // this shouldn't be right but we assume that if the user has written a parser, then the dates are in the correct format


t.is(isFormatDateValid('2019-01-15, 24:00:00'), false);
t.is(isFormatDateValid('2019-01-15, 23:60:00'), false);
t.is(isFormatDateValid('2019-01-15, 23:59:60'), false);
t.is(isFormatDateValid('2019-01-15 13:12:29.0'), false);
t.is(isFormatDateValid('17-02-2019'), false);
t.is(isFormatDateValid('0000-01-01'), false);
Expand Down
6 changes: 4 additions & 2 deletions tslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@
"no-class": true,
"no-mixed-interface": false,
"no-expression-statement": [
true,
false,
ilariaventurini marked this conversation as resolved.
Show resolved Hide resolved
{ "ignore-prefix": ["console.", "process.exit"] }
],
"no-if-statement": false,
/* end tslint-immutable rules */

"ordered-imports": false
"ordered-imports": false,
"curly": false,
"no-shadowed-variable": false
ilariaventurini marked this conversation as resolved.
Show resolved Hide resolved
}
}