Skip to content

Commit bc3f4ef

Browse files
committed
feat(util): add string byte size function
1 parent 19961a8 commit bc3f4ef

File tree

3 files changed

+17
-0
lines changed

3 files changed

+17
-0
lines changed

Diff for: packages/util/src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export * from './opaqueTypes';
1616
export * from './environment';
1717
export * from './patchObject';
1818
export * from './isPromise';
19+
export * from './string';
1920
export * from './transformer';
2021
export * from './Percent';
2122
export * from './util';

Diff for: packages/util/src/string.ts

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export const StringUtils = {
2+
byteSize: (str: string) => new Blob([str]).size
3+
};

Diff for: packages/util/test/string.test.ts

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { StringUtils } from '../src';
2+
3+
// Test vectors sourced from https://www.javainuse.com/bytesize
4+
5+
describe('StringUtils', () => {
6+
describe('byteSize', () => {
7+
it('returns the byte size of the string', () => {
8+
expect(StringUtils.byteSize('The quick brown fox jumps over the lazy dog')).toEqual(43);
9+
expect(StringUtils.byteSize('helloWorld!')).toEqual(11);
10+
expect(StringUtils.byteSize('👋')).toEqual(4);
11+
});
12+
});
13+
});

0 commit comments

Comments
 (0)