makes prettifyDate case insensitive
This commit is contained in:
parent
8bc66b2169
commit
8a688d8407
5 changed files with 79 additions and 34 deletions
16
src/components/Functions/Utils.mdx
Normal file
16
src/components/Functions/Utils.mdx
Normal file
|
|
@ -0,0 +1,16 @@
|
||||||
|
import { Meta, Canvas } from '@storybook/blocks';
|
||||||
|
|
||||||
|
import * as UtilFunctionStories from './Utils.stories.svelte';
|
||||||
|
|
||||||
|
<Meta of={UtilFunctionStories} />
|
||||||
|
|
||||||
|
|
||||||
|
# Utils
|
||||||
|
|
||||||
|
Utils TKTK
|
||||||
|
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
import {prettifyDate} from '@reuters-graphics/graphics-components';
|
||||||
|
|
||||||
|
```
|
||||||
9
src/components/Functions/Utils.stories.svelte
Normal file
9
src/components/Functions/Utils.stories.svelte
Normal file
|
|
@ -0,0 +1,9 @@
|
||||||
|
<script module lang="ts">
|
||||||
|
import { defineMeta } from '@storybook/addon-svelte-csf';
|
||||||
|
|
||||||
|
const { Story } = defineMeta({
|
||||||
|
title: 'Components/Utilities/Functions',
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<Story name="Demo" tags={['!autodocs', '!dev']} />
|
||||||
|
|
@ -2,6 +2,9 @@
|
||||||
export { default as cssVariables } from './actions/cssVariables/index';
|
export { default as cssVariables } from './actions/cssVariables/index';
|
||||||
export { default as resizeObserver } from './actions/resizeObserver/index';
|
export { default as resizeObserver } from './actions/resizeObserver/index';
|
||||||
|
|
||||||
|
// Utils
|
||||||
|
export { prettifyDate } from './utils/index';
|
||||||
|
|
||||||
// Components
|
// Components
|
||||||
export {
|
export {
|
||||||
default as Analytics,
|
default as Analytics,
|
||||||
|
|
|
||||||
|
|
@ -79,7 +79,7 @@ describe('Utils tests', () => {
|
||||||
const formattedMay = 'May';
|
const formattedMay = 'May';
|
||||||
expect(prettifyDate(unformattedMay)).toBe(formattedMay);
|
expect(prettifyDate(unformattedMay)).toBe(formattedMay);
|
||||||
|
|
||||||
const unformattedJune = 'Jun.';
|
const unformattedJune = 'JUN.';
|
||||||
const formattedJune = 'June';
|
const formattedJune = 'June';
|
||||||
expect(prettifyDate(unformattedJune)).toBe(formattedJune);
|
expect(prettifyDate(unformattedJune)).toBe(formattedJune);
|
||||||
|
|
||||||
|
|
@ -87,17 +87,17 @@ describe('Utils tests', () => {
|
||||||
const formattedJuly = 'July';
|
const formattedJuly = 'July';
|
||||||
expect(prettifyDate(unformattedJuly)).toBe(formattedJuly);
|
expect(prettifyDate(unformattedJuly)).toBe(formattedJuly);
|
||||||
|
|
||||||
const unformattedSept = 'Sep.';
|
const unformattedSept = 'sep.';
|
||||||
const formattedSept = 'Sept.';
|
const formattedSept = 'Sept.';
|
||||||
expect(prettifyDate(unformattedSept)).toBe(formattedSept);
|
expect(prettifyDate(unformattedSept)).toBe(formattedSept);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should format months with year properly', () => {
|
it('should format months with year properly', () => {
|
||||||
const unformattedMarch = 'Mar. 2025';
|
const unformattedMarch = 'MAR. 2025';
|
||||||
const formattedMarch = 'March 2025';
|
const formattedMarch = 'March 2025';
|
||||||
expect(prettifyDate(unformattedMarch)).toBe(formattedMarch);
|
expect(prettifyDate(unformattedMarch)).toBe(formattedMarch);
|
||||||
|
|
||||||
const unformattedApril = 'Apr. 2025';
|
const unformattedApril = 'apr. 2025';
|
||||||
const formattedApril = 'April 2025';
|
const formattedApril = 'April 2025';
|
||||||
expect(prettifyDate(unformattedApril)).toBe(formattedApril);
|
expect(prettifyDate(unformattedApril)).toBe(formattedApril);
|
||||||
|
|
||||||
|
|
@ -136,6 +136,22 @@ describe('Utils tests', () => {
|
||||||
expect(prettifyDate(unformattedJune)).toBe(formattedJune);
|
expect(prettifyDate(unformattedJune)).toBe(formattedJune);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should work with lower or upper case', () => {
|
||||||
|
const unformattedMarch = 'MAR. 1, 2023, 10:00pm';
|
||||||
|
const formattedMarch = 'March 1, 2023, 10:00 p.m.';
|
||||||
|
expect(prettifyDate(unformattedMarch)).toBe(formattedMarch);
|
||||||
|
|
||||||
|
const unformattedApril = 'APR. 1, 2023, 10:00AM';
|
||||||
|
const formattedApril = 'April 1, 2023, 10:00 a.m.';
|
||||||
|
expect(prettifyDate(unformattedApril)).toBe(formattedApril);
|
||||||
|
|
||||||
|
const unformattedMay = 'may. 1, 2023, 10:00am';
|
||||||
|
const formattedMay = 'May 1, 2023, 10:00 a.m.';
|
||||||
|
expect(prettifyDate(unformattedMay)).toBe(formattedMay);
|
||||||
|
|
||||||
|
const unformattedJune = 'JUN. 1, 2023, 10:00AM';
|
||||||
|
const formattedJune = 'June 1, 2023, 10:00 a.m.';
|
||||||
|
expect(prettifyDate(unformattedJune)).toBe(formattedJune);
|
||||||
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -34,49 +34,50 @@ export const getAuthorPageUrl = (author: string): string => {
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
export const prettifyDate = (input: string) => {
|
export const prettifyDate = (input: string) => {
|
||||||
// Define a object to map full month names to their Reuters style equivalents
|
// Define an object to map full month names to their Reuters style equivalents
|
||||||
const conversions: { [key: string]: string } = {
|
const conversions: { [key: string]: string } = {
|
||||||
// full months
|
// full months
|
||||||
January: 'Jan.',
|
january: 'Jan.',
|
||||||
February: 'Feb.',
|
february: 'Feb.',
|
||||||
August: 'Aug.',
|
august: 'Aug.',
|
||||||
September: 'Sept.',
|
september: 'Sept.',
|
||||||
October: 'Oct.',
|
october: 'Oct.',
|
||||||
November: 'Nov.',
|
november: 'Nov.',
|
||||||
December: 'Dec.',
|
december: 'Dec.',
|
||||||
|
|
||||||
// 3-letter abbreviations that need fixing
|
// 3-letter abbreviations that need fixing
|
||||||
Jan: 'Jan.',
|
jan: 'Jan.',
|
||||||
Feb: 'Feb.',
|
feb: 'Feb.',
|
||||||
Mar: 'March',
|
mar: 'March',
|
||||||
Apr: 'April',
|
apr: 'April',
|
||||||
Jun: 'June',
|
jun: 'June',
|
||||||
Jul: 'July',
|
jul: 'July',
|
||||||
Sep: 'Sept.',
|
sep: 'Sept.',
|
||||||
};
|
};
|
||||||
|
|
||||||
// If the key in conversions is found in the input, replace it with the corresponding value
|
// If the key in conversions is found in the input (case insensitive), replace it with the corresponding value
|
||||||
let formatted = Object.keys(conversions).reduce((acc, key) => {
|
let formatted = Object.keys(conversions).reduce((acc, key) => {
|
||||||
const regex = new RegExp(`\\b${key}\\b`, 'g');
|
const regex = new RegExp(`\\b${key}\\b`, 'gi'); // Added 'i' flag for case insensitive
|
||||||
return acc.replace(regex, conversions[key]);
|
return acc.replace(regex, conversions[key]);
|
||||||
}, input);
|
}, input);
|
||||||
|
|
||||||
// Fix rogue periods in abbreviations
|
// Fix rogue periods in abbreviations (case insensitive)
|
||||||
let fixedAbbr = formatted.replace('Mar.', 'March')
|
let fixedAbbr = formatted
|
||||||
.replace('March.', 'March')
|
.replace(/\bmar\./gi, 'March')
|
||||||
.replace('Apr.', 'April')
|
.replace(/\bmarch\./gi, 'March')
|
||||||
.replace('April.', 'April')
|
.replace(/\bapr\./gi, 'April')
|
||||||
.replace('May.', 'May')
|
.replace(/\bapril\./gi, 'April')
|
||||||
.replace('June.', 'June')
|
.replace(/\bmay\./gi, 'May')
|
||||||
.replace('July.', 'July')
|
.replace(/\bjune\./gi, 'June')
|
||||||
.replace('Sep.', 'Sept.');
|
.replace(/\bjuly\./gi, 'July')
|
||||||
|
.replace(/\bsep\./gi, 'Sept.');
|
||||||
|
|
||||||
// Replace double periods with a single period
|
// Replace double periods with a single period
|
||||||
let fixedPeriods = fixedAbbr.replace('..', '.');
|
let fixedPeriods = fixedAbbr.replace(/\.{2,}/g, '.');
|
||||||
|
|
||||||
// Fix 'Mar. 1, 2023, 10:00pm' to 'March 1, 2023, 10:00 p.m.', with a space before 'p.m.'
|
// Fix am/pm formatting
|
||||||
return prettifyAmPm(fixedPeriods)
|
return prettifyAmPm(fixedPeriods);
|
||||||
}
|
};
|
||||||
|
|
||||||
const prettifyAmPm = (text: string) => {
|
const prettifyAmPm = (text: string) => {
|
||||||
return text.replace(/(\d)\s*(am|AM|pm|PM)\b/g, (match, digit, timeDesignator) => {
|
return text.replace(/(\d)\s*(am|AM|pm|PM)\b/g, (match, digit, timeDesignator) => {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue