deconstruct-js

Home > deconstruct-js > after

after() function

Lets you analyze previous bytes to declare an appropiate Deconstructor for the following ones.

Signature:

declare function after<T, U>(previous: Deconstructor<T>, innerFn: (deconstruction: Deconstruction<T>) => Deconstructor<U>): Deconstructor<U>;

Parameters

Parameter Type Description
previous Deconstructor<T>  
innerFn (deconstruction: Deconstruction<T>) => Deconstructor<U>  

Returns:

Deconstructor<U>

Example

const d = require("deconstruct")
// This Buffer consists of [length of string, ascii data] pairs, repeated three times.
const buf = Buffer.from([
// length    string data
  0x05,      0x68, 0x65, 0x6c, 0x6c, 0x6f,
  0x05,      0x77, 0x6f, 0x72, 0x6c, 0x64,
  0x01,      0x21
])
const stringPair = d.after(d.u8(), len => d.string(len, "ascii"))
const stringList = d.list(stringPair, 3)
console.log(d.fromBuffer(stringList, buf)) // -> ["hello", "world", "!"]