Merge pull request #303 from ing-bank/fix/normalize-date-in-validators
Fix/normalize date in validators
This commit is contained in:
commit
e60b819188
10 changed files with 32 additions and 14 deletions
|
|
@ -1,5 +1,11 @@
|
||||||
import { html, LitElement } from '@lion/core';
|
import { html, LitElement } from '@lion/core';
|
||||||
import { localize, getWeekdayNames, getMonthNames, LocalizeMixin } from '@lion/localize';
|
import {
|
||||||
|
localize,
|
||||||
|
getWeekdayNames,
|
||||||
|
getMonthNames,
|
||||||
|
normalizeDateTime,
|
||||||
|
LocalizeMixin,
|
||||||
|
} from '@lion/localize';
|
||||||
import '@lion/core/src/differentKeyEventNamesShimIE.js';
|
import '@lion/core/src/differentKeyEventNamesShimIE.js';
|
||||||
import { createMultipleMonth } from './utils/createMultipleMonth.js';
|
import { createMultipleMonth } from './utils/createMultipleMonth.js';
|
||||||
import { dayTemplate } from './utils/dayTemplate.js';
|
import { dayTemplate } from './utils/dayTemplate.js';
|
||||||
|
|
@ -9,7 +15,6 @@ import { getLastDayPreviousMonth } from './utils/getLastDayPreviousMonth.js';
|
||||||
import { isSameDate } from './utils/isSameDate.js';
|
import { isSameDate } from './utils/isSameDate.js';
|
||||||
import { calendarStyle } from './calendarStyle.js';
|
import { calendarStyle } from './calendarStyle.js';
|
||||||
import { createDay } from './utils/createDay.js';
|
import { createDay } from './utils/createDay.js';
|
||||||
import { normalizeDateTime } from './utils/normalizeDateTime.js';
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @customElement
|
* @customElement
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@ export { formatDate } from './src/date/formatDate.js';
|
||||||
export { getDateFormatBasedOnLocale } from './src/date/getDateFormatBasedOnLocale.js';
|
export { getDateFormatBasedOnLocale } from './src/date/getDateFormatBasedOnLocale.js';
|
||||||
export { getMonthNames } from './src/date/getMonthNames.js';
|
export { getMonthNames } from './src/date/getMonthNames.js';
|
||||||
export { getWeekdayNames } from './src/date/getWeekdayNames.js';
|
export { getWeekdayNames } from './src/date/getWeekdayNames.js';
|
||||||
|
export { normalizeDateTime } from './src/date/normalizeDateTime.js';
|
||||||
export { parseDate } from './src/date/parseDate.js';
|
export { parseDate } from './src/date/parseDate.js';
|
||||||
export { formatNumber } from './src/number/formatNumber.js';
|
export { formatNumber } from './src/number/formatNumber.js';
|
||||||
export { formatNumberToParts } from './src/number/formatNumberToParts.js';
|
export { formatNumberToParts } from './src/number/formatNumberToParts.js';
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
import { getLocale } from './getLocale.js';
|
import { getLocale } from './getLocale.js';
|
||||||
import { normalizeDate } from './normalizeDate.js';
|
import { normalizeIntlDate } from './normalizeIntlDate.js';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Formats date based on locale and options
|
* Formats date based on locale and options
|
||||||
|
|
@ -36,5 +36,5 @@ export function formatDate(date, options) {
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
formattedDate = '';
|
formattedDate = '';
|
||||||
}
|
}
|
||||||
return normalizeDate(formattedDate);
|
return normalizeIntlDate(formattedDate);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
import { normalizeDate } from './normalizeDate.js';
|
import { normalizeIntlDate } from './normalizeIntlDate.js';
|
||||||
|
|
||||||
const monthsLocaleCache = {};
|
const monthsLocaleCache = {};
|
||||||
|
|
||||||
|
|
@ -6,7 +6,7 @@ const monthsLocaleCache = {};
|
||||||
* @desc Returns month names for locale
|
* @desc Returns month names for locale
|
||||||
* @param {string} options.locale locale
|
* @param {string} options.locale locale
|
||||||
* @param {string} [options.style=long] long, short or narrow
|
* @param {string} [options.style=long] long, short or narrow
|
||||||
* @returns {Array} like: ['Januray', 'February', ...etc].
|
* @returns {Array} like: ['January', 'February', ...etc].
|
||||||
*/
|
*/
|
||||||
export function getMonthNames({ locale, style = 'long' } = {}) {
|
export function getMonthNames({ locale, style = 'long' } = {}) {
|
||||||
let months = monthsLocaleCache[locale] && monthsLocaleCache[locale][style];
|
let months = monthsLocaleCache[locale] && monthsLocaleCache[locale][style];
|
||||||
|
|
@ -21,7 +21,7 @@ export function getMonthNames({ locale, style = 'long' } = {}) {
|
||||||
for (let i = 0; i < 12; i += 1) {
|
for (let i = 0; i < 12; i += 1) {
|
||||||
const date = new Date(2019, i, 1);
|
const date = new Date(2019, i, 1);
|
||||||
const formattedDate = formatter.format(date);
|
const formattedDate = formatter.format(date);
|
||||||
const normalizedDate = normalizeDate(formattedDate);
|
const normalizedDate = normalizeIntlDate(formattedDate);
|
||||||
months.push(normalizedDate);
|
months.push(normalizedDate);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
import { normalizeDate } from './normalizeDate.js';
|
import { normalizeIntlDate } from './normalizeIntlDate.js';
|
||||||
|
|
||||||
const weekdayNamesCache = {};
|
const weekdayNamesCache = {};
|
||||||
|
|
||||||
|
|
@ -23,7 +23,7 @@ function getCachedWeekdayNames(locale) {
|
||||||
const date = new Date('2019/04/07'); // start from Sunday
|
const date = new Date('2019/04/07'); // start from Sunday
|
||||||
for (let i = 0; i < 7; i += 1) {
|
for (let i = 0; i < 7; i += 1) {
|
||||||
const weekday = formatter.format(date);
|
const weekday = formatter.format(date);
|
||||||
const normalizedWeekday = normalizeDate(weekday);
|
const normalizedWeekday = normalizeIntlDate(weekday);
|
||||||
weekdays.push(normalizedWeekday);
|
weekdays.push(normalizedWeekday);
|
||||||
date.setDate(date.getDate() + 1);
|
date.setDate(date.getDate() + 1);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,7 @@
|
||||||
* @param str
|
* @param str
|
||||||
* @returns {string}
|
* @returns {string}
|
||||||
*/
|
*/
|
||||||
export function normalizeDate(str) {
|
export function normalizeIntlDate(str) {
|
||||||
const dateString = [];
|
const dateString = [];
|
||||||
for (let i = 0, n = str.length; i < n; i += 1) {
|
for (let i = 0, n = str.length; i < n; i += 1) {
|
||||||
// remove unicode 160
|
// remove unicode 160
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
import { expect } from '@open-wc/testing';
|
import { expect } from '@open-wc/testing';
|
||||||
import { normalizeDateTime } from '../../src/utils/normalizeDateTime.js';
|
import { normalizeDateTime } from '../../src/date/normalizeDateTime.js';
|
||||||
|
|
||||||
describe('normalizeDateTime', () => {
|
describe('normalizeDateTime', () => {
|
||||||
it('returns a date with hours, minutes and seconds set to 0', () => {
|
it('returns a date with hours, minutes and seconds set to 0', () => {
|
||||||
|
|
@ -1,3 +1,5 @@
|
||||||
|
import { normalizeDateTime } from '@lion/localize';
|
||||||
|
|
||||||
export const isString = value => typeof value === 'string';
|
export const isString = value => typeof value === 'string';
|
||||||
export const isStringValidator = () => [(...params) => ({ isString: isString(...params) })];
|
export const isStringValidator = () => [(...params) => ({ isString: isString(...params) })];
|
||||||
|
|
||||||
|
|
@ -61,20 +63,20 @@ export const isDate = value =>
|
||||||
Object.prototype.toString.call(value) === '[object Date]' && !Number.isNaN(value.getTime());
|
Object.prototype.toString.call(value) === '[object Date]' && !Number.isNaN(value.getTime());
|
||||||
export const isDateValidator = () => [(...params) => ({ isDate: isDate(...params) })];
|
export const isDateValidator = () => [(...params) => ({ isDate: isDate(...params) })];
|
||||||
|
|
||||||
export const minDate = (value, min) => isDate(value) && value >= min;
|
export const minDate = (value, min) => isDate(value) && value >= normalizeDateTime(min);
|
||||||
export const minDateValidator = (...factoryParams) => [
|
export const minDateValidator = (...factoryParams) => [
|
||||||
(...params) => ({ minDate: minDate(...params) }),
|
(...params) => ({ minDate: minDate(...params) }),
|
||||||
...factoryParams,
|
...factoryParams,
|
||||||
];
|
];
|
||||||
|
|
||||||
export const maxDate = (value, max) => isDate(value) && value <= max;
|
export const maxDate = (value, max) => isDate(value) && value <= normalizeDateTime(max);
|
||||||
export const maxDateValidator = (...factoryParams) => [
|
export const maxDateValidator = (...factoryParams) => [
|
||||||
(...params) => ({ maxDate: maxDate(...params) }),
|
(...params) => ({ maxDate: maxDate(...params) }),
|
||||||
...factoryParams,
|
...factoryParams,
|
||||||
];
|
];
|
||||||
|
|
||||||
export const minMaxDate = (value, { min = 0, max = 0 }) =>
|
export const minMaxDate = (value, { min = 0, max = 0 }) =>
|
||||||
isDate(value) && value >= min && value <= max;
|
isDate(value) && value >= normalizeDateTime(min) && value <= normalizeDateTime(max);
|
||||||
export const minMaxDateValidator = (...factoryParams) => [
|
export const minMaxDateValidator = (...factoryParams) => [
|
||||||
(...params) => ({ minMaxDate: minMaxDate(...params) }),
|
(...params) => ({ minMaxDate: minMaxDate(...params) }),
|
||||||
...factoryParams,
|
...factoryParams,
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
import { expect } from '@open-wc/testing';
|
import { expect } from '@open-wc/testing';
|
||||||
|
import { normalizeDateTime } from '@lion/localize';
|
||||||
import { smokeTestValidator } from '../test-helpers.js';
|
import { smokeTestValidator } from '../test-helpers.js';
|
||||||
|
|
||||||
import {
|
import {
|
||||||
|
|
@ -134,11 +135,17 @@ describe('LionValidate', () => {
|
||||||
it('provides minDate() to allow only dates after min', () => {
|
it('provides minDate() to allow only dates after min', () => {
|
||||||
expect(minDate(new Date('2018-02-03'), new Date('2018/02/02'))).to.be.true;
|
expect(minDate(new Date('2018-02-03'), new Date('2018/02/02'))).to.be.true;
|
||||||
expect(minDate(new Date('2018-02-01'), new Date('2018/02/02'))).to.be.false;
|
expect(minDate(new Date('2018-02-01'), new Date('2018/02/02'))).to.be.false;
|
||||||
|
const today = new Date();
|
||||||
|
const todayFormatted = normalizeDateTime(today);
|
||||||
|
expect(minDate(todayFormatted, today)).to.be.true;
|
||||||
});
|
});
|
||||||
|
|
||||||
it('provides maxDate() to allow only dates before max', () => {
|
it('provides maxDate() to allow only dates before max', () => {
|
||||||
expect(maxDate(new Date('2018-02-01'), new Date('2018/02/02'))).to.be.true;
|
expect(maxDate(new Date('2018-02-01'), new Date('2018/02/02'))).to.be.true;
|
||||||
expect(maxDate(new Date('2018-02-03'), new Date('2018/02/02'))).to.be.false;
|
expect(maxDate(new Date('2018-02-03'), new Date('2018/02/02'))).to.be.false;
|
||||||
|
const today = new Date();
|
||||||
|
const todayFormatted = normalizeDateTime(today);
|
||||||
|
expect(maxDate(todayFormatted, today)).to.be.true;
|
||||||
});
|
});
|
||||||
|
|
||||||
it('provides minMaxDate() to allow only dates between min and max', () => {
|
it('provides minMaxDate() to allow only dates between min and max', () => {
|
||||||
|
|
@ -149,6 +156,9 @@ describe('LionValidate', () => {
|
||||||
expect(minMaxDate(new Date('2018/02/03'), minMaxSetting)).to.be.true;
|
expect(minMaxDate(new Date('2018/02/03'), minMaxSetting)).to.be.true;
|
||||||
expect(minMaxDate(new Date('2018/02/01'), minMaxSetting)).to.be.false;
|
expect(minMaxDate(new Date('2018/02/01'), minMaxSetting)).to.be.false;
|
||||||
expect(minMaxDate(new Date('2018/02/05'), minMaxSetting)).to.be.false;
|
expect(minMaxDate(new Date('2018/02/05'), minMaxSetting)).to.be.false;
|
||||||
|
const today = new Date();
|
||||||
|
const todayFormatted = normalizeDateTime(today);
|
||||||
|
expect(minMaxDate(todayFormatted, { min: today, max: today })).to.be.true;
|
||||||
});
|
});
|
||||||
|
|
||||||
it('provides isDateDisabled() to disable dates matching specified condition', () => {
|
it('provides isDateDisabled() to disable dates matching specified condition', () => {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue