[APPS-2202] Added custom utils for syncing moment and date-fns formats (#8934)

* [APPS-2202] added custom methods for moment to date-fns migration

* [APPS-2202] addressed review comments
This commit is contained in:
SheenaMalhotra182
2023-09-26 19:34:32 +05:30
committed by GitHub
parent 0933e2dada
commit 83b2d9f8bc
2 changed files with 133 additions and 2 deletions

View File

@@ -0,0 +1,58 @@
/*!
* @license
* Copyright © 2005-2023 Hyland Software, Inc. and its affiliates. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { DateFnsUtils } from './date-fns-utils';
describe('Date Format Translations', () => {
it('should convert moment to date-fns format correctly', () => {
const momentFormat = 'YYYY-MM-DD';
const expectedDateFnsFormat = 'yyyy-MM-dd';
const result = DateFnsUtils.convertMomentToDateFnsFormat(momentFormat);
expect(result).toBe(expectedDateFnsFormat);
});
it('should convert date-fns to moment format correctly', () => {
const dateFnsFormat = 'yyyy-MM-dd';
const expectedMomentFormat = 'YYYY-MM-DD';
const result = DateFnsUtils.convertDateFnsToMomentFormat(dateFnsFormat);
expect(result).toBe(expectedMomentFormat);
});
it('should format a date correctly', () => {
const date = new Date('2023-09-22T12:00:00Z');
const dateFormat = 'yyyy-MM-dd';
const expectedFormattedDate = '2023-09-22';
const result = DateFnsUtils.formatDate(date, dateFormat);
expect(result).toBe(expectedFormattedDate);
});
it('should parse a date correctly', () => {
const dateString = '2023-09-22';
const dateFormat = 'yyyy-MM-dd';
const expectedParsedDate = new Date('2023-09-22T00:00:00Z');
const result = DateFnsUtils.parseDate(dateString, dateFormat);
expect(result).toEqual(expectedParsedDate);
});
});

View File

@@ -15,12 +15,13 @@
* limitations under the License.
*/
import {ar, cs, da, de, enUS, es, fi, fr, it, ja, nb, nl, pl, ptBR, ru, sv, zhCN} from 'date-fns/locale';
import { format, parse } from 'date-fns';
import { ar, cs, da, de, enUS, es, fi, fr, it, ja, nb, nl, pl, ptBR, ru, sv, zhCN } from 'date-fns/locale';
export class DateFnsUtils {
static getLocaleFromString(locale: string): Locale {
let dateFnsLocale: Locale;
switch(locale) {
switch (locale) {
case 'ar':
dateFnsLocale = ar;
break;
@@ -78,4 +79,76 @@ export class DateFnsUtils {
}
return dateFnsLocale;
}
/**
* A mapping of Moment.js format tokens to date-fns format tokens.
*/
static momentToDateFnsMap = {
D: 'd',
Y: 'y',
A: 'a'
};
/**
* A mapping of date-fns format tokens to Moment.js format tokens.
*/
static dateFnsToMomentMap = {
d: 'D',
y: 'Y',
a: 'A'
};
/**
* Converts a Moment.js date format string to the equivalent date-fns format string.
*
* @param dateDisplayFormat - The Moment.js date format string to convert.
* @returns The equivalent date-fns format string.
*/
static convertMomentToDateFnsFormat(dateDisplayFormat: string): string {
if (dateDisplayFormat && dateDisplayFormat.trim() !== '') {
for (const [search, replace] of Object.entries(this.momentToDateFnsMap)) {
dateDisplayFormat = dateDisplayFormat.replace(new RegExp(search, 'g'), replace);
}
return dateDisplayFormat;
}
return '';
}
/**
* Converts a date-fns date format string to the equivalent Moment.js format string.
*
* @param dateDisplayFormat - The date-fns date format string to convert.
* @returns The equivalent Moment.js format string.
*/
static convertDateFnsToMomentFormat(dateDisplayFormat: string): string {
if (dateDisplayFormat && dateDisplayFormat.trim() !== '') {
for (const [search, replace] of Object.entries(this.dateFnsToMomentMap)) {
dateDisplayFormat = dateDisplayFormat.replace(new RegExp(search, 'g'), replace);
}
return dateDisplayFormat;
}
return '';
}
/**
* Formats a date using the specified date format.
*
* @param date - The date to format, can be a number or a Date object.
* @param dateFormat - The date format string to use for formatting.
* @returns The formatted date as a string.
*/
static formatDate(date: number | Date, dateFormat: string): string {
return format(date, this.convertMomentToDateFnsFormat(dateFormat));
}
/**
* Parses a date string using the specified date format.
*
* @param value - The date string to parse.
* @param dateFormat - The date format string to use for parsing.
* @returns The parsed Date object.
*/
static parseDate(value: string, dateFormat: string): Date {
return parse(value, this.convertMomentToDateFnsFormat(dateFormat), new Date());
}
}