Skip to content

Commit 7664d98

Browse files
committed
Make file size more compact
1 parent 0de5602 commit 7664d98

File tree

2 files changed

+45
-46
lines changed

2 files changed

+45
-46
lines changed

src/lib/helpers.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,21 @@ export function getFileContent(file: Buffer | string): string {
88
return buffer.toString();
99
}
1010

11-
const BYTE_SIZES = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
11+
const BYTE_SIZES = ['B', 'K', 'M', 'G', 'TB', 'PB', 'EB', 'ZB', 'YB'];
1212
// Bytes
1313
const SIZE_BASE = 1024;
1414

1515
/**
1616
* Format number of bytes as string
17-
* Source @see https://stackoverflow.com/a/18650828/388951
1817
*/
19-
export function formatBytes(bytes: number, decimals = 2): string {
20-
if (bytes === 0) return `0 ${BYTE_SIZES[0]}`;
18+
export function formatBytes(bytes: number, maximumSignificantDigits = 3): string {
19+
if (bytes === 0) return `0${BYTE_SIZES[0]}`;
2120

2221
const exponent = Math.floor(Math.log(bytes) / Math.log(SIZE_BASE));
2322
const value = bytes / Math.pow(SIZE_BASE, exponent);
2423

2524
// `parseFloat` removes trailing zero
26-
return `${parseFloat(value.toFixed(decimals))} ${BYTE_SIZES[exponent]}`;
25+
return `${new Intl.NumberFormat('default', { maximumSignificantDigits }).format(value)}${BYTE_SIZES[exponent]}`;
2726
}
2827

2928
export function formatPercent(value: number, total: number, fractionDigits?: number): string {

tests/unit/helpers.test.ts

Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ import type { MappingRange } from '../../src/lib/types';
1414
describe('helpers', () => {
1515
describe('formatBytes', () => {
1616
const tests: { bytes: number; expected: string }[] = [
17-
{ bytes: 0, expected: '0 B' },
18-
{ bytes: 1024, expected: '1 KB' },
19-
{ bytes: 27291, expected: '26.65 KB' },
20-
{ bytes: 6232294, expected: '5.94 MB' },
17+
{ bytes: 0, expected: '0B' },
18+
{ bytes: 1024, expected: '1K' },
19+
{ bytes: 27291, expected: '26.7K' },
20+
{ bytes: 6232294, expected: '5.94M' },
2121
];
2222

2323
tests.forEach(({ bytes, expected }) => {
@@ -81,43 +81,43 @@ describe('helpers', () => {
8181
position: [number, number];
8282
expected: boolean;
8383
}[] = [
84-
{
85-
name: 'should handle CR LF',
86-
string: 'function calculate(a, b) {\r\n return a + b;\r\n}\r\n',
87-
position: [2, 15],
88-
expected: true,
89-
},
90-
{
91-
name: 'should handle LF',
92-
string: 'function calculate(a, b) {\n return a + b;\n}\n',
93-
position: [2, 15],
94-
expected: true,
95-
},
96-
{
97-
name: 'should handle string w/o EOLs',
98-
string: 'Math.random().toString(36).substring(2);',
99-
position: [1, 40],
100-
expected: false,
101-
},
102-
{
103-
name: 'should handle out of range line',
104-
string: 'const result = await process();\nconsole.log(result)',
105-
position: [3, 10],
106-
expected: false,
107-
},
108-
{
109-
name: 'should handle out of range column',
110-
string: 'const result = await process();\nconsole.log(result)\n',
111-
position: [2, 23],
112-
expected: false,
113-
},
114-
{
115-
name: 'should handle string with unicode',
116-
string: 'A 🦊 went a hunting in the 🌲.\r\nThe 🦊 wasted no ⏳ in talking.\r\n',
117-
position: [2, 30],
118-
expected: true,
119-
},
120-
];
84+
{
85+
name: 'should handle CR LF',
86+
string: 'function calculate(a, b) {\r\n return a + b;\r\n}\r\n',
87+
position: [2, 15],
88+
expected: true,
89+
},
90+
{
91+
name: 'should handle LF',
92+
string: 'function calculate(a, b) {\n return a + b;\n}\n',
93+
position: [2, 15],
94+
expected: true,
95+
},
96+
{
97+
name: 'should handle string w/o EOLs',
98+
string: 'Math.random().toString(36).substring(2);',
99+
position: [1, 40],
100+
expected: false,
101+
},
102+
{
103+
name: 'should handle out of range line',
104+
string: 'const result = await process();\nconsole.log(result)',
105+
position: [3, 10],
106+
expected: false,
107+
},
108+
{
109+
name: 'should handle out of range column',
110+
string: 'const result = await process();\nconsole.log(result)\n',
111+
position: [2, 23],
112+
expected: false,
113+
},
114+
{
115+
name: 'should handle string with unicode',
116+
string: 'A 🦊 went a hunting in the 🌲.\r\nThe 🦊 wasted no ⏳ in talking.\r\n',
117+
position: [2, 30],
118+
expected: true,
119+
},
120+
];
121121

122122
tests.forEach(({ name, string, position, expected }) => {
123123
it(name, () => {

0 commit comments

Comments
 (0)