@etsoo/shared 1.1.93 → 1.1.95

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
@@ -105,7 +105,7 @@ Array related utilities
105
105
  |toUnique|Make all items are unique|
106
106
 
107
107
  ## DataTypes
108
- Data type definitions and type safe functions. ListItemType and ListItemType1 are sugar types.
108
+ Data type definitions and type safe functions. ListItemType, ListItemType1 and ListItemType2 are sugar types.
109
109
 
110
110
  |Name|Description|
111
111
  |---:|---|
@@ -231,6 +231,7 @@ Numbers related utilities
231
231
  |---:|---|
232
232
  |format|Format number|
233
233
  |formatMoney|Format money number|
234
+ |getCurrencySymbol|Get currency symbol or name from ISO code|
234
235
  |parse|Parse float value|
235
236
  |toExact|To the exact precision number avoiding precision lost|
236
237
 
@@ -11,6 +11,17 @@ test('Tests for formatMoney', () => {
11
11
  expect(NumberUtils.formatMoney(1282, 'CNY', 'zh-CN', true)).toBe('¥1,282');
12
12
  });
13
13
 
14
+ test('Tests for getCurrencySymbol', () => {
15
+ expect(NumberUtils.getCurrencySymbol('CNY')).toBe('¥');
16
+ expect(NumberUtils.getCurrencySymbol('USD')).toBe('$');
17
+
18
+ // When locale = 'en-US' will be failed with '$'
19
+ expect(NumberUtils.getCurrencySymbol('USD', 'symbol', 'zh-CN')).toBe('US$');
20
+ expect(NumberUtils.getCurrencySymbol('CNY', 'name', 'zh-CN')).toBe(
21
+ '人民币'
22
+ );
23
+ });
24
+
14
25
  test('Tests for parse', () => {
15
26
  expect(NumberUtils.parse('123')).toBe(123);
16
27
  expect(NumberUtils.parse(Object(123))).toBe(123);
@@ -395,6 +395,16 @@ export type ListType = DataTypes.IdLabelItem<number>;
395
395
  * List item with string id type
396
396
  */
397
397
  export type ListType1 = DataTypes.IdLabelItem<string>;
398
+ /**
399
+ * List item with compatible id and name / label
400
+ */
401
+ export type ListType2 = {
402
+ id: DataTypes.IdType;
403
+ } & ({
404
+ label: string;
405
+ } | {
406
+ name: string;
407
+ });
398
408
  /**
399
409
  * Id default type
400
410
  */
@@ -26,6 +26,14 @@ export declare namespace NumberUtils {
26
26
  * @returns Result
27
27
  */
28
28
  function formatMoney(input: number | bigint, currency?: string, locale?: string | string[], isInteger?: boolean, options?: Intl.NumberFormatOptions): string;
29
+ /**
30
+ * Get currency symbol or name from ISO code
31
+ * @param code ISO currency code, like USD / CNY
32
+ * @param display Display format
33
+ * @param locale Locale
34
+ * @returns Result
35
+ */
36
+ function getCurrencySymbol(code: string, display?: 'symbol' | 'narrowSymbol' | 'name', locale?: string): string | undefined;
29
37
  /**
30
38
  * Parse float value
31
39
  * @param rawData Raw data
@@ -50,6 +50,25 @@ var NumberUtils;
50
50
  return format(input, locale, options);
51
51
  }
52
52
  NumberUtils.formatMoney = formatMoney;
53
+ /**
54
+ * Get currency symbol or name from ISO code
55
+ * @param code ISO currency code, like USD / CNY
56
+ * @param display Display format
57
+ * @param locale Locale
58
+ * @returns Result
59
+ */
60
+ function getCurrencySymbol(code, display = 'narrowSymbol', locale) {
61
+ var _a;
62
+ const formatter = new Intl.NumberFormat(locale, {
63
+ style: 'currency',
64
+ currency: code,
65
+ currencyDisplay: display
66
+ });
67
+ const parts = formatter.formatToParts();
68
+ const symbol = (_a = parts.find((part) => part.type === 'currency')) === null || _a === void 0 ? void 0 : _a.value;
69
+ return symbol;
70
+ }
71
+ NumberUtils.getCurrencySymbol = getCurrencySymbol;
53
72
  /**
54
73
  * Parse float value
55
74
  * @param rawData Raw data
@@ -395,6 +395,16 @@ export type ListType = DataTypes.IdLabelItem<number>;
395
395
  * List item with string id type
396
396
  */
397
397
  export type ListType1 = DataTypes.IdLabelItem<string>;
398
+ /**
399
+ * List item with compatible id and name / label
400
+ */
401
+ export type ListType2 = {
402
+ id: DataTypes.IdType;
403
+ } & ({
404
+ label: string;
405
+ } | {
406
+ name: string;
407
+ });
398
408
  /**
399
409
  * Id default type
400
410
  */
@@ -26,6 +26,14 @@ export declare namespace NumberUtils {
26
26
  * @returns Result
27
27
  */
28
28
  function formatMoney(input: number | bigint, currency?: string, locale?: string | string[], isInteger?: boolean, options?: Intl.NumberFormatOptions): string;
29
+ /**
30
+ * Get currency symbol or name from ISO code
31
+ * @param code ISO currency code, like USD / CNY
32
+ * @param display Display format
33
+ * @param locale Locale
34
+ * @returns Result
35
+ */
36
+ function getCurrencySymbol(code: string, display?: 'symbol' | 'narrowSymbol' | 'name', locale?: string): string | undefined;
29
37
  /**
30
38
  * Parse float value
31
39
  * @param rawData Raw data
@@ -47,6 +47,25 @@ export var NumberUtils;
47
47
  return format(input, locale, options);
48
48
  }
49
49
  NumberUtils.formatMoney = formatMoney;
50
+ /**
51
+ * Get currency symbol or name from ISO code
52
+ * @param code ISO currency code, like USD / CNY
53
+ * @param display Display format
54
+ * @param locale Locale
55
+ * @returns Result
56
+ */
57
+ function getCurrencySymbol(code, display = 'narrowSymbol', locale) {
58
+ var _a;
59
+ const formatter = new Intl.NumberFormat(locale, {
60
+ style: 'currency',
61
+ currency: code,
62
+ currencyDisplay: display
63
+ });
64
+ const parts = formatter.formatToParts();
65
+ const symbol = (_a = parts.find((part) => part.type === 'currency')) === null || _a === void 0 ? void 0 : _a.value;
66
+ return symbol;
67
+ }
68
+ NumberUtils.getCurrencySymbol = getCurrencySymbol;
50
69
  /**
51
70
  * Parse float value
52
71
  * @param rawData Raw data
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@etsoo/shared",
3
- "version": "1.1.93",
3
+ "version": "1.1.95",
4
4
  "description": "TypeScript shared utilities and functions",
5
5
  "main": "lib/cjs/index.js",
6
6
  "module": "lib/mjs/index.js",
package/src/DataTypes.ts CHANGED
@@ -754,6 +754,13 @@ export type ListType = DataTypes.IdLabelItem<number>;
754
754
  */
755
755
  export type ListType1 = DataTypes.IdLabelItem<string>;
756
756
 
757
+ /**
758
+ * List item with compatible id and name / label
759
+ */
760
+ export type ListType2 = {
761
+ id: DataTypes.IdType;
762
+ } & ({ label: string } | { name: string });
763
+
757
764
  /**
758
765
  * Id default type
759
766
  */
@@ -66,6 +66,30 @@ export namespace NumberUtils {
66
66
  return format(input, locale, options);
67
67
  }
68
68
 
69
+ /**
70
+ * Get currency symbol or name from ISO code
71
+ * @param code ISO currency code, like USD / CNY
72
+ * @param display Display format
73
+ * @param locale Locale
74
+ * @returns Result
75
+ */
76
+ export function getCurrencySymbol(
77
+ code: string,
78
+ display: 'symbol' | 'narrowSymbol' | 'name' = 'narrowSymbol',
79
+ locale?: string
80
+ ): string | undefined {
81
+ const formatter = new Intl.NumberFormat(locale, {
82
+ style: 'currency',
83
+ currency: code,
84
+ currencyDisplay: display
85
+ });
86
+
87
+ const parts = formatter.formatToParts();
88
+ const symbol = parts.find((part) => part.type === 'currency')?.value;
89
+
90
+ return symbol;
91
+ }
92
+
69
93
  /**
70
94
  * Parse float value
71
95
  * @param rawData Raw data