Created abstraction function to parse dates

This commit is contained in:
Alejandro Celaya 2021-06-25 19:52:50 +02:00
parent 99c77622cd
commit 3999d14bab
5 changed files with 22 additions and 20 deletions

View file

@ -1,4 +1,4 @@
import { format, formatISO } from 'date-fns';
import { format, formatISO, parse } from 'date-fns';
import { OptionalString } from '../utils';
type DateOrString = Date | string;
@ -19,3 +19,5 @@ export const formatDate = (format = 'yyyy-MM-dd') => (date?: NullableDate) => fo
export const formatIsoDate = (date?: NullableDate) => formatDateFromFormat(date, undefined);
export const formatInternational = formatDate();
export const parseDate = (date: string, format: string) => parse(date, format, new Date());

View file

@ -1,5 +1,5 @@
import { shallow, ShallowWrapper } from 'enzyme';
import { formatISO, parse } from 'date-fns';
import { formatISO } from 'date-fns';
import { identity } from 'ramda';
import { Mock } from 'ts-mockery';
import { Input } from 'reactstrap';
@ -8,6 +8,7 @@ import DateInput from '../../src/utils/DateInput';
import { ShortUrlData } from '../../src/short-urls/data';
import { ReachableServer, SelectedServer } from '../../src/servers/data';
import { SimpleCard } from '../../src/utils/SimpleCard';
import { parseDate } from '../../src/utils/helpers/date';
describe('<ShortUrlForm />', () => {
let wrapper: ShallowWrapper;
@ -34,8 +35,8 @@ describe('<ShortUrlForm />', () => {
it('saves short URL with data set in form controls', () => {
const wrapper = createWrapper();
const validSince = parse('2017-01-01', 'yyyy-MM-dd', new Date());
const validUntil = parse('2017-01-06', 'yyyy-MM-dd', new Date());
const validSince = parseDate('2017-01-01', 'yyyy-MM-dd');
const validUntil = parseDate('2017-01-06', 'yyyy-MM-dd');
wrapper.find(Input).first().simulate('change', { target: { value: 'https://long-domain.com/foo/bar' } });
wrapper.find('TagsSelector').simulate('change', [ 'tag_foo', 'tag_bar' ]);

View file

@ -2,7 +2,7 @@ import { shallow, ShallowWrapper } from 'enzyme';
import { assoc, toString } from 'ramda';
import { Mock } from 'ts-mockery';
import { ExternalLink } from 'react-external-link';
import { formatISO, parse } from 'date-fns';
import { formatISO } from 'date-fns';
import createShortUrlsRow from '../../../src/short-urls/helpers/ShortUrlsRow';
import Tag from '../../../src/tags/helpers/Tag';
import ColorGenerator from '../../../src/utils/services/ColorGenerator';
@ -11,6 +11,7 @@ import { ShortUrl } from '../../../src/short-urls/data';
import { ReachableServer } from '../../../src/servers/data';
import { CopyToClipboardIcon } from '../../../src/utils/CopyToClipboardIcon';
import { Time } from '../../../src/utils/Time';
import { parseDate } from '../../../src/utils/helpers/date';
describe('<ShortUrlsRow />', () => {
let wrapper: ShallowWrapper;
@ -27,7 +28,7 @@ describe('<ShortUrlsRow />', () => {
shortCode: 'abc123',
shortUrl: 'http://doma.in/abc123',
longUrl: 'http://foo.com/bar',
dateCreated: formatISO(parse('2018-05-23 18:30:41', 'yyyy-MM-dd HH:mm:ss', new Date())),
dateCreated: formatISO(parseDate('2018-05-23 18:30:41', 'yyyy-MM-dd HH:mm:ss')),
tags: [ 'nodejs', 'reactjs' ],
visitsCount: 45,
domain: null,

View file

@ -1,4 +1,4 @@
import { format, parse, subDays } from 'date-fns';
import { format, subDays } from 'date-fns';
import {
DateInterval,
dateRangeIsEmpty,
@ -6,6 +6,7 @@ import {
rangeIsInterval,
rangeOrIntervalToString,
} from '../../../../src/utils/dates/types';
import { parseDate } from '../../../../src/utils/helpers/date';
describe('date-types', () => {
describe('dateRangeIsEmpty', () => {
@ -58,13 +59,10 @@ describe('date-types', () => {
[{ startDate: undefined, endDate: undefined }, undefined ],
[{ startDate: undefined, endDate: null }, undefined ],
[{ startDate: null, endDate: undefined }, undefined ],
[{ startDate: parse('2020-01-01', 'yyyy-MM-dd', new Date()) }, 'Since 2020-01-01' ],
[{ endDate: parse('2020-01-01', 'yyyy-MM-dd', new Date()) }, 'Until 2020-01-01' ],
[{ startDate: parseDate('2020-01-01', 'yyyy-MM-dd') }, 'Since 2020-01-01' ],
[{ endDate: parseDate('2020-01-01', 'yyyy-MM-dd') }, 'Until 2020-01-01' ],
[
{
startDate: parse('2020-01-01', 'yyyy-MM-dd', new Date()),
endDate: parse('2021-02-02', 'yyyy-MM-dd', new Date()),
},
{ startDate: parseDate('2020-01-01', 'yyyy-MM-dd'), endDate: parseDate('2021-02-02', 'yyyy-MM-dd') },
'2020-01-01 - 2021-02-02',
],
])('proper result is returned', (range, expectedValue) => {

View file

@ -1,12 +1,12 @@
import { formatISO, parse } from 'date-fns';
import { formatDate, formatIsoDate } from '../../../src/utils/helpers/date';
import { formatISO } from 'date-fns';
import { formatDate, formatIsoDate, parseDate } from '../../../src/utils/helpers/date';
describe('date', () => {
describe('formatDate', () => {
it.each([
[ parse('2020-03-05 10:00:10', 'yyyy-MM-dd HH:mm:ss', new Date()), 'dd/MM/yyyy', '05/03/2020' ],
[ parse('2020-03-05 10:00:10', 'yyyy-MM-dd HH:mm:ss', new Date()), 'yyyy-MM', '2020-03' ],
[ parse('2020-03-05 10:00:10', 'yyyy-MM-dd HH:mm:ss', new Date()), undefined, '2020-03-05' ],
[ parseDate('2020-03-05 10:00:10', 'yyyy-MM-dd HH:mm:ss'), 'dd/MM/yyyy', '05/03/2020' ],
[ parseDate('2020-03-05 10:00:10', 'yyyy-MM-dd HH:mm:ss'), 'yyyy-MM', '2020-03' ],
[ parseDate('2020-03-05 10:00:10', 'yyyy-MM-dd HH:mm:ss'), undefined, '2020-03-05' ],
[ '2020-03-05 10:00:10', 'dd-MM-yyyy', '2020-03-05 10:00:10' ],
[ '2020-03-05 10:00:10', undefined, '2020-03-05 10:00:10' ],
[ undefined, undefined, undefined ],
@ -19,8 +19,8 @@ describe('date', () => {
describe('formatIsoDate', () => {
it.each([
[
parse('2020-03-05 10:00:10', 'yyyy-MM-dd HH:mm:ss', new Date()),
formatISO(parse('2020-03-05 10:00:10', 'yyyy-MM-dd HH:mm:ss', new Date())),
parseDate('2020-03-05 10:00:10', 'yyyy-MM-dd HH:mm:ss'),
formatISO(parseDate('2020-03-05 10:00:10', 'yyyy-MM-dd HH:mm:ss')),
],
[ '2020-03-05 10:00:10', '2020-03-05 10:00:10' ],
[ 'foo', 'foo' ],