@procore/globalization-toolkit 1.13.0 → 1.15.0
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/README.md +324 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +2 -1
- package/dist/index.js.map +1 -1
- package/dist/numbers/CurrencyFormatter.d.ts +3 -1
- package/dist/numbers/CurrencyFormatter.js +13 -1
- package/dist/numbers/CurrencyFormatter.js.map +1 -1
- package/dist/numbers/NumberFormatter.js +1 -1
- package/dist/numbers/NumberFormatter.js.map +1 -1
- package/dist/numbers/NumberUtils.d.ts +3 -1
- package/dist/numbers/NumberUtils.js +20 -9
- package/dist/numbers/NumberUtils.js.map +1 -1
- package/dist/tsconfig.build.tsbuildinfo +1 -1
- package/docs/assets/search.js +1 -1
- package/docs/classes/CurrencyFormatter.html +10 -10
- package/docs/classes/DateTimeFormatter.html +149 -0
- package/docs/classes/NumberFormatter.html +12 -12
- package/docs/functions/formatCurrency.html +40 -4
- package/docs/functions/formatCurrencyToParts.html +38 -2
- package/docs/functions/formatDateTime.html +121 -0
- package/docs/functions/formatDateTimeToParts.html +121 -0
- package/docs/functions/formatNumber.html +40 -4
- package/docs/functions/formatNumberToParts.html +38 -2
- package/docs/functions/getNumberSeparators.html +38 -2
- package/docs/functions/getStartDayOfTheWeek.html +117 -0
- package/docs/functions/parseCurrency.html +38 -2
- package/docs/functions/parseNumber.html +38 -2
- package/docs/index.html +37 -1
- package/docs/interfaces/CurrencyOptions.html +7 -7
- package/docs/interfaces/DateTimeOptionsBasic.html +85 -0
- package/docs/interfaces/DateTimeOptionsComponentsAll.html +140 -0
- package/docs/interfaces/DateTimeOptionsComponentsHourMinute.html +140 -0
- package/docs/interfaces/DateTimeOptionsComponentsHourMinuteSecond.html +140 -0
- package/docs/interfaces/DateTimeOptionsComponentsMonthDay.html +140 -0
- package/docs/interfaces/DateTimeOptionsComponentsWeekdayYearMonthDay.html +140 -0
- package/docs/interfaces/DateTimeOptionsComponentsYearMonth.html +140 -0
- package/docs/interfaces/DateTimeOptionsComponentsYearMonthDay.html +140 -0
- package/docs/interfaces/DateTimeOptionsStyle.html +140 -0
- package/docs/interfaces/NumberOptions.html +5 -5
- package/docs/modules.html +74 -2
- package/docs/types/CurrencyDisplay.html +39 -3
- package/docs/types/CurrencySign.html +39 -3
- package/docs/types/DateStyle.html +103 -0
- package/docs/types/{NumberFormatPartTypes.html → DateTimeFormatPart.html} +42 -6
- package/docs/types/DateTimeOptions.html +102 -0
- package/docs/types/DateTimeStyle.html +103 -0
- package/docs/types/Day.html +103 -0
- package/docs/types/FormatterOptions.html +38 -2
- package/docs/types/Hour.html +103 -0
- package/docs/types/Locale.html +38 -2
- package/docs/types/Minute.html +103 -0
- package/docs/types/Month.html +103 -0
- package/docs/types/NumberFormatPart.html +39 -10
- package/docs/types/NumericTwoDigit.html +103 -0
- package/docs/types/Percent.html +39 -3
- package/docs/types/Second.html +103 -0
- package/docs/types/Separators.html +38 -2
- package/docs/types/TimeStyle.html +103 -0
- package/docs/types/Weekday.html +103 -0
- package/docs/types/Year.html +103 -0
- package/docs/variables/CurrencyDisplay-1.html +39 -3
- package/docs/variables/CurrencySign-1.html +39 -3
- package/docs/variables/DateStyle-1.html +114 -0
- package/docs/variables/DateTimeStyle-1.html +114 -0
- package/docs/variables/Day-1.html +110 -0
- package/docs/variables/Hour-1.html +110 -0
- package/docs/variables/Minute-1.html +110 -0
- package/docs/variables/Month-1.html +116 -0
- package/docs/variables/NumericTwoDigit-1.html +110 -0
- package/docs/variables/Percent-1.html +41 -5
- package/docs/variables/Second-1.html +110 -0
- package/docs/variables/TimeStyle-1.html +114 -0
- package/docs/variables/Weekday-1.html +112 -0
- package/docs/variables/Year-1.html +110 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -6,12 +6,335 @@ i18n formatting library
|
|
|
6
6
|
[](https://circleci.com/gh/procore/globalization-toolkit/tree/main)
|
|
7
7
|
[](https://www.npmjs.com/package/@procore/globalization-toolkit)
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
Before implementing the Globalization Toolkit (GTK), please review the [Globalization Toolkit Adoption Diagram](https://lucid.app/lucidchart/47c65380-9d87-409a-a15e-77c668e4c474/edit?invitationId=inv_abb5b5f8-3d6f-499f-920d-6dc6b7c9734c&page=0_0#) to determine the best implementation strategy for you. Continue reading for instructions on direct usage of the GTK.
|
|
10
|
+
|
|
11
|
+
## [Engineering Documentation](https://procore.github.io/globalization-toolkit/)
|
|
12
|
+
The engineering documentation contains technical documentation for the GTK repo. It details classes, types, interfaces, and features. Continue reading for install and usage instructions.
|
|
13
|
+
Further reading on the GTK:
|
|
14
|
+
- [GTK Adoption Training Presentation](https://docs.google.com/presentation/d/1FtB2DR5qroKgGcxxWsu5Ac9QHA01I7WM75FUeB67izE/edit?usp=sharing)
|
|
15
|
+
- [GTK SDR](https://github.com/procore/system-design-records/blob/main/lifecycle/2-pilot/internationalization/0002-globalization-toolkit.md)
|
|
16
|
+
- [GTK FAQ]() - Coming soon!
|
|
10
17
|
|
|
18
|
+
## Install
|
|
19
|
+
NPM
|
|
11
20
|
```sh
|
|
12
21
|
$ npm i --save @procore/globalization-toolkit
|
|
13
22
|
```
|
|
23
|
+
YARN
|
|
24
|
+
```sh
|
|
25
|
+
$ yarn add @procore/globalization-toolkit
|
|
26
|
+
```
|
|
27
|
+
## Usage
|
|
28
|
+
The GTK has 3 available classes: NumberFormatter, DateTimeFormatter, CurrencyFormatter. The structure of the GTK Library allows for instantiation of a formatting class with defined options. The instantiated class gives access to related functions. In addition, the GTK makes the class functions available as stand-alone functions. Below are examples of GTK usage with class formatter and with stand-alone functions.
|
|
29
|
+
|
|
30
|
+
### A Note on Locales
|
|
31
|
+
The GTK supports industry standard locale codes. The GTK does NOT support [Procore Custom Locales](https://procoretech.atlassian.net/wiki/spaces/PLATFORM/pages/1039111578/Levels+of+Support+for+Locales+Product+Coverage#Custom-Locales). When implementing the GTK, please sanitize the locale to follow industry standard of 'region-language' like 'en-US'.
|
|
32
|
+
|
|
33
|
+
### NumberFormatter Class
|
|
34
|
+
The GTK `NumberFormatter` class has the ability to format numbers for a given locale. The [NumberFormatter](https://procore.github.io/globalization-toolkit/classes/NumberFormatter) class is instantiated with [NumberOptions](https://procore.github.io/globalization-toolkit/interfaces/NumberOptions.html) and has 4 available functions:
|
|
35
|
+
- `formatNumber(value: number)`,
|
|
36
|
+
- `formatNumberToParts(value: number)`,
|
|
37
|
+
- `parseNumber(value: string)`, and
|
|
38
|
+
- `getNumberSeparators(locale: string)`.
|
|
39
|
+
|
|
40
|
+
The [NumberOptions](https://procore.github.io/globalization-toolkit/interfaces/CurrencyOptions.html) are summarized below:
|
|
41
|
+
- `locale`: string
|
|
42
|
+
- `maximumFractionDigits?`: number
|
|
43
|
+
- `minimumFractionDigits?`: number
|
|
44
|
+
- `percent?`: 'standard', 'decimal'
|
|
45
|
+
|
|
46
|
+
Below code blocks display usage of the NumberFormatter class and available functions.
|
|
47
|
+
#### [formatNumber()](https://procore.github.io/globalization-toolkit/classes/NumberFormatter.html#formatNumber) & [formatNumberToParts()](https://procore.github.io/globalization-toolkit/classes/NumberFormatter.html#formatNumberToParts)
|
|
48
|
+
```ts
|
|
49
|
+
import { NumberFormatter } from '@procore/globalization-toolkit';
|
|
50
|
+
|
|
51
|
+
const value = 1234;
|
|
52
|
+
const numberOptions = { locale: "en-US", minimumFractionDigits: 3 };
|
|
53
|
+
const formatter = new NumberFormatter(numberOptions);
|
|
54
|
+
|
|
55
|
+
console.log(formatter.formatNumber(value)) // expected output: '1,234.000'
|
|
56
|
+
|
|
57
|
+
console.log(formatter.formatNumberToParts(value))
|
|
58
|
+
/**
|
|
59
|
+
* expected output is an array of objects representing the parts of the formatted number:
|
|
60
|
+
* [
|
|
61
|
+
* { type: "integer", value: "1", },
|
|
62
|
+
* { type: "group", value: ",", },
|
|
63
|
+
* { type: "integer", value: "234", },
|
|
64
|
+
* { type: "decimal", value: ".", },
|
|
65
|
+
* { type: "fraction", value: "000", },
|
|
66
|
+
* ];
|
|
67
|
+
*/
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
#### [parseNumber()](https://procore.github.io/globalization-toolkit/classes/NumberFormatter.html#parseNumber)
|
|
71
|
+
```ts
|
|
72
|
+
import { NumberFormatter } from '@procore/globalization-toolkit';
|
|
73
|
+
|
|
74
|
+
const value = "123 456 789,12";
|
|
75
|
+
const numberOptions = { locale: "fr-FR" };
|
|
76
|
+
const formatter = new NumberFormatter(numberOptions);
|
|
77
|
+
|
|
78
|
+
console.log(formatter.parseNumber(value)); // expected output: '123456789.12'
|
|
79
|
+
|
|
80
|
+
console.log(formatter.parseNumber("Abcd")); // expected output: NaN
|
|
81
|
+
```
|
|
82
|
+
#### [getSeparators()](https://procore.github.io/globalization-toolkit/classes/NumberFormatter.html#getNumberSeparators)
|
|
83
|
+
|
|
84
|
+
```ts
|
|
85
|
+
import { NumberFormatter } from '@procore/globalization-toolkit';
|
|
86
|
+
|
|
87
|
+
const germanNumberOptions = { locale: "de-DE" };
|
|
88
|
+
const germanFormatter = new NumberFormatter(germanNumberOptions);
|
|
89
|
+
|
|
90
|
+
console.log(germanFormatter.getSeparators()); // expected output: { decimal: ",", group: "." }
|
|
91
|
+
|
|
92
|
+
const usNumberOptions = { locale: 'en-US' };
|
|
93
|
+
const formatter = new NumberFormatter(usNumberOptions);
|
|
94
|
+
|
|
95
|
+
console.log(formatter.getSeparators()); // expected output: { decimal: ".", group: "," }
|
|
96
|
+
```
|
|
97
|
+
### NumberFormatter Functions
|
|
98
|
+
The functions used on the NumberFormatter class are also available as stand alone functions. Read more information about each function in the GTK Documentation.
|
|
99
|
+
- [formatNumber](https://procore.github.io/globalization-toolkit/functions/formatNumber.html)
|
|
100
|
+
- [formatNumberToParts](https://procore.github.io/globalization-toolkit/functions/formatNumberToParts.html)
|
|
101
|
+
- [parseNumber](https://procore.github.io/globalization-toolkit/functions/parseNumber.html)
|
|
102
|
+
- [getNumberSeparators](https://procore.github.io/globalization-toolkit/functions/getNumberSeparators.html)
|
|
103
|
+
|
|
104
|
+
The example below shows usage of the stand alone function: `formatNumber()`.
|
|
105
|
+
```ts
|
|
106
|
+
import { formatNumber } from '@procore/globalization-toolkit';
|
|
107
|
+
|
|
108
|
+
const value = 1234;
|
|
109
|
+
const numberOptions = { locale: "en-US", minimumFractionDigits: 3 };
|
|
110
|
+
|
|
111
|
+
console.log(formatNumber(value, numberOptions)) // expected output: '1,234.000'
|
|
112
|
+
```
|
|
113
|
+
### CurrencyFormatter class
|
|
114
|
+
The GTK CurrencyFormatter class has the ability to format a number with given options. The [CurrencyFormatter](https://procore.github.io/globalization-toolkit/classes/CurrencyFormatter.html) class is instantiated with [CurrencyOptions](https://procore.github.io/globalization-toolkit/interfaces/CurrencyOptions.html).
|
|
115
|
+
The CurrencyFormatter has 4 available functions:
|
|
116
|
+
- `formatCurrency(value: number)`,
|
|
117
|
+
- `formatCurrencyToParts(value: number)`
|
|
118
|
+
- `parseCurrency(value: string)`
|
|
119
|
+
- `getCurrencySeparators()`
|
|
120
|
+
|
|
121
|
+
The [CurrencyOptions](https://procore.github.io/globalization-toolkit/interfaces/CurrencyOptions.html) are summarized below:
|
|
122
|
+
- `currencyIsoCode`: string ([acceptable ISO codes](https://github.com/formatjs/formatjs/blob/main/packages/intl-enumerator/src/currencies.generated.ts))
|
|
123
|
+
- `locale`: string
|
|
124
|
+
- `currencySign?`: 'accounting', 'standard'
|
|
125
|
+
- `currencyDisplay?`: 'code', 'name', 'narrowSymbol', 'symbol'
|
|
126
|
+
- `maximumFractionDigits?`: number
|
|
127
|
+
- `minimumFractionDigits?`: number
|
|
128
|
+
|
|
129
|
+
Below code blocks display usage of the CurrencyFormatter class and available functions.
|
|
130
|
+
|
|
131
|
+
[formatCurrency(value: number)](https://procore.github.io/globalization-toolkit/classes/CurrencyFormatter.html#formatCurrency)
|
|
132
|
+
```ts
|
|
133
|
+
const value = 1234;
|
|
134
|
+
const currencyOptions = { locale: "en-US", currencyIsoCode: "USD" };
|
|
135
|
+
const formatter = new CurrencyFormatter(currencyOptions);
|
|
136
|
+
|
|
137
|
+
console.log(formatter.formatCurrency(value)) // expected outcome: '$1,234.00'
|
|
138
|
+
```
|
|
139
|
+
Usage with currencyDisplay option
|
|
140
|
+
```ts
|
|
141
|
+
import { CurrencyFormatter } from '@procore/globalization-toolkit';
|
|
142
|
+
|
|
143
|
+
const value = 1234.567;
|
|
144
|
+
const currencyOptions: CurrencyOptions = {
|
|
145
|
+
locale: "de-DE",
|
|
146
|
+
currencyIsoCode: "USD",
|
|
147
|
+
currencyDisplay: "name",
|
|
148
|
+
};
|
|
149
|
+
const formatter = new CurrencyFormatter(currencyOptions);
|
|
150
|
+
|
|
151
|
+
console.log(formatter.formatCurrency(value)); // expected outcome: '1.234,57 US-Dollar'
|
|
152
|
+
```
|
|
153
|
+
Usage with minimumFractionDigits and currencyDisplay
|
|
154
|
+
```ts
|
|
155
|
+
const value = 1234.567;
|
|
156
|
+
const currencyOptions = {
|
|
157
|
+
locale: "ja-JP",
|
|
158
|
+
minimumFractionDigits: 2,
|
|
159
|
+
currencyIsoCode: "JPY",
|
|
160
|
+
};
|
|
161
|
+
const formatter = new CurrencyFormatter(currencyOptions);
|
|
162
|
+
|
|
163
|
+
console.log(formatter.formatCurrency(value)); // expected outcome: '¥1,234.57'
|
|
164
|
+
```
|
|
165
|
+
[formatCurrencyToParts()](https://procore.github.io/globalization-toolkit/classes/CurrencyFormatter.html#formatCurrencyToParts)
|
|
166
|
+
```ts
|
|
167
|
+
import { CurrencyFormatter } from '@procore/globalization-toolkit';
|
|
168
|
+
|
|
169
|
+
const value = -1234.56;
|
|
170
|
+
const currencyOptions: CurrencyOptions = {
|
|
171
|
+
locale: "en-US",
|
|
172
|
+
currencyIsoCode: "CAD",
|
|
173
|
+
currencySign: "accounting",
|
|
174
|
+
};
|
|
175
|
+
const formatter = new CurrencyFormatter(currencyOptions);
|
|
176
|
+
console.log(formatter.formatCurrencyToParts(value));
|
|
177
|
+
/** expected output:
|
|
178
|
+
* [
|
|
179
|
+
* { type: "literal", value: "(", },
|
|
180
|
+
* { type: "currency", value: "CA$", },
|
|
181
|
+
* { type: "integer", value: "1", },
|
|
182
|
+
* { type: "group", value: ",", },
|
|
183
|
+
* { type: "integer", value: "234", },
|
|
184
|
+
* { type: "decimal", value: ".", },
|
|
185
|
+
* { type: "fraction", value: "56", },
|
|
186
|
+
* { type: "literal", value: ")", },
|
|
187
|
+
* ]
|
|
188
|
+
**/
|
|
189
|
+
```
|
|
190
|
+
[parseCurrency(value: string)](https://procore.github.io/globalization-toolkit/classes/CurrencyFormatter.html#parseCurrency)
|
|
191
|
+
```ts
|
|
192
|
+
const value = "(1 234 567,56 €)";
|
|
193
|
+
const currencyOptions: CurrencyOptions = {
|
|
194
|
+
locale: "fr-FR",
|
|
195
|
+
currencyIsoCode: "EUR",
|
|
196
|
+
currencySign: "accounting",
|
|
197
|
+
};
|
|
198
|
+
const formatter = new CurrencyFormatter(currencyOptions);
|
|
199
|
+
|
|
200
|
+
console.log(formatter.parseCurrency(value)); // expected outcome: -1234567.56
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
[getCurrencySeparators](https://procore.github.io/globalization-toolkit/classes/CurrencyFormatter.html#getCurrencySeparators)
|
|
204
|
+
```ts
|
|
205
|
+
const currencyOptions = { locale: "de-DE", currencyIsoCode: "EUR" };
|
|
206
|
+
const formatter = new CurrencyFormatter(currencyOptions);
|
|
207
|
+
|
|
208
|
+
console.log(formatter.getCurrencySeparators()) // expected outcome: { decimal: ",", group: "." }
|
|
209
|
+
```
|
|
210
|
+
### CurrencyFormatter Functions
|
|
211
|
+
The functions used on the CurrencyFormatter class are also available as stand alone functions. Read more information about each function in the GTK Documentation.
|
|
212
|
+
- [formatCurrency](https://procore.github.io/globalization-toolkit/functions/formatCurrency.html)
|
|
213
|
+
- [formatCurrencyToParts](https://procore.github.io/globalization-toolkit/functions/formatCurrencyToParts.html)
|
|
214
|
+
- [parseCurrency](https://procore.github.io/globalization-toolkit/functions/parseCurrency.html)
|
|
215
|
+
- [getCurrencySeparators](https://procore.github.io/globalization-toolkit/functions/getCurrencySeparators.html)
|
|
216
|
+
|
|
217
|
+
The example below shows usage of the stand alone function: `formatCurrency(value, options)`.
|
|
218
|
+
```ts
|
|
219
|
+
import { formatCurrency, getCurrencySeparators } from '@procore/globalization-toolkit';
|
|
220
|
+
|
|
221
|
+
const value = 1234;
|
|
222
|
+
const currencyOptions = { locale: 'en-US', currencyIsoCode: 'USD' };
|
|
223
|
+
|
|
224
|
+
console.log(formatCurrency(value, currencyOptions)) // expected output: '$1,234.00'
|
|
225
|
+
|
|
226
|
+
console.log(getCurrencySeparators(currencyOptions)) // expected output: { decimal: ".", group: "," }
|
|
227
|
+
```
|
|
228
|
+
|
|
229
|
+
### DateTimeFormatter
|
|
230
|
+
The GTK DateTimeFormatter class has the ability to format date and time for the correct timezone. The [DateTimeFormatter](https://procore.github.io/globalization-toolkit/classes/DateTimeFormatter.html) class is instantiated with [DateTimeOptions](https://procore.github.io/globalization-toolkit/types/DateTimeOptions.html).
|
|
231
|
+
The DateTimeFormatter has 3 available functions:
|
|
232
|
+
- `formatDateTime(value: Date)`,
|
|
233
|
+
- `formatDateTimeToParts(value: Date)`, and
|
|
234
|
+
- `getStartDayOfTheWeek()`
|
|
235
|
+
|
|
236
|
+
The DateTimeOptions can be used in two ways, but the two options cannot be combined. This follows the pattern of Intl.DateTimeFormat.
|
|
237
|
+
1. Style Options:
|
|
238
|
+
- timeStyle: 'short', 'medium', 'long', 'full'
|
|
239
|
+
- dateStyle: 'short', 'medium', 'long', 'full'
|
|
240
|
+
2. Component Options
|
|
241
|
+
- weekday: 'long', 'short', 'narrow'
|
|
242
|
+
- year: 'numeric', '2-digit'
|
|
243
|
+
- month: numeric', '2-digit', 'long', 'short', 'narrow'
|
|
244
|
+
- day: 'numeric', '2-digit'
|
|
245
|
+
- hour: 'numeric', '2-digit'
|
|
246
|
+
- minute: 'numeric', '2-digit'
|
|
247
|
+
- second: 'numeric', '2-digit'
|
|
248
|
+
|
|
249
|
+
Below code blocks display usage of the DateTimeFormatter class and available functions.
|
|
250
|
+
[formatDateTime(value: Date)](https://procore.github.io/globalization-toolkit/classes/DateTimeFormatter.html#formatDateTime)
|
|
251
|
+
SIMPLE OPTIONS
|
|
252
|
+
```ts
|
|
253
|
+
import { DateTimeFormatter } from '@procore/globalization-toolkit';
|
|
254
|
+
|
|
255
|
+
const value = new Date(1445419756738);
|
|
256
|
+
const dateTimeOptions = {
|
|
257
|
+
locale: 'en-US',
|
|
258
|
+
timeZone: 'UTC',
|
|
259
|
+
dateStyle: 'full',
|
|
260
|
+
timeStyle: 'long',
|
|
261
|
+
};
|
|
262
|
+
const formatter = new DateTimeFormatter(dateTimeOptions);
|
|
263
|
+
|
|
264
|
+
console.log(formatter.formatDateTime(value))
|
|
265
|
+
// expected output: Wednesday, October 21, 2015 at 9:29:16 AM UTC
|
|
266
|
+
```
|
|
267
|
+
COMPLEX OPTIONS
|
|
268
|
+
```js
|
|
269
|
+
const value = new Date(Date.UTC(1885, 8, 1, 12, 0, 16, 738));
|
|
270
|
+
const dateTimeOptions = {
|
|
271
|
+
locale: 'en-US',
|
|
272
|
+
timeZone: 'UTC',
|
|
273
|
+
weekday: 'long',
|
|
274
|
+
year: 'numeric',
|
|
275
|
+
month: '2-digit',
|
|
276
|
+
day: '2-digit',
|
|
277
|
+
hour: 'numeric',
|
|
278
|
+
minute: '2-digit',
|
|
279
|
+
second: '2-digit',
|
|
280
|
+
};
|
|
281
|
+
const formatter = new DateTimeFormatter(dateTimeOptions);
|
|
282
|
+
|
|
283
|
+
console.log(formatter.formatDateTime(value))
|
|
284
|
+
// expected output: "Tuesday, 09/01/1885, 12:00:16 PM";
|
|
285
|
+
```
|
|
286
|
+
[formatDateTimeToParts(value: Date)](https://procore.github.io/globalization-toolkit/classes/DateTimeFormatter.html#formatDateTimeToParts)
|
|
287
|
+
|
|
288
|
+
```ts
|
|
289
|
+
const value = new Date(Date.UTC(1955, 10, 5, 6, 0, 16, 738));
|
|
290
|
+
const dateTimeOptions = {
|
|
291
|
+
locale: "en-GB",
|
|
292
|
+
timeZone: "UTC",
|
|
293
|
+
dateStyle: DateStyle.FULL,
|
|
294
|
+
timeStyle: TimeStyle.SHORT,
|
|
295
|
+
};
|
|
296
|
+
const formatter = new DateTimeFormatter(dateTimeOptions);
|
|
297
|
+
|
|
298
|
+
console.log(formatter.formatDateTime(value))
|
|
299
|
+
/** expected output:
|
|
300
|
+
* [ { type: "weekday", value: "Saturday", },
|
|
301
|
+
* { type: "literal", value: ", ", },
|
|
302
|
+
* { type: "day", value: "5", },
|
|
303
|
+
* { type: "literal", value: " ", },
|
|
304
|
+
* { type: "month", value: "November", },
|
|
305
|
+
* { type: "literal", value: " ", },
|
|
306
|
+
* { type: "year", value: "1955", },
|
|
307
|
+
* { type: "literal", value: " at ",},
|
|
308
|
+
* { type: "hour", value: "06",},
|
|
309
|
+
* { type: "literal", value: ":",},
|
|
310
|
+
* { type: "minute", value: "00",}, ];
|
|
311
|
+
**/
|
|
312
|
+
```
|
|
313
|
+
[getStartDayOfTheWeek()](https://procore.github.io/globalization-toolkit/classes/DateTimeFormatter.html#getStartDayOfTheWeek)
|
|
314
|
+
This function returns an index value representing the start day of the week on the locale's calendar { Sunday: 0, Monday: 1 ...etc }
|
|
315
|
+
|
|
316
|
+
```ts
|
|
317
|
+
const dateTimeOptions = { locale: 'en-US' };
|
|
318
|
+
const formatter = new DateTimeFormatter(dateTimeOptions);
|
|
14
319
|
|
|
320
|
+
console.log(formatter.startDayOfTheWeek()); // expected output: 0
|
|
321
|
+
```
|
|
322
|
+
### DateTimeFormatter Functions
|
|
323
|
+
The functions used on the NumberFormatter class are also available as stand alone functions. Read more information about each function in the GTK Documentation.
|
|
324
|
+
- [formatDateTime()](https://procore.github.io/globalization-toolkit/functions/formatDateTime.html)
|
|
325
|
+
- [formaDateTimeToParts()](https://procore.github.io/globalization-toolkit/functions/formatDateTimeToParts.html)
|
|
326
|
+
- [getStartDayOfTheWeek(locale: string)](https://procore.github.io/globalization-toolkit/functions/getStartDayOfTheWeek.html)
|
|
327
|
+
|
|
328
|
+
The example below shows usage of the stand alone function: `formatDateTime()`.
|
|
329
|
+
```ts
|
|
330
|
+
import { formatDateTime } from '@procore/globalization-toolkit';
|
|
331
|
+
|
|
332
|
+
const value = new Date(Date.UTC(1955, 10, 5, 6, 0, 16, 738));
|
|
333
|
+
const dateTimeOptions = { locale: "en-US", timeZone: "UTC" };
|
|
334
|
+
|
|
335
|
+
console.log(formatDateTime(value, dateTimeOptions)) // expected output: 11/5/1955
|
|
336
|
+
```
|
|
337
|
+
____
|
|
15
338
|
# Developing
|
|
16
339
|
|
|
17
340
|
Install dependencies:
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export { NumberFormatter, formatNumber, formatNumberToParts, parseNumber, getNumberSeparators, } from "./numbers/NumberFormatter";
|
|
2
|
-
export { CurrencyFormatter, formatCurrency, formatCurrencyToParts, parseCurrency, } from "./numbers/CurrencyFormatter";
|
|
2
|
+
export { CurrencyFormatter, formatCurrency, formatCurrencyToParts, parseCurrency, getCurrencySeparators, } from "./numbers/CurrencyFormatter";
|
|
3
3
|
export { DateTimeFormatter, formatDateTime, formatDateTimeToParts, getStartDayOfTheWeek, } from "./dateTimes/DateTimeFormatter";
|
|
4
4
|
export type { Percent, NumberOptions, NumberFormatPart, Separators, } from "./numbers/Number.types";
|
|
5
5
|
export type { CurrencyDisplay, CurrencySign, CurrencyOptions, } from "./numbers/Currency.types";
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getStartDayOfTheWeek = exports.formatDateTimeToParts = exports.formatDateTime = exports.DateTimeFormatter = exports.parseCurrency = exports.formatCurrencyToParts = exports.formatCurrency = exports.CurrencyFormatter = exports.getNumberSeparators = exports.parseNumber = exports.formatNumberToParts = exports.formatNumber = exports.NumberFormatter = void 0;
|
|
3
|
+
exports.getStartDayOfTheWeek = exports.formatDateTimeToParts = exports.formatDateTime = exports.DateTimeFormatter = exports.getCurrencySeparators = exports.parseCurrency = exports.formatCurrencyToParts = exports.formatCurrency = exports.CurrencyFormatter = exports.getNumberSeparators = exports.parseNumber = exports.formatNumberToParts = exports.formatNumber = exports.NumberFormatter = void 0;
|
|
4
4
|
var NumberFormatter_1 = require("./numbers/NumberFormatter");
|
|
5
5
|
Object.defineProperty(exports, "NumberFormatter", { enumerable: true, get: function () { return NumberFormatter_1.NumberFormatter; } });
|
|
6
6
|
Object.defineProperty(exports, "formatNumber", { enumerable: true, get: function () { return NumberFormatter_1.formatNumber; } });
|
|
@@ -12,6 +12,7 @@ Object.defineProperty(exports, "CurrencyFormatter", { enumerable: true, get: fun
|
|
|
12
12
|
Object.defineProperty(exports, "formatCurrency", { enumerable: true, get: function () { return CurrencyFormatter_1.formatCurrency; } });
|
|
13
13
|
Object.defineProperty(exports, "formatCurrencyToParts", { enumerable: true, get: function () { return CurrencyFormatter_1.formatCurrencyToParts; } });
|
|
14
14
|
Object.defineProperty(exports, "parseCurrency", { enumerable: true, get: function () { return CurrencyFormatter_1.parseCurrency; } });
|
|
15
|
+
Object.defineProperty(exports, "getCurrencySeparators", { enumerable: true, get: function () { return CurrencyFormatter_1.getCurrencySeparators; } });
|
|
15
16
|
var DateTimeFormatter_1 = require("./dateTimes/DateTimeFormatter");
|
|
16
17
|
Object.defineProperty(exports, "DateTimeFormatter", { enumerable: true, get: function () { return DateTimeFormatter_1.DateTimeFormatter; } });
|
|
17
18
|
Object.defineProperty(exports, "formatDateTime", { enumerable: true, get: function () { return DateTimeFormatter_1.formatDateTime; } });
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,6DAMmC;AALjC,kHAAA,eAAe,OAAA;AACf,+GAAA,YAAY,OAAA;AACZ,sHAAA,mBAAmB,OAAA;AACnB,8GAAA,WAAW,OAAA;AACX,sHAAA,mBAAmB,OAAA;AAErB,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,6DAMmC;AALjC,kHAAA,eAAe,OAAA;AACf,+GAAA,YAAY,OAAA;AACZ,sHAAA,mBAAmB,OAAA;AACnB,8GAAA,WAAW,OAAA;AACX,sHAAA,mBAAmB,OAAA;AAErB,iEAMqC;AALnC,sHAAA,iBAAiB,OAAA;AACjB,mHAAA,cAAc,OAAA;AACd,0HAAA,qBAAqB,OAAA;AACrB,kHAAA,aAAa,OAAA;AACb,0HAAA,qBAAqB,OAAA;AAEvB,mEAKuC;AAJrC,sHAAA,iBAAiB,OAAA;AACjB,mHAAA,cAAc,OAAA;AACd,0HAAA,qBAAqB,OAAA;AACrB,yHAAA,oBAAoB,OAAA"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { NumberFormatPart } from "./Number.types";
|
|
1
|
+
import { NumberFormatPart, Separators } from "./Number.types";
|
|
2
2
|
import { CurrencyOptions } from "./Currency.types";
|
|
3
3
|
export declare class CurrencyFormatter {
|
|
4
4
|
formatter: Intl.NumberFormat;
|
|
@@ -6,7 +6,9 @@ export declare class CurrencyFormatter {
|
|
|
6
6
|
formatCurrency(value: number | string): string;
|
|
7
7
|
formatCurrencyToParts(value: number): NumberFormatPart[];
|
|
8
8
|
parseCurrency(value: string): number;
|
|
9
|
+
getCurrencySeparators(): Separators;
|
|
9
10
|
}
|
|
10
11
|
export declare function formatCurrency(value: number | string, currencyOptions: CurrencyOptions): string;
|
|
11
12
|
export declare function formatCurrencyToParts(value: number, currencyOptions: CurrencyOptions): NumberFormatPart[];
|
|
12
13
|
export declare function parseCurrency(value: string, currencyOptions: CurrencyOptions): number;
|
|
14
|
+
export declare function getCurrencySeparators(currencyOptions: CurrencyOptions): Separators;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.parseCurrency = exports.formatCurrencyToParts = exports.formatCurrency = exports.CurrencyFormatter = void 0;
|
|
3
|
+
exports.getCurrencySeparators = exports.parseCurrency = exports.formatCurrencyToParts = exports.formatCurrency = exports.CurrencyFormatter = void 0;
|
|
4
4
|
const CurrencyUtils_1 = require("./CurrencyUtils");
|
|
5
5
|
const FormatterUtils_1 = require("../FormatterUtils");
|
|
6
6
|
const NumberUtils_1 = require("./NumberUtils");
|
|
@@ -39,6 +39,13 @@ class CurrencyFormatter {
|
|
|
39
39
|
.trim();
|
|
40
40
|
return parseFloat(converted) * sign;
|
|
41
41
|
}
|
|
42
|
+
getCurrencySeparators() {
|
|
43
|
+
const parts = this.formatCurrencyToParts(1e9 + 0.11);
|
|
44
|
+
return {
|
|
45
|
+
decimal: parts.find(({ type }) => type === "decimal")?.value,
|
|
46
|
+
group: parts.find(({ type }) => type === "group").value,
|
|
47
|
+
};
|
|
48
|
+
}
|
|
42
49
|
}
|
|
43
50
|
exports.CurrencyFormatter = CurrencyFormatter;
|
|
44
51
|
function formatCurrency(value, currencyOptions) {
|
|
@@ -56,4 +63,9 @@ function parseCurrency(value, currencyOptions) {
|
|
|
56
63
|
return formatter.parseCurrency(value);
|
|
57
64
|
}
|
|
58
65
|
exports.parseCurrency = parseCurrency;
|
|
66
|
+
function getCurrencySeparators(currencyOptions) {
|
|
67
|
+
const formatter = new CurrencyFormatter(currencyOptions);
|
|
68
|
+
return formatter.getCurrencySeparators();
|
|
69
|
+
}
|
|
70
|
+
exports.getCurrencySeparators = getCurrencySeparators;
|
|
59
71
|
//# sourceMappingURL=CurrencyFormatter.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"CurrencyFormatter.js","sourceRoot":"","sources":["../../src/numbers/CurrencyFormatter.ts"],"names":[],"mappings":";;;AAGA,mDA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I,CAAC,CAAC,EAAE,IAAI,EAAE,EAAE,EAAE,CAAC,IAAI,KAAK,OAAO,CAAE,CAAC,KAAK;SACzD,CAAC;IACJ,CAAC;CACF;AAxFD,8CAwFC;AASD,SAAgB,cAAc,CAC5B,KAAsB,EACtB,eAAgC;IAEhC,MAAM,SAAS,GAAG,IAAI,iBAAiB,CAAC,eAAe,CAAC,CAAC;IACzD,OAAO,SAAS,CAAC,cAAc,CAAC,KAAK,CAAC,CAAC;AACzC,CAAC;AAND,wCAMC;AASD,SAAgB,qBAAqB,CACnC,KAAa,EACb,eAAgC;IAEhC,MAAM,SAAS,GAAG,IAAI,iBAAiB,CAAC,eAAe,CAAC,CAAC;IACzD,OAAO,SAAS,CAAC,qBAAqB,CAAC,KAAK,CAAC,CAAC;AAChD,CAAC;AAND,sDAMC;AASD,SAAgB,aAAa,CAC3B,KAAa,EACb,eAAgC;IAEhC,MAAM,SAAS,GAAG,IAAI,iBAAiB,CAAC,eAAe,CAAC,CAAC;IACzD,OAAO,SAAS,CAAC,aAAa,CAAC,KAAK,CAAC,CAAC;AACxC,CAAC;AAND,sCAMC;AAQD,SAAgB,qBAAqB,CACnC,eAAgC;IAEhC,MAAM,SAAS,GAAG,IAAI,iBAAiB,CAAC,eAAe,CAAC,CAAC;IACzD,OAAO,SAAS,CAAC,qBAAqB,EAAE,CAAC;AAC3C,CAAC;AALD,sDAKC"}
|
|
@@ -13,7 +13,7 @@ class NumberFormatter {
|
|
|
13
13
|
return (0, NumberUtils_1.formatNumberOrCurrency)(value, this.formatter, this.percent);
|
|
14
14
|
}
|
|
15
15
|
formatNumberToParts(value) {
|
|
16
|
-
return this.formatter.formatToParts((0, NumberUtils_1.transformPercentValue)(value, this.percent));
|
|
16
|
+
return this.formatter.formatToParts(Number((0, NumberUtils_1.transformPercentValue)(value, this.percent)));
|
|
17
17
|
}
|
|
18
18
|
parseNumber(value) {
|
|
19
19
|
const control = this.formatNumberToParts(1e9 + 0.111);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"NumberFormatter.js","sourceRoot":"","sources":["../../src/numbers/NumberFormatter.ts"],"names":[],"mappings":";;;AACA,+CAIuB;AACvB,iDAKwB;AAExB,sDAAgD;AAEhD,MAAa,eAAe;IAO1B,YAAY,aAA4B;QACtC,IAAI,CAAC,SAAS,GAAG,IAAA,gCAAkB,EAAC,aAAa,CAAC,CAAC;QACnD,IAAI,CAAC,OAAO,GAAG,aAAa,EAAE,OAAO,CAAC;IACxC,CAAC;IAOD,YAAY,CAAC,KAAsB;QACjC,OAAO,IAAA,oCAAsB,EAAC,KAAK,EAAE,IAAI,CAAC,SAAS,EAAE,IAAI,CAAC,OAAO,CAAC,CAAC;IACrE,CAAC;IAOD,mBAAmB,CAAC,KAAa;QAC/B,OAAO,IAAI,CAAC,SAAS,CAAC,aAAa,CACjC,IAAA,mCAAqB,EAAC,KAAK,EAAE,IAAI,CAAC,OAAO,CAAC,
|
|
1
|
+
{"version":3,"file":"NumberFormatter.js","sourceRoot":"","sources":["../../src/numbers/NumberFormatter.ts"],"names":[],"mappings":";;;AACA,+CAIuB;AACvB,iDAKwB;AAExB,sDAAgD;AAEhD,MAAa,eAAe;IAO1B,YAAY,aAA4B;QACtC,IAAI,CAAC,SAAS,GAAG,IAAA,gCAAkB,EAAC,aAAa,CAAC,CAAC;QACnD,IAAI,CAAC,OAAO,GAAG,aAAa,EAAE,OAAO,CAAC;IACxC,CAAC;IAOD,YAAY,CAAC,KAAsB;QACjC,OAAO,IAAA,oCAAsB,EAAC,KAAK,EAAE,IAAI,CAAC,SAAS,EAAE,IAAI,CAAC,OAAO,CAAC,CAAC;IACrE,CAAC;IAOD,mBAAmB,CAAC,KAAa;QAC/B,OAAO,IAAI,CAAC,SAAS,CAAC,aAAa,CACjC,MAAM,CAAC,IAAA,mCAAqB,EAAC,KAAK,EAAE,IAAI,CAAC,OAAO,CAAC,CAAC,CACnD,CAAC;IACJ,CAAC;IAOD,WAAW,CAAC,KAAa;QACvB,MAA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C,IAAI,KAAK,OAAO,CAAE,CAAC,KAAK;SAC1D,CAAC;IACJ,CAAC;CACF;AAnED,0CAmEC;AASD,SAAgB,YAAY,CAC1B,KAAsB,EACtB,aAA4B;IAE5B,MAAM,SAAS,GAAG,IAAI,eAAe,CAAC,aAAa,CAAC,CAAC;IACrD,OAAO,SAAS,CAAC,YAAY,CAAC,KAAK,CAAC,CAAC;AACvC,CAAC;AAND,oCAMC;AASD,SAAgB,mBAAmB,CACjC,KAAa,EACb,aAA4B;IAE5B,MAAM,SAAS,GAAG,IAAI,eAAe,CAAC,aAAa,CAAC,CAAC;IACrD,OAAO,SAAS,CAAC,mBAAmB,CAAC,KAAK,CAAC,CAAC;AAC9C,CAAC;AAND,kDAMC;AASD,SAAgB,WAAW,CACzB,KAAa,EACb,aAA4B;IAE5B,MAAM,SAAS,GAAG,IAAI,eAAe,CAAC,aAAa,CAAC,CAAC;IACrD,OAAO,SAAS,CAAC,WAAW,CAAC,KAAK,CAAC,CAAC;AACtC,CAAC;AAND,kCAMC;AAOD,SAAgB,mBAAmB,CAAC,MAAc;IAChD,MAAM,SAAS,GAAG,IAAI,eAAe,CAAC,EAAE,MAAM,EAAE,CAAC,CAAC;IAClD,OAAO,SAAS,CAAC,mBAAmB,EAAE,CAAC;AACzC,CAAC;AAHD,kDAGC"}
|
|
@@ -1,14 +1,16 @@
|
|
|
1
1
|
import { NumberOptions, NumberOptionsBasic, Percent } from "./Number.types";
|
|
2
|
+
export declare const MAXIMUM_PRECISE_DIGITS = 15;
|
|
2
3
|
export declare function getNumberFormatter(options: NumberOptions): Intl.NumberFormat;
|
|
3
4
|
export declare function transformOptions(options: NumberOptions): Intl.NumberFormatOptions;
|
|
4
5
|
export declare function transformNumberOptionsBasic(options: NumberOptionsBasic): Intl.NumberFormatOptions;
|
|
5
6
|
export declare function transformNumberOptions(options: NumberOptions): Intl.NumberFormatOptions;
|
|
6
|
-
export declare function transformPercentValue(value: number, percent?: Percent): number;
|
|
7
|
+
export declare function transformPercentValue(value: number | string, percent?: Percent): number | string;
|
|
7
8
|
export declare function validateOptions(options: NumberOptions): NumberOptions;
|
|
8
9
|
export declare function validateNumberOptionsBasic(options: NumberOptionsBasic): NumberOptionsBasic;
|
|
9
10
|
export declare function validateFractionDigits(options: NumberOptionsBasic): NumberOptionsBasic;
|
|
10
11
|
export declare function validateFractionDigitValue(value: number, minMax: "minimum" | "maximum"): number;
|
|
11
12
|
export declare function validateNumberString(value: string): string;
|
|
12
13
|
export declare function getDecimalSeparator(formatter: Intl.NumberFormat): string | undefined;
|
|
14
|
+
export declare function isBigNumberString(value: number | string): boolean;
|
|
13
15
|
export declare function formatBigNumber(value: string, formatter: Intl.NumberFormat): string;
|
|
14
16
|
export declare function formatNumberOrCurrency(value: number | string, formatter: Intl.NumberFormat, percent?: Percent): string;
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.formatNumberOrCurrency = exports.formatBigNumber = exports.getDecimalSeparator = exports.validateNumberString = exports.validateFractionDigitValue = exports.validateFractionDigits = exports.validateNumberOptionsBasic = exports.validateOptions = exports.transformPercentValue = exports.transformNumberOptions = exports.transformNumberOptionsBasic = exports.transformOptions = exports.getNumberFormatter = void 0;
|
|
3
|
+
exports.formatNumberOrCurrency = exports.formatBigNumber = exports.isBigNumberString = exports.getDecimalSeparator = exports.validateNumberString = exports.validateFractionDigitValue = exports.validateFractionDigits = exports.validateNumberOptionsBasic = exports.validateOptions = exports.transformPercentValue = exports.transformNumberOptions = exports.transformNumberOptionsBasic = exports.transformOptions = exports.getNumberFormatter = exports.MAXIMUM_PRECISE_DIGITS = void 0;
|
|
4
4
|
const Number_types_1 = require("./Number.types");
|
|
5
5
|
const FormatterUtils_1 = require("../FormatterUtils");
|
|
6
6
|
const big_js_1 = require("big.js");
|
|
7
|
+
exports.MAXIMUM_PRECISE_DIGITS = 15;
|
|
7
8
|
function getNumberFormatter(options) {
|
|
8
9
|
exports.validateOptions(options);
|
|
9
10
|
const intlOptions = exports.transformOptions(options);
|
|
@@ -32,7 +33,14 @@ function transformNumberOptions(options) {
|
|
|
32
33
|
}
|
|
33
34
|
exports.transformNumberOptions = transformNumberOptions;
|
|
34
35
|
function transformPercentValue(value, percent) {
|
|
35
|
-
|
|
36
|
+
const hundredth = (value) => {
|
|
37
|
+
return exports.isBigNumberString(value)
|
|
38
|
+
? new big_js_1.Big(value).div(100).toString()
|
|
39
|
+
: Number(value) / 100;
|
|
40
|
+
};
|
|
41
|
+
return percent === Number_types_1.Percent.STANDARD || percent === true
|
|
42
|
+
? hundredth(value)
|
|
43
|
+
: value;
|
|
36
44
|
}
|
|
37
45
|
exports.transformPercentValue = transformPercentValue;
|
|
38
46
|
function validateOptions(options) {
|
|
@@ -84,6 +92,11 @@ function getDecimalSeparator(formatter) {
|
|
|
84
92
|
?.value;
|
|
85
93
|
}
|
|
86
94
|
exports.getDecimalSeparator = getDecimalSeparator;
|
|
95
|
+
function isBigNumberString(value) {
|
|
96
|
+
return (typeof value === "string" &&
|
|
97
|
+
value.replace(".", "").length > exports.MAXIMUM_PRECISE_DIGITS);
|
|
98
|
+
}
|
|
99
|
+
exports.isBigNumberString = isBigNumberString;
|
|
87
100
|
function formatBigNumber(value, formatter) {
|
|
88
101
|
const fixed = new big_js_1.Big(value).toFixed(formatter.resolvedOptions().maximumFractionDigits);
|
|
89
102
|
const parts = fixed.split(".");
|
|
@@ -112,13 +125,11 @@ function formatBigNumber(value, formatter) {
|
|
|
112
125
|
}
|
|
113
126
|
exports.formatBigNumber = formatBigNumber;
|
|
114
127
|
function formatNumberOrCurrency(value, formatter, percent) {
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
}
|
|
121
|
-
return formatter.format(exports.transformPercentValue(typeof value === "string" ? parseFloat(value) : value, percent));
|
|
128
|
+
typeof value === "string" && exports.validateNumberString(value);
|
|
129
|
+
const transformed = exports.transformPercentValue(value, percent);
|
|
130
|
+
return exports.isBigNumberString(value)
|
|
131
|
+
? exports.formatBigNumber(transformed, formatter)
|
|
132
|
+
: formatter.format(Number(transformed));
|
|
122
133
|
}
|
|
123
134
|
exports.formatNumberOrCurrency = formatNumberOrCurrency;
|
|
124
135
|
//# sourceMappingURL=NumberUtils.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"NumberUtils.js","sourceRoot":"","sources":["../../src/numbers/NumberUtils.ts"],"names":[],"mappings":";;;AAAA,iDAA4E;AAC5E,sDAAmD;AACnD,mCAA6B;
|
|
1
|
+
{"version":3,"file":"NumberUtils.js","sourceRoot":"","sources":["../../src/numbers/NumberUtils.ts"],"names":[],"mappings":";;;AAAA,iDAA4E;AAC5E,sDAAmD;AACnD,mCAA6B;AAEhB,QAAA,sBAAsB,GAAG,EAAE,CAAC;AAEzC,SAAgB,kBAAkB,CAAC,OAAsB;IACvD,OAAO,CAAC,eAAe,CAAC,OAAO,CAAC,CAAC;IACjC,MAAM,WAAW,GACf,OAAO,CAAC,gBAAgB,CAAC,OAAO,CAAC,CAAC;IAEpC,OAAO,IAAI,IAAI,CAAC,YAAY,CAAC,OAAO,CAAC,MAAM,EAAE,WAAW,CAAC,CAAC;AAC5D,CAAC;AAND,gDAMC;AAED,SAAgB,gBAAgB,CAC9B,OAAsB;IAEtB,OAAO;QACL,GAAG,OAAO,CAAC,2BAA2B,CAAC,OAAO,CAAC;QAC/C,GAAG,OAAO,CAAC,sBAAsB,CAAC,OAAO,CAAC;KAC3C,CAAC;AACJ,CAAC;AAPD,4CAOC;AAED,SAAgB,2BAA2B,CACzC,OAA2B;IAE3B,MAAM,mBAAmB,GAA6B,EAAE,CAAC;IAEzD,mBAAmB,CAAC,qBAAqB,GAAG,OAAO,CAAC,qBAAqB,CAAC;IAC1E,mBAAmB,CAAC,qBAAqB,GAAG,OAAO,CAAC,qBAAqB,CAAC;IAE1E,OAAO,mBAAmB,CAAC;AAC7B,C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