The parsing process can be extended with custom functions. It allows to parse non trivial files with ease.

It works both in the browser and NodeJS as it is powered by jDataView.

API

Primitive Structures:

Unsigned Int: uint8, uint16, uint32

Signed Int: int8, int16, int32

Float: float32, float64

String: char, string(len)

Array: array(type, len)

BitField: (bitCount)

Position: tell, skip(len), seek(pos), seek(pos, func)

Conditionals: if(predicate, type)

jParser Methods:

parse(value): Run the parsing, can be used recursively.

Number: Reads bitfield of given length in left-to-right mode and returns them as unsigned integer
(so you can work with them using simple JavaScript binary operators).
Please note that you can mix bitfields with primitive and complex types in one structure or even use
them in own functions, but ALWAYS make sure that consecutive bitfields are padded to integer
byte count (or 8*N bit count) before reading any other data types; most popular data formats
already follow this rule but better to check out when writing own structures if you don't want
to get unexpected behavior.

Function: Calls the function.

String: Dereferences the value in the structure.

Array: Function call, the function is the first element and arguments are the following.

Object: Returns an object with the same keys and parses the values.

tell(): Return the current position.

skip(count): Advance in the file by count bytes.

seek(position): Go to position.

seek(position, callback): Go to position, execute the callback and return to the previous position.

current: The current object being parsed. See it as a way to use what has been parsed just before.

Helpers
It is really easy to make new primitive types. You can either use existing constructions such as objects (float3) or arrays (float4). In case you want to do something more complicated, you always have the option to define a new function and use this.parse to keep parsing (hex32, string0).

Back Reference Instead of using an integer for the array size, you can put a function that will return an integer. In this function, you can use this.current to reference the englobing object being parsed.

Caveats

This tool works thanks to a feature that is not in the Javascript specification: When you iterate over an object keys, the keys will be listed in their order of insertion. Note that Chrome and Opera do not respect this implicit rule for keys that are numbers.

If you follow those two rules, the library will work in all the current Javascript implementations.

Do not start a key name with a digit

Do not put the same key twice in the same object

Demos

ICO Parser. This is a basic example to parse a binary file in NodeJS. It shows how to solve many common issues with binary file parsing.