@tstdl/base 0.91.18 → 0.91.19
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/formats.d.ts +42 -0
- package/formats.js +141 -0
- package/package.json +1 -1
package/formats.d.ts
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
export declare let locale: string;
|
|
2
|
+
export declare function configureFormats(options: {
|
|
3
|
+
locale?: string;
|
|
4
|
+
}): void;
|
|
5
|
+
export declare const integerFormat: Intl.NumberFormatOptions;
|
|
6
|
+
export declare const decimalFormat: Intl.NumberFormatOptions;
|
|
7
|
+
export declare const decimal1Format: Intl.NumberFormatOptions;
|
|
8
|
+
export declare const yearFormat: Intl.NumberFormatOptions;
|
|
9
|
+
export declare const dateTimeNumeric: Intl.DateTimeFormatOptions;
|
|
10
|
+
export declare const dateTimeShort: Intl.DateTimeFormatOptions;
|
|
11
|
+
export declare const dateTimeLong: Intl.DateTimeFormatOptions;
|
|
12
|
+
export declare const dateShort: Intl.DateTimeFormatOptions;
|
|
13
|
+
export declare const dateMedium: Intl.DateTimeFormatOptions;
|
|
14
|
+
export declare const dateLong: Intl.DateTimeFormatOptions;
|
|
15
|
+
export declare const timeShort: Intl.DateTimeFormatOptions;
|
|
16
|
+
export declare const euroFormat: Intl.NumberFormatOptions;
|
|
17
|
+
export declare const euroFormatWithoutCents: Intl.NumberFormatOptions;
|
|
18
|
+
export declare const percentFormat: Intl.NumberFormatOptions;
|
|
19
|
+
export declare function formatNumber(value: number, format?: Intl.NumberFormatOptions): string;
|
|
20
|
+
export declare function formatInteger(value: number): string;
|
|
21
|
+
export declare function formatDecimal(value: number, minimumFractionDigits?: number, maximumFractionDigits?: number): string;
|
|
22
|
+
export declare function formatYear(value: number): string;
|
|
23
|
+
export declare function formatTimeShort(value: number): string;
|
|
24
|
+
export declare function formatDateShort(value: Date | number): string;
|
|
25
|
+
export declare function formatDate(dateOrTimestamp: number | Date): string;
|
|
26
|
+
export declare function formatNumericDate(numericDate: number): string;
|
|
27
|
+
export declare function formatEuro(value: number): string;
|
|
28
|
+
export declare function formatPercent(value: number): string;
|
|
29
|
+
export type FormatUserNameOptions = {
|
|
30
|
+
lastNameFirst?: boolean;
|
|
31
|
+
nullOnMissing?: boolean;
|
|
32
|
+
};
|
|
33
|
+
export declare function formatUserName(user: {
|
|
34
|
+
firstName: string;
|
|
35
|
+
lastName: string | null;
|
|
36
|
+
} | null | undefined, options: FormatUserNameOptions & {
|
|
37
|
+
nullOnMissing: true;
|
|
38
|
+
}): string | null;
|
|
39
|
+
export declare function formatUserName(user: {
|
|
40
|
+
firstName: string;
|
|
41
|
+
lastName: string | null;
|
|
42
|
+
} | null | undefined, options?: FormatUserNameOptions): string;
|
package/formats.js
ADDED
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import { numericDateToTimestamp } from './utils/date-time.js';
|
|
2
|
+
import { memoize, memoizeSingle } from './utils/function/memoize.js';
|
|
3
|
+
import { isNull, isNullOrUndefined, isUndefined } from './utils/type-guards.js';
|
|
4
|
+
export let locale = 'de-DE';
|
|
5
|
+
export function configureFormats(options) {
|
|
6
|
+
locale = options.locale ?? locale;
|
|
7
|
+
}
|
|
8
|
+
export const integerFormat = {
|
|
9
|
+
useGrouping: true,
|
|
10
|
+
maximumFractionDigits: 0
|
|
11
|
+
};
|
|
12
|
+
export const decimalFormat = {
|
|
13
|
+
useGrouping: true,
|
|
14
|
+
minimumFractionDigits: 2,
|
|
15
|
+
maximumFractionDigits: 2
|
|
16
|
+
};
|
|
17
|
+
export const decimal1Format = {
|
|
18
|
+
useGrouping: true,
|
|
19
|
+
minimumFractionDigits: 0,
|
|
20
|
+
maximumFractionDigits: 1
|
|
21
|
+
};
|
|
22
|
+
export const yearFormat = {
|
|
23
|
+
useGrouping: false,
|
|
24
|
+
maximumFractionDigits: 0
|
|
25
|
+
};
|
|
26
|
+
export const dateTimeNumeric = {
|
|
27
|
+
year: 'numeric',
|
|
28
|
+
month: '2-digit',
|
|
29
|
+
day: '2-digit',
|
|
30
|
+
hour: '2-digit',
|
|
31
|
+
minute: '2-digit'
|
|
32
|
+
};
|
|
33
|
+
export const dateTimeShort = {
|
|
34
|
+
year: 'numeric',
|
|
35
|
+
month: 'short',
|
|
36
|
+
day: '2-digit',
|
|
37
|
+
hour: '2-digit',
|
|
38
|
+
minute: '2-digit'
|
|
39
|
+
};
|
|
40
|
+
export const dateTimeLong = {
|
|
41
|
+
year: 'numeric',
|
|
42
|
+
weekday: 'long',
|
|
43
|
+
month: 'long',
|
|
44
|
+
day: '2-digit',
|
|
45
|
+
hour: '2-digit',
|
|
46
|
+
minute: '2-digit'
|
|
47
|
+
};
|
|
48
|
+
export const dateShort = {
|
|
49
|
+
year: 'numeric',
|
|
50
|
+
month: '2-digit',
|
|
51
|
+
day: '2-digit'
|
|
52
|
+
};
|
|
53
|
+
export const dateMedium = {
|
|
54
|
+
year: 'numeric',
|
|
55
|
+
month: 'long',
|
|
56
|
+
day: '2-digit'
|
|
57
|
+
};
|
|
58
|
+
export const dateLong = {
|
|
59
|
+
year: 'numeric',
|
|
60
|
+
weekday: 'long',
|
|
61
|
+
month: 'long',
|
|
62
|
+
day: '2-digit'
|
|
63
|
+
};
|
|
64
|
+
export const timeShort = {
|
|
65
|
+
hour: '2-digit',
|
|
66
|
+
minute: '2-digit'
|
|
67
|
+
};
|
|
68
|
+
export const euroFormat = {
|
|
69
|
+
style: 'currency',
|
|
70
|
+
currency: 'EUR'
|
|
71
|
+
};
|
|
72
|
+
export const euroFormatWithoutCents = {
|
|
73
|
+
...euroFormat,
|
|
74
|
+
minimumFractionDigits: 0
|
|
75
|
+
};
|
|
76
|
+
export const percentFormat = {
|
|
77
|
+
style: 'percent',
|
|
78
|
+
minimumFractionDigits: 2,
|
|
79
|
+
maximumFractionDigits: 2
|
|
80
|
+
};
|
|
81
|
+
const getDecimalFormatter = memoize(_getDecimalFormatter);
|
|
82
|
+
const dateFormatter = memoizeSingle((loc) => Intl.DateTimeFormat(loc, dateShort));
|
|
83
|
+
const integerFormatter = memoizeSingle((loc) => Intl.NumberFormat(loc, integerFormat));
|
|
84
|
+
const decimalFormatter = memoizeSingle((loc) => Intl.NumberFormat(loc, decimalFormat));
|
|
85
|
+
const yearFormatter = memoizeSingle((loc) => Intl.NumberFormat(loc, yearFormat));
|
|
86
|
+
const euroFormatter = memoizeSingle((loc) => Intl.NumberFormat(loc, euroFormat));
|
|
87
|
+
const percentFormatter = memoizeSingle((loc) => Intl.NumberFormat(loc, percentFormat));
|
|
88
|
+
const timeShortFormatter = memoizeSingle((loc) => Intl.DateTimeFormat(loc, timeShort));
|
|
89
|
+
const dateShortFormatter = memoizeSingle((loc) => Intl.DateTimeFormat(loc, dateShort));
|
|
90
|
+
export function formatNumber(value, format) {
|
|
91
|
+
return Intl.NumberFormat(locale, format).format(value);
|
|
92
|
+
}
|
|
93
|
+
export function formatInteger(value) {
|
|
94
|
+
return integerFormatter(locale).format(value);
|
|
95
|
+
}
|
|
96
|
+
export function formatDecimal(value, minimumFractionDigits, maximumFractionDigits) {
|
|
97
|
+
if (isUndefined(minimumFractionDigits) && isUndefined(maximumFractionDigits)) {
|
|
98
|
+
return decimalFormatter(locale).format(value);
|
|
99
|
+
}
|
|
100
|
+
return getDecimalFormatter(minimumFractionDigits ?? 2, maximumFractionDigits ?? 2).format(value);
|
|
101
|
+
}
|
|
102
|
+
export function formatYear(value) {
|
|
103
|
+
return yearFormatter(locale).format(value);
|
|
104
|
+
}
|
|
105
|
+
export function formatTimeShort(value) {
|
|
106
|
+
return timeShortFormatter(locale).format(new Date(1970, 1, 1, 0, 0, 0, value));
|
|
107
|
+
}
|
|
108
|
+
export function formatDateShort(value) {
|
|
109
|
+
return dateShortFormatter(locale).format(value);
|
|
110
|
+
}
|
|
111
|
+
export function formatDate(dateOrTimestamp) {
|
|
112
|
+
return dateFormatter(locale).format(dateOrTimestamp);
|
|
113
|
+
}
|
|
114
|
+
export function formatNumericDate(numericDate) {
|
|
115
|
+
return formatDate(numericDateToTimestamp(numericDate));
|
|
116
|
+
}
|
|
117
|
+
export function formatEuro(value) {
|
|
118
|
+
return euroFormatter(locale).format(value);
|
|
119
|
+
}
|
|
120
|
+
export function formatPercent(value) {
|
|
121
|
+
return percentFormatter(locale).format(value);
|
|
122
|
+
}
|
|
123
|
+
export function formatUserName(user, { lastNameFirst = false, nullOnMissing = false } = {}) {
|
|
124
|
+
if (isNullOrUndefined(user)) {
|
|
125
|
+
return nullOnMissing ? null : '-';
|
|
126
|
+
}
|
|
127
|
+
if (isNull(user.lastName)) {
|
|
128
|
+
return user.firstName;
|
|
129
|
+
}
|
|
130
|
+
if (lastNameFirst) {
|
|
131
|
+
return `${user.lastName}, ${user.firstName}`;
|
|
132
|
+
}
|
|
133
|
+
return `${user.firstName} ${user.lastName}`;
|
|
134
|
+
}
|
|
135
|
+
function _getDecimalFormatter(minimumFractionDigits = 2, maximumFractionDigits = 2) {
|
|
136
|
+
return Intl.NumberFormat(locale, {
|
|
137
|
+
useGrouping: true,
|
|
138
|
+
minimumFractionDigits,
|
|
139
|
+
maximumFractionDigits
|
|
140
|
+
});
|
|
141
|
+
}
|