-
Notifications
You must be signed in to change notification settings - Fork 131
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
Comments
I know this is probably too late for you, but hopefully it helps someone. When I addressed this by adding a 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. |
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) |
NVM, it still appears to be global for the whole parser. Fuck me... |
Hello Team,
can u help me please. How can i use moment in a formatter?
The Exception is:
I think the Problem is that Parser dont know moment. But how can i realize that?
The text was updated successfully, but these errors were encountered: