Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/juampi92/typedstruct.js
A simple library for Typed Structs in js for reading a binary file.
https://github.com/juampi92/typedstruct.js
Last synced: 9 days ago
JSON representation
A simple library for Typed Structs in js for reading a binary file.
- Host: GitHub
- URL: https://github.com/juampi92/typedstruct.js
- Owner: juampi92
- Created: 2014-07-17T01:14:23.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2015-08-28T14:57:24.000Z (about 9 years ago)
- Last Synced: 2024-11-01T10:07:40.362Z (15 days ago)
- Language: JavaScript
- Homepage:
- Size: 203 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# TypedStruct.js
[![Build Status](https://travis-ci.org/juampi92/TypedStruct.js.svg)](https://travis-ci.org/juampi92/TypedStruct.js)A simple plugin for Typed Structs in js for reading a binary file.
### Install
Usin npm
```
npm install typedstruct.js
```Using Bower:
```
bower install typedstruct.js
```Or download it from [here](https://github.com/juampi92/TypedStruct.js/releases/).
### Create Struct
```js
TypedStruct.add('struct_name', {
// Property name : type
'name_of_property': 'int',
// Also you could use previously defined structures
'sub_struct': 'sub_struct_name',
// Or specify arrays. First you tell it's type, and then it's dimensions (as needed)
'array_property': [ 'short' , 2 , 2 , ... ],
});
```### Basic Types
```js
// Name bytes
'byte': 1
'Ubyte': 1
'char': 1
'short': 2
'Ushort': 2
'int' : 4
'Uint': 4
'long': 4
'Ulong': 4
'float': 4
'double': 8
```### Use
##### Create a struct
```js
TypedStruct.add('point', {
'x': 'int',
'y': 'int',
'z': 'int'
});
```Now, given a binary file (arrayBuffer) and a DataView:
```js
var dataview = DataView(arrayBuffer);// Assuming that it's a binary content of one vector
var DataViewCursor = TypedStruct.from(dataview);
var point = DataViewCursor.create('point');// Now the DataViewCursor is increased the size of the
// point (in this case, each int is 4, so 3x4 = 24)
var cursor = DataViewCursor.cursor;// Keep creating more structures, and never mind about
// the cursor, as long as they are in order
var int = DataViewCursor.create('Uint');
```##### LittleEndian
Default value is `true`.
```js
structs.setLittleEndian(false);
```##### Use DataView:
```js
var DataViewCursor = TypedStruct.from(dataview, starting_offset); // 0 by default// Get the cursor position anytime
var cursor = DataViewCursor.cursor; // cursor = 0// Set the cursor position anytime
DataViewCursor.setCursor(10); // cursor = 10// Increment the cursor position anytime
DataViewCursor.incrementCursor(15); // cursor = 15// The cursor increments itself when creating structures
DataViewCursor.create('int'); // cursor = 15 + 4 = 19
```Except for create (which returns the created structure), every DataViewCursor method is chainable.
##### Create Arrays
```js
var DataViewCursor = TypedStruct.from(dataview, offset);var array = DataViewCursor.create('int', 10);
// Now array is an Array of 10 integers
```##### Char Support
If you create a char type, the output will be a single character in string format. If you create an array of chars, the result will be a string
### To-Do
- MultiDimension arrays (currently 1, 2 and 3 dimensions)
- Strings for array of chars
- support for int4/uint4 and flags
- Allow type declarations directly (uint8 instead of Ubyte)