@etsoo/shared 1.2.5 → 1.2.6
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/__tests__/NumberUtils.ts +9 -0
- package/lib/cjs/ArrayUtils.js +1 -1
- package/lib/cjs/ColorUtils.js +1 -1
- package/lib/cjs/DataTypes.js +1 -1
- package/lib/cjs/DateUtils.js +1 -1
- package/lib/cjs/DomUtils.js +1 -1
- package/lib/cjs/ExtendUtils.js +1 -1
- package/lib/cjs/Keyboard.js +1 -1
- package/lib/cjs/NumberUtils.d.ts +7 -0
- package/lib/cjs/NumberUtils.js +14 -1
- package/lib/cjs/StorageUtils.js +1 -1
- package/lib/cjs/Utils.d.ts +1 -1
- package/lib/cjs/Utils.js +1 -1
- package/lib/mjs/NumberUtils.d.ts +7 -0
- package/lib/mjs/NumberUtils.js +13 -0
- package/lib/mjs/Utils.d.ts +1 -1
- package/package.json +3 -3
- package/src/NumberUtils.ts +15 -0
- package/src/Utils.ts +3 -1
package/__tests__/NumberUtils.ts
CHANGED
|
@@ -40,3 +40,12 @@ test('Tests for toExact', () => {
|
|
|
40
40
|
expect(result).not.toBe(0.7);
|
|
41
41
|
expect(result.toExact()).toBe(0.7);
|
|
42
42
|
});
|
|
43
|
+
|
|
44
|
+
test('Tests for toFileSize', () => {
|
|
45
|
+
expect(NumberUtils.formatFileSize(1551859712)).toBe('1.45 GB');
|
|
46
|
+
expect(NumberUtils.formatFileSize(1551859712, 1)).toBe('1.4 GB');
|
|
47
|
+
expect(NumberUtils.formatFileSize(5000)).toBe('4.88 KB');
|
|
48
|
+
expect(NumberUtils.formatFileSize(999949)).toBe('976.51 KB');
|
|
49
|
+
expect(NumberUtils.formatFileSize(1125000)).toBe('1.07 MB');
|
|
50
|
+
expect(NumberUtils.formatFileSize(1125000, 1)).toBe('1.1 MB');
|
|
51
|
+
});
|
package/lib/cjs/ArrayUtils.js
CHANGED
package/lib/cjs/ColorUtils.js
CHANGED
package/lib/cjs/DataTypes.js
CHANGED
package/lib/cjs/DateUtils.js
CHANGED
package/lib/cjs/DomUtils.js
CHANGED
package/lib/cjs/ExtendUtils.js
CHANGED
package/lib/cjs/Keyboard.js
CHANGED
package/lib/cjs/NumberUtils.d.ts
CHANGED
|
@@ -26,6 +26,13 @@ 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
|
+
* Format file size
|
|
31
|
+
* @param size File size
|
|
32
|
+
* @param fractionDigits Fraction digits
|
|
33
|
+
* @returns Result
|
|
34
|
+
*/
|
|
35
|
+
function formatFileSize(size: number, fractionDigits?: number): string;
|
|
29
36
|
/**
|
|
30
37
|
* Get currency symbol or name from ISO code
|
|
31
38
|
* @param code ISO currency code, like USD / CNY
|
package/lib/cjs/NumberUtils.js
CHANGED
|
@@ -50,6 +50,19 @@ var NumberUtils;
|
|
|
50
50
|
return format(input, locale, options);
|
|
51
51
|
}
|
|
52
52
|
NumberUtils.formatMoney = formatMoney;
|
|
53
|
+
/**
|
|
54
|
+
* Format file size
|
|
55
|
+
* @param size File size
|
|
56
|
+
* @param fractionDigits Fraction digits
|
|
57
|
+
* @returns Result
|
|
58
|
+
*/
|
|
59
|
+
function formatFileSize(size, fractionDigits = 2) {
|
|
60
|
+
const i = size === 0 ? 0 : Math.floor(Math.log(size) / Math.log(1024));
|
|
61
|
+
return ((size / Math.pow(1024, i)).toFixed(fractionDigits) +
|
|
62
|
+
' ' +
|
|
63
|
+
['B', 'KB', 'MB', 'GB', 'TB'][i]);
|
|
64
|
+
}
|
|
65
|
+
NumberUtils.formatFileSize = formatFileSize;
|
|
53
66
|
/**
|
|
54
67
|
* Get currency symbol or name from ISO code
|
|
55
68
|
* @param code ISO currency code, like USD / CNY
|
|
@@ -93,4 +106,4 @@ var NumberUtils;
|
|
|
93
106
|
}
|
|
94
107
|
return undefined;
|
|
95
108
|
};
|
|
96
|
-
})(NumberUtils
|
|
109
|
+
})(NumberUtils || (exports.NumberUtils = NumberUtils = {}));
|
package/lib/cjs/StorageUtils.js
CHANGED
package/lib/cjs/Utils.d.ts
CHANGED
|
@@ -140,7 +140,7 @@ export declare namespace Utils {
|
|
|
140
140
|
* @param args Arguments
|
|
141
141
|
* @returns Result
|
|
142
142
|
*/
|
|
143
|
-
const getResult: <R, T = R | DataTypes.Func<R>>(input: T, ...args: T extends DataTypes.Func<R> ? Parameters<T> :
|
|
143
|
+
const getResult: <R, T = R | DataTypes.Func<R>>(input: T, ...args: T extends DataTypes.Func<R> ? Parameters<T> : []) => T extends DataTypes.Func<R> ? ReturnType<T> : T;
|
|
144
144
|
/**
|
|
145
145
|
* Get time zone
|
|
146
146
|
* @returns Timezone
|
package/lib/cjs/Utils.js
CHANGED
package/lib/mjs/NumberUtils.d.ts
CHANGED
|
@@ -26,6 +26,13 @@ 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
|
+
* Format file size
|
|
31
|
+
* @param size File size
|
|
32
|
+
* @param fractionDigits Fraction digits
|
|
33
|
+
* @returns Result
|
|
34
|
+
*/
|
|
35
|
+
function formatFileSize(size: number, fractionDigits?: number): string;
|
|
29
36
|
/**
|
|
30
37
|
* Get currency symbol or name from ISO code
|
|
31
38
|
* @param code ISO currency code, like USD / CNY
|
package/lib/mjs/NumberUtils.js
CHANGED
|
@@ -47,6 +47,19 @@ export var NumberUtils;
|
|
|
47
47
|
return format(input, locale, options);
|
|
48
48
|
}
|
|
49
49
|
NumberUtils.formatMoney = formatMoney;
|
|
50
|
+
/**
|
|
51
|
+
* Format file size
|
|
52
|
+
* @param size File size
|
|
53
|
+
* @param fractionDigits Fraction digits
|
|
54
|
+
* @returns Result
|
|
55
|
+
*/
|
|
56
|
+
function formatFileSize(size, fractionDigits = 2) {
|
|
57
|
+
const i = size === 0 ? 0 : Math.floor(Math.log(size) / Math.log(1024));
|
|
58
|
+
return ((size / Math.pow(1024, i)).toFixed(fractionDigits) +
|
|
59
|
+
' ' +
|
|
60
|
+
['B', 'KB', 'MB', 'GB', 'TB'][i]);
|
|
61
|
+
}
|
|
62
|
+
NumberUtils.formatFileSize = formatFileSize;
|
|
50
63
|
/**
|
|
51
64
|
* Get currency symbol or name from ISO code
|
|
52
65
|
* @param code ISO currency code, like USD / CNY
|
package/lib/mjs/Utils.d.ts
CHANGED
|
@@ -140,7 +140,7 @@ export declare namespace Utils {
|
|
|
140
140
|
* @param args Arguments
|
|
141
141
|
* @returns Result
|
|
142
142
|
*/
|
|
143
|
-
const getResult: <R, T = R | DataTypes.Func<R>>(input: T, ...args: T extends DataTypes.Func<R> ? Parameters<T> :
|
|
143
|
+
const getResult: <R, T = R | DataTypes.Func<R>>(input: T, ...args: T extends DataTypes.Func<R> ? Parameters<T> : []) => T extends DataTypes.Func<R> ? ReturnType<T> : T;
|
|
144
144
|
/**
|
|
145
145
|
* Get time zone
|
|
146
146
|
* @returns Timezone
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@etsoo/shared",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.6",
|
|
4
4
|
"description": "TypeScript shared utilities and functions",
|
|
5
5
|
"main": "lib/cjs/index.js",
|
|
6
6
|
"module": "lib/mjs/index.js",
|
|
@@ -54,12 +54,12 @@
|
|
|
54
54
|
},
|
|
55
55
|
"homepage": "https://github.com/ETSOO/Shared#readme",
|
|
56
56
|
"devDependencies": {
|
|
57
|
-
"@types/jest": "^29.5.
|
|
57
|
+
"@types/jest": "^29.5.2",
|
|
58
58
|
"@types/lodash.isequal": "^4.5.6",
|
|
59
59
|
"jest": "^29.5.0",
|
|
60
60
|
"jest-environment-jsdom": "^29.5.0",
|
|
61
61
|
"ts-jest": "^29.1.0",
|
|
62
|
-
"typescript": "^5.
|
|
62
|
+
"typescript": "^5.1.3"
|
|
63
63
|
},
|
|
64
64
|
"dependencies": {
|
|
65
65
|
"lodash.isequal": "^4.5.0"
|
package/src/NumberUtils.ts
CHANGED
|
@@ -66,6 +66,21 @@ export namespace NumberUtils {
|
|
|
66
66
|
return format(input, locale, options);
|
|
67
67
|
}
|
|
68
68
|
|
|
69
|
+
/**
|
|
70
|
+
* Format file size
|
|
71
|
+
* @param size File size
|
|
72
|
+
* @param fractionDigits Fraction digits
|
|
73
|
+
* @returns Result
|
|
74
|
+
*/
|
|
75
|
+
export function formatFileSize(size: number, fractionDigits: number = 2) {
|
|
76
|
+
const i = size === 0 ? 0 : Math.floor(Math.log(size) / Math.log(1024));
|
|
77
|
+
return (
|
|
78
|
+
(size / Math.pow(1024, i)).toFixed(fractionDigits) +
|
|
79
|
+
' ' +
|
|
80
|
+
['B', 'KB', 'MB', 'GB', 'TB'][i]
|
|
81
|
+
);
|
|
82
|
+
}
|
|
83
|
+
|
|
69
84
|
/**
|
|
70
85
|
* Get currency symbol or name from ISO code
|
|
71
86
|
* @param code ISO currency code, like USD / CNY
|
package/src/Utils.ts
CHANGED
|
@@ -359,7 +359,9 @@ export namespace Utils {
|
|
|
359
359
|
*/
|
|
360
360
|
export const getResult = <R, T = DataTypes.Func<R> | R>(
|
|
361
361
|
input: T,
|
|
362
|
-
...args: T extends DataTypes.Func<R>
|
|
362
|
+
...args: T extends DataTypes.Func<R>
|
|
363
|
+
? Parameters<typeof input>
|
|
364
|
+
: never | []
|
|
363
365
|
): T extends DataTypes.Func<R> ? ReturnType<T> : T => {
|
|
364
366
|
return typeof input === 'function' ? input(...args) : input;
|
|
365
367
|
};
|