Merge pull request #303 from ing-bank/fix/normalize-date-in-validators

Fix/normalize date in validators
This commit is contained in:
Erik Kroes 2019-10-07 14:00:39 +02:00 committed by GitHub
commit e60b819188
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 32 additions and 14 deletions

View file

@ -1,5 +1,11 @@
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 { createMultipleMonth } from './utils/createMultipleMonth.js';
import { dayTemplate } from './utils/dayTemplate.js';
@ -9,7 +15,6 @@ import { getLastDayPreviousMonth } from './utils/getLastDayPreviousMonth.js';
import { isSameDate } from './utils/isSameDate.js';
import { calendarStyle } from './calendarStyle.js';
import { createDay } from './utils/createDay.js';
import { normalizeDateTime } from './utils/normalizeDateTime.js';
/**
* @customElement

View file

@ -2,6 +2,7 @@ export { formatDate } from './src/date/formatDate.js';
export { getDateFormatBasedOnLocale } from './src/date/getDateFormatBasedOnLocale.js';
export { getMonthNames } from './src/date/getMonthNames.js';
export { getWeekdayNames } from './src/date/getWeekdayNames.js';
export { normalizeDateTime } from './src/date/normalizeDateTime.js';
export { parseDate } from './src/date/parseDate.js';
export { formatNumber } from './src/number/formatNumber.js';
export { formatNumberToParts } from './src/number/formatNumberToParts.js';

View file

@ -1,5 +1,5 @@
import { getLocale } from './getLocale.js';
import { normalizeDate } from './normalizeDate.js';
import { normalizeIntlDate } from './normalizeIntlDate.js';
/**
* Formats date based on locale and options
@ -36,5 +36,5 @@ export function formatDate(date, options) {
} catch (e) {
formattedDate = '';
}
return normalizeDate(formattedDate);
return normalizeIntlDate(formattedDate);
}

View file

@ -1,4 +1,4 @@
import { normalizeDate } from './normalizeDate.js';
import { normalizeIntlDate } from './normalizeIntlDate.js';
const monthsLocaleCache = {};
@ -6,7 +6,7 @@ const monthsLocaleCache = {};
* @desc Returns month names for locale
* @param {string} options.locale locale
* @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' } = {}) {
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) {
const date = new Date(2019, i, 1);
const formattedDate = formatter.format(date);
const normalizedDate = normalizeDate(formattedDate);
const normalizedDate = normalizeIntlDate(formattedDate);
months.push(normalizedDate);
}

View file

@ -1,4 +1,4 @@
import { normalizeDate } from './normalizeDate.js';
import { normalizeIntlDate } from './normalizeIntlDate.js';
const weekdayNamesCache = {};
@ -23,7 +23,7 @@ function getCachedWeekdayNames(locale) {
const date = new Date('2019/04/07'); // start from Sunday
for (let i = 0; i < 7; i += 1) {
const weekday = formatter.format(date);
const normalizedWeekday = normalizeDate(weekday);
const normalizedWeekday = normalizeIntlDate(weekday);
weekdays.push(normalizedWeekday);
date.setDate(date.getDate() + 1);
}

View file

@ -4,7 +4,7 @@
* @param str
* @returns {string}
*/
export function normalizeDate(str) {
export function normalizeIntlDate(str) {
const dateString = [];
for (let i = 0, n = str.length; i < n; i += 1) {
// remove unicode 160

View file

@ -1,5 +1,5 @@
import { expect } from '@open-wc/testing';
import { normalizeDateTime } from '../../src/utils/normalizeDateTime.js';
import { normalizeDateTime } from '../../src/date/normalizeDateTime.js';
describe('normalizeDateTime', () => {
it('returns a date with hours, minutes and seconds set to 0', () => {

View file

@ -1,3 +1,5 @@
import { normalizeDateTime } from '@lion/localize';
export const isString = value => typeof value === 'string';
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());
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) => [
(...params) => ({ minDate: minDate(...params) }),
...factoryParams,
];
export const maxDate = (value, max) => isDate(value) && value <= max;
export const maxDate = (value, max) => isDate(value) && value <= normalizeDateTime(max);
export const maxDateValidator = (...factoryParams) => [
(...params) => ({ maxDate: maxDate(...params) }),
...factoryParams,
];
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) => [
(...params) => ({ minMaxDate: minMaxDate(...params) }),
...factoryParams,

View file

@ -1,4 +1,5 @@
import { expect } from '@open-wc/testing';
import { normalizeDateTime } from '@lion/localize';
import { smokeTestValidator } from '../test-helpers.js';
import {
@ -134,11 +135,17 @@ describe('LionValidate', () => {
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-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', () => {
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;
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', () => {
@ -149,6 +156,9 @@ describe('LionValidate', () => {
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/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', () => {