Skip to content

Commit

Permalink
added support for DataView
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielJDufour committed Nov 28, 2020
1 parent 61570c6 commit 55a3b44
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 10 deletions.
25 changes: 15 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,23 @@ const url = context.toDataURL('image/jpeg');
const arrayBuffer = await toab(url);
```

# convert data view to array buffer
```javascript
const arrayBuffer = await toab(dataView)
```

# convert typed arrays to array buffers
```javascript
const arrayBuffer = await toab(Int8Array)
const arrayBuffer = await toab(Uint8Array)
const arrayBuffer = await toab(Int16Array)
const arrayBuffer = await toab(Uint16Array)
const arrayBuffer = await toab(Int32Array)
const arrayBuffer = await toab(Uint32Array)
const arrayBuffer = await toab(Float32Array)
const arrayBuffer = await toab(Float64Array)
const arrayBuffer = await toab(BigInt64Array)
const arrayBuffer = await toab(BigUint64Array)
const arrayBuffer = await toab(int8Array)
const arrayBuffer = await toab(uint8Array)
const arrayBuffer = await toab(int16Array)
const arrayBuffer = await toab(uint16Array)
const arrayBuffer = await toab(int32Array)
const arrayBuffer = await toab(uint32Array)
const arrayBuffer = await toab(float32Array)
const arrayBuffer = await toab(float64Array)
const arrayBuffer = await toab(bigInt64Array)
const arrayBuffer = await toab(bigUint64Array)
```

# convert text to an array buffer (in UTF-8)
Expand Down
7 changes: 7 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,10 @@ test("converting data url in NodeJS", async t => {
t.is(ab.byteLength, 3979);
fs.writeFileSync("./data/test.tmp.jpg", Buffer.from(ab));
});

test("converting dataview", async t => {
const buffer = Buffer.alloc(1024);
const dataView = new DataView(await toab(buffer, { debug: false }));
const ab = await toab(dataView);
t.true(ab instanceof ArrayBuffer);
});
1 change: 1 addition & 0 deletions toab.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ function toab(data, { debug = false } = { debug: false }) {
data.byteOffset + data.byteLength
);
} else if (
data instanceof DataView ||
data instanceof Int8Array ||
data instanceof Uint8Array ||
data instanceof Int16Array ||
Expand Down

0 comments on commit 55a3b44

Please sign in to comment.