Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/component/struct
C style structs for reading and writing to Uint8Arrays
https://github.com/component/struct
Last synced: 13 days ago
JSON representation
C style structs for reading and writing to Uint8Arrays
- Host: GitHub
- URL: https://github.com/component/struct
- Owner: component
- Created: 2013-04-30T01:10:11.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2013-04-30T01:13:15.000Z (over 11 years ago)
- Last Synced: 2024-05-08T17:06:07.481Z (8 months ago)
- Language: JavaScript
- Size: 96.7 KB
- Stars: 9
- Watchers: 7
- Forks: 1
- Open Issues: 5
-
Metadata Files:
- Readme: Readme.md
- Changelog: History.md
Awesome Lists containing this project
README
# struct
C struct style buffer views for reading and writing structures of varying types.
## Installation
$ component install component/struct
## Example
```js
var struct = require('struct');// buffer big enough for all 3 structs
var buf = new Uint8Array(3 * (32 + 5 + 16));var Pet = struct({
id: 'uint32',
name: 'string',
age: 'uint16'
});// write
var tobi = new Pet({
id: 1,
name: 'Tobi',
age: 2
});var loki = new Pet({
id: 2,
name: 'Loki',
age: 2
});var jane = new Pet({
id: 3,
name: 'Jane',
age: 6
});var off = 0;
var pets = [tobi, loki, jane];
for (var i = 0; i < pets.length; i++) {
off = pets[i].write(buf, off);
}// read
var n = 3;
var off = 0;
var pet = new Pet;
while (n--) {
off = pet.read(buf, off);
console.log('%s (%s) is %s years old', pet.name, pet.id, pet.age);
}
```## API
...
## License
MIT