From 8ca71b8f0643466f51a1b8b7eb987f756f25cbfc Mon Sep 17 00:00:00 2001 From: qa46hx Date: Mon, 26 Oct 2020 15:33:07 +0100 Subject: [PATCH] fix(localize): do not parse non-existing dates --- .changeset/swift-windows-train.md | 5 ++++ packages/localize/src/date/parseDate.js | 13 ++++++++-- packages/localize/test/date/parseDate.test.js | 26 ++++++++++++++++--- 3 files changed, 39 insertions(+), 5 deletions(-) create mode 100644 .changeset/swift-windows-train.md diff --git a/.changeset/swift-windows-train.md b/.changeset/swift-windows-train.md new file mode 100644 index 000000000..c0b1cec7f --- /dev/null +++ b/.changeset/swift-windows-train.md @@ -0,0 +1,5 @@ +--- +'@lion/localize': patch +--- + +`parseDate('31.02.2020')` returned `'Mon Mar 02 2020 00:00:00 GMT+0100....'`. But not anymore, now it returns `undefined`. diff --git a/packages/localize/src/date/parseDate.js b/packages/localize/src/date/parseDate.js index 6b13a7f4a..4dd5a59fb 100644 --- a/packages/localize/src/date/parseDate.js +++ b/packages/localize/src/date/parseDate.js @@ -56,8 +56,17 @@ export function parseDate(dateString) { } const [year, month, day] = parsedString.split('/').map(Number); - if (year > 0 && month > 0 && day > 0) { - return new Date(Date.UTC(year, month - 1, day)); + const parsedDate = new Date(Date.UTC(year, month - 1, day)); + + // Check if parsedDate is not `Invalid Date` or that the date has changed (e.g. the not existing 31.02.2020) + if ( + year > 0 && + month > 0 && + day > 0 && + parsedDate.getDate() === day && + parsedDate.getMonth() === month - 1 + ) { + return parsedDate; } return undefined; } diff --git a/packages/localize/test/date/parseDate.test.js b/packages/localize/test/date/parseDate.test.js index 82fec7b5d..22ab50311 100644 --- a/packages/localize/test/date/parseDate.test.js +++ b/packages/localize/test/date/parseDate.test.js @@ -1,7 +1,7 @@ import { expect } from '@open-wc/testing'; -import { localizeTearDown } from '../../test-helpers.js'; - import { parseDate } from '../../src/date/parseDate.js'; +import { localizeTearDown } from '../../test-helpers.js'; +import { localize } from '../../src/localize.js'; /** * @@ -27,13 +27,16 @@ describe('parseDate()', () => { expect(equalsDate(parseDate('1-1-1979'), new Date('1979/01/01'))).to.equal(true); expect(equalsDate(parseDate('1-11-1979'), new Date('1979/11/01'))).to.equal(true); }); + it('creates a date object', () => { expect(parseDate('10/10/2000') instanceof Date).to.equal(true); }); + it('returns a date object', () => { expect(equalsDate(parseDate('1-1-1979'), new Date('1979/01/01'))).to.equal(true); expect(equalsDate(parseDate('31.12.1970'), new Date('1970/12/31'))).to.equal(true); }); + it('handles all kind of delimiters', () => { expect(equalsDate(parseDate('12-12-1976'), new Date('1976/12/12'))).to.equal(true); expect(equalsDate(parseDate('13 12 1976'), new Date('1976/12/13'))).to.equal(true); @@ -41,7 +44,24 @@ describe('parseDate()', () => { expect(equalsDate(parseDate('14. 12. 1976.'), new Date('1976/12/14'))).to.equal(true); expect(equalsDate(parseDate('14.12.1976 r.'), new Date('1976/12/14'))).to.equal(true); }); + + it('handles different locales', () => { + localize.locale = 'en-GB'; + expect(equalsDate(parseDate('31-12-1976'), new Date('1976/12/31'))).to.equal(true); + localize.locale = 'en-US'; + expect(equalsDate(parseDate('12-31-1976'), new Date('1976/12/31'))).to.equal(true); + }); + it('return undefined when no valid date provided', () => { - expect(parseDate('12.12.1976.,')).to.equal(undefined); + expect(parseDate('12.12.1976.,')).to.equal(undefined); // wrong delimiter + expect(parseDate('foo')).to.equal(undefined); // no date + + localize.locale = 'en-GB'; + expect(parseDate('31.02.2020')).to.equal(undefined); // non existing date + expect(parseDate('12.31.2020')).to.equal(undefined); // day & month switched places + + localize.locale = 'en-US'; + expect(parseDate('02.31.2020')).to.equal(undefined); // non existing date + expect(parseDate('31.12.2020')).to.equal(undefined); // day & month switched places }); });