use Symbol.hasInstance (#46)

This commit is contained in:
Konstantin Vyatkin
2020-06-11 10:00:58 -04:00
committed by GitHub
parent 126b4ec697
commit 903a4c1bb0
+16 -23
View File
@@ -15,20 +15,6 @@ async function * read(parts) {
}
}
/**
* @template T
* @param {T} object
* @returns {T is Blob}
*/
const isBlob = object => {
return (
typeof object === 'object' &&
typeof object.stream === 'function' &&
typeof object.constructor === 'function' &&
/^(Blob|File)$/.test(object[Symbol.toStringTag])
);
};
class Blob {
/**
* The Blob() constructor returns a new Blob object. The content
@@ -43,13 +29,13 @@ class Blob {
const parts = blobParts.map(element => {
let buffer;
if (Buffer.isBuffer(element)) {
if (element instanceof Buffer) {
buffer = element;
} else if (ArrayBuffer.isView(element)) {
buffer = Buffer.from(element.buffer, element.byteOffset, element.byteLength);
} else if (element instanceof ArrayBuffer) {
buffer = Buffer.from(element);
} else if (isBlob(element)) {
} else if (element instanceof Blob) {
buffer = element;
} else {
buffer = Buffer.from(typeof element === 'string' ? element : String(element));
@@ -167,6 +153,20 @@ class Blob {
return blob;
}
get [Symbol.toStringTag]() {
return 'Blob';
}
static [Symbol.hasInstance](object) {
return (
typeof object === 'object' &&
typeof object.stream === 'function' &&
object.stream.length === 0 &&
typeof object.constructor === 'function' &&
/^(Blob|File)$/.test(object[Symbol.toStringTag])
);
}
}
Object.defineProperties(Blob.prototype, {
@@ -175,11 +175,4 @@ Object.defineProperties(Blob.prototype, {
slice: {enumerable: true}
});
Object.defineProperty(Blob.prototype, Symbol.toStringTag, {
value: 'Blob',
writable: false,
enumerable: false,
configurable: true
});
module.exports = Blob;