Skip to content

Commit

Permalink
Merge pull request #21 from kislakiruben/rk/fix-year
Browse files Browse the repository at this point in the history
  • Loading branch information
kislakiruben authored Nov 25, 2022
2 parents 2e2f7fa + 3f1c75f commit 6863c99
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 15 deletions.
27 changes: 13 additions & 14 deletions src/dateParser.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
import { isValidDate } from './utils';

const CENTURY_MAP = {
1: '19',
2: '19',
3: '18',
4: '18',
5: '20',
6: '20',
};
const dateParser = (sexCode, birthdate) => {
const centuryMapping = {
1: '19',
2: '19',
3: '18',
4: '18',
5: '20',
6: '20',
7: '20',
8: '20',
};
const year = birthdate.substr(0, 2);
const month = birthdate.substr(2, 2);
const day = birthdate.substr(4, 2);
let year = `${centuryMapping[sexCode]}${birthdate.substr(0, 2)}`;
if (year > (new Date()).getFullYear() - 14) year = `${year - 100}`
const date = new Date(year, month - 1, day);
const isResident = sexCode === 7 || sexCode === 8;
const fullYear = `${isResident ? year < new Date().getFullYear().toString().substr(2, 2) ? 20 : 19 : CENTURY_MAP[sexCode]}${year}`;
const date = new Date(fullYear, month - 1, day, 0, 0, 0, 0);

return isValidDate(year, month, day) ? date : new Date('');
return isValidDate(fullYear, month, day) ? date : new Date('');
};
const dateValidator = (date) => !isNaN(date);

Expand Down
7 changes: 6 additions & 1 deletion test/dateParser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@ import { expect } from 'chai';
import { default as dateParser, dateValidator } from '../src/dateParser';

describe(`date parser`, () => {
it(`should return create a correct date object`, () => {
it(`should create a correct date object`, () => {
expect(dateParser(1, '880110')).to.equalDate(new Date(1988, 0, 10));
expect(dateParser(5, '020202')).to.equalDate(new Date(2002, 1, 2));
expect(dateParser(6, '120401')).to.equalDate(new Date(2012, 3, 1));
});

it(`should correctly parse resident birthdate`, () => {
expect(dateParser(7, '120401')).to.equalDate(new Date(2012, 3, 1));
expect(dateParser(7, '880401')).to.equalDate(new Date(1988, 3, 1));
})
});

describe(`date validator`, () => {
Expand Down

0 comments on commit 6863c99

Please sign in to comment.