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

Add inferCentury, isFutureDate, isValidBirthdate #17

Merged
merged 1 commit into from
Apr 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@
"iso8601-duration": "^1.1.7",
"mobx": "^4",
"mobx-react": "^5",
"moment": "^2.24.0",
"numeral": "^2.0.6"
}
}
30 changes: 30 additions & 0 deletions src/date.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import moment from 'moment';

import { formatDate } from './formatting';
import { DATE_FORMATS } from './constants';

export function dateToday () {
return formatDate((new Date()).toISOString(), DATE_FORMATS.date_value);
}

export function isFutureDate (date: string) {
return new Date(date) > new Date(new Date().toDateString());
}

export function inferCentury (year: string) {
if (year.length !== 2) {
return year;
}

const thisCentury = dateToday().substr(0, 2)
, lastCentury = moment().subtract(100, 'years').format('YYYY').substr(0, 2)
, thisCenturyGuess = `${thisCentury}${year}`
, lastCenturyGuess = `${lastCentury}${year}`
;

if (isFutureDate(`${thisCenturyGuess}-01-01`)) {
return lastCenturyGuess;
}

return thisCenturyGuess;
}
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export * from './constants';
export * from './containers';
export * from './date';
export * from './formatting';
export * from './utils';
export * from './validation';
7 changes: 1 addition & 6 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
import Decimal from 'decimal.js';

import { CENT_DECIMAL, DATE_FORMATS } from './constants';
import { formatDate } from './formatting';
import { CENT_DECIMAL } from './constants';

export function insertIf (condition: boolean, element: any): any[] {
return condition ? [element] : [];
}

export function dateToday () {
return formatDate((new Date()).toISOString(), DATE_FORMATS.date_value);
}

export function getPercentValue (value?: null | string): string {
if (typeof value === 'undefined' || value === null || value === '') { return ''; }
return new Decimal(value).div(CENT_DECIMAL).toString();
Expand Down
10 changes: 10 additions & 0 deletions src/validation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import moment from 'moment';

export function isValidBirthdate (value: string) {
return (
!!value // Not empty
&& value.length === '####-##-##'.length // ISO date
&& moment(value).isValid() // Real day
&& moment(value).isBefore(moment()) // In the past
);
}
20 changes: 20 additions & 0 deletions test/date.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import * as util from '../src';

describe('date', () => {
it('isFutureDate', () => {
expect(util.isFutureDate('2019-04-08')).toEqual(false);
expect(util.isFutureDate('1998-01-01')).toEqual(false);
expect(util.isFutureDate('2998-01-01')).toEqual(true);
});

it('inferCentury', () => {
expect(util.inferCentury('00')).toBe('2000');
expect(util.inferCentury('19')).toBe('2019');
expect(util.inferCentury('50')).toBe('1950');
expect(util.inferCentury('98')).toBe('1998');

expect(util.inferCentury('9')).toBe('9');
expect(util.inferCentury('998')).toBe('998');
expect(util.inferCentury('1998')).toBe('1998');
});
});
21 changes: 21 additions & 0 deletions test/validation.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import * as util from '../src';

describe('validation', () => {
it('isValidBirthdate', () => {
expect(util.isValidBirthdate('8-01-01')).toBe(false);
expect(util.isValidBirthdate('98-01-01')).toBe(false);
expect(util.isValidBirthdate('998-01-01')).toBe(false);
expect(util.isValidBirthdate('1998-1-1')).toBe(false);

expect(util.isValidBirthdate('0000-00-00')).toBe(false);
expect(util.isValidBirthdate('0000-00-01')).toBe(false);
expect(util.isValidBirthdate('0000-01-00')).toBe(false);
expect(util.isValidBirthdate('0000-01-01')).toBe(true);

expect(util.isValidBirthdate('0998-01-01')).toBe(true);
expect(util.isValidBirthdate('1989-11-22')).toBe(true);
expect(util.isValidBirthdate('1998-01-01')).toBe(true);
expect(util.isValidBirthdate('2020-01-01')).toBe(false);
});
});

5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4984,6 +4984,11 @@ mobx@^4:
resolved "https://registry.yarnpkg.com/mobx/-/mobx-4.9.4.tgz#bb37a0e4e05f0b02be89ced9d23445cad73377ad"
integrity sha512-RaEpydw7D1ebp1pdFHrEMZcLk4nALAZyHAroCPQpqLzuIXIxJpLmMIe5PUZwYHqvlcWL6DVqDYCANZpPOi9iXA==

moment@^2.24.0:
version "2.24.0"
resolved "https://registry.yarnpkg.com/moment/-/moment-2.24.0.tgz#0d055d53f5052aa653c9f6eb68bb5d12bf5c2b5b"
integrity sha512-bV7f+6l2QigeBBZSM/6yTNq4P2fNpSWj/0e7jQcy87A8e7o2nAfP/34/2ky5Vw4B9S446EtIhodAzkFCcR4dQg==

moo@^0.4.3:
version "0.4.3"
resolved "https://registry.yarnpkg.com/moo/-/moo-0.4.3.tgz#3f847a26f31cf625a956a87f2b10fbc013bfd10e"
Expand Down