I'm using the 'binary-parser' NPM module in node.js and am trying to use my own 'assert' function instead of supplying it inline. I don't know if the problem is that of 'binary-parser' or is that of my ignorance of calling functions and/or function references in javascript.
Here's the code that works but not what I want:
var Parser = require('binary-parser').Parser;
var data = new Buffer([0x23, 0x36, 0x74, 0x0e, 0x01, 0xff, 0xff]);
function range (min, val, max) {
return (min <= val && val <= max);
}
var dataParser = new Parser()
.endianess('little')
.uint8('inside_humidity', {assert: function(x){return(x <= 100);}})
.uint16('barometer', {assert: function(x){return(20000 <= x && x <= 32500);},
formatter: function(x) {return x/1000;}})
.uint16('wind_direction', {assert: function(x){return(x <= 360);}})
.uint16('crc');
console.log(dataParser.parse(data));
I'd like to call my own 'range' function like this:
.uint16('barometer',
{assert: range(20000, x, 32500),
formatter: function(x) {return x/1000;}})
But I get:
ReferenceError: x is not defined
So I tried:
assert: function(x){range(20000, x, 325000)}
and
{assert: function(x){range.bind(this, 20000, x, 32500)},
and get:
ReferenceError: range is not defined
Is it possible to do what I want?