@uxf/localize 10.0.0-beta.25 → 10.0.0-beta.30
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 +111 -17
- package/locale/cs.d.ts +2 -2
- package/locale/de.d.ts +2 -2
- package/locale/en.d.ts +2 -2
- package/locale/es.d.ts +2 -2
- package/locale/fr.d.ts +2 -2
- package/locale/pl.d.ts +2 -2
- package/locale/sk.d.ts +2 -2
- package/package.json +1 -1
- package/src/context/context.d.ts +2 -2
- package/src/format-datetime/format-datetime.d.ts +3 -3
- package/src/format-datetime/format-datetime.js +16 -11
- package/src/format-datetime/format-datetime.test.js +1 -1
- package/src/format-money/format-money.d.ts +3 -3
- package/src/format-money/format-money.js +23 -18
- package/src/format-money/format-money.test.js +1 -1
- package/src/format-number/format-number.d.ts +3 -3
- package/src/format-number/format-number.js +18 -13
- package/src/format-number/format-number.test.js +1 -1
- package/src/format-percentage/format-percentage.d.ts +3 -3
- package/src/format-percentage/format-percentage.js +17 -12
- package/src/format-percentage/format-percentage.test.js +1 -1
- package/src/index.d.ts +2 -2
- package/src/index.js +10 -10
- package/src/types.d.ts +6 -6
- package/src/utils/map-options.d.ts +2 -2
package/README.md
CHANGED
|
@@ -14,14 +14,15 @@ yarn add @uxf/localize
|
|
|
14
14
|
npm install @uxf/localize
|
|
15
15
|
```
|
|
16
16
|
|
|
17
|
-
##
|
|
17
|
+
## Initialize and configure package
|
|
18
18
|
|
|
19
|
-
###
|
|
19
|
+
### Configure package
|
|
20
20
|
|
|
21
21
|
```ts
|
|
22
|
+
// localize.ts
|
|
22
23
|
import { createLocalize } from "@uxf/localize";
|
|
23
|
-
import cs from "@uxf/localize/locale/cs";
|
|
24
|
-
import en from "@uxf/localize/locale/en";
|
|
24
|
+
import cs from "@uxf/localize/locale/cs"; // LocalizeConfig
|
|
25
|
+
import en from "@uxf/localize/locale/en"; // LocalizeConfig
|
|
25
26
|
|
|
26
27
|
export const {
|
|
27
28
|
LocalizeProvider,
|
|
@@ -36,31 +37,124 @@ export const {
|
|
|
36
37
|
} = createLocale({ cs, en });
|
|
37
38
|
```
|
|
38
39
|
|
|
39
|
-
###
|
|
40
|
+
### Localize config examples
|
|
40
41
|
|
|
41
|
-
|
|
42
|
+
```ts
|
|
43
|
+
import {DateTimes, LocalizeConfig} from "@uxf/localize";
|
|
44
|
+
|
|
45
|
+
const en: LocalizeConfig = {
|
|
46
|
+
number: {
|
|
47
|
+
thousandsSeparator: ",",
|
|
48
|
+
decimalSeparator: ".",
|
|
49
|
+
},
|
|
50
|
+
currency: {
|
|
51
|
+
thousandsSeparator: ",",
|
|
52
|
+
decimalSeparator: ".",
|
|
53
|
+
pattern: "!#",
|
|
54
|
+
negativePattern: "-!#",
|
|
55
|
+
},
|
|
56
|
+
dateTime: {
|
|
57
|
+
timeShort: "H:mm A",
|
|
58
|
+
timeFull: "H:mm:ss A",
|
|
59
|
+
dateShort: "M/D/YY",
|
|
60
|
+
dateMedium: "M/D/YYYY",
|
|
61
|
+
dateLong: "MMMM D. YYYY",
|
|
62
|
+
dateShortNoYear: "M/D",
|
|
63
|
+
dateLongNoYear: "MMMM D.",
|
|
64
|
+
dateTimeShort: "M/D/YY H:mm A",
|
|
65
|
+
dateTimeMedium: "M/D/YYYY H:mm A",
|
|
66
|
+
dateTimeLong: "MMMM D. YYYY H:mm:ss A",
|
|
67
|
+
},
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
const cs: LocalizeConfig<DateTimes> = {
|
|
71
|
+
number: {
|
|
72
|
+
thousandsSeparator: "\xa0",
|
|
73
|
+
decimalSeparator: ",",
|
|
74
|
+
},
|
|
75
|
+
currency: {
|
|
76
|
+
thousandsSeparator: "\xa0",
|
|
77
|
+
decimalSeparator: ",",
|
|
78
|
+
pattern: "#\xa0!",
|
|
79
|
+
negativePattern: "-#\xa0!",
|
|
80
|
+
},
|
|
81
|
+
dateTime: {
|
|
82
|
+
timeShort: "H:mm",
|
|
83
|
+
timeFull: "H:mm:ss",
|
|
84
|
+
dateShort: "D. M. YY",
|
|
85
|
+
dateMedium: "D. M. YYYY",
|
|
86
|
+
dateLong: "D. MMMM YYYY",
|
|
87
|
+
dateShortNoYear: "D. M.",
|
|
88
|
+
dateLongNoYear: "D. MMMM",
|
|
89
|
+
dateTimeShort: "D. M. YY, H:mm",
|
|
90
|
+
dateTimeMedium: "D. M. YYYY, H:mm",
|
|
91
|
+
dateTimeLong: "D. MMMM YYYY, H:mm:ss",
|
|
92
|
+
},
|
|
93
|
+
};
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
### Add LocalizeProvider to _app.tsx
|
|
42
97
|
|
|
43
98
|
```tsx
|
|
44
|
-
|
|
99
|
+
// _app.tsx
|
|
100
|
+
|
|
101
|
+
<LocalizeProvider locale="cs">{props.children}</LocalizeProvider>
|
|
45
102
|
```
|
|
46
103
|
|
|
47
|
-
|
|
104
|
+
## Format number
|
|
105
|
+
```tsx
|
|
106
|
+
const { useFormatNumber } from "./localize";
|
|
48
107
|
|
|
49
|
-
|
|
50
|
-
|
|
108
|
+
const formatNumber = useFormatNumber();
|
|
109
|
+
|
|
110
|
+
formatNumber(1000.23); // 1 000
|
|
111
|
+
formatNumber(1000.23, { precision: 2 }); // 1 000,23
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
## Format datetime
|
|
115
|
+
```tsx
|
|
116
|
+
const { useFormatDateTime } from "./localize";
|
|
117
|
+
|
|
118
|
+
const formatDateTime = useFormatDateTime();
|
|
119
|
+
|
|
120
|
+
formatDateTime(new Date('2000-01-01'), "dateShort") // 1. 1. 20
|
|
121
|
+
formatDateTime(new Date('2000-01-01'), "dateTimeMedium") // 1. 1. 2000 00:00
|
|
122
|
+
formatDateTime(new Date('2000-01-01'), "timeFull") // 00:00:00
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
## Format money
|
|
126
|
+
```tsx
|
|
127
|
+
const { useFormatMoney } from "./localize";
|
|
51
128
|
|
|
52
129
|
const formatMoney = useFormatMoney();
|
|
53
130
|
|
|
54
|
-
formatMoney(
|
|
131
|
+
formatMoney(1000, "CZK") // 1 000 Kč
|
|
132
|
+
formatMoney(1000, "USD") // 1 000 $
|
|
133
|
+
formatMoney(1000.23, "CZK", { precision: 1 }) // 1 000,2 Kč
|
|
134
|
+
formatMoney(1000.23, "CZK", { preferIsoCode: true }) // 1 000,23 CZK
|
|
135
|
+
formatMoney(1000.23, "CZK", { hideSymbol: true }) // 1 000,23
|
|
55
136
|
```
|
|
56
137
|
|
|
57
|
-
|
|
138
|
+
## Format percentage
|
|
58
139
|
|
|
59
|
-
```
|
|
60
|
-
|
|
140
|
+
```tsx
|
|
141
|
+
const { useFormatPercentage } from "./localize";
|
|
61
142
|
|
|
62
|
-
|
|
63
|
-
|
|
143
|
+
const formatPercentage = useFormatPercentage();
|
|
144
|
+
|
|
145
|
+
formatPercentage(0.782) // 78,2 %
|
|
146
|
+
formatPercentage(0.782, "nearest") // 78,2 %
|
|
147
|
+
formatPercentage(0.782, "up") // 78,2 %
|
|
148
|
+
formatPercentage(0.782, "down") // 78,2 %
|
|
64
149
|
```
|
|
65
150
|
|
|
66
|
-
|
|
151
|
+
## Format another locale
|
|
152
|
+
|
|
153
|
+
```tsx
|
|
154
|
+
const { formatNumber, formatDateTime, formatMoney, formatPercentage } from "./localize";
|
|
155
|
+
|
|
156
|
+
formatNumber("cs", 1000.23); // 1 000
|
|
157
|
+
formatDateTime("cs", new Date('2000-01-01'), "dateMedium") // 1. 1. 2000
|
|
158
|
+
formatMoney("cs", 1000, "CZK") // 1 000 Kč
|
|
159
|
+
formatPercentage("cs", 0.782) // 78,2 %
|
|
160
|
+
```
|
package/locale/cs.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import { LocalizeConfig } from "../src/types";
|
|
2
|
-
declare const locale: LocalizeConfig
|
|
1
|
+
import { DateTimes, LocalizeConfig } from "../src/types";
|
|
2
|
+
declare const locale: LocalizeConfig<DateTimes>;
|
|
3
3
|
export default locale;
|
package/locale/de.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import { LocalizeConfig } from "../src/types";
|
|
2
|
-
declare const locale: LocalizeConfig
|
|
1
|
+
import { DateTimes, LocalizeConfig } from "../src/types";
|
|
2
|
+
declare const locale: LocalizeConfig<DateTimes>;
|
|
3
3
|
export default locale;
|
package/locale/en.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import { LocalizeConfig } from "../src/types";
|
|
2
|
-
declare const locale: LocalizeConfig
|
|
1
|
+
import { DateTimes, LocalizeConfig } from "../src/types";
|
|
2
|
+
declare const locale: LocalizeConfig<DateTimes>;
|
|
3
3
|
export default locale;
|
package/locale/es.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import { LocalizeConfig } from "../src/types";
|
|
2
|
-
declare const locale: LocalizeConfig
|
|
1
|
+
import { DateTimes, LocalizeConfig } from "../src/types";
|
|
2
|
+
declare const locale: LocalizeConfig<DateTimes>;
|
|
3
3
|
export default locale;
|
package/locale/fr.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import { LocalizeConfig } from "../src/types";
|
|
2
|
-
declare const locale: LocalizeConfig
|
|
1
|
+
import { DateTimes, LocalizeConfig } from "../src/types";
|
|
2
|
+
declare const locale: LocalizeConfig<DateTimes>;
|
|
3
3
|
export default locale;
|
package/locale/pl.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import { LocalizeConfig } from "../src/types";
|
|
2
|
-
declare const locale: LocalizeConfig
|
|
1
|
+
import { DateTimes, LocalizeConfig } from "../src/types";
|
|
2
|
+
declare const locale: LocalizeConfig<DateTimes>;
|
|
3
3
|
export default locale;
|
package/locale/sk.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import { LocalizeConfig } from "../src/types";
|
|
2
|
-
declare const locale: LocalizeConfig
|
|
1
|
+
import { DateTimes, LocalizeConfig } from "../src/types";
|
|
2
|
+
declare const locale: LocalizeConfig<DateTimes>;
|
|
3
3
|
export default locale;
|
package/package.json
CHANGED
package/src/context/context.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/// <reference types="react" />
|
|
2
|
-
import {
|
|
2
|
+
import { LocalizeConfigMap, DateTimes } from "../types";
|
|
3
3
|
export declare const _useLocalizeContext: () => string;
|
|
4
4
|
export declare const _LocalizeProvider: import("react").Provider<string>;
|
|
5
|
-
export declare const getLocaleConfigHook: <DT extends
|
|
5
|
+
export declare const getLocaleConfigHook: <DT extends DateTimes, Locales extends string>(allConfigurations: LocalizeConfigMap<DT, Locales>) => () => LocalizeConfigMap<DT, Locales>[Locales];
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import { DateTimes, FormatDatetimeFunction,
|
|
2
|
-
export declare
|
|
3
|
-
export declare
|
|
1
|
+
import { DateTimes, FormatDatetimeFunction, LocalizeConfigMap } from "../types";
|
|
2
|
+
export declare function createFormatDatetime<DT extends DateTimes, Locales extends string>(localizeConfigs: LocalizeConfigMap<DT, Locales>): FormatDatetimeFunction<DT, Locales>;
|
|
3
|
+
export declare function createUseFormatDatetime<DT extends DateTimes, Locales extends string>(localizeConfigs: LocalizeConfigMap<DT, Locales>): () => (value: Date, format: DT) => string;
|
|
@@ -3,17 +3,22 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.
|
|
6
|
+
exports.createUseFormatDatetime = exports.createFormatDatetime = void 0;
|
|
7
7
|
const context_1 = require("../context/context");
|
|
8
8
|
const curry_1 = require("../utils/curry");
|
|
9
9
|
const dayjs_1 = __importDefault(require("dayjs"));
|
|
10
|
-
const
|
|
11
|
-
|
|
12
|
-
return (
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
10
|
+
const react_1 = require("react");
|
|
11
|
+
function createFormatDatetime(localizeConfigs) {
|
|
12
|
+
return (locale, value, format) => {
|
|
13
|
+
const localeDateTimes = localizeConfigs[locale].dateTime;
|
|
14
|
+
return (0, dayjs_1.default)(value).format(localeDateTimes[format]);
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
exports.createFormatDatetime = createFormatDatetime;
|
|
18
|
+
function createUseFormatDatetime(localizeConfigs) {
|
|
19
|
+
return () => {
|
|
20
|
+
const locale = (0, context_1._useLocalizeContext)();
|
|
21
|
+
return (0, react_1.useMemo)(() => (0, curry_1._curry)(createFormatDatetime(localizeConfigs))(locale), [locale]);
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
exports.createUseFormatDatetime = createUseFormatDatetime;
|
|
@@ -8,7 +8,7 @@ const cs_1 = __importDefault(require("../../locale/cs"));
|
|
|
8
8
|
const en_1 = __importDefault(require("../../locale/en"));
|
|
9
9
|
const date = new Date("2023-07-21T07:58:35");
|
|
10
10
|
describe("datetime formatter", function () {
|
|
11
|
-
const formatDatetimeWithLocales = (0, format_datetime_1.
|
|
11
|
+
const formatDatetimeWithLocales = (0, format_datetime_1.createFormatDatetime)({ cs: cs_1.default, en: en_1.default });
|
|
12
12
|
it("format datetime with 'en' locale", function () {
|
|
13
13
|
expect(formatDatetimeWithLocales("cs", date, "dateShort")).toBe("21. 7. 23");
|
|
14
14
|
expect(formatDatetimeWithLocales("cs", date, "dateMedium")).toBe("21. 7. 2023");
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import { DateTimes, FormatMoneyFunction,
|
|
2
|
-
export declare
|
|
3
|
-
export declare
|
|
1
|
+
import { DateTimes, FormatMoneyFunction, LocalizeConfigMap } from "../types";
|
|
2
|
+
export declare function createFormatMoney<DT extends DateTimes, Locales extends string>(localizeConfigs: LocalizeConfigMap<DT, Locales>): FormatMoneyFunction<Locales>;
|
|
3
|
+
export declare function createUseFormatMoney<DT extends DateTimes, Locales extends string>(localizeConfigs: LocalizeConfigMap<DT, Locales>): () => (value: number, currency: "CHF" | "AED" | "AFN" | "AMD" | "ANG" | "ARS" | "AUD" | "AWG" | "AZN" | "BDT" | "BGN" | "BHD" | "BIF" | "BRL" | "BTN" | "BYN" | "CAD" | "CLP" | "CNY" | "COP" | "CRC" | "CUP" | "CVE" | "CZK" | "DJF" | "DKK" | "EGP" | "ERN" | "EUR" | "FJD" | "GBP" | "GEL" | "GMD" | "GNF" | "HKD" | "HRK" | "HTG" | "HUF" | "IDR" | "INR" | "IQD" | "ISK" | "JPY" | "KGS" | "KHR" | "KMF" | "KRW" | "KWD" | "KZT" | "LKR" | "LRD" | "LSL" | "MDL" | "MMK" | "MNT" | "MRO" | "MWK" | "MXN" | "MYR" | "MZN" | "NGN" | "NOK" | "NPR" | "NZD" | "PEN" | "PGK" | "PHP" | "PKR" | "PLN" | "PYG" | "RON" | "RSD" | "RUB" | "SBD" | "SDG" | "SEK" | "SGD" | "SLL" | "SRD" | "SYP" | "SZL" | "THB" | "TJS" | "TMT" | "TRY" | "TZS" | "UGX" | "USD" | "UYU" | "UZS" | "VES" | "VND" | "VUV" | "WST" | "XOF" | "XPF" | "YER" | "ZAR" | "ZMW", options?: import("../types").FormatMoneyOptions | undefined) => string;
|
|
@@ -3,26 +3,31 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.
|
|
6
|
+
exports.createUseFormatMoney = exports.createFormatMoney = void 0;
|
|
7
7
|
const context_1 = require("../context/context");
|
|
8
8
|
const curry_1 = require("../utils/curry");
|
|
9
9
|
const currency_js_1 = __importDefault(require("currency.js"));
|
|
10
10
|
const map_options_1 = require("../utils/map-options");
|
|
11
11
|
const data_1 = require("../utils/data");
|
|
12
|
-
const
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
formatOptions
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
12
|
+
const react_1 = require("react");
|
|
13
|
+
function createFormatMoney(localizeConfigs) {
|
|
14
|
+
return (locale, value, currency, options) => {
|
|
15
|
+
var _a, _b, _c;
|
|
16
|
+
const precision = (_a = options === null || options === void 0 ? void 0 : options.precision) !== null && _a !== void 0 ? _a : 0;
|
|
17
|
+
const symbol = (options === null || options === void 0 ? void 0 : options.preferIsoCode) ? currency : data_1.CURRENCIES[currency].symbol;
|
|
18
|
+
const formatOptions = { ...(0, map_options_1._mapOptions)(localizeConfigs[locale].currency), precision, symbol };
|
|
19
|
+
if (options === null || options === void 0 ? void 0 : options.hideSymbol) {
|
|
20
|
+
formatOptions.pattern = (_b = formatOptions.pattern) === null || _b === void 0 ? void 0 : _b.replace("!", "").trim();
|
|
21
|
+
formatOptions.negativePattern = (_c = formatOptions.negativePattern) === null || _c === void 0 ? void 0 : _c.replace("!", "").trim();
|
|
22
|
+
}
|
|
23
|
+
return (0, currency_js_1.default)(value, { precision }).format(formatOptions);
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
exports.createFormatMoney = createFormatMoney;
|
|
27
|
+
function createUseFormatMoney(localizeConfigs) {
|
|
28
|
+
return () => {
|
|
29
|
+
const locale = (0, context_1._useLocalizeContext)();
|
|
30
|
+
return (0, react_1.useMemo)(() => (0, curry_1._curry)(createFormatMoney(localizeConfigs))(locale), [locale]);
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
exports.createUseFormatMoney = createUseFormatMoney;
|
|
@@ -7,7 +7,7 @@ const format_money_1 = require("./format-money");
|
|
|
7
7
|
const cs_1 = __importDefault(require("../../locale/cs"));
|
|
8
8
|
const en_1 = __importDefault(require("../../locale/en"));
|
|
9
9
|
describe("money formatter", function () {
|
|
10
|
-
const formatMoneyWithLocales = (0, format_money_1.
|
|
10
|
+
const formatMoneyWithLocales = (0, format_money_1.createFormatMoney)({ cs: cs_1.default, en: en_1.default });
|
|
11
11
|
it("format money with 'cs' locale", function () {
|
|
12
12
|
expect(formatMoneyWithLocales("cs", 2000.78, "CZK")).toBe("2\xa0001\xa0Kč");
|
|
13
13
|
expect(formatMoneyWithLocales("cs", 2000.78, "CZK", { precision: 1 })).toBe("2\xa0000,8\xa0Kč");
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import { DateTimes, FormatNumberFunction,
|
|
2
|
-
export declare
|
|
3
|
-
export declare
|
|
1
|
+
import { DateTimes, FormatNumberFunction, LocalizeConfigMap } from "../types";
|
|
2
|
+
export declare function createFormatNumber<DT extends DateTimes, Locales extends string>(localizeConfigs: LocalizeConfigMap<DT, Locales>): FormatNumberFunction<Locales>;
|
|
3
|
+
export declare function createUseFormatNumber<DT extends DateTimes, Locales extends string>(localizeConfigs: LocalizeConfigMap<DT, Locales>): () => (value: number, options?: import("../types").FormatNumberOptions | undefined) => string;
|
|
@@ -3,20 +3,25 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.
|
|
6
|
+
exports.createUseFormatNumber = exports.createFormatNumber = void 0;
|
|
7
7
|
const currency_js_1 = __importDefault(require("currency.js"));
|
|
8
8
|
const context_1 = require("../context/context");
|
|
9
9
|
const curry_1 = require("../utils/curry");
|
|
10
10
|
const map_options_1 = require("../utils/map-options");
|
|
11
|
-
const
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
};
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
11
|
+
const react_1 = require("react");
|
|
12
|
+
function createFormatNumber(localizeConfigs) {
|
|
13
|
+
return (locale, value, options) => {
|
|
14
|
+
var _a;
|
|
15
|
+
const precision = (_a = options === null || options === void 0 ? void 0 : options.precision) !== null && _a !== void 0 ? _a : 0;
|
|
16
|
+
const formatOptions = { ...(0, map_options_1._mapOptions)(localizeConfigs[locale].number), precision };
|
|
17
|
+
return (0, currency_js_1.default)(value, { precision }).format(formatOptions);
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
exports.createFormatNumber = createFormatNumber;
|
|
21
|
+
function createUseFormatNumber(localizeConfigs) {
|
|
22
|
+
return () => {
|
|
23
|
+
const locale = (0, context_1._useLocalizeContext)();
|
|
24
|
+
return (0, react_1.useMemo)(() => (0, curry_1._curry)(createFormatNumber(localizeConfigs))(locale), [locale]);
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
exports.createUseFormatNumber = createUseFormatNumber;
|
|
@@ -7,7 +7,7 @@ const format_number_1 = require("./format-number");
|
|
|
7
7
|
const cs_1 = __importDefault(require("../../locale/cs"));
|
|
8
8
|
const en_1 = __importDefault(require("../../locale/en"));
|
|
9
9
|
describe("number formatter", function () {
|
|
10
|
-
const formatNumberWithLocales = (0, format_number_1.
|
|
10
|
+
const formatNumberWithLocales = (0, format_number_1.createFormatNumber)({ cs: cs_1.default, en: en_1.default });
|
|
11
11
|
it("format number with 'cs' locale", function () {
|
|
12
12
|
expect(formatNumberWithLocales("cs", 2000.78)).toBe("2\xa0001");
|
|
13
13
|
expect(formatNumberWithLocales("cs", 2000.78, { precision: 2 })).toBe("2\xa0000,78");
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import { DateTimes, FormatPercentageFunction,
|
|
2
|
-
export declare
|
|
3
|
-
export declare
|
|
1
|
+
import { DateTimes, FormatPercentageFunction, LocalizeConfigMap, RoundingType } from "../types";
|
|
2
|
+
export declare function createFormatPercentage<DT extends DateTimes, Locales extends string>(localizeConfigs: LocalizeConfigMap<DT, Locales>): FormatPercentageFunction<Locales>;
|
|
3
|
+
export declare function createUseFormatPercentage<DT extends DateTimes, Locales extends string>(localizeConfigs: LocalizeConfigMap<DT, Locales>): () => (value: number, roundingType?: RoundingType | null | undefined, options?: import("../types").FormatPercentageOptions | undefined) => string;
|
|
@@ -1,23 +1,28 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.createUseFormatPercentage = exports.createFormatPercentage = void 0;
|
|
4
4
|
const format_number_1 = require("../format-number/format-number");
|
|
5
5
|
const context_1 = require("../context/context");
|
|
6
6
|
const curry_1 = require("../utils/curry");
|
|
7
|
+
const react_1 = require("react");
|
|
7
8
|
const ROUND_FUNCTION_MAP = {
|
|
8
9
|
nearest: Math.round,
|
|
9
10
|
up: Math.ceil,
|
|
10
11
|
down: Math.floor,
|
|
11
12
|
};
|
|
12
13
|
const PERCENTAGE_MULTIPLIER = 100;
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
14
|
+
function createFormatPercentage(localizeConfigs) {
|
|
15
|
+
return (locale, value, roundingType = null, options) => {
|
|
16
|
+
const percentage = value * PERCENTAGE_MULTIPLIER;
|
|
17
|
+
const percentageValue = roundingType ? ROUND_FUNCTION_MAP[roundingType](percentage) : percentage;
|
|
18
|
+
return `${(0, format_number_1.createFormatNumber)(localizeConfigs)(locale, percentageValue, options)}\xa0%`;
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
exports.createFormatPercentage = createFormatPercentage;
|
|
22
|
+
function createUseFormatPercentage(localizeConfigs) {
|
|
23
|
+
return () => {
|
|
24
|
+
const locale = (0, context_1._useLocalizeContext)();
|
|
25
|
+
return (0, react_1.useMemo)(() => (0, curry_1._curry)(createFormatPercentage(localizeConfigs))(locale), [locale]);
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
exports.createUseFormatPercentage = createUseFormatPercentage;
|
|
@@ -7,7 +7,7 @@ const format_percentage_1 = require("./format-percentage");
|
|
|
7
7
|
const cs_1 = __importDefault(require("../../locale/cs"));
|
|
8
8
|
const en_1 = __importDefault(require("../../locale/en"));
|
|
9
9
|
describe("percentage formatter", function () {
|
|
10
|
-
const formatPercentageWithLocales = (0, format_percentage_1.
|
|
10
|
+
const formatPercentageWithLocales = (0, format_percentage_1.createFormatPercentage)({ cs: cs_1.default, en: en_1.default });
|
|
11
11
|
it("format percentage with 'cs' locale", function () {
|
|
12
12
|
expect(formatPercentageWithLocales("cs", 0.782)).toBe("78\xa0%");
|
|
13
13
|
expect(formatPercentageWithLocales("cs", 0.782, null, { precision: 2 })).toBe("78,20\xa0%");
|
package/src/index.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { LocalizeConfigMap, CreateLocalizeReturn, DateTimes } from "./types";
|
|
2
2
|
export * from "./get-currency-symbol/get-currency-symbol";
|
|
3
|
-
export declare function createLocalize<DT extends
|
|
3
|
+
export declare function createLocalize<DT extends DateTimes, Locales extends string>(config: LocalizeConfigMap<DT, Locales>): CreateLocalizeReturn<DT, Locales>;
|
package/src/index.js
CHANGED
|
@@ -21,18 +21,18 @@ const format_percentage_1 = require("./format-percentage/format-percentage");
|
|
|
21
21
|
const format_datetime_1 = require("./format-datetime/format-datetime");
|
|
22
22
|
const format_money_1 = require("./format-money/format-money");
|
|
23
23
|
__exportStar(require("./get-currency-symbol/get-currency-symbol"), exports);
|
|
24
|
-
function createLocalize(
|
|
24
|
+
function createLocalize(config) {
|
|
25
25
|
return {
|
|
26
26
|
LocalizeProvider: context_1._LocalizeProvider,
|
|
27
|
-
formatDateTime: (0, format_datetime_1.
|
|
28
|
-
formatNumber: (0, format_number_1.
|
|
29
|
-
formatPercentage: (0, format_percentage_1.
|
|
30
|
-
formatMoney: (0, format_money_1.
|
|
31
|
-
useFormatDateTime: (0, format_datetime_1.
|
|
32
|
-
useFormatNumber: (0, format_number_1.
|
|
33
|
-
useFormatPercentage: (0, format_percentage_1.
|
|
34
|
-
useFormatMoney: (0, format_money_1.
|
|
35
|
-
useLocaleConfig: (0, context_1.getLocaleConfigHook)(
|
|
27
|
+
formatDateTime: (0, format_datetime_1.createFormatDatetime)(config),
|
|
28
|
+
formatNumber: (0, format_number_1.createFormatNumber)(config),
|
|
29
|
+
formatPercentage: (0, format_percentage_1.createFormatPercentage)(config),
|
|
30
|
+
formatMoney: (0, format_money_1.createFormatMoney)(config),
|
|
31
|
+
useFormatDateTime: (0, format_datetime_1.createUseFormatDatetime)(config),
|
|
32
|
+
useFormatNumber: (0, format_number_1.createUseFormatNumber)(config),
|
|
33
|
+
useFormatPercentage: (0, format_percentage_1.createUseFormatPercentage)(config),
|
|
34
|
+
useFormatMoney: (0, format_money_1.createUseFormatMoney)(config),
|
|
35
|
+
useLocaleConfig: (0, context_1.getLocaleConfigHook)(config),
|
|
36
36
|
};
|
|
37
37
|
}
|
|
38
38
|
exports.createLocalize = createLocalize;
|
package/src/types.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { CURRENCIES } from "./utils/data";
|
|
2
2
|
import { Provider } from "react";
|
|
3
3
|
export type DateTimes = "timeShort" | "timeFull" | "dateShort" | "dateMedium" | "dateLong" | "dateShortNoYear" | "dateLongNoYear" | "dateTimeShort" | "dateTimeMedium" | "dateTimeLong";
|
|
4
|
-
export type LocalizeConfig<DT extends
|
|
4
|
+
export type LocalizeConfig<DT extends DateTimes> = {
|
|
5
5
|
number: {
|
|
6
6
|
thousandsSeparator: string;
|
|
7
7
|
decimalSeparator: string;
|
|
@@ -14,18 +14,18 @@ export type LocalizeConfig<DT extends string = DateTimes> = {
|
|
|
14
14
|
};
|
|
15
15
|
dateTime: Record<DT, string>;
|
|
16
16
|
};
|
|
17
|
+
export type LocalizeConfigMap<DT extends DateTimes, Locales extends string> = Record<Locales, LocalizeConfig<DT>>;
|
|
17
18
|
export type LocalizeProviderType = Provider<string>;
|
|
18
|
-
export type UseLocaleConfigType<DT extends
|
|
19
|
+
export type UseLocaleConfigType<DT extends DateTimes> = () => LocalizeConfig<DT>;
|
|
19
20
|
export type FormatNumberFunction<Locales extends string> = (locale: Locales, value: number, options?: FormatNumberOptions) => string;
|
|
20
21
|
export type UseFormatNumberFunction = () => (value: number, options?: FormatNumberOptions) => string;
|
|
21
22
|
export type FormatMoneyFunction<Locales extends string> = (locale: Locales, value: number, currency: Currency, options?: FormatMoneyOptions) => string;
|
|
22
23
|
export type UseFormatMoneyFunction = () => (value: number, currency: Currency, options?: FormatMoneyOptions) => string;
|
|
23
24
|
export type FormatPercentageFunction<Locales extends string> = (locale: Locales, value: number, roundingType?: RoundingType | null, options?: FormatPercentageOptions) => string;
|
|
24
25
|
export type UseFormatPercentageFunction = () => (value: number, roundingType?: RoundingType | null, options?: FormatPercentageOptions) => string;
|
|
25
|
-
export type FormatDatetimeFunction<DT extends
|
|
26
|
-
export type UseFormatDatetimeFunction<DT extends
|
|
27
|
-
export type
|
|
28
|
-
export type CreateLocalizeReturn<DT extends string = DateTimes, Locales extends string = string> = {
|
|
26
|
+
export type FormatDatetimeFunction<DT extends DateTimes, Locales extends string> = (locale: Locales, value: Date, format: DT) => string;
|
|
27
|
+
export type UseFormatDatetimeFunction<DT extends DateTimes> = () => (value: Date, format: DT) => string;
|
|
28
|
+
export type CreateLocalizeReturn<DT extends DateTimes, Locales extends string> = {
|
|
29
29
|
LocalizeProvider: LocalizeProviderType;
|
|
30
30
|
useLocaleConfig: UseLocaleConfigType<DT>;
|
|
31
31
|
formatNumber: FormatNumberFunction<Locales>;
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import { LocalizeConfig } from "../types";
|
|
1
|
+
import { DateTimes, LocalizeConfig } from "../types";
|
|
2
2
|
import currencyjs from "currency.js";
|
|
3
|
-
export declare const _mapOptions: (options: LocalizeConfig["number"] | LocalizeConfig["currency"]) => currencyjs.Options;
|
|
3
|
+
export declare const _mapOptions: (options: LocalizeConfig<DateTimes>["number"] | LocalizeConfig<DateTimes>["currency"]) => currencyjs.Options;
|