Skip to content

Commit e98d462

Browse files
committed
refactor(binary): reduce code dupe, remove invalid branches
1 parent db2ddd0 commit e98d462

File tree

1 file changed

+14
-24
lines changed

1 file changed

+14
-24
lines changed

lib/binary.js

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -27,27 +27,19 @@ class Binary {
2727
!(buffer instanceof Uint8Array) &&
2828
!Array.isArray(buffer)
2929
) {
30-
throw new Error('only String, Buffer, Uint8Array or Array accepted');
30+
throw new TypeError('only String, Buffer, Uint8Array or Array accepted');
3131
}
3232

33-
if (buffer instanceof Number) {
34-
this.sub_type = buffer;
35-
this.position = 0;
36-
} else {
37-
this.sub_type = subType == null ? BSON_BINARY_SUBTYPE_DEFAULT : subType;
38-
this.position = 0;
39-
}
33+
this.sub_type = subType == null ? BSON_BINARY_SUBTYPE_DEFAULT : subType;
34+
this.position = 0;
4035

4136
if (buffer != null && !(buffer instanceof Number)) {
4237
// Only accept Buffer, Uint8Array or Arrays
4338
if (typeof buffer === 'string') {
4439
// Different ways of writing the length of the string for the different types
4540
if (typeof Buffer !== 'undefined') {
4641
this.buffer = new Buffer(buffer);
47-
} else if (
48-
typeof Uint8Array !== 'undefined' ||
49-
Object.prototype.toString.call(buffer) === '[object Array]'
50-
) {
42+
} else if (typeof Uint8Array !== 'undefined' || Array.isArray(buffer)) {
5143
this.buffer = writeStringToArray(buffer);
5244
} else {
5345
throw new TypeError('only String, Buffer, Uint8Array or Array accepted');
@@ -64,8 +56,6 @@ class Binary {
6456
} else {
6557
this.buffer = new Array(Binary.BUFFER_SIZE);
6658
}
67-
// Set position to start of buffer
68-
this.position = 0;
6959
}
7060
}
7161

@@ -105,7 +95,7 @@ class Binary {
10595
} else {
10696
let buffer = null;
10797
// Create a new buffer (typed or normal array)
108-
if (Object.prototype.toString.call(this.buffer) === '[object Uint8Array]') {
98+
if (isUint8Array(this.buffer)) {
10999
buffer = new Uint8Array(new ArrayBuffer(Binary.BUFFER_SIZE + this.buffer.length));
110100
} else {
111101
buffer = new Array(Binary.BUFFER_SIZE + this.buffer.length);
@@ -142,7 +132,7 @@ class Binary {
142132
if (typeof Buffer !== 'undefined' && Buffer.isBuffer(this.buffer)) {
143133
buffer = new Buffer(this.buffer.length + string.length);
144134
this.buffer.copy(buffer, 0, 0, this.buffer.length);
145-
} else if (Object.prototype.toString.call(this.buffer) === '[object Uint8Array]') {
135+
} else if (isUint8Array(this.buffer)) {
146136
// Create a new buffer
147137
buffer = new Uint8Array(new ArrayBuffer(this.buffer.length + string.length));
148138
// Copy the content
@@ -169,10 +159,7 @@ class Binary {
169159
this.position =
170160
offset + string.length > this.position ? offset + string.length : this.position;
171161
// offset = string.length;
172-
} else if (
173-
Object.prototype.toString.call(string) === '[object Uint8Array]' ||
174-
(Object.prototype.toString.call(string) === '[object Array]' && typeof string !== 'string')
175-
) {
162+
} else if (isUint8Array(string) || (Array.isArray(string) && typeof string !== 'string')) {
176163
for (let i = 0; i < string.length; i++) {
177164
this.buffer[offset++] = string[i];
178165
}
@@ -246,10 +233,9 @@ class Binary {
246233
return this.buffer.slice(0, this.position);
247234
} else {
248235
// Create a new buffer to copy content to
249-
const newBuffer =
250-
Object.prototype.toString.call(this.buffer) === '[object Uint8Array]'
251-
? new Uint8Array(new ArrayBuffer(this.position))
252-
: new Array(this.position);
236+
const newBuffer = isUint8Array(this.buffer)
237+
? new Uint8Array(new ArrayBuffer(this.position))
238+
: new Array(this.position);
253239

254240
// Copy content
255241
for (let i = 0; i < this.position; i++) {
@@ -296,6 +282,10 @@ class Binary {
296282
*/
297283
const BSON_BINARY_SUBTYPE_DEFAULT = 0;
298284

285+
function isUint8Array(obj) {
286+
return Object.prototype.toString.call(obj) === '[object Uint8Array]';
287+
}
288+
299289
/**
300290
* @ignore
301291
*/

0 commit comments

Comments
 (0)