@uxf/localize 10.0.0-beta.42 → 10.0.0-beta.44

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@uxf/localize",
3
- "version": "10.0.0-beta.42",
3
+ "version": "10.0.0-beta.44",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -0,0 +1,2 @@
1
+ import { DateTimes, FormatDatetimeComponent, LocalizeConfigMap } from "../types";
2
+ export declare function createFormatDatetimeComponent<DT extends DateTimes, Locales extends string>(localizeConfigs: LocalizeConfigMap<DT, Locales>): FormatDatetimeComponent<DT>;
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.createFormatDatetimeComponent = void 0;
7
+ const react_1 = __importDefault(require("react"));
8
+ const context_1 = require("../context/context");
9
+ const format_datetime_1 = require("../format-datetime/format-datetime");
10
+ function createFormatDatetimeComponent(localizeConfigs) {
11
+ const Component = (props) => {
12
+ const locale = (0, context_1._useLocalizeContext)();
13
+ return react_1.default.createElement(react_1.default.Fragment, null, (0, format_datetime_1.createFormatDatetime)(localizeConfigs)(locale, props.value, props.format));
14
+ };
15
+ return Component;
16
+ }
17
+ exports.createFormatDatetimeComponent = createFormatDatetimeComponent;
@@ -0,0 +1,2 @@
1
+ import { DateTimes, FormatMoneyComponent, LocalizeConfigMap } from "../types";
2
+ export declare function createFormatMoneyComponent<DT extends DateTimes, Locales extends string>(localizeConfigs: LocalizeConfigMap<DT, Locales>): FormatMoneyComponent;
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.createFormatMoneyComponent = void 0;
7
+ const react_1 = __importDefault(require("react"));
8
+ const context_1 = require("../context/context");
9
+ const format_money_1 = require("../format-money/format-money");
10
+ function createFormatMoneyComponent(localizeConfigs) {
11
+ const Component = (props) => {
12
+ const locale = (0, context_1._useLocalizeContext)();
13
+ return react_1.default.createElement(react_1.default.Fragment, null, (0, format_money_1.createFormatMoney)(localizeConfigs)(locale, props.money, props.options));
14
+ };
15
+ return Component;
16
+ }
17
+ exports.createFormatMoneyComponent = createFormatMoneyComponent;
@@ -0,0 +1,2 @@
1
+ import { DateTimes, FormatNumberComponent, LocalizeConfigMap } from "../types";
2
+ export declare function createFormatNumberComponent<DT extends DateTimes, Locales extends string>(localizeConfigs: LocalizeConfigMap<DT, Locales>): FormatNumberComponent;
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.createFormatNumberComponent = void 0;
7
+ const react_1 = __importDefault(require("react"));
8
+ const context_1 = require("../context/context");
9
+ const format_number_1 = require("../format-number/format-number");
10
+ function createFormatNumberComponent(localizeConfigs) {
11
+ const Component = (props) => {
12
+ const locale = (0, context_1._useLocalizeContext)();
13
+ return react_1.default.createElement(react_1.default.Fragment, null, (0, format_number_1.createFormatNumber)(localizeConfigs)(locale, props.value, props.options));
14
+ };
15
+ return Component;
16
+ }
17
+ exports.createFormatNumberComponent = createFormatNumberComponent;
@@ -0,0 +1,2 @@
1
+ import { DateTimes, FormatPercentageComponent, LocalizeConfigMap } from "../types";
2
+ export declare function createFormatPercentageComponent<DT extends DateTimes, Locales extends string>(localizeConfigs: LocalizeConfigMap<DT, Locales>): FormatPercentageComponent;
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.createFormatPercentageComponent = void 0;
7
+ const react_1 = __importDefault(require("react"));
8
+ const context_1 = require("../context/context");
9
+ const format_percentage_1 = require("../format-percentage/format-percentage");
10
+ function createFormatPercentageComponent(localizeConfigs) {
11
+ const Component = (props) => {
12
+ const locale = (0, context_1._useLocalizeContext)();
13
+ return react_1.default.createElement(react_1.default.Fragment, null, (0, format_percentage_1.createFormatPercentage)(localizeConfigs)(locale, props.value, props.roundingType, props.options));
14
+ };
15
+ return Component;
16
+ }
17
+ exports.createFormatPercentageComponent = createFormatPercentageComponent;
@@ -1,3 +1,3 @@
1
1
  import { DateTimes, FormatMoneyFunction, LocalizeConfigMap } from "../types";
2
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" | "ALL" | "AMD" | "ANG" | "AOA" | "ARS" | "AUD" | "AWG" | "AZN" | "BAM" | "BBD" | "BDT" | "BGN" | "BHD" | "BIF" | "BMD" | "BND" | "BOB" | "BOV" | "BRL" | "BSD" | "BTN" | "BWP" | "BYN" | "BZD" | "CAD" | "CDF" | "CHE" | "CHW" | "CLF" | "CLP" | "CNY" | "COP" | "COU" | "CRC" | "CUC" | "CUP" | "CVE" | "CZK" | "DJF" | "DKK" | "DOP" | "DZD" | "EGP" | "ERN" | "ETB" | "EUR" | "FJD" | "FKP" | "GBP" | "GEL" | "GHS" | "GIP" | "GMD" | "GNF" | "GTQ" | "GYD" | "HKD" | "HNL" | "HTG" | "HUF" | "IDR" | "ILS" | "INR" | "IQD" | "IRR" | "ISK" | "JMD" | "JOD" | "JPY" | "KES" | "KGS" | "KHR" | "KMF" | "KPW" | "KRW" | "KWD" | "KYD" | "KZT" | "LAK" | "LBP" | "LKR" | "LRD" | "LSL" | "LYD" | "MAD" | "MDL" | "MGA" | "MKD" | "MMK" | "MNT" | "MOP" | "MRU" | "MUR" | "MVR" | "MWK" | "MXN" | "MXV" | "MYR" | "MZN" | "NAD" | "NGN" | "NIO" | "NOK" | "NPR" | "NZD" | "OMR" | "PAB" | "PEN" | "PGK" | "PHP" | "PKR" | "PLN" | "PYG" | "QAR" | "RON" | "RSD" | "RUB" | "RWF" | "SAR" | "SBD" | "SCR" | "SDG" | "SEK" | "SGD" | "SHP" | "SLE" | "SLL" | "SOS" | "SRD" | "SSP" | "STN" | "SVC" | "SYP" | "SZL" | "THB" | "TJS" | "TMT" | "TND" | "TOP" | "TRY" | "TTD" | "TWD" | "TZS" | "UAH" | "UGX" | "USD" | "USN" | "UYI" | "UYU" | "UYW" | "UZS" | "VED" | "VES" | "VND" | "VUV" | "WST" | "XAF" | "XCD" | "XOF" | "XPF" | "YER" | "ZAR" | "ZMW" | "ZWL", options?: import("../types").FormatMoneyOptions | undefined) => string;
3
+ export declare function createUseFormatMoney<DT extends DateTimes, Locales extends string>(localizeConfigs: LocalizeConfigMap<DT, Locales>): () => (money: import("../types").Money, options?: import("../types").FormatMoneyOptions | undefined) => string;
@@ -11,16 +11,16 @@ const map_options_1 = require("../utils/map-options");
11
11
  const data_1 = require("../utils/data");
12
12
  const react_1 = require("react");
13
13
  function createFormatMoney(localizeConfigs) {
14
- return (locale, value, currency, options) => {
14
+ return (locale, money, options) => {
15
15
  var _a, _b, _c;
16
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;
17
+ const symbol = (options === null || options === void 0 ? void 0 : options.preferIsoCode) ? money.currency : data_1.CURRENCIES[money.currency].symbol;
18
18
  const formatOptions = { ...(0, map_options_1._mapOptions)(localizeConfigs[locale].currency), precision, symbol };
19
19
  if (options === null || options === void 0 ? void 0 : options.hideSymbol) {
20
20
  formatOptions.pattern = (_b = formatOptions.pattern) === null || _b === void 0 ? void 0 : _b.replace("!", "").trim();
21
21
  formatOptions.negativePattern = (_c = formatOptions.negativePattern) === null || _c === void 0 ? void 0 : _c.replace("!", "").trim();
22
22
  }
23
- return (0, currency_js_1.default)(value, { precision }).format(formatOptions);
23
+ return (0, currency_js_1.default)(money.amount, { precision }).format(formatOptions);
24
24
  };
25
25
  }
26
26
  exports.createFormatMoney = createFormatMoney;
@@ -9,23 +9,23 @@ const en_1 = __importDefault(require("../../locale/en"));
9
9
  describe("money formatter", function () {
10
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
- expect(formatMoneyWithLocales("cs", 2000.78, "CZK")).toBe("2\xa0001\xa0Kč");
13
- expect(formatMoneyWithLocales("cs", 2000.78, "CZK", { precision: 1 })).toBe("2\xa0000,8\xa0Kč");
14
- expect(formatMoneyWithLocales("cs", 2000.78, "CZK", { precision: 1, preferIsoCode: true })).toBe("2\xa0000,8\xa0CZK");
15
- expect(formatMoneyWithLocales("cs", 2000.78, "CZK", { precision: 1, hideSymbol: true })).toBe("2\xa0000,8");
16
- expect(formatMoneyWithLocales("cs", 2000.78, "USD")).toBe("2\xa0001\xa0$");
17
- expect(formatMoneyWithLocales("cs", 2000.78, "USD", { precision: 1 })).toBe("2\xa0000,8\xa0$");
18
- expect(formatMoneyWithLocales("cs", 2000.78, "USD", { precision: 1, preferIsoCode: true })).toBe("2\xa0000,8\xa0USD");
19
- expect(formatMoneyWithLocales("cs", 2000.78, "USD", { precision: 1, hideSymbol: true })).toBe("2\xa0000,8");
12
+ expect(formatMoneyWithLocales("cs", { amount: "2000.78", currency: "CZK" })).toBe("2\xa0001\xa0Kč");
13
+ expect(formatMoneyWithLocales("cs", { amount: "2000.78", currency: "CZK" }, { precision: 1 })).toBe("2\xa0000,8\xa0Kč");
14
+ expect(formatMoneyWithLocales("cs", { amount: "2000.78", currency: "CZK" }, { precision: 1, preferIsoCode: true })).toBe("2\xa0000,8\xa0CZK");
15
+ expect(formatMoneyWithLocales("cs", { amount: "2000.78", currency: "CZK" }, { precision: 1, hideSymbol: true })).toBe("2\xa0000,8");
16
+ expect(formatMoneyWithLocales("cs", { amount: "2000.78", currency: "USD" })).toBe("2\xa0001\xa0$");
17
+ expect(formatMoneyWithLocales("cs", { amount: "2000.78", currency: "USD" }, { precision: 1 })).toBe("2\xa0000,8\xa0$");
18
+ expect(formatMoneyWithLocales("cs", { amount: "2000.78", currency: "USD" }, { precision: 1, preferIsoCode: true })).toBe("2\xa0000,8\xa0USD");
19
+ expect(formatMoneyWithLocales("cs", { amount: "2000.78", currency: "USD" }, { precision: 1, hideSymbol: true })).toBe("2\xa0000,8");
20
20
  });
21
21
  it("format money with 'en' locale", function () {
22
- expect(formatMoneyWithLocales("en", 2000.78, "CZK")).toBe("Kč2,001");
23
- expect(formatMoneyWithLocales("en", 2000.78, "CZK", { precision: 1 })).toBe("Kč2,000.8");
24
- expect(formatMoneyWithLocales("en", 2000.78, "CZK", { precision: 1, preferIsoCode: true })).toBe("CZK2,000.8");
25
- expect(formatMoneyWithLocales("en", 2000.78, "CZK", { precision: 1, hideSymbol: true })).toBe("2,000.8");
26
- expect(formatMoneyWithLocales("en", 2000.78, "USD")).toBe("$2,001");
27
- expect(formatMoneyWithLocales("en", 2000.78, "USD", { precision: 1 })).toBe("$2,000.8");
28
- expect(formatMoneyWithLocales("en", 2000.78, "USD", { precision: 1, preferIsoCode: true })).toBe("USD2,000.8");
29
- expect(formatMoneyWithLocales("en", 2000.78, "USD", { precision: 1, hideSymbol: true })).toBe("2,000.8");
22
+ expect(formatMoneyWithLocales("en", { amount: "2000.78", currency: "CZK" })).toBe("Kč2,001");
23
+ expect(formatMoneyWithLocales("en", { amount: "2000.78", currency: "CZK" }, { precision: 1 })).toBe("Kč2,000.8");
24
+ expect(formatMoneyWithLocales("en", { amount: "2000.78", currency: "CZK" }, { precision: 1, preferIsoCode: true })).toBe("CZK2,000.8");
25
+ expect(formatMoneyWithLocales("en", { amount: "2000.78", currency: "CZK" }, { precision: 1, hideSymbol: true })).toBe("2,000.8");
26
+ expect(formatMoneyWithLocales("en", { amount: "2000.78", currency: "USD" })).toBe("$2,001");
27
+ expect(formatMoneyWithLocales("en", { amount: "2000.78", currency: "USD" }, { precision: 1 })).toBe("$2,000.8");
28
+ expect(formatMoneyWithLocales("en", { amount: "2000.78", currency: "USD" }, { precision: 1, preferIsoCode: true })).toBe("USD2,000.8");
29
+ expect(formatMoneyWithLocales("en", { amount: "2000.78", currency: "USD" }, { precision: 1, hideSymbol: true })).toBe("2,000.8");
30
30
  });
31
31
  });
package/src/index.js CHANGED
@@ -20,6 +20,10 @@ const format_number_1 = require("./format-number/format-number");
20
20
  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
+ const format_money_2 = require("./components/format-money");
24
+ const format_percentage_2 = require("./components/format-percentage");
25
+ const format_number_2 = require("./components/format-number");
26
+ const format_datetime_2 = require("./components/format-datetime");
23
27
  __exportStar(require("./get-currency-symbol/get-currency-symbol"), exports);
24
28
  function createLocalize(config) {
25
29
  return {
@@ -28,6 +32,10 @@ function createLocalize(config) {
28
32
  formatNumber: (0, format_number_1.createFormatNumber)(config),
29
33
  formatPercentage: (0, format_percentage_1.createFormatPercentage)(config),
30
34
  formatMoney: (0, format_money_1.createFormatMoney)(config),
35
+ FormatDateTime: (0, format_datetime_2.createFormatDatetimeComponent)(config),
36
+ FormatNumber: (0, format_number_2.createFormatNumberComponent)(config),
37
+ FormatPercentage: (0, format_percentage_2.createFormatPercentageComponent)(config),
38
+ FormatMoney: (0, format_money_2.createFormatMoneyComponent)(config),
31
39
  useFormatDateTime: (0, format_datetime_1.createUseFormatDatetime)(config),
32
40
  useFormatNumber: (0, format_number_1.createUseFormatNumber)(config),
33
41
  useFormatPercentage: (0, format_percentage_1.createUseFormatPercentage)(config),
package/src/types.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { CURRENCIES } from "./utils/data";
2
- import { Provider } from "react";
2
+ import { FC, Provider } from "react";
3
3
  export type DateTimes = "timeShort" | "timeFull" | "dateShort" | "dateMedium" | "dateLong" | "dateShortNoYear" | "dateLongNoYear" | "dateTimeShort" | "dateTimeMedium" | "dateTimeLong";
4
4
  export type LocalizeConfig<DT extends DateTimes> = {
5
5
  number: {
@@ -17,25 +17,50 @@ export type LocalizeConfig<DT extends DateTimes> = {
17
17
  export type LocalizeConfigMap<DT extends DateTimes, Locales extends string> = Record<Locales, LocalizeConfig<DT>>;
18
18
  export type LocalizeProviderType = Provider<string>;
19
19
  export type UseLocaleConfigType<DT extends DateTimes> = () => LocalizeConfig<DT>;
20
+ export type Money = {
21
+ amount: string;
22
+ currency: Currency;
23
+ };
20
24
  export type FormatNumberFunction<Locales extends string> = (locale: Locales, value: number, options?: FormatNumberOptions) => string;
21
25
  export type UseFormatNumberFunction = () => (value: number, options?: FormatNumberOptions) => string;
22
- export type FormatMoneyFunction<Locales extends string> = (locale: Locales, value: number, currency: Currency, options?: FormatMoneyOptions) => string;
23
- export type UseFormatMoneyFunction = () => (value: number, currency: Currency, options?: FormatMoneyOptions) => string;
26
+ export type FormatNumberComponent = FC<{
27
+ value: number;
28
+ options?: FormatNumberOptions;
29
+ }>;
30
+ export type FormatMoneyFunction<Locales extends string> = (locale: Locales, money: Money, options?: FormatMoneyOptions) => string;
31
+ export type UseFormatMoneyFunction = () => (money: Money, options?: FormatMoneyOptions) => string;
32
+ export type FormatMoneyComponent = FC<{
33
+ money: Money;
34
+ options?: FormatMoneyOptions;
35
+ }>;
24
36
  export type FormatPercentageFunction<Locales extends string> = (locale: Locales, value: number, roundingType?: RoundingType | null, options?: FormatPercentageOptions) => string;
25
37
  export type UseFormatPercentageFunction = () => (value: number, roundingType?: RoundingType | null, options?: FormatPercentageOptions) => string;
38
+ export type FormatPercentageComponent = FC<{
39
+ value: number;
40
+ roundingType?: RoundingType | null;
41
+ options?: FormatPercentageOptions;
42
+ }>;
26
43
  export type FormatDatetimeFunction<DT extends DateTimes, Locales extends string> = (locale: Locales, value: Date, format: DT) => string;
27
44
  export type UseFormatDatetimeFunction<DT extends DateTimes> = () => (value: Date, format: DT) => string;
45
+ export type FormatDatetimeComponent<DT extends DateTimes> = FC<{
46
+ value: Date;
47
+ format: DT;
48
+ }>;
28
49
  export type CreateLocalizeReturn<DT extends DateTimes, Locales extends string> = {
29
50
  LocalizeProvider: LocalizeProviderType;
30
51
  useLocaleConfig: UseLocaleConfigType<DT>;
31
52
  formatNumber: FormatNumberFunction<Locales>;
32
53
  useFormatNumber: UseFormatNumberFunction;
54
+ FormatNumber: FormatNumberComponent;
33
55
  formatMoney: FormatMoneyFunction<Locales>;
34
56
  useFormatMoney: UseFormatMoneyFunction;
57
+ FormatMoney: FormatMoneyComponent;
35
58
  formatPercentage: FormatPercentageFunction<Locales>;
36
59
  useFormatPercentage: UseFormatPercentageFunction;
60
+ FormatPercentage: FormatPercentageComponent;
37
61
  formatDateTime: FormatDatetimeFunction<DT, Locales>;
38
62
  useFormatDateTime: UseFormatDatetimeFunction<DT>;
63
+ FormatDateTime: FormatDatetimeComponent<DT>;
39
64
  };
40
65
  export type Currency = keyof typeof CURRENCIES;
41
66
  export type FormatMoneyOptions = {