Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can i use moment in a formatter? #75

Closed
mok-liee opened this issue Feb 9, 2018 · 3 comments
Closed

How can i use moment in a formatter? #75

mok-liee opened this issue Feb 9, 2018 · 3 comments

Comments

@mok-liee
Copy link

mok-liee commented Feb 9, 2018

Hello Team,

can u help me please. How can i use moment in a formatter?

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

let time = function(timestamp) {
	return moment(timestamp, 'YYMMDDHHmmssSS').format('YYYY-MM-DD HH:mm:ss.SS');
};

let Telegram = new Parser()
	.string('timestamp', {encoding: 'hex', length: 7, formatter: time});

The Exception is:

evalmachine.<anonymous>:9
        return moment(timestamp, 'YYMMDDHHmmssSS').format('YYYY-MM-DD HH:mm:ss.SS');
        ^

ReferenceError: moment is not defined
    at Parser.<anonymous> (evalmachine.<anonymous>:9:2)
...

I think the Problem is that Parser dont know moment. But how can i realize that?

@ElbowBaggins
Copy link

I know this is probably too late for you, but hopefully it helps someone.

When Parser invokes your formatter, it does so with Function.prototype.apply() and passes itself as the this argument. As a result, the function is now running in the context of the Parser and not the lexical context that you are expecting.

I addressed this by adding a $imports property to Parser.prototype where one could add anything they'd like to make available from within the Parser's context. Basically, using the above example, you want something like this.

const Parser = require('binary-parser').Parser;
const moment = require('moment');
Parser.prototype.$imports = {
    moment: moment
};

let time = function(timestamp) {
    return this.$imports.moment(timestamp, 'YYMMDDHHmmssSS').format('YYYY-MM-DD HH:mm:ss.SS');
};

let Telegram = new Parser().string('timestamp', { encoding: 'hex', length: 7, formatter: time });

You can use this approach to inject pretty much whatever you want in there.

@keichi keichi closed this as completed Jun 6, 2019
@p0358
Copy link

p0358 commented Dec 12, 2019

Here's what appears to be possible, if you want to add a property to specific Parser object, and not a global:

let opts = parser.encoderOpts;
opts.test = "test";
parser.encoderSetOptions(opts);

parser = parser.array(name, {
    /* ... */
    formatter: item => this.encoderOpts.test
});

(this would replace all objects there to "test", but it shows that it's indeed accessible this way)
[you might also be interested in related comment in #33]

@p0358
Copy link

p0358 commented Dec 12, 2019

NVM, it still appears to be global for the whole parser. Fuck me...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants