Skip to content

Commit

Permalink
Merge pull request capricorn86#541 from a-wing/master
Browse files Browse the repository at this point in the history
add blob arrayBuffer()
  • Loading branch information
capricorn86 committed Jul 22, 2022
2 parents ee69730 + bd7be1d commit ec5a307
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
14 changes: 14 additions & 0 deletions packages/happy-dom/src/file/Blob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,20 @@ export default class Blob implements IBlob {
return blob;
}


/**
* Returns a Promise that resolves to a ArrayBuffer.
*
* @returns ArrayBuffer.
*/

// Reference:
// https://github.com/web-std/io/blob/c88170bf24f064adfbb3586a21fb76650ca5a9ab/packages/blob/src/blob.js#L139-L148
// https://stackoverflow.com/questions/8609289/convert-a-binary-nodejs-buffer-to-javascript-arraybuffer
public async arrayBuffer(): Promise<ArrayBuffer> {
return new Uint8Array(this._buffer).buffer
}

/**
* Returns a Promise that resolves to a text.
*
Expand Down
14 changes: 14 additions & 0 deletions packages/happy-dom/test/file/Blob.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,20 @@ describe('Blob', () => {
});
});

// Reference:
// https://github.com/web-std/io/blob/c88170bf24f064adfbb3586a21fb76650ca5a9ab/packages/blob/test/blob.spec.js#L35-L44
describe('arrayBuffer()', () => {
it('Returns "Promise<ArrayBuffer>".', async () => {
const str = 'TEST';
const blob = new Blob([str]);
const buffer = await blob.arrayBuffer();
const result = new Uint8Array(buffer);
for (let i = 0; i < result.length; ++i) {
expect(result[i]).toBe(str[i].charCodeAt(0));
}
});
});

describe('toString()', () => {
it('Returns "[object Blob]".', () => {
const blob = new Blob(['TEST']);
Expand Down

0 comments on commit ec5a307

Please sign in to comment.