From ff8f42755370b4bb69c907e7b47c6c70db0f4a9f Mon Sep 17 00:00:00 2001 From: Mikhail Bashkirov Date: Fri, 26 Apr 2019 17:16:18 +0200 Subject: [PATCH 1/2] chore: run deep test files --- karma.conf.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/karma.conf.js b/karma.conf.js index 477e5b214..a0d44e413 100644 --- a/karma.conf.js +++ b/karma.conf.js @@ -11,7 +11,7 @@ module.exports = config => { // // npm run test -- --grep test/foo/bar.test.js // npm run test -- --grep test/bar/* - config.grep ? config.grep : 'packages/*/test/*.test.js', + config.grep ? config.grep : 'packages/*/test/**/*.test.js', ], // TODO: improve coverage From aafe3d9428266cb5dbba969e23ccd40c6753d4c9 Mon Sep 17 00:00:00 2001 From: Mikhail Bashkirov Date: Fri, 26 Apr 2019 17:17:12 +0200 Subject: [PATCH 2/2] chore(localize): split date related functions into their own files --- packages/localize/index.js | 4 +- packages/localize/src/date/addLeadingZero.js | 20 ++ packages/localize/src/date/clean.js | 13 + packages/localize/src/date/formatDate.js | 40 +++ .../src/date/getDateFormatBasedOnLocale.js | 33 +++ packages/localize/src/date/getLocale.js | 17 ++ packages/localize/src/date/normalizeDate.js | 22 ++ packages/localize/src/date/pad.js | 11 + packages/localize/src/date/parseDate.js | 58 +++++ .../src/date/sanitizedDateTimeFormat.js | 13 + packages/localize/src/date/splitDate.js | 9 + packages/localize/src/date/trim.js | 9 + packages/localize/src/formatDate.js | 236 ------------------ packages/localize/stories/index.stories.js | 4 +- .../test/{ => date}/formatDate.test.js | 61 +---- .../date/getDateFormatBasedOnLocale.test.js | 18 ++ packages/localize/test/date/parseDate.test.js | 36 +++ 17 files changed, 313 insertions(+), 291 deletions(-) create mode 100644 packages/localize/src/date/addLeadingZero.js create mode 100644 packages/localize/src/date/clean.js create mode 100644 packages/localize/src/date/formatDate.js create mode 100644 packages/localize/src/date/getDateFormatBasedOnLocale.js create mode 100644 packages/localize/src/date/getLocale.js create mode 100644 packages/localize/src/date/normalizeDate.js create mode 100644 packages/localize/src/date/pad.js create mode 100644 packages/localize/src/date/parseDate.js create mode 100644 packages/localize/src/date/sanitizedDateTimeFormat.js create mode 100644 packages/localize/src/date/splitDate.js create mode 100644 packages/localize/src/date/trim.js delete mode 100644 packages/localize/src/formatDate.js rename packages/localize/test/{ => date}/formatDate.test.js (58%) create mode 100644 packages/localize/test/date/getDateFormatBasedOnLocale.test.js create mode 100644 packages/localize/test/date/parseDate.test.js diff --git a/packages/localize/index.js b/packages/localize/index.js index f139078e0..efb5d6cb3 100644 --- a/packages/localize/index.js +++ b/packages/localize/index.js @@ -1,4 +1,6 @@ -export { formatDate, getDateFormatBasedOnLocale, parseDate } from './src/formatDate.js'; +export { formatDate } from './src/date/formatDate.js'; +export { getDateFormatBasedOnLocale } from './src/date/getDateFormatBasedOnLocale.js'; +export { parseDate } from './src/date/parseDate.js'; export { formatNumber, formatNumberToParts, diff --git a/packages/localize/src/date/addLeadingZero.js b/packages/localize/src/date/addLeadingZero.js new file mode 100644 index 000000000..62e3f49fb --- /dev/null +++ b/packages/localize/src/date/addLeadingZero.js @@ -0,0 +1,20 @@ +import { splitDate } from './splitDate.js'; +import { pad } from './pad.js'; + +/** + * To add a leading zero to a single number + * + * @param dateString + * @returns {*} + */ +export function addLeadingZero(dateString) { + const dateParts = splitDate(dateString); + const delimiter = dateParts ? dateParts[2] : ''; + const uniformDateString = dateString.replace(/[.\-/\s]/g, delimiter); + const dateArray = uniformDateString.split && uniformDateString.split(delimiter); + if (!dateArray || dateArray.length !== 3) { + // prevent fail on invalid dates + return ''; + } + return dateArray.map(pad).join('-'); +} diff --git a/packages/localize/src/date/clean.js b/packages/localize/src/date/clean.js new file mode 100644 index 000000000..fe817867a --- /dev/null +++ b/packages/localize/src/date/clean.js @@ -0,0 +1,13 @@ +import { trim } from './trim.js'; + +/** + * To clean date from added characters from IE + * + * @param dateAsString + * @returns {string|XML} + */ +export function clean(dateAsString) { + // list of separators is from wikipedia https://www.wikiwand.com/en/Date_format_by_country + // slash, point, dash or space + return trim(dateAsString.replace(/[^\d-. /]/g, '')); +} diff --git a/packages/localize/src/date/formatDate.js b/packages/localize/src/date/formatDate.js new file mode 100644 index 000000000..c544ab8ec --- /dev/null +++ b/packages/localize/src/date/formatDate.js @@ -0,0 +1,40 @@ +import { getLocale } from './getLocale.js'; +import { normalizeDate } from './normalizeDate.js'; + +/** + * Formats date based on locale and options + * + * @param date + * @param options + * @returns {*} + */ +export function formatDate(date, options) { + if (!(date instanceof Date)) { + return '0000-00-00'; + } + const formatOptions = options || {}; + // make sure months and days are always 2-digits + if (!options) { + formatOptions.year = 'numeric'; + formatOptions.month = '2-digit'; + formatOptions.day = '2-digit'; + } + if (options && !(options && options.year)) { + formatOptions.year = 'numeric'; + } + if (options && !(options && options.month)) { + formatOptions.month = '2-digit'; + } + if (options && !(options && options.day)) { + formatOptions.day = '2-digit'; + } + + const computedLocale = getLocale(formatOptions && formatOptions.locale); + let formattedDate = ''; + try { + formattedDate = new Intl.DateTimeFormat(computedLocale, formatOptions).format(date); + } catch (e) { + formattedDate = ''; + } + return normalizeDate(formattedDate); +} diff --git a/packages/localize/src/date/getDateFormatBasedOnLocale.js b/packages/localize/src/date/getDateFormatBasedOnLocale.js new file mode 100644 index 000000000..a4e44cdd1 --- /dev/null +++ b/packages/localize/src/date/getDateFormatBasedOnLocale.js @@ -0,0 +1,33 @@ +import { sanitizedDateTimeFormat } from './sanitizedDateTimeFormat.js'; +import { splitDate } from './splitDate.js'; + +/** + * To compute the localized date format + * + * @returns {string} + */ +export function getDateFormatBasedOnLocale() { + function computePositions(dateParts) { + function getPartByIndex(index) { + return { 2012: 'year', 12: 'month', 20: 'day' }[dateParts[index]]; + } + + return [1, 3, 5].map(getPartByIndex); + } + + // Arbitrary date with different values for year,month,day + const date = new Date(); + date.setDate(20); + date.setMonth(11); + date.setFullYear(2012); + + // Strange characters added by IE11 need to be taken into account here + const formattedDate = sanitizedDateTimeFormat(date); + + // For Dutch locale, dateParts would match: [ 1:'20', 2:'-', 3:'12', 4:'-', 5:'2012' ] + const dateParts = splitDate(formattedDate); + + const dateFormat = {}; + dateFormat.positions = computePositions(dateParts); + return `${dateFormat.positions[0]}-${dateFormat.positions[1]}-${dateFormat.positions[2]}`; +} diff --git a/packages/localize/src/date/getLocale.js b/packages/localize/src/date/getLocale.js new file mode 100644 index 000000000..a8e96b988 --- /dev/null +++ b/packages/localize/src/date/getLocale.js @@ -0,0 +1,17 @@ +import { localize } from '../localize.js'; + +/** + * Gets the locale to use + * + * @param {string} locale Locale to override browser locale + * @returns {string} + */ +export function getLocale(locale) { + if (locale) { + return locale; + } + if (localize && localize.locale) { + return localize.locale; + } + return 'en-GB'; +} diff --git a/packages/localize/src/date/normalizeDate.js b/packages/localize/src/date/normalizeDate.js new file mode 100644 index 000000000..83f6dc0a6 --- /dev/null +++ b/packages/localize/src/date/normalizeDate.js @@ -0,0 +1,22 @@ +/** + * To filter out some added characters in IE + * + * @param str + * @returns {string} + */ +export function normalizeDate(str) { + const dateString = []; + for (let i = 0, n = str.length; i < n; i += 1) { + // remove unicode 160 + if (str.charCodeAt(i) === 160) { + dateString.push(' '); + // remove unicode 8206 + } else if (str.charCodeAt(i) === 8206) { + dateString.push(''); + } else { + dateString.push(str.charAt(i)); + } + } + + return dateString.join(''); +} diff --git a/packages/localize/src/date/pad.js b/packages/localize/src/date/pad.js new file mode 100644 index 000000000..7165f9d9c --- /dev/null +++ b/packages/localize/src/date/pad.js @@ -0,0 +1,11 @@ +/** + * To get the absolute value of a number. + * + * @param n + * @returns {string} + */ +export function pad(n) { + const v = Math.abs(n); + + return String(v < 10 ? `0${v}` : v); +} diff --git a/packages/localize/src/date/parseDate.js b/packages/localize/src/date/parseDate.js new file mode 100644 index 000000000..2b1039698 --- /dev/null +++ b/packages/localize/src/date/parseDate.js @@ -0,0 +1,58 @@ +import { localize } from '../localize.js'; +import { getDateFormatBasedOnLocale } from './getDateFormatBasedOnLocale.js'; +import { addLeadingZero } from './addLeadingZero.js'; + +const memoize = (fn, parm) => { + const cache = {}; + return () => { + const n = parm; + if (n in cache) { + return cache[n]; + } + const result = fn(n); + cache[n] = result; + return result; + }; +}; + +const memoizedGetDateFormatBasedOnLocale = memoize(getDateFormatBasedOnLocale, localize.locale); + +/** + * To parse a date into the right format + * + * @param date + * @returns {Date} + */ +export function parseDate(date) { + const stringToParse = addLeadingZero(date); + let parsedString; + switch (memoizedGetDateFormatBasedOnLocale()) { + case 'day-month-year': + parsedString = `${stringToParse.slice(6, 10)}/${stringToParse.slice( + 3, + 5, + )}/${stringToParse.slice(0, 2)}`; + break; + case 'month-day-year': + parsedString = `${stringToParse.slice(6, 10)}/${stringToParse.slice( + 0, + 2, + )}/${stringToParse.slice(3, 5)}`; + break; + case 'year-month-day': + parsedString = `${stringToParse.slice(0, 4)}/${stringToParse.slice( + 5, + 7, + )}/${stringToParse.slice(8, 10)}`; + break; + default: + parsedString = '0000/00/00'; + } + const parsedDate = new Date(parsedString); + // Check if parsedDate is not `Invalid Date` + // eslint-disable-next-line no-restricted-globals + if (!isNaN(parsedDate)) { + return parsedDate; + } + return undefined; +} diff --git a/packages/localize/src/date/sanitizedDateTimeFormat.js b/packages/localize/src/date/sanitizedDateTimeFormat.js new file mode 100644 index 000000000..4c734e10a --- /dev/null +++ b/packages/localize/src/date/sanitizedDateTimeFormat.js @@ -0,0 +1,13 @@ +import { formatDate } from './formatDate.js'; +import { clean } from './clean.js'; + +/** + * To sanitize a date from IE11 handling + * + * @param date + * @returns {string|XML} + */ +export function sanitizedDateTimeFormat(date) { + const fDate = formatDate(date); + return clean(fDate); +} diff --git a/packages/localize/src/date/splitDate.js b/packages/localize/src/date/splitDate.js new file mode 100644 index 000000000..6c09d9429 --- /dev/null +++ b/packages/localize/src/date/splitDate.js @@ -0,0 +1,9 @@ +/** + * To split a date into days, months, years, etc + * + * @param date + * @returns {Array|{index: number, input: string}|*} + */ +export function splitDate(date) { + return date.match(/(\d{1,4})([^\d]+)(\d{1,4})([^\d]+)(\d{1,4})/); +} diff --git a/packages/localize/src/date/trim.js b/packages/localize/src/date/trim.js new file mode 100644 index 000000000..b6add67b2 --- /dev/null +++ b/packages/localize/src/date/trim.js @@ -0,0 +1,9 @@ +/** + * To trim the date + * + * @param dateAsString + * @returns {string|XML} + */ +export function trim(dateAsString) { + return dateAsString.replace(/^[^\d]*/g, '').replace(/[^\d]*$/g, ''); +} diff --git a/packages/localize/src/formatDate.js b/packages/localize/src/formatDate.js deleted file mode 100644 index ddfbbbaa2..000000000 --- a/packages/localize/src/formatDate.js +++ /dev/null @@ -1,236 +0,0 @@ -import { localize } from './localize.js'; - -/** - * Gets the locale to use - * - * @param {string} locale Locale to override browser locale - * @returns {string} - */ -function getLocale(locale) { - if (locale) { - return locale; - } - if (localize && localize.locale) { - return localize.locale; - } - return 'en-GB'; -} - -/** - * To filter out some added characters in IE - * - * @param str - * @returns {string} - */ -function normalizeDate(str) { - const dateString = []; - for (let i = 0, n = str.length; i < n; i += 1) { - // remove unicode 160 - if (str.charCodeAt(i) === 160) { - dateString.push(' '); - // remove unicode 8206 - } else if (str.charCodeAt(i) === 8206) { - dateString.push(''); - } else { - dateString.push(str.charAt(i)); - } - } - - return dateString.join(''); -} - -/** - * Formats date based on locale and options - * - * @param date - * @param options - * @returns {*} - */ -export function formatDate(date, options) { - if (!(date instanceof Date)) { - return '0000-00-00'; - } - const formatOptions = options || {}; - // make sure months and days are always 2-digits - if (!options) { - formatOptions.year = 'numeric'; - formatOptions.month = '2-digit'; - formatOptions.day = '2-digit'; - } - if (options && !(options && options.year)) { - formatOptions.year = 'numeric'; - } - if (options && !(options && options.month)) { - formatOptions.month = '2-digit'; - } - if (options && !(options && options.day)) { - formatOptions.day = '2-digit'; - } - - const computedLocale = getLocale(formatOptions && formatOptions.locale); - let formattedDate = ''; - try { - formattedDate = new Intl.DateTimeFormat(computedLocale, formatOptions).format(date); - } catch (e) { - formattedDate = ''; - } - return normalizeDate(formattedDate); -} -/** - * To trim the date - * - * @param dateAsString - * @returns {string|XML} - */ -function trim(dateAsString) { - return dateAsString.replace(/^[^\d]*/g, '').replace(/[^\d]*$/g, ''); -} - -/** - * To clean date from added characters from IE - * - * @param dateAsString - * @returns {string|XML} - */ -function clean(dateAsString) { - // list of separators is from wikipedia https://www.wikiwand.com/en/Date_format_by_country - // slash, point, dash or space - return trim(dateAsString.replace(/[^\d-. /]/g, '')); -} - -/** - * To get the absolute value of a number. - * - * @param n - * @returns {string} - */ -function pad(n) { - const v = Math.abs(n); - - return String(v < 10 ? `0${v}` : v); -} - -/** - * To sanitize a date from IE11 handling - * - * @param date - * @returns {string|XML} - */ -function sanitizedDateTimeFormat(date) { - const fDate = formatDate(date); - return clean(fDate); -} - -/** - * To split a date into days, months, years, etc - * - * @param date - * @returns {Array|{index: number, input: string}|*} - */ -function splitDate(date) { - return date.match(/(\d{1,4})([^\d]+)(\d{1,4})([^\d]+)(\d{1,4})/); -} - -/** - * To add a leading zero to a single number - * - * @param dateString - * @returns {*} - */ -function addLeadingZero(dateString) { - const dateParts = splitDate(dateString); - const delimiter = dateParts ? dateParts[2] : ''; - const uniformDateString = dateString.replace(/[.\-/\s]/g, delimiter); - const dateArray = uniformDateString.split && uniformDateString.split(delimiter); - if (!dateArray || dateArray.length !== 3) { - // prevent fail on invalid dates - return ''; - } - return dateArray.map(pad).join('-'); -} - -/** - * To compute the localized date format - * - * @returns {string} - */ -export function getDateFormatBasedOnLocale() { - function computePositions(dateParts) { - function getPartByIndex(index) { - return { 2012: 'year', 12: 'month', 20: 'day' }[dateParts[index]]; - } - - return [1, 3, 5].map(getPartByIndex); - } - - // Arbitrary date with different values for year,month,day - const date = new Date(); - date.setDate(20); - date.setMonth(11); - date.setFullYear(2012); - - // Strange characters added by IE11 need to be taken into account here - const formattedDate = sanitizedDateTimeFormat(date); - - // For Dutch locale, dateParts would match: [ 1:'20', 2:'-', 3:'12', 4:'-', 5:'2012' ] - const dateParts = splitDate(formattedDate); - - const dateFormat = {}; - dateFormat.positions = computePositions(dateParts); - return `${dateFormat.positions[0]}-${dateFormat.positions[1]}-${dateFormat.positions[2]}`; -} - -const memoize = (fn, parm) => { - const cache = {}; - return () => { - const n = parm; - if (n in cache) { - return cache[n]; - } - const result = fn(n); - cache[n] = result; - return result; - }; -}; - -const memoizedGetDateFormatBasedOnLocale = memoize(getDateFormatBasedOnLocale, localize.locale); - -/** - * To parse a date into the right format - * - * @param date - * @returns {Date} - */ -export function parseDate(date) { - const stringToParse = addLeadingZero(date); - let parsedString; - switch (memoizedGetDateFormatBasedOnLocale()) { - case 'day-month-year': - parsedString = `${stringToParse.slice(6, 10)}/${stringToParse.slice( - 3, - 5, - )}/${stringToParse.slice(0, 2)}`; - break; - case 'month-day-year': - parsedString = `${stringToParse.slice(6, 10)}/${stringToParse.slice( - 0, - 2, - )}/${stringToParse.slice(3, 5)}`; - break; - case 'year-month-day': - parsedString = `${stringToParse.slice(0, 4)}/${stringToParse.slice( - 5, - 7, - )}/${stringToParse.slice(8, 10)}`; - break; - default: - parsedString = '0000/00/00'; - } - const parsedDate = new Date(parsedString); - // Check if parsedDate is not `Invalid Date` - // eslint-disable-next-line no-restricted-globals - if (!isNaN(parsedDate)) { - return parsedDate; - } - return undefined; -} diff --git a/packages/localize/stories/index.stories.js b/packages/localize/stories/index.stories.js index d82183959..7f5deae44 100644 --- a/packages/localize/stories/index.stories.js +++ b/packages/localize/stories/index.stories.js @@ -10,7 +10,9 @@ import { getGroupSeparator, getDecimalSeparator, } from '../src/formatNumber.js'; -import { formatDate, parseDate, getDateFormatBasedOnLocale } from '../src/formatDate.js'; +import { formatDate } from '../src/date/formatDate.js'; +import { parseDate } from '../src/date/parseDate.js'; +import { getDateFormatBasedOnLocale } from '../src/date/getDateFormatBasedOnLocale.js'; storiesOf('Localize System|Localize', module).add('lit component', () => { class LitHtmlExample extends LocalizeMixin(LionLitElement) { diff --git a/packages/localize/test/formatDate.test.js b/packages/localize/test/date/formatDate.test.js similarity index 58% rename from packages/localize/test/formatDate.test.js rename to packages/localize/test/date/formatDate.test.js index f87662acd..4efd8a8d1 100644 --- a/packages/localize/test/formatDate.test.js +++ b/packages/localize/test/date/formatDate.test.js @@ -1,14 +1,15 @@ import { expect } from '@open-wc/testing'; -import { localize } from '../src/localize.js'; -import { localizeTearDown } from '../test-helpers.js'; +import { localize } from '../../src/localize.js'; +import { localizeTearDown } from '../../test-helpers.js'; -import { formatDate, parseDate, getDateFormatBasedOnLocale } from '../src/formatDate.js'; - -beforeEach(() => { - localizeTearDown(); -}); +import { formatDate } from '../../src/date/formatDate.js'; +import { parseDate } from '../../src/date/parseDate.js'; describe('formatDate', () => { + beforeEach(() => { + localizeTearDown(); + }); + it('displays the appropriate date based on locale', async () => { const testDate = new Date('2012/05/21'); @@ -112,49 +113,3 @@ describe('formatDate', () => { expect(formatDate(date)).to.equal('0000-00-00'); }); }); - -describe('parseDate()', () => { - function equalsDate(value, date) { - return ( - Object.prototype.toString.call(value) === '[object Date]' && // is Date Object - value.getDate() === date.getDate() && // day - value.getMonth() === date.getMonth() && // month - value.getFullYear() === date.getFullYear() // year - ); - } - - afterEach(() => { - // makes sure that between tests the localization is reset to default state - document.documentElement.lang = 'en-GB'; - }); - it('adds leading zeros', () => { - 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); - expect(equalsDate(parseDate('14.12.1976'), new Date('1976/12/14'))).to.equal(true); - expect(equalsDate(parseDate('14.12-1976'), new Date('1976/12/14'))).to.equal(true); - expect(equalsDate(parseDate('14-12/1976'), new Date('1976/12/14'))).to.equal(true); - }); - it('return undefined when no valid date provided', () => { - expect(parseDate('12.12.1976.,')).to.equal(undefined); - }); -}); - -describe('getDateFormatBasedOnLocale()', () => { - it('returns the positions of day, month and year', async () => { - localize.locale = 'en-GB'; - expect(getDateFormatBasedOnLocale()).to.equal('day-month-year'); - localize.locale = 'en-US'; - expect(getDateFormatBasedOnLocale()).to.equal('month-day-year'); - }); -}); diff --git a/packages/localize/test/date/getDateFormatBasedOnLocale.test.js b/packages/localize/test/date/getDateFormatBasedOnLocale.test.js new file mode 100644 index 000000000..9a227ae9b --- /dev/null +++ b/packages/localize/test/date/getDateFormatBasedOnLocale.test.js @@ -0,0 +1,18 @@ +import { expect } from '@open-wc/testing'; +import { localize } from '../../src/localize.js'; +import { localizeTearDown } from '../../test-helpers.js'; + +import { getDateFormatBasedOnLocale } from '../../src/date/getDateFormatBasedOnLocale.js'; + +describe('getDateFormatBasedOnLocale()', () => { + beforeEach(() => { + localizeTearDown(); + }); + + it('returns the positions of day, month and year', async () => { + localize.locale = 'en-GB'; + expect(getDateFormatBasedOnLocale()).to.equal('day-month-year'); + localize.locale = 'en-US'; + expect(getDateFormatBasedOnLocale()).to.equal('month-day-year'); + }); +}); diff --git a/packages/localize/test/date/parseDate.test.js b/packages/localize/test/date/parseDate.test.js new file mode 100644 index 000000000..cc273c1ad --- /dev/null +++ b/packages/localize/test/date/parseDate.test.js @@ -0,0 +1,36 @@ +import { expect } from '@open-wc/testing'; + +import { parseDate } from '../../src/date/parseDate.js'; + +function equalsDate(value, date) { + return ( + Object.prototype.toString.call(value) === '[object Date]' && // is Date Object + value.getDate() === date.getDate() && // day + value.getMonth() === date.getMonth() && // month + value.getFullYear() === date.getFullYear() // year + ); +} + +describe('parseDate()', () => { + it('adds leading zeros', () => { + 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); + expect(equalsDate(parseDate('14.12.1976'), new Date('1976/12/14'))).to.equal(true); + expect(equalsDate(parseDate('14.12-1976'), new Date('1976/12/14'))).to.equal(true); + expect(equalsDate(parseDate('14-12/1976'), new Date('1976/12/14'))).to.equal(true); + }); + it('return undefined when no valid date provided', () => { + expect(parseDate('12.12.1976.,')).to.equal(undefined); + }); +});