diff --git a/package.json b/package.json index 19e7b1eb..c557f173 100644 --- a/package.json +++ b/package.json @@ -126,4 +126,4 @@ "bugs": { "url": "https://github.com/reuters-graphics/graphics-components/issues" } -} \ No newline at end of file +} diff --git a/src/components/Functions/Utils.mdx b/src/components/Functions/Utils.mdx index af20dd8f..f4e1b54d 100644 --- a/src/components/Functions/Utils.mdx +++ b/src/components/Functions/Utils.mdx @@ -1,18 +1,15 @@ -import { Meta, Canvas } from '@storybook/blocks'; +import { Meta } from '@storybook/blocks'; import * as UtilFunctionStories from './Utils.stories.svelte'; - # Util functions This library provides utility functions that can be used across various components and applications. - ## Prettify date in the Reuters format - The function `prettifyDate` formats the input string, which is expected to be in English, to format the month and time designator (AM/PM) according to the Reuters style guide. The function is case agnostic and will format both full month names and their 3-letter abbreviations (i.e. `Mar` or `Jun`) correctly. ```javascript @@ -23,4 +20,4 @@ prettifyDate('January 1, 2023, 10:00 AM'); // returns 'Jan. 1, 2023, 10:00 a.m.' prettifyDate('Jan 1, 2023, 10:00 PM'); // returns 'Jan. 1, 2023, 10:00 p.m.' prettifyDate('MAR. 2025'); // returns 'March 2025' prettifyDate('sep. 1, 2023, 10:00PM'); // returns 'Sept. 1, 2023, 10:00 p.m.' -``` \ No newline at end of file +``` diff --git a/src/test/utils.test.ts b/src/test/utils.test.ts index e60e0837..c52cb30b 100644 --- a/src/test/utils.test.ts +++ b/src/test/utils.test.ts @@ -4,7 +4,6 @@ import { describe, it, expect } from 'vitest'; process.env.TESTING = 'true'; describe('Utils tests', () => { - it('should format full month correctly', () => { const unformatted = 'January 1, 2023, 10:00 AM'; const formatted = 'Jan. 1, 2023, 10:00 a.m.'; @@ -65,7 +64,6 @@ describe('Utils tests', () => { expect(prettifyDate(unformattedSept)).toBe(formattedSept); }); - it('should format months on their own properly', () => { const unformattedMarch = 'Mar.'; const formattedMarch = 'March'; @@ -157,5 +155,4 @@ describe('Utils tests', () => { const formattedJune = 'June 1, 2023, 10:00 a.m.'; expect(prettifyDate(unformattedJune)).toBe(formattedJune); }); - }); diff --git a/src/utils/index.ts b/src/utils/index.ts index f854de2a..c7207022 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -56,13 +56,13 @@ export const prettifyDate = (input: string) => { }; // 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) => { + const formatted = Object.keys(conversions).reduce((acc, key) => { const regex = new RegExp(`\\b${key}\\b`, 'gi'); // Added 'i' flag for case insensitive return acc.replace(regex, conversions[key]); }, input); // Fix rogue periods in abbreviations (case insensitive) - let fixedAbbr = formatted + const fixedAbbr = formatted .replace(/\bmar\./gi, 'March') .replace(/\bmarch\./gi, 'March') .replace(/\bapr\./gi, 'April') @@ -73,17 +73,19 @@ export const prettifyDate = (input: string) => { .replace(/\bsep\./gi, 'Sept.'); // Replace double periods with a single period - let fixedPeriods = fixedAbbr.replace(/\.{2,}/g, '.'); + const fixedPeriods = fixedAbbr.replace(/\.{2,}/g, '.'); // Fix am/pm formatting return prettifyAmPm(fixedPeriods); }; const prettifyAmPm = (text: string) => { - return text.replace(/(\d)\s*(am|AM|pm|PM)\b/g, (match, digit, timeDesignator) => { - const formattedDesignator = timeDesignator.toLowerCase() === 'am' - ? 'a.m.' - : 'p.m.'; - return `${digit} ${formattedDesignator}`; - }); -} \ No newline at end of file + return text.replace( + /(\d)\s*(am|AM|pm|PM)\b/g, + (_match, digit, timeDesignator) => { + const formattedDesignator = + timeDesignator.toLowerCase() === 'am' ? 'a.m.' : 'p.m.'; + return `${digit} ${formattedDesignator}`; + } + ); +};