npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

typedstruct.js

v0.1.0

Published

Simple library for reading binary structs

Downloads

5

Readme

TypedStruct.js

A simple plugin for Typed Structs in js for reading a binary file.

Create Struct

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

  // 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
 TypedStruct.add('point', {
   'x': 'int',
   'y': 'int',
   'z': 'int'
 });

Now, given a binary file (arrayBuffer) and a DataView:

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.

structs.setLittleEndian(false);
Use DataView:
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
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)