formats
This commit is contained in:
parent
68b51a1097
commit
48ca087154
4 changed files with 15 additions and 19 deletions
|
|
@ -126,4 +126,4 @@
|
|||
"bugs": {
|
||||
"url": "https://github.com/reuters-graphics/graphics-components/issues"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,18 +1,15 @@
|
|||
import { Meta, Canvas } from '@storybook/blocks';
|
||||
import { Meta } from '@storybook/blocks';
|
||||
|
||||
import * as UtilFunctionStories from './Utils.stories.svelte';
|
||||
|
||||
<Meta of={UtilFunctionStories} />
|
||||
|
||||
|
||||
# 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.'
|
||||
```
|
||||
```
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
});
|
||||
|
||||
});
|
||||
|
|
|
|||
|
|
@ -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}`;
|
||||
});
|
||||
}
|
||||
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}`;
|
||||
}
|
||||
);
|
||||
};
|
||||
|
|
|
|||
Loading…
Reference in a new issue