typed-buffer-struct
TypeScript icon, indicating that this package has built-in type declarations

0.1.2 • Public • Published

typed-buffer-struct

This is a package which helps the user to create a Struct with automatic Buffer creation.

How to use

// import it first
import newStruct from "typed-buffer-struct";

// then, create typed structs with it!
const sStats = newStruct()
  /* 0 to  3 */ .uint("level")
  /* 4 to  7 */ .uint("attack")
  /* 8 to 11 */ .uint("defense")
  .build(); // total of 12 bytes

const sCharacter = newStruct()
  /*  0 to 11 */ .string("name", 12)
  /* 12 to 23 */ .struct("stats", sStats)
  .build(); // total of 24 bytes

const sAccount = newStruct()
  /* 0 to 95 */ .array("characters", 4, (b) => b.struct(sCharacter))
  .build(); // total of 96 bytes

// now, lets create a new account
const account = sAccount();

// lets add a name to the second and third array indexes
account.characters[1].name = "Amazing!";
account.characters[2].name = "Testing";

// and finally, lets get the Buffer
console.log("Account buffer:", account.buffer);

/*
OUTPUT:
0: 000000000000000000000000000000000000000000000000
1: 416d617a696e672100000000000000000000000000000000
2: 54657374696e670000000000000000000000000000000000
3: 000000000000000000000000000000000000000000000000
(here I divided the output in 4 sections, just to show the 4 characters in the account)
*/

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
0.1.20latest
0.1.1-dev.20dev
0.0.20alpha

Version History

VersionDownloads (Last 7 Days)Published
0.1.20
0.1.11
0.1.1-dev.20
0.1.1-dev.10
0.1.00
0.0.80
0.0.70
0.0.60
0.0.50
0.0.40
0.0.20
0.0.10

Package Sidebar

Install

npm i typed-buffer-struct

Weekly Downloads

1

Version

0.1.2

License

MIT

Unpacked Size

70 kB

Total Files

21

Last publish

Collaborators

  • rechdan