@etsoo/shared 1.1.94 → 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
@@ -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);
@@ -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
@@ -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.94",
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",
@@ -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