-
Notifications
You must be signed in to change notification settings - Fork 0
/
FormattingService.ts
352 lines (313 loc) · 11.2 KB
/
FormattingService.ts
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
interface FormattingConfig {
locale?: string;
currency?: string;
currencySignDisplay?: keyof Intl.NumberFormatOptionsSignDisplayRegistry;
percentageDigits?: number;
numbersDigits?: number;
suppressTrailingZeros?: boolean;
sectionTitleLevel?: 1 | 2 | 3 | 4 | 5 | 6;
formatTitleCase?: boolean;
}
type FormatterType = 'currency' | 'percentage' | 'text' | 'number' | 'compact';
export interface ReportField {
value: number | string;
formatter?: FormatterType;
note?: string;
highlight?: boolean;
}
interface ReportSection {
title: string;
description?: string;
data: Record<string, ReportField | number | string>;
defaultFormatter?: FormatterType;
collapsed?: boolean;
}
interface ReportData {
title: string;
description?: string;
sections: ReportSection[];
}
class FormattingError extends Error {
constructor(message: string) {
super(message);
this.name = 'FormattingError';
}
}
class FormattingService {
private readonly formatters: Map<FormatterType, Intl.NumberFormat> =
new Map();
private readonly config: Required<FormattingConfig>;
private readonly titleCaseMap: Map<string, string> = new Map();
private readonly acronyms = new Set(['ROI', 'CoC', 'PMI', 'HOA', 'CapEx']);
constructor(config: FormattingConfig = {}) {
this.config = {
locale: config.locale ?? 'en-CA',
currency: config.currency ?? 'CAD',
currencySignDisplay: config.currencySignDisplay ?? 'auto',
percentageDigits: config.percentageDigits ?? 2,
numbersDigits: config.numbersDigits ?? 2,
suppressTrailingZeros: config.suppressTrailingZeros ?? true,
sectionTitleLevel: config.sectionTitleLevel ?? 2,
formatTitleCase: config.formatTitleCase ?? true,
};
this.initializeFormatters();
}
/**
* Initializes and stores the number formatters for different types of data.
*
* @remarks
* This method creates four number formatters:
* - Currency: Formats numbers as currency values.
* - Percentage: Formats numbers as percentages.
* - Number: Formats numbers with a specified number of fractional digits.
* - Compact: Formats numbers in a compact notation (e.g., 1.2M, 450K).
*
* If any error occurs during the initialization process, a `FormattingError` is thrown.
*
* @throws {FormattingError} - If an error occurs during the initialization process.
*/
private initializeFormatters = (): void => {
try {
// Currency formatter
this.formatters.set(
'currency',
new Intl.NumberFormat(this.config.locale, {
style: 'currency',
currency: this.config.currency,
signDisplay: this.config.currencySignDisplay,
minimumFractionDigits: this.config.suppressTrailingZeros ? 0 : 2,
})
);
// Percentage formatter
this.formatters.set(
'percentage',
new Intl.NumberFormat(this.config.locale, {
style: 'percent',
minimumFractionDigits: this.config.percentageDigits,
maximumFractionDigits: this.config.percentageDigits,
})
);
// Number formatter
this.formatters.set(
'number',
new Intl.NumberFormat(this.config.locale, {
minimumFractionDigits: this.config.numbersDigits,
maximumFractionDigits: this.config.numbersDigits,
})
);
// Compact number formatter (e.g., 1.2M, 450K)
this.formatters.set(
'compact',
new Intl.NumberFormat(this.config.locale, {
notation: 'compact',
compactDisplay: 'short',
})
);
} catch (error) {
throw new FormattingError(
`Failed to initialize formatters: ${(error as Error).message}`
);
}
};
/**
* Formats a given string into title case, handling acronyms and optionally caching the result.
*
* @param str - The string to be formatted.
* @returns The formatted string.
*
* @remarks
* This function first checks if the input string is already cached. If it is, the cached result is returned.
* If the `formatTitleCase` configuration option is set to `false`, the input string is returned as is.
* Otherwise, the function applies the following transformations to the input string:
* - Words are separated by a space.
* - The first character of the string is capitalized.
* - Acronyms are converted to uppercase.
* - The formatted string is trimmed.
* Finally, the formatted string is cached and returned.
*/
private formatTitle = (str: string): string => {
// Check cache first
const cached = this.titleCaseMap.get(str);
if (cached) return cached;
if (!this.config.formatTitleCase) return str;
const formatted = str
.replace(/([A-Z])/g, ' $1')
.replace(/^./, (char) => char.toUpperCase())
.replace(/\b\w+\b/g, (word) => {
if (this.acronyms.has(word.toUpperCase())) {
return word.toUpperCase();
}
return word;
})
.trim();
// Cache the result
this.titleCaseMap.set(str, formatted);
return formatted;
};
/**
* Formats a given value based on the specified formatter type.
*
* @param value - The value to be formatted. If the value is a string, it will be returned as is.
* @param formatter - The type of formatter to use. If not provided, the 'text' formatter will be used.
*
* @returns The formatted value as a string. If the value is a number and a formatter is specified,
* the number will be formatted according to the specified formatter. If no formatter is specified,
* the number will be converted to a string.
*
* @throws {FormattingError} - If the specified formatter type is not recognized.
*/
private formatValue = (
value: number | string,
formatter?: FormatterType
): string => {
if (typeof value === 'string') return value;
const numberFormatter = this.formatters.get(formatter ?? 'text');
if (numberFormatter) {
return numberFormatter.format(value);
}
return String(value);
};
/**
* Generates a Markdown section for a report based on the provided data.
*
* @param title - The title of the section.
* @param description - An optional description for the section.
* @param data - The data to be included in the section.
* @param defaultFormatter - The default formatter to use for values in the section.
* @param collapsed - Indicates whether the section should be collapsed by default.
*
* @returns A Markdown string representing the section.
*
* @remarks
* This function processes the provided data and generates a Markdown section with the specified title,
* description, and data. It applies formatting to the values based on the provided formatter or the default
* formatter. If the `collapsed` parameter is set to `true`, the section will be wrapped in a `<details>`
* tag to allow collapsing the content.
*/
private generateSection = ({
title,
description,
data,
defaultFormatter,
collapsed,
}: ReportSection): string => {
const lines = Object.entries(data).map(([key, fieldData]) => {
let value: number | string;
let formatter = defaultFormatter;
let note: string | undefined;
let highlight: boolean | undefined;
if (typeof fieldData === 'object' && 'value' in fieldData) {
value = fieldData.value;
formatter = fieldData.formatter ?? defaultFormatter;
note = fieldData.note;
highlight = fieldData.highlight;
} else {
value = fieldData;
}
let line = `${this.formatTitle(key)}: ${this.formatValue(
value,
formatter
)}`;
if (note) {
line += ` _(${note})_`;
}
if (highlight) {
line = `\n**${line}**`;
}
return `- ${line}`;
});
const titleHashes = '#'.repeat(this.config.sectionTitleLevel);
const output = [`${titleHashes} ${title}:`, ''];
if (description) {
output.push(description, '');
}
if (collapsed) {
output.push(
'<details>',
'<summary>Show details</summary>',
'',
...lines,
'',
'</details>'
);
} else {
output.push(...lines);
}
return output.join('\n');
};
/**
* Generates a Markdown report based on the provided report data.
*
* @param reportData - The report data to be included in the Markdown report.
* @returns A Markdown string representing the report.
*
* @throws {FormattingError} - If an error occurs during the report generation process.
*
* @remarks
* This function processes the provided report data and generates a Markdown report with the specified title,
* description, and sections. It applies formatting to the values based on the provided formatter or the default
* formatter. If an error occurs during the report generation process, a `FormattingError` is thrown.
*/
generateMarkdownReport = (reportData: ReportData): string => {
try {
const output = [`# ${reportData.title}`, ''];
if (reportData.description) {
output.push(reportData.description, '');
}
const sections = reportData.sections.map(this.generateSection);
return output.concat(sections).join('\n');
} catch (error) {
throw new FormattingError(
`Failed to generate report: ${(error as Error).message}`
);
}
};
/**
* Formats a given number as a currency value using the configured locale and currency settings.
*
* @param value - The number to be formatted as currency.
*
* @returns The formatted currency value as a string.
*
* @remarks
* This function uses the configured locale and currency settings to format the given number as a currency value.
* The currency value is obtained from the 'currency' number formatter stored in the `formatters` map.
*
* @throws {FormattingError} - If the 'currency' number formatter is not found in the `formatters` map.
*
* @example
* ```typescript
* const formattingService = new FormattingService();
* const formattedValue = formattingService.formatCurrency(12345.67);
* console.log(formattedValue); // Output: $12,345.67
* ```
*/
formatCurrency = (value: number): string => {
return this.formatters.get('currency')!.format(value);
};
/**
* Formats a given number as a percentage value using the configured locale and percentage settings.
*
* @param value - The number to be formatted as a percentage. The value should be a number between 0 and 1,
* where 1 represents 100%.
*
* @returns The formatted percentage value as a string.
*
* @remarks
* This function uses the configured locale and percentage settings to format the given number as a percentage value.
* The percentage value is obtained from the 'percentage' number formatter stored in the `formatters` map.
*
* @throws {FormattingError} - If the 'percentage' number formatter is not found in the `formatters` map.
*
* @example
* ```typescript
* const formattingService = new FormattingService();
* const formattedValue = formattingService.formatPercentage(0.5);
* console.log(formattedValue); // Output: 50%
* ```
*/
formatPercentage = (value: number): string => {
return this.formatters.get('percentage')!.format(value);
};
}
export default FormattingService;