Skip to content

Commit

Permalink
feat(fileSize): turn back to bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
surunzi committed Nov 26, 2024
1 parent 2f67f7c commit 7f4ba1f
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 8 deletions.
8 changes: 8 additions & 0 deletions DOC.md
Original file line number Diff line number Diff line change
Expand Up @@ -6186,6 +6186,7 @@ Turn bytes into human readable file size.

```typescript
function fileSize(bytes: number): string;
function fileSize(size: string): number;
```

</details>
Expand All @@ -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'
Expand Down
8 changes: 8 additions & 0 deletions DOC_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -6179,6 +6179,7 @@ fibonacci(3); // -> 2

```typescript
function fileSize(bytes: number): string;
function fileSize(size: string): number;
```

</details>
Expand All @@ -6188,6 +6189,13 @@ function fileSize(bytes: number): string;
|bytes|文件字节大小|
|返回值|易于阅读的文件大小|

将易于阅读的文件大小转换为字节数。

|参数名|说明|
|-----|---|
|size|易于阅读的文件大小|
|返回值|文件字节大小|

```javascript
fileSize(5); // -> '5'
fileSize(1500); // -> '1.46K'
Expand Down
6 changes: 6 additions & 0 deletions i18n/fileSize.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,9 @@
|bytes|文件字节大小|
|返回值|易于阅读的文件大小|

将易于阅读的文件大小转换为字节数。

|参数名|说明|
|-----|---|
|size|易于阅读的文件大小|
|返回值|文件字节大小|
8 changes: 5 additions & 3 deletions index.json
Original file line number Diff line number Diff line change
Expand Up @@ -2530,7 +2530,10 @@
]
},
"fileSize": {
"dependencies": [],
"dependencies": [
"isStr",
"toNum"
],
"description": "Turn bytes into human readable file size.",
"env": [
"node",
Expand Down Expand Up @@ -6416,8 +6419,7 @@
"defaults",
"trim",
"rtrim",
"filter",
"last"
"filter"
],
"description": "Output table string.",
"env": [
Expand Down
36 changes: 32 additions & 4 deletions src/fileSize.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)?$/;
11 changes: 10 additions & 1 deletion test/fileSize.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]
]);

0 comments on commit 7f4ba1f

Please sign in to comment.