Home Manual Reference Source Test

Function

Static Public Summary
public

allEqual(name: string, testValue: mixed)

assert deep equality each value in Object|Array

public

arrayLength(name: string, length: number | string)

assert array to be a given length

public

bind each function in an object to a given context

public

bitmask(name: string, mask: number, assertMatch: boolean)

asserts a variable matches a given bitmask

public

bitmask(name: string, maskObject: Object | number): *

map a value by checking whether it has some bits set

public

callback(name: string, fn: function, testname: string)

assert value via callback

public

callback(name: string, fn: function(val: *))

replace a variable in the stack by a mapped version of itself

public

deepEqual(name: string, value: object)

assert equality objects

public

equal(name: string, value: mixed)

assert strict equal single value

public

exists(name: string)

asserts a variable exists in the first place

public

find(name: string, array: Array<Object>, attr: string, src: string)

retrieve the first object from an array of objects, matching a specified attribute against a specified value like findAll, but returning only the first element

public

findAll(name: string, array: Array<Object>, attr: string, src: string)

retrieve a filtered array of objects from an array of objects, matching a specified attribute against a specified value

public

get(name: string, accessable: array | object | string, src: string)

retrieve a value from an accessable type (like array[0] or object['foo'])

public

inBounds(name: string, value: array)

assert value to be within the bounds of an array

public

includes(name: string, arr: array | object)

assert array|object to contain item

public

push(name: string)

replace Corrode#vars completely with a value from Corrode#vars especially useful when pushing a variable further up in the stack

public

bind each function in an object with a tap to a given context

Static Public

public allEqual(name: string, testValue: mixed) source

import {allEqual} from 'corrode/src/assert/index.js'

assert deep equality each value in Object|Array

Params:

NameTypeAttributeDescription
name string

key of the object|array to test

testValue mixed

comparision, undefined for auto-detect

Throw:

*

TypeError assertion-error

Test:

public arrayLength(name: string, length: number | string) source

import {arrayLength} from 'corrode/src/assert/index.js'

assert array to be a given length

Params:

NameTypeAttributeDescription
name string

key of the value to test

length number | string

comparision

Throw:

*

TypeError assertion-error

Test:

public bindObject(obj: object, ctx: object): object source

import {bindObject} from 'corrode/src/utils/index.js'

bind each function in an object to a given context

Params:

NameTypeAttributeDescription
obj object

object with functions

ctx object

context

Return:

object

copy of object obj with each function bound to ctx

Test:

public bitmask(name: string, mask: number, assertMatch: boolean) source

import {bitmask} from 'corrode/src/assert/index.js'

asserts a variable matches a given bitmask

Params:

NameTypeAttributeDescription
name string

key of the value to test

mask number

bitmask to match

assertMatch boolean

true: should match; false: shouldn't match

Throw:

*

TypeError assertion-error

Test:

public bitmask(name: string, maskObject: Object | number): * source

import {bitmask} from 'corrode/src/map/index.js'

map a value by checking whether it has some bits set

Params:

NameTypeAttributeDescription
name string

identifier of the variable, to write to Corrode#vars

maskObject Object | number

Object or number by which to check the bits of the variable to map

Return:

*

Example:

map via number
parser.uint8('bits').map.bitmask('bits', 0x80)

// [0b10111110] => { bits: true }
map via object
parser.uint8('bits').map.bitmask('bits', {
  isCompressed: 0x80,
  isReadOnly: 0x40
})

// [0b10111110] => { bits: { isCompressed: true, isReadOnly: false } }

public callback(name: string, fn: function, testname: string) source

import {callback} from 'corrode/src/assert/index.js'

assert value via callback

Params:

NameTypeAttributeDescription
name string

key of the value to test

fn function

callback

testname string

optional test-name

Throw:

*

TypeError assertion-error

Test:

public callback(name: string, fn: function(val: *)) source

import {callback} from 'corrode/src/map/index.js'

replace a variable in the stack by a mapped version of itself

Params:

NameTypeAttributeDescription
name string

identifier of the variable to map

fn function(val: *)

map-function

Example:

parser.uint8('value').map.callback('value', val => (val - 1) * 2)

// [21] => { value: 10 }

public deepEqual(name: string, value: object) source

import {deepEqual} from 'corrode/src/assert/index.js'

assert equality objects

Params:

NameTypeAttributeDescription
name string

key of the object to test

value object

comparision

Throw:

*

TypeError assertion-error

Test:

public equal(name: string, value: mixed) source

import {equal} from 'corrode/src/assert/index.js'

assert strict equal single value

Params:

NameTypeAttributeDescription
name string

key of the value to test

value mixed

comparision

Throw:

*

TypeError assertion-error

Test:

public exists(name: string) source

import {exists} from 'corrode/src/assert/index.js'

asserts a variable exists in the first place

Params:

NameTypeAttributeDescription
name string

key of the value to test

Throw:

*

TypeError assertion-error

Test:

public find(name: string, array: Array<Object>, attr: string, src: string) source

import {find} from 'corrode/src/map/index.js'

retrieve the first object from an array of objects, matching a specified attribute against a specified value like findAll, but returning only the first element

Params:

NameTypeAttributeDescription
name string

identifier of the variable, to write to Corrode#vars

array Array<Object>

array, containing the objects to filter

attr string

identifier of the attribute from an object of array to compare against

src string
  • optional
  • default: name

Corrode#vars-identifier to read from

Throw:

Error

when no object can be found

Example:

parser.uint8('matchAgainst').map.find('matchAgainst', [
  { id: 1, name: 'foo' },
  { id: 7, name: 'bar' },
  { id: 4, name: 'qux' }
], 'id')

// [4] => { matchAgainst: { id: 4, name: 'qux' } }

// [2] => Error cannot find object!

Test:

public findAll(name: string, array: Array<Object>, attr: string, src: string) source

import {findAll} from 'corrode/src/map/index.js'

retrieve a filtered array of objects from an array of objects, matching a specified attribute against a specified value

Params:

NameTypeAttributeDescription
name string

identifier of the variable, to write to Corrode#vars

array Array<Object>

array, containing the objects to filter

attr string

identifier of the attribute from an object of array to compare against

src string
  • optional
  • default: name

Corrode#vars-identifier to read from

Throw:

Error

when no object can be found

Example:

parser.uint8('matchAgainst').map.findAll('matchAgainst', [
  { children: 1, name: 'foo' },
  { children: 2, name: 'bar' },
  { children: 2, name: 'qux' }
], 'children')

// [2] => { matchAgainst: [
//   { children: 2, name: 'bar' },
//   { children: 2, name: 'qux' }
// ]}

// [1] => { matchAgainst: [
//   { children: 1, name: 'foo' }
// ]}

Test:

public get(name: string, accessable: array | object | string, src: string) source

import {get} from 'corrode/src/map/index.js'

retrieve a value from an accessable type (like array[0] or object['foo'])

Params:

NameTypeAttributeDescription
name string

identifier of the variable to map

accessable array | object | string

accessable variable

src string
  • optional
  • default: name

identifier of the variable in Corrode#vars by which to access accessable

Example:

get from array
parser.uint8('accessor').map.get('accessor', ['A', 'B', 'C', 'D'])

// [2] => { accessor: 'C' }
get from object
parser.terminatedString('accessor').map.get('accessor', { foo: 'A', bar: 'B', qux: 'C' })

// ['q', 'u', 'x', 0x00] => { accessor: 'C' }

Test:

public inBounds(name: string, value: array) source

import {inBounds} from 'corrode/src/assert/index.js'

assert value to be within the bounds of an array

Params:

NameTypeAttributeDescription
name string

key of the number to test

value array

comparision

Throw:

*

TypeError assertion-error

Test:

public includes(name: string, arr: array | object) source

import {includes} from 'corrode/src/assert/index.js'

assert array|object to contain item

Params:

NameTypeAttributeDescription
name string

key of the value to test

arr array | object

comparision

Throw:

*

TypeError assertion-error

Test:

public push(name: string) source

import {push} from 'corrode/src/map/index.js'

replace Corrode#vars completely with a value from Corrode#vars especially useful when pushing a variable further up in the stack

Params:

NameTypeAttributeDescription
name string
  • optional
  • default: 'values'

identifier of the variable being used as replacement

Example:

push loop-variables up
parser.loop('array', function(){
    this
        .uint8('value')
        .map.double()
        .map.push('value');
});

// [1, 2, 3, 4] => { array: [2, 4, 6, 8] }
push values in an extension
Corrode.addExtension('doStuff', function(){
    this
        .uint32('address')
        .tap(function(){
            this.vars.address = `0x${this.vars.address.toString(16)}`;
        })
        .map.push('address');
});

parser.ext.doStuff('hexAddress');

// [245] => { hexAddress: '0xf5' }

public tapBindObject(obj: object, ctx: object): object source

import {tapBindObject} from 'corrode/src/utils/index.js'

bind each function in an object with a tap to a given context

Params:

NameTypeAttributeDescription
obj object

object with functions

ctx object

context

Return:

object

copy of object with each function wrapped in a tap

Test: