Skip to content

use Symbol.hasInstance #46

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 11, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 16 additions & 23 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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));
Expand Down Expand Up @@ -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, {
Expand All @@ -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;