binary-parser
Advanced tools
Comparing version 1.1.1 to 1.1.2
@@ -65,2 +65,3 @@ //======================================================================================== | ||
this.endian = 'be'; | ||
this.constructorFn = null; | ||
}; | ||
@@ -188,2 +189,12 @@ | ||
Parser.prototype.create = function(constructorFn) { | ||
if (!(constructorFn instanceof Function)) { | ||
throw new Error('Constructor must be a Function object.'); | ||
} | ||
this.constructorFn = constructorFn; | ||
return this; | ||
}; | ||
Parser.prototype.getCode = function() { | ||
@@ -195,3 +206,7 @@ var ctx = new Context(); | ||
ctx.pushCode('var vars = {};'); | ||
if (this.constructorFn) { | ||
ctx.pushCode('var vars = new constructorFn();'); | ||
} else { | ||
ctx.pushCode('var vars = {};'); | ||
} | ||
ctx.pushCode('var offset = 0;'); | ||
@@ -212,3 +227,3 @@ ctx.pushCode('if (!Buffer.isBuffer(buffer)) {'); | ||
Parser.prototype.compile = function() { | ||
this.compiled = new Function('buffer', 'callback', this.getCode()); | ||
this.compiled = new Function('buffer', 'callback', 'constructorFn', this.getCode()); | ||
}; | ||
@@ -257,3 +272,3 @@ | ||
return this.compiled(buffer, callback); | ||
return this.compiled(buffer, callback, this.constructorFn); | ||
}; | ||
@@ -260,0 +275,0 @@ |
{ | ||
"name": "binary-parser", | ||
"version": "1.1.1", | ||
"version": "1.1.2", | ||
"description": "Blazing-fast binary parser builder", | ||
@@ -5,0 +5,0 @@ "main": "lib/binary_parser.js", |
@@ -82,5 +82,9 @@ # Binary-parser | ||
If parser's `async` option is `true`, then a callback function has to be passed as an | ||
argument. This callback should take two arguements like other node.js callbacks: | ||
argument. This callback should take two arguments like other node.js callbacks: | ||
`function(err, result)`. | ||
### create(constructorFunction) | ||
Set the constructor function that should be called to create the object returned from | ||
the `parse` method. | ||
### [u]int{8, 16, 32}{le, be}(name [,options]) | ||
@@ -90,3 +94,3 @@ Parse bytes as an integer and store it in a variable named `name`. `name` should consist | ||
Number of bits can be chosen from 8, 16 and 32. | ||
Byte-ordering can be either `l` for litte endian or `b` for big endian. | ||
Byte-ordering can be either `l` for little endian or `b` for big endian. | ||
With no prefix, it parses as a signed number, with `u` prefixed as an unsigned number. | ||
@@ -133,3 +137,3 @@ | ||
### buffer(name [,options]) | ||
Parse bytes as a string. `name` should consist only of alpha numeric characters and start | ||
Parse bytes as a buffer. `name` should consist only of alpha numeric characters and start | ||
with an alphabet. `options` is an object; following options are available: | ||
@@ -238,3 +242,3 @@ | ||
### compile() | ||
Compile this parser on-the-fly and chache its result. Usually, there is no need to | ||
Compile this parser on-the-fly and cache its result. Usually, there is no need to | ||
call this method directly, since it's called when `parse(buffer)` is executed | ||
@@ -241,0 +245,0 @@ for the first time. |
@@ -469,2 +469,22 @@ var assert = require('assert'); | ||
describe('Constructors', function() { | ||
it('should create a custom object type', function() { | ||
function Person () { | ||
this.name = '' | ||
}; | ||
Person.prototype.toString = function () { return '[object Person]'; }; | ||
var parser = | ||
Parser.start() | ||
.create(Person) | ||
.string('name', { | ||
zeroTerminated: true | ||
}); | ||
var buffer = new Buffer('John Doe\0'); | ||
var person = parser.parse(buffer); | ||
assert.ok(person instanceof Person); | ||
assert.equal(person.name, 'John Doe'); | ||
}); | ||
}); | ||
describe('Utilities', function() { | ||
@@ -471,0 +491,0 @@ it('should count size for fixed size structs', function() { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
106971
1304
308