-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathFormattingService.tsx
More file actions
35 lines (30 loc) · 1.04 KB
/
FormattingService.tsx
File metadata and controls
35 lines (30 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import DateAndTimeService from './DateAndTimeService';
import { TimeAgo } from '../helpers/timeago';
const spiffFormatFunctions: { [key: string]: Function } = {
convert_seconds_to_date_time_for_display: DateAndTimeService.formatDateTime,
convert_seconds_to_time_ago_for_display: TimeAgo.inWords,
convert_seconds_to_duration_for_display:
DateAndTimeService.formatDurationForDisplay,
convert_date_to_date_for_display:
DateAndTimeService.ymdDateStringToConfiguredFormat,
};
const checkForSpiffFormats = (markdown: string) => {
const replacer = (
match: string,
spiffFormat: string,
originalValue: string,
) => {
if (spiffFormat in spiffFormatFunctions) {
return spiffFormatFunctions[spiffFormat](originalValue);
}
console.warn(
`attempted: ${match}, but ${spiffFormat} is not a valid conversion function`,
);
return match;
};
return markdown.replaceAll(/SPIFF_FORMAT:::(\w+)\(([^)]+)\)/g, replacer);
};
const FormattingService = {
checkForSpiffFormats,
};
export default FormattingService;