From 94c5bd45a05ace215de33233c7846c0216a9f85c Mon Sep 17 00:00:00 2001 From: Nick K Date: Thu, 29 Jul 2021 15:41:17 +0300 Subject: [PATCH 1/2] Allow usage of iterable object in Blob constructor. --- index.js | 7 ++++--- test.js | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 3d9d162..38b4b0a 100644 --- a/index.js +++ b/index.js @@ -58,9 +58,10 @@ const _Blob = class Blob { * @param {{ type?: string }} [options] */ constructor(blobParts = [], options = {}) { + const parts = []; let size = 0; - const parts = blobParts.map(element => { + for (const element of blobParts) { let part; if (ArrayBuffer.isView(element)) { part = new Uint8Array(element.buffer.slice(element.byteOffset, element.byteOffset + element.byteLength)); @@ -73,8 +74,8 @@ const _Blob = class Blob { } size += ArrayBuffer.isView(part) ? part.byteLength : part.size; - return part; - }); + parts.push(part); + } const type = options.type === undefined ? '' : String(options.type); diff --git a/test.js b/test.js index 48067e8..bdde857 100644 --- a/test.js +++ b/test.js @@ -36,6 +36,41 @@ test('Blob ctor parts', async t => { t.is(await blob.text(), 'abcdefg[object Object]foo='); }); +test("Blob ctor threats an object with @@iterator as a sequence", async t => { + const blob = new Blob({[Symbol.iterator]: Array.prototype[Symbol.iterator]}) + + t.is(blob.size, 0) + t.is(await blob.text(), "") +}) + +test("Blob ctor reads blob parts from object with @@iterator", async t => { + const input = ["one", "two", "three"] + const expected = input.join("") + + const blob = new Blob({ + * [Symbol.iterator]() { + yield* input + } + }) + + t.is(blob.size, new TextEncoder().encode(expected).byteLength) + t.is(await blob.text(), expected) +}) + +test("Blob ctor threats a string as a sequence", async t => { + const expected = "abc" + const blob = new Blob(expected) + + t.is(await blob.text(), expected) +}) + +test("Blob ctor threats Uint8Array as a sequence", async t => { + const input = [1, 2, 3] + const blob = new Blob(new Uint8Array(input)) + + t.is(await blob.text(), input.join("")) +}) + test('Blob size', t => { const data = 'a=1'; const blob = new Blob([data]); From fabbd8a74fa1a9f50ad9cad4bb8d041cf1a7e0df Mon Sep 17 00:00:00 2001 From: Nick K Date: Thu, 29 Jul 2021 15:51:11 +0300 Subject: [PATCH 2/2] Fix ESLint errors --- test.js | 46 +++++++++++++++++++++++----------------------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/test.js b/test.js index bdde857..388bd68 100644 --- a/test.js +++ b/test.js @@ -36,40 +36,40 @@ test('Blob ctor parts', async t => { t.is(await blob.text(), 'abcdefg[object Object]foo='); }); -test("Blob ctor threats an object with @@iterator as a sequence", async t => { - const blob = new Blob({[Symbol.iterator]: Array.prototype[Symbol.iterator]}) +test('Blob ctor threats an object with @@iterator as a sequence', async t => { + const blob = new Blob({[Symbol.iterator]: Array.prototype[Symbol.iterator]}); - t.is(blob.size, 0) - t.is(await blob.text(), "") -}) + t.is(blob.size, 0); + t.is(await blob.text(), ''); +}); -test("Blob ctor reads blob parts from object with @@iterator", async t => { - const input = ["one", "two", "three"] - const expected = input.join("") +test('Blob ctor reads blob parts from object with @@iterator', async t => { + const input = ['one', 'two', 'three']; + const expected = input.join(''); const blob = new Blob({ * [Symbol.iterator]() { - yield* input + yield * input; } - }) + }); - t.is(blob.size, new TextEncoder().encode(expected).byteLength) - t.is(await blob.text(), expected) -}) + t.is(blob.size, new TextEncoder().encode(expected).byteLength); + t.is(await blob.text(), expected); +}); -test("Blob ctor threats a string as a sequence", async t => { - const expected = "abc" - const blob = new Blob(expected) +test('Blob ctor threats a string as a sequence', async t => { + const expected = 'abc'; + const blob = new Blob(expected); - t.is(await blob.text(), expected) -}) + t.is(await blob.text(), expected); +}); -test("Blob ctor threats Uint8Array as a sequence", async t => { - const input = [1, 2, 3] - const blob = new Blob(new Uint8Array(input)) +test('Blob ctor threats Uint8Array as a sequence', async t => { + const input = [1, 2, 3]; + const blob = new Blob(new Uint8Array(input)); - t.is(await blob.text(), input.join("")) -}) + t.is(await blob.text(), input.join('')); +}); test('Blob size', t => { const data = 'a=1';