Skip to content

Commit 930139a

Browse files
committed
feat: detect ArrayBuffer instance via isArrayBuffer()
1 parent c5f8a72 commit 930139a

6 files changed

Lines changed: 64 additions & 5 deletions

File tree

src/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,7 @@
22
export * from './lib/index'
33
export * from './lib/model'
44
export { ErrorMsg } from './lib/config'
5-
export { validB64Chars } from './lib/helper'
5+
export {
6+
isArrayBuffer,
7+
validB64Chars,
8+
} from './lib/helper'

src/lib/browser.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
} from './config'
44
import { fromUint8Array } from './from_buffer'
55
import {
6+
isArrayBuffer,
67
parseDecodeInputBase64,
78
parseEncodeInputString,
89
parseTextDecoder,
@@ -36,7 +37,7 @@ export function fromBuffer(buf: ArrayBuffer | Uint8Array): string {
3637
else if (ArrayBuffer.isView(buf)) {
3738
input = buf
3839
}
39-
else if (buf instanceof ArrayBuffer) {
40+
else if (isArrayBuffer(buf)) {
4041
input = new Uint8Array(buf)
4142
}
4243
else {

src/lib/helper.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,9 @@ export function isBrowser(): boolean {
7777
: true
7878
return ret
7979
}
80+
81+
82+
/** Whether input is instance of ArrayBuffer */
83+
export function isArrayBuffer(buffer: any): boolean {
84+
return buffer && buffer instanceof ArrayBuffer ? true : false
85+
}

src/lib/nodejs.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { ErrorMsg } from './config'
22
import {
3-
parseDecodeInputBase64, parseEncodeInputString,
3+
isArrayBuffer, parseDecodeInputBase64, parseEncodeInputString,
44
} from './helper'
55

66

@@ -36,7 +36,7 @@ export function fromBuffer(buf: ArrayBuffer | Uint8Array): string {
3636
else if (ArrayBuffer.isView(buf)) {
3737
inst = Buffer.from(buf)
3838
}
39-
else if (buf instanceof ArrayBuffer) {
39+
else if (isArrayBuffer(buf)) {
4040
inst = Buffer.from(buf)
4141
}
4242
else {

test/30_helper.test.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,16 @@ import {
1313

1414
import { ErrorMsg } from '../src/index'
1515
import {
16+
isArrayBuffer,
17+
isBrowser,
1618
parseDecodeInputBase64,
1719
parseEncodeInputString,
1820
parseTextDecoder,
1921
parseTextEncoder,
2022
validateEncoder,
2123
} from '../src/lib/helper'
2224

23-
import { input9 } from './config'
25+
import { input44, input9, inputArrayBuffer, inputTypedArrayExcludingUint8Array, inputUint8Array } from './config'
2426

2527

2628
const filename = basename(__filename)
@@ -135,4 +137,26 @@ describe(filename, () => {
135137
})
136138
})
137139

140+
141+
describe('isArrayBuffer() works', () => {
142+
it('with valid input', () => {
143+
inputArrayBuffer.forEach(value => {
144+
const ret = isArrayBuffer(value)
145+
assert(ret === true)
146+
})
147+
})
148+
149+
it('with invalid input', () => {
150+
const arr = input44.concat(
151+
inputTypedArrayExcludingUint8Array,
152+
inputUint8Array,
153+
)
154+
arr.forEach(value => {
155+
const ret = isArrayBuffer(value)
156+
assert(ret === false)
157+
})
158+
})
159+
})
160+
161+
138162
})

test/config.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,3 +119,28 @@ export const input9 = [
119119
'*',
120120
'?',
121121
]
122+
123+
124+
export const inputTypedArrayExcludingUint8Array = [
125+
new Uint16Array(),
126+
new Uint32Array(),
127+
new Uint8ClampedArray(),
128+
new Int8Array(),
129+
new Int16Array(),
130+
new Int32Array(),
131+
new Float32Array(),
132+
new Float64Array(),
133+
new BigInt64Array(),
134+
]
135+
136+
export const inputUint8Array = [
137+
new Uint8Array(0),
138+
new Uint8Array(1),
139+
new Uint8Array(1024),
140+
]
141+
142+
export const inputArrayBuffer = [
143+
new ArrayBuffer(0),
144+
new ArrayBuffer(1),
145+
new ArrayBuffer(1024),
146+
]

0 commit comments

Comments
 (0)