@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 CHANGED
@@ -14,14 +14,15 @@ yarn add @uxf/localize
14
14
  npm install @uxf/localize
15
15
  ```
16
16
 
17
- ## Basic usage
17
+ ## Initialize and configure package
18
18
 
19
- ### Initialize and configure package
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
- ### Implement context provider
40
+ ### Localize config examples
40
41
 
41
- use LocalizeProvider in **_app.tsx**
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
- <LocalizeProvider locale="en">{props.children}</LocalizeProvider>
99
+ // _app.tsx
100
+
101
+ <LocalizeProvider locale="cs">{props.children}</LocalizeProvider>
45
102
  ```
46
103
 
47
- ### Format for default locale
104
+ ## Format number
105
+ ```tsx
106
+ const { useFormatNumber } from "./localize";
48
107
 
49
- ```ts
50
- import { useFormatMoney } from "...";
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(5800, "EUR"); // €5.800
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
- ### Format for another locale
138
+ ## Format percentage
58
139
 
59
- ```ts
60
- import { formatMoney } from "...";
140
+ ```tsx
141
+ const { useFormatPercentage } from "./localize";
61
142
 
62
- formatMoney("cs", 5800, "EUR"); // 5 800 €
63
- formatMoney("cs", 5800, "EUR", { preferIsoCode: true }); // 5 800 EUR
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
- This approach is consistent for all the methods, so you can format numbers, money, percentage and date times.
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@uxf/localize",
3
- "version": "10.0.0-beta.25",
3
+ "version": "10.0.0-beta.30",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -1,5 +1,5 @@
1
1
  /// <reference types="react" />
2
- import { CreateLocalizeProps, DateTimes } from "../types";
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 string = DateTimes>(allConfigurations: CreateLocalizeProps<DT>) => () => import("../types").LocalizeConfig<DT>;
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, LocalizeConfig } from "../types";
2
- export declare const _formatDatetime: <DT extends string = DateTimes, Locales extends string = string>(localizeConfigs: Record<Locales, LocalizeConfig<DT>>) => FormatDatetimeFunction<DT, Locales>;
3
- export declare const _useFormatDatetime: <DT extends string = DateTimes>(localizeConfigs: Record<string, LocalizeConfig<DT>>) => () => (value: Date, format: DT) => string;
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._useFormatDatetime = exports._formatDatetime = void 0;
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 _formatDatetime = (localizeConfigs) => (locale, value, format) => {
11
- const localeDateTimes = localizeConfigs[locale].dateTime;
12
- return (0, dayjs_1.default)(value).format(localeDateTimes[format]);
13
- };
14
- exports._formatDatetime = _formatDatetime;
15
- const _useFormatDatetime = (localizeConfigs) => () => {
16
- const locale = (0, context_1._useLocalizeContext)();
17
- return (0, curry_1._curry)((0, exports._formatDatetime)(localizeConfigs))(locale);
18
- };
19
- exports._useFormatDatetime = _useFormatDatetime;
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._formatDatetime)({ cs: cs_1.default, en: en_1.default });
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, LocalizeConfig } from "../types";
2
- export declare const _formatMoney: <DT extends string = DateTimes, Locales extends string = string>(localizeConfigs: Record<Locales, LocalizeConfig<DT>>) => FormatMoneyFunction<Locales>;
3
- export declare const _useFormatMoney: <DT extends string = DateTimes>(localizeConfigs: Record<string, LocalizeConfig<DT>>) => () => (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;
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._useFormatMoney = exports._formatMoney = void 0;
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 _formatMoney = (localizeConfigs) => (locale, value, currency, options) => {
13
- var _a, _b, _c;
14
- const precision = (_a = options === null || options === void 0 ? void 0 : options.precision) !== null && _a !== void 0 ? _a : 0;
15
- const symbol = (options === null || options === void 0 ? void 0 : options.preferIsoCode) ? currency : data_1.CURRENCIES[currency].symbol;
16
- const formatOptions = { ...(0, map_options_1._mapOptions)(localizeConfigs[locale].currency), precision, symbol };
17
- if (options === null || options === void 0 ? void 0 : options.hideSymbol) {
18
- formatOptions.pattern = (_b = formatOptions.pattern) === null || _b === void 0 ? void 0 : _b.replace("!", "").trim();
19
- formatOptions.negativePattern = (_c = formatOptions.negativePattern) === null || _c === void 0 ? void 0 : _c.replace("!", "").trim();
20
- }
21
- return (0, currency_js_1.default)(value, { precision }).format(formatOptions);
22
- };
23
- exports._formatMoney = _formatMoney;
24
- const _useFormatMoney = (localizeConfigs) => () => {
25
- const locale = (0, context_1._useLocalizeContext)();
26
- return (0, curry_1._curry)((0, exports._formatMoney)(localizeConfigs))(locale);
27
- };
28
- exports._useFormatMoney = _useFormatMoney;
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._formatMoney)({ cs: cs_1.default, en: en_1.default });
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, LocalizeConfig } from "../types";
2
- export declare const _formatNumber: <DT extends string = DateTimes, Locales extends string = string>(localizeConfigs: Record<Locales, LocalizeConfig<DT>>) => FormatNumberFunction<Locales>;
3
- export declare const _useFormatNumber: <DT extends string = DateTimes>(localizeConfigs: Record<string, LocalizeConfig<DT>>) => () => (value: number, options?: import("../types").FormatNumberOptions | undefined) => string;
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._useFormatNumber = exports._formatNumber = void 0;
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 _formatNumber = (localizeConfigs) => (locale, value, options) => {
12
- var _a;
13
- const precision = (_a = options === null || options === void 0 ? void 0 : options.precision) !== null && _a !== void 0 ? _a : 0;
14
- const formatOptions = { ...(0, map_options_1._mapOptions)(localizeConfigs[locale].number), precision };
15
- return (0, currency_js_1.default)(value, { precision }).format(formatOptions);
16
- };
17
- exports._formatNumber = _formatNumber;
18
- const _useFormatNumber = (localizeConfigs) => () => {
19
- const locale = (0, context_1._useLocalizeContext)();
20
- return (0, curry_1._curry)((0, exports._formatNumber)(localizeConfigs))(locale);
21
- };
22
- exports._useFormatNumber = _useFormatNumber;
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._formatNumber)({ cs: cs_1.default, en: en_1.default });
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, LocalizeConfig, RoundingType } from "../types";
2
- export declare const _formatPercentage: <DT extends string = DateTimes, Locales extends string = string>(localizeConfigs: Record<Locales, LocalizeConfig<DT>>) => FormatPercentageFunction<Locales>;
3
- export declare const _useFormatPercentage: <DT extends string = DateTimes>(localizeConfigs: Record<string, LocalizeConfig<DT>>) => () => (value: number, roundingType?: RoundingType | null | undefined, options?: import("../types").FormatPercentageOptions | undefined) => string;
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._useFormatPercentage = exports._formatPercentage = void 0;
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
- const _formatPercentage = (localizeConfigs) => (locale, value, roundingType = null, options) => {
14
- const percentage = value * PERCENTAGE_MULTIPLIER;
15
- const percentageValue = roundingType ? ROUND_FUNCTION_MAP[roundingType](percentage) : percentage;
16
- return `${(0, format_number_1._formatNumber)(localizeConfigs)(locale, percentageValue, options)}\xa0%`;
17
- };
18
- exports._formatPercentage = _formatPercentage;
19
- const _useFormatPercentage = (localizeConfigs) => () => {
20
- const locale = (0, context_1._useLocalizeContext)();
21
- return (0, curry_1._curry)((0, exports._formatPercentage)(localizeConfigs))(locale);
22
- };
23
- exports._useFormatPercentage = _useFormatPercentage;
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._formatPercentage)({ cs: cs_1.default, en: en_1.default });
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 { CreateLocalizeProps, CreateLocalizeReturn, DateTimes } from "./types";
1
+ import { LocalizeConfigMap, CreateLocalizeReturn, DateTimes } from "./types";
2
2
  export * from "./get-currency-symbol/get-currency-symbol";
3
- export declare function createLocalize<DT extends string = DateTimes, Locales extends string = string>(props: CreateLocalizeProps<DT, Locales>): CreateLocalizeReturn<DT, Locales>;
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(props) {
24
+ function createLocalize(config) {
25
25
  return {
26
26
  LocalizeProvider: context_1._LocalizeProvider,
27
- formatDateTime: (0, format_datetime_1._formatDatetime)(props),
28
- formatNumber: (0, format_number_1._formatNumber)(props),
29
- formatPercentage: (0, format_percentage_1._formatPercentage)(props),
30
- formatMoney: (0, format_money_1._formatMoney)(props),
31
- useFormatDateTime: (0, format_datetime_1._useFormatDatetime)(props),
32
- useFormatNumber: (0, format_number_1._useFormatNumber)(props),
33
- useFormatPercentage: (0, format_percentage_1._useFormatPercentage)(props),
34
- useFormatMoney: (0, format_money_1._useFormatMoney)(props),
35
- useLocaleConfig: (0, context_1.getLocaleConfigHook)(props),
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 string = DateTimes> = {
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 string = DateTimes> = () => LocalizeConfig<DT>;
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 string = DateTimes, Locales extends string = string> = (locale: Locales, value: Date, format: DT) => string;
26
- export type UseFormatDatetimeFunction<DT extends string = DateTimes> = () => (value: Date, format: DT) => string;
27
- export type CreateLocalizeProps<DT extends string = DateTimes, Locales extends string = string> = Record<Locales, LocalizeConfig<DT>>;
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;