@etsoo/shared 1.1.94 → 1.1.96

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
 
@@ -71,6 +71,12 @@ test('Tests for formatForInput', () => {
71
71
 
72
72
  const result41 = DateUtils.formatForInput(d, 'datetime-local');
73
73
  expect(result41).toBe('2021-06-06T20:08:45');
74
+
75
+ const result5 = DateUtils.formatForInput('');
76
+ expect(result5).toBeUndefined();
77
+
78
+ const result51 = DateUtils.formatForInput(null);
79
+ expect(result51).toBeUndefined();
74
80
  });
75
81
 
76
82
  test('Tests for isExpired', () => {
@@ -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);
@@ -49,7 +49,7 @@ export declare namespace DateUtils {
49
49
  * @param date Input date
50
50
  * @param hasSecondOrType 'undefined' for date only, 'false' for hour:minute only, 'true' for all, or input field type
51
51
  */
52
- function formatForInput(date?: Date | string | null, hasSecondOrType?: boolean | string): string;
52
+ function formatForInput(date?: Date | string | null, hasSecondOrType?: boolean | string): string | undefined;
53
53
  /**
54
54
  * Get month's days
55
55
  * @param year Year
@@ -97,11 +97,12 @@ var DateUtils;
97
97
  * @param hasSecondOrType 'undefined' for date only, 'false' for hour:minute only, 'true' for all, or input field type
98
98
  */
99
99
  function formatForInput(date, hasSecondOrType) {
100
+ // Return when null
101
+ if (date == null || date === '')
102
+ return undefined;
100
103
  // Parse string as date
101
104
  if (typeof date === 'string')
102
105
  date = new Date(date);
103
- // Default is now
104
- date !== null && date !== void 0 ? date : (date = new Date());
105
106
  const hasSecond = typeof hasSecondOrType === 'string'
106
107
  ? hasSecondOrType === 'date'
107
108
  ? undefined
@@ -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
@@ -49,7 +49,7 @@ export declare namespace DateUtils {
49
49
  * @param date Input date
50
50
  * @param hasSecondOrType 'undefined' for date only, 'false' for hour:minute only, 'true' for all, or input field type
51
51
  */
52
- function formatForInput(date?: Date | string | null, hasSecondOrType?: boolean | string): string;
52
+ function formatForInput(date?: Date | string | null, hasSecondOrType?: boolean | string): string | undefined;
53
53
  /**
54
54
  * Get month's days
55
55
  * @param year Year
@@ -94,11 +94,12 @@ export var DateUtils;
94
94
  * @param hasSecondOrType 'undefined' for date only, 'false' for hour:minute only, 'true' for all, or input field type
95
95
  */
96
96
  function formatForInput(date, hasSecondOrType) {
97
+ // Return when null
98
+ if (date == null || date === '')
99
+ return undefined;
97
100
  // Parse string as date
98
101
  if (typeof date === 'string')
99
102
  date = new Date(date);
100
- // Default is now
101
- date !== null && date !== void 0 ? date : (date = new Date());
102
103
  const hasSecond = typeof hasSecondOrType === 'string'
103
104
  ? hasSecondOrType === 'date'
104
105
  ? undefined
@@ -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.96",
4
4
  "description": "TypeScript shared utilities and functions",
5
5
  "main": "lib/cjs/index.js",
6
6
  "module": "lib/mjs/index.js",
@@ -58,8 +58,8 @@
58
58
  "@types/lodash.isequal": "^4.5.6",
59
59
  "jest": "^29.5.0",
60
60
  "jest-environment-jsdom": "^29.5.0",
61
- "ts-jest": "^29.0.5",
62
- "typescript": "^5.0.2"
61
+ "ts-jest": "^29.1.0",
62
+ "typescript": "^5.0.3"
63
63
  },
64
64
  "dependencies": {
65
65
  "lodash.isequal": "^4.5.0"
package/src/DateUtils.ts CHANGED
@@ -153,12 +153,12 @@ export namespace DateUtils {
153
153
  date?: Date | string | null,
154
154
  hasSecondOrType?: boolean | string
155
155
  ) {
156
+ // Return when null
157
+ if (date == null || date === '') return undefined;
158
+
156
159
  // Parse string as date
157
160
  if (typeof date === 'string') date = new Date(date);
158
161
 
159
- // Default is now
160
- date ??= new Date();
161
-
162
162
  const hasSecond =
163
163
  typeof hasSecondOrType === 'string'
164
164
  ? hasSecondOrType === 'date'
@@ -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