Skip to content

Commit eed6a1d

Browse files
authored
Add nullish values checking in Symbol.hasInstance (#82)
* Add nullish values checking in Symbol.hasInstance * Fix for a test description * Add an extension. * Disable unicorn/import-index rule. * Fix a typo
1 parent 3b12a39 commit eed6a1d

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

from.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
const {statSync, createReadStream} = require('fs');
2-
const Blob = require('./index.js');
32
const DOMException = require('domexception');
43

4+
// eslint-disable-next-line unicorn/import-index
5+
const Blob = require('./index.js');
6+
57
/**
68
* @param {string} path filepath on the disk
79
* @returns {Blob}
@@ -27,7 +29,7 @@ class BlobDataItem {
2729
this.mtime = options.mtime;
2830
}
2931

30-
// Slicing arguments is first validated and formated
32+
// Slicing arguments is first validated and formated
3133
// to not be out of range by Blob.prototype.slice
3234
slice(start, end) {
3335
return new BlobDataItem({

index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ class Blob {
160160

161161
static [Symbol.hasInstance](object) {
162162
return (
163+
object &&
163164
typeof object === 'object' &&
164165
typeof object.stream === 'function' &&
165166
object.stream.length === 0 &&

test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,3 +160,19 @@ test('Reading after modified should fail', async t => {
160160
const error = await blob.text().catch(error => error);
161161
t.is(error.name, 'NotReadableError');
162162
});
163+
164+
test('Blob-ish class is an instance of Blob', t => {
165+
class File {
166+
stream() { }
167+
168+
get [Symbol.toStringTag]() {
169+
return 'File';
170+
}
171+
}
172+
173+
t.true(new File() instanceof Blob);
174+
});
175+
176+
test('Instanceof check returns false for nullish values', t => {
177+
t.false(null instanceof Blob);
178+
});

0 commit comments

Comments
 (0)