Skip to content

Nested object constructors #92

Open

Description

@ppslim

Are nested parsers intended to function with the constructor?

The following sample. Parsing from male works, I get a MaleOBJ instance.

But nesting male in male_nesteda, the constructor is not used.

const Parser = require("binary-parser").Parser;

function MaleOBJ() {
    this.name = "";
};
MaleOBJ.prototype.toString = function() {
    return "[object MaleOBJ]";
};

var male = new Parser()
    .create(MaleOBJ)
    .string("name", {
        zeroTerminated: true
    });
    
var nested_malea = new Parser()
    .string("other_name", {
        zeroTerminated: true
    })
    .nest("other", {
        type: male
    });

var buffer = Buffer.from("Alpha male\0John Doe\0");
var denested  = male.parse(buffer);
console.log(typeof denested);
console.log(denested);

var nesteda = nested_malea.parse(buffer);
console.log(typeof nesteda);
console.log(nesteda);

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions