Skip to content

Commit 903a4c1

Browse files
authored
use Symbol.hasInstance (#46)
1 parent 126b4ec commit 903a4c1

File tree

1 file changed

+16
-23
lines changed

1 file changed

+16
-23
lines changed

index.js

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,6 @@ async function * read(parts) {
1515
}
1616
}
1717

18-
/**
19-
* @template T
20-
* @param {T} object
21-
* @returns {T is Blob}
22-
*/
23-
const isBlob = object => {
24-
return (
25-
typeof object === 'object' &&
26-
typeof object.stream === 'function' &&
27-
typeof object.constructor === 'function' &&
28-
/^(Blob|File)$/.test(object[Symbol.toStringTag])
29-
);
30-
};
31-
3218
class Blob {
3319
/**
3420
* The Blob() constructor returns a new Blob object. The content
@@ -43,13 +29,13 @@ class Blob {
4329

4430
const parts = blobParts.map(element => {
4531
let buffer;
46-
if (Buffer.isBuffer(element)) {
32+
if (element instanceof Buffer) {
4733
buffer = element;
4834
} else if (ArrayBuffer.isView(element)) {
4935
buffer = Buffer.from(element.buffer, element.byteOffset, element.byteLength);
5036
} else if (element instanceof ArrayBuffer) {
5137
buffer = Buffer.from(element);
52-
} else if (isBlob(element)) {
38+
} else if (element instanceof Blob) {
5339
buffer = element;
5440
} else {
5541
buffer = Buffer.from(typeof element === 'string' ? element : String(element));
@@ -167,6 +153,20 @@ class Blob {
167153

168154
return blob;
169155
}
156+
157+
get [Symbol.toStringTag]() {
158+
return 'Blob';
159+
}
160+
161+
static [Symbol.hasInstance](object) {
162+
return (
163+
typeof object === 'object' &&
164+
typeof object.stream === 'function' &&
165+
object.stream.length === 0 &&
166+
typeof object.constructor === 'function' &&
167+
/^(Blob|File)$/.test(object[Symbol.toStringTag])
168+
);
169+
}
170170
}
171171

172172
Object.defineProperties(Blob.prototype, {
@@ -175,11 +175,4 @@ Object.defineProperties(Blob.prototype, {
175175
slice: {enumerable: true}
176176
});
177177

178-
Object.defineProperty(Blob.prototype, Symbol.toStringTag, {
179-
value: 'Blob',
180-
writable: false,
181-
enumerable: false,
182-
configurable: true
183-
});
184-
185178
module.exports = Blob;

0 commit comments

Comments
 (0)