From 7f4ba1f6e2a44dca976f0ff6e1f08df094370a44 Mon Sep 17 00:00:00 2001 From: redhoodsu Date: Tue, 26 Nov 2024 20:35:02 +0800 Subject: [PATCH] feat(fileSize): turn back to bytes --- DOC.md | 8 ++++++++ DOC_CN.md | 8 ++++++++ i18n/fileSize.md | 6 ++++++ index.json | 8 +++++--- src/fileSize.js | 36 ++++++++++++++++++++++++++++++++---- test/fileSize.js | 11 ++++++++++- 6 files changed, 69 insertions(+), 8 deletions(-) diff --git a/DOC.md b/DOC.md index 7738615f..3813ca18 100644 --- a/DOC.md +++ b/DOC.md @@ -6186,6 +6186,7 @@ Turn bytes into human readable file size. ```typescript function fileSize(bytes: number): string; +function fileSize(size: string): number; ``` @@ -6195,6 +6196,13 @@ function fileSize(bytes: number): string; |bytes |File bytes | |return|Readable file size| +Turn human readable file size into bytes. + +|Name |Desc | +|------|------------------| +|size |Readable file size| +|return|File bytes | + ```javascript fileSize(5); // -> '5' fileSize(1500); // -> '1.46K' diff --git a/DOC_CN.md b/DOC_CN.md index c3bc59d3..88d6fe2e 100644 --- a/DOC_CN.md +++ b/DOC_CN.md @@ -6179,6 +6179,7 @@ fibonacci(3); // -> 2 ```typescript function fileSize(bytes: number): string; +function fileSize(size: string): number; ``` @@ -6188,6 +6189,13 @@ function fileSize(bytes: number): string; |bytes|文件字节大小| |返回值|易于阅读的文件大小| +将易于阅读的文件大小转换为字节数。 + +|参数名|说明| +|-----|---| +|size|易于阅读的文件大小| +|返回值|文件字节大小| + ```javascript fileSize(5); // -> '5' fileSize(1500); // -> '1.46K' diff --git a/i18n/fileSize.md b/i18n/fileSize.md index f0463516..116b0715 100644 --- a/i18n/fileSize.md +++ b/i18n/fileSize.md @@ -7,3 +7,9 @@ |bytes|文件字节大小| |返回值|易于阅读的文件大小| +将易于阅读的文件大小转换为字节数。 + +|参数名|说明| +|-----|---| +|size|易于阅读的文件大小| +|返回值|文件字节大小| diff --git a/index.json b/index.json index 30c41dd8..f4131a94 100644 --- a/index.json +++ b/index.json @@ -2530,7 +2530,10 @@ ] }, "fileSize": { - "dependencies": [], + "dependencies": [ + "isStr", + "toNum" + ], "description": "Turn bytes into human readable file size.", "env": [ "node", @@ -6416,8 +6419,7 @@ "defaults", "trim", "rtrim", - "filter", - "last" + "filter" ], "description": "Output table string.", "env": [ diff --git a/src/fileSize.js b/src/fileSize.js index c0013ccd..2958f7c9 100644 --- a/src/fileSize.js +++ b/src/fileSize.js @@ -4,6 +4,13 @@ * |------|------------------| * |bytes |File bytes | * |return|Readable file size| + * + * Turn human readable file size into bytes. + * + * |Name |Desc | + * |------|------------------| + * |size |Readable file size| + * |return|File bytes | */ /* example @@ -20,15 +27,36 @@ /* typescript * export declare function fileSize(bytes: number): string; + * export declare function fileSize(size: string): number; */ +_('isStr toNum'); + exports = function(bytes) { - if (bytes <= 0) return '0'; + if (isStr(bytes)) { + const match = bytes.match(regStrSize); - const suffixIdx = Math.floor(Math.log(bytes) / Math.log(1024)); - const val = bytes / Math.pow(2, suffixIdx * 10); + if (!match) return 0; - return +val.toFixed(2) + suffixList[suffixIdx]; + return Math.round(toNum(match[1]) * factor[match[2] || 'B']); + } else { + if (bytes <= 0) return '0'; + + const suffixIdx = Math.floor(Math.log(bytes) / Math.log(1024)); + const val = bytes / Math.pow(2, suffixIdx * 10); + + return +val.toFixed(2) + suffixList[suffixIdx]; + } }; +const factor = { + B: 1, + K: 1024 +}; +factor.M = factor.K * 1024; +factor.G = factor.M * 1024; +factor.T = factor.G * 1024; + const suffixList = ['', 'K', 'M', 'G', 'T']; + +const regStrSize = /^(\d+(?:\.\d+)?) *(K|M|G|T)?$/; diff --git a/test/fileSize.js b/test/fileSize.js index 65749c7b..c6387ea8 100644 --- a/test/fileSize.js +++ b/test/fileSize.js @@ -7,5 +7,14 @@ tests([ [1500000, '1.43M'], [1500000000, '1.4G'], [4015492000, '3.74G'], - [1500000000000, '1.36T'] + [1500000000000, '1.36T'], + ['5', 5], + ['0', 0], + ['761', 761], + ['1.46K', 1495], + ['2.4K', 2458], + ['1.43M', 1499464], + ['1.4G', 1503238554], + ['3.74G', 4015794422], + ['1.36T', 1495335813775] ]);