Home
Note
This page has been updated for jDataView 3
Explanation
jDataView is a drop-in replacement for JavaScript's built-in DataView class, adding methods for writing strings, individual bits, arbitrary-sized integers, and more.
Installation
jDataView is published on npm, so you may install it like so:
Note that TypeScript definitions are now included in the
jdataviewpackage automatically.
Usage
jDataView functions as a drop-in replacement for the native DataView API, with extra features on top:
import { jDataView } from "jdataview"; // Construct jDataView the same way you would construct a regular DataView // You may also pass a string or array of numbers, and jDataView will parse // that into an ArrayBuffer automatically const view = new jDataView(new ArrayBuffer(100)); // or you can use the static jDataView.from(..data) method // You can also use `const view = jDataView.from(0x00, 0x01, ...etc)` // The regular methods all work as before view.setInt16(0, 256, true); const msg = "Hello there"; // jDataView maintains an internal byte-cursor, which lets you use // the API in a much more ergonomic way. For example, jDataView's // writeXXX methods are much the same as the setXXX methods, // but the byte-cursor position is used instead of passing an offset view.writeString(msg); view.writeBigInt64(2011n); // seek(pos) changes the byte position of jDataView's internal 'cursor.' view.seek(0); // You need to specify the byte length for strings (which for ASCII text is just the length) console.log(view.getString(msg.length)); // > Hello there // Passing an offset is optional - if you don't, jDataView uses the byte-cursor position // jDataView also has methods that let you use bigger Numbers than JavaScript supports - you'll just lose precision console.log(view.getInt64()); // > 2011