Skip to content

Commit 35812e7

Browse files
committed
clean up sizeOfStringInKb
1 parent 818b0db commit 35812e7

File tree

2 files changed

+9
-14
lines changed

2 files changed

+9
-14
lines changed

src/formatting.js

+2-11
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,8 @@ import { puzzleBaseUrl } from './api/urls.js';
1414
* @param {String} value - The value whose size to calculate.
1515
* @param {BufferEncoding} encoding - The strings encoding
1616
*/
17-
export const sizeOfStringInKb = (value, encoding = 'utf-8') => {
18-
if (value == null) {
19-
throw new Error('null or undefined value');
20-
}
21-
22-
if (value === '') {
23-
return '0kb';
24-
}
25-
26-
return `${(Buffer.byteLength(value, encoding) / 1000).toFixed(2)}kb`;
27-
};
17+
export const sizeOfStringInKb = (value, encoding = 'utf-8') =>
18+
value ? `${(Buffer.byteLength(value, encoding) / 1000).toFixed(2)}kb` : '0kb';
2819

2920
/**
3021
* Converts a duration in nanoseconds to a human readable duration, up to seconds.

tests/formatting.test.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,13 @@ describe('formatting', () => {
2424
});
2525

2626
describe('sizeOfStringInKb()', () => {
27-
test.each([null, undefined])('throws if value is: "%s"', (value) => {
28-
expect(() => sizeOfStringInKb(value)).toThrow('null or undefined');
29-
});
27+
test.each([null, undefined, 0, ''])(
28+
'returns 0kb if value is: "%s"',
29+
(value) => {
30+
const result = sizeOfStringInKb(value);
31+
expect(result).toBe('0kb');
32+
}
33+
);
3034

3135
test.each([1234, {}])('throws if value is non-string: "%s"', (value) => {
3236
expect(() => sizeOfStringInKb(value)).toThrow(

0 commit comments

Comments
 (0)