new Format()
- See:
-
For a list of the builtin formats, check the source.
Examples
// This is a format describing a sequence of two integers of diffent lengths
const exampleFormat =
new Format()
.uint16('A 16 bit integer', 'orange')
.uint32('A longer 32 bit integer', 'blue');
// This is a format for a Pascal-like string, i.e. a string with its length prefixed.
const pascalString =
new Format()
.uint8('String length', 'orange', 'len')
.array('String contents', 'len', 'blue');
// The line above is a shortcut for:
// .array('String contents', vars => vars.len, 'blue');
Methods
-
array(namenullable, length, coloropt) → {Format}
-
Adds a span of raw bytes to the format.
Parameters:
Name Type Attributes Default Description namestring <nullable>
Name of this segment.
lengthinteger | function | string How many times to repeat the child format. If it is an integer, it represents a fixed number of repetitions. If it is a function, it is called with a dictionary of all the available variables and is expected to return an integer. If it is a string, it is treated as a shortcut for
vars => vars[length].colorstring <optional>
white Color of the span.
Returns:
Format -
array(namenullable, length, child) → {Format}
-
Repeates a child format a fixed number of times.
Parameters:
Name Type Attributes Description namestring <nullable>
Name of this segment.
lengthinteger | function | string How many times to repeat the child format. If it is an integer, it represents a fixed number of repetitions. If it is a function, it is called with a dictionary of all the available variables and is expected to return an integer. If it is a string, it is treated as a shortcut for
vars => vars[length].childFormat Child format to repeat.
Returns:
Format -
child(name, c) → {Format}
-
Adds a child format to this format. This is equivalent to
array(name, 1, c).Parameters:
Name Type Description namestring Name of this span.
cFormat Child format to insert.
Returns:
Format -
child(c) → {Format}
-
Adds a child format to this format. This is equivalent to
array(null, 1, c).Parameters:
Name Type Description cFormat Child format to insert.
Returns:
Format -
endgroup() → {Format}
-
Ends a group started with group(). There must be a matching call to group().
Returns:
FormatExample
// This generates the following two spans with the following names: // Group > A byte // Group > Another byte new Format() .group('Group') .uint8('A byte') .uint8('Another byte') .endgroup(); -
group(name) → {Format}
-
Groups multiple spans under a single name. There must be a matching call to endgroup().
Parameters:
Name Type Description namestring Group name.
Returns:
FormatExample
// This generates the following two spans with the following names: // Group > A byte // Group > Another byte new Format() .group('Group') .uint8('A byte') .uint8('Another byte') .endgroup(); -
sequence(name, c) → {Format}
-
Adds an infinite sequence of the given child format to this format. This is equivalent to
array(name, Infinity, c).Parameters:
Name Type Description namestring Name of this span.
cFormat Child format to repeat indefinitely.
Returns:
Format -
sequence(c) → {Format}
-
Adds an infinite sequence of the given child format to this format. This is equivalent to
array(null, Infinity, c).Parameters:
Name Type Description cFormat Child format to repeat indefinitely.
Returns:
Format