Skip to content

Commit fac29ca

Browse files
committed
Make uint8ArrayToString also accept an ArrayBuffer
Fixes #14
1 parent 7dad071 commit fac29ca

File tree

4 files changed

+30
-6
lines changed

4 files changed

+30
-6
lines changed

index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ console.log(uint8ArrayToString(ja, 'shift-jis'));
144144
//=> 'こんにちは'
145145
```
146146
*/
147-
export function uint8ArrayToString(array: Uint8Array, encoding?: string): string;
147+
export function uint8ArrayToString(array: Uint8Array | ArrayBuffer, encoding?: string): string;
148148

149149
/**
150150
Convert a string to a `Uint8Array` (using UTF-8 encoding).

index.js

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,29 @@
11
const objectToString = Object.prototype.toString;
22
const uint8ArrayStringified = '[object Uint8Array]';
3+
const arrayBufferStringified = '[object ArrayBuffer]';
34

4-
export function isUint8Array(value) {
5+
function isType(value, typeConstructor, typeStringified) {
56
if (!value) {
67
return false;
78
}
89

9-
if (value.constructor === Uint8Array) {
10+
if (value.constructor === typeConstructor) {
1011
return true;
1112
}
1213

13-
return objectToString.call(value) === uint8ArrayStringified;
14+
return objectToString.call(value) === typeStringified;
15+
}
16+
17+
export function isUint8Array(value) {
18+
return isType(value, Uint8Array, uint8ArrayStringified);
19+
}
20+
21+
function isArrayBuffer(value) {
22+
return isType(value, ArrayBuffer, arrayBufferStringified);
23+
}
24+
25+
function isUint8ArrayOrArrayBuffer(value) {
26+
return isUint8Array(value) || isArrayBuffer(value);
1427
}
1528

1629
export function assertUint8Array(value) {
@@ -19,6 +32,12 @@ export function assertUint8Array(value) {
1932
}
2033
}
2134

35+
export function assertUint8ArrayOrArrayBuffer(value) {
36+
if (!isUint8ArrayOrArrayBuffer(value)) {
37+
throw new TypeError(`Expected \`Uint8Array\` or \`ArrayBuffer\`, got \`${typeof value}\``);
38+
}
39+
}
40+
2241
export function toUint8Array(value) {
2342
if (value instanceof ArrayBuffer) {
2443
return new Uint8Array(value);
@@ -95,7 +114,7 @@ const cachedDecoders = {
95114
};
96115

97116
export function uint8ArrayToString(array, encoding = 'utf8') {
98-
assertUint8Array(array);
117+
assertUint8ArrayOrArrayBuffer(array);
99118
cachedDecoders[encoding] ??= new globalThis.TextDecoder(encoding);
100119
return cachedDecoders[encoding].decode(array);
101120
}

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ const array3 = new Uint8Array([7, 8, 9]);
130130
//=> [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
131131
```
132132

133-
### `uint8ArrayToString(array: Uint8Array, encoding?: string = 'utf8'): string`
133+
### `uint8ArrayToString(array: Uint8Array | ArrayBuffer, encoding?: string = 'utf8'): string`
134134

135135
Convert a `Uint8Array` to a string.
136136

test.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,11 @@ test('uint8ArrayToString with encoding', t => {
136136
]), 'shift-jis'), 'こんにちは');
137137
});
138138

139+
test('uint8ArrayToString with ArrayBuffer', t => {
140+
const fixture = new Uint8Array([72, 101, 108, 108, 111]).buffer;
141+
t.is(uint8ArrayToString(fixture), 'Hello');
142+
});
143+
139144
test('uint8ArrayToBase64 and base64ToUint8Array', t => {
140145
const fixture = stringToUint8Array('Hello');
141146
const base64 = uint8ArrayToBase64(fixture);

0 commit comments

Comments
 (0)