binary-parser
Advanced tools
Comparing version 0.0.3 to 0.0.4
@@ -27,3 +27,4 @@ //======================================================================================== | ||
'Skip' : null, | ||
'Choice' : null | ||
'Choice' : null, | ||
'Nest' : null | ||
}; | ||
@@ -134,2 +135,13 @@ | ||
Parser.prototype.nest = function(varName, options) { | ||
if (!options.type) { | ||
throw new Error('Type option of nest is not defined.'); | ||
} | ||
if (!(options.type instanceof Parser)) { | ||
throw new Error('Type option of nest must be a Parser object.'); | ||
} | ||
return this.setNextParser('nest', varName, options); | ||
}; | ||
Parser.prototype.endianess = function(endianess) { | ||
@@ -396,2 +408,9 @@ if (endianess !== 'little' && endianess !== 'big') { | ||
Parser.prototype.generateNest = function(ctx) { | ||
ctx.pushCode('{0} = {};', ctx.generateVariable(this.varName)); | ||
ctx.scopes.push(this.varName); | ||
this.options.type.generate(ctx); | ||
ctx.scopes.pop(); | ||
}; | ||
//======================================================================================== | ||
@@ -398,0 +417,0 @@ // class Context |
{ | ||
"name": "binary-parser", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"description": "Blazing-fast binary parser builder", | ||
@@ -5,0 +5,0 @@ "main": "lib/binary_parser.js", |
@@ -171,2 +171,8 @@ # Binary-parser | ||
### nest(name [,options]) | ||
Nest a parser in this position. Parse result of the nested parser is stored in the variable | ||
`name`. | ||
- `type` - (Required) A `Parser` object. | ||
### skip(length) | ||
@@ -173,0 +179,0 @@ Skip parsing for `length` bytes. |
@@ -107,3 +107,4 @@ var fs = require('fs'); | ||
it('should parse JPG file header', function() { | ||
fs.readFile('test/kindle.jpg', function(err, data) { | ||
fs.readFile('test/test.jpg', function(err, data) { | ||
JPEG.parse(data).segments.forEach(function(item) { | ||
@@ -116,6 +117,6 @@ if (item.marker === 0xffd8) { | ||
id: 'JFIF', | ||
version: 257, | ||
unit: 1, | ||
xDensity: 72, | ||
yDensity: 72, | ||
version: 258, | ||
unit: 0, | ||
xDensity: 100, | ||
yDensity: 100, | ||
thumbWidth: 0, | ||
@@ -125,15 +126,2 @@ thumbHeight: 0, | ||
}); | ||
} else if (item.marker === 0xffc0) { | ||
assert.deepEqual(item.segment, { | ||
length: 17, | ||
precision: 8, | ||
width: 1024, | ||
height: 768, | ||
componentCount: 3, | ||
components: [ | ||
{ id: 1, samplingFactor: 34, quantizationTableId: 0 }, | ||
{ id: 2, samplingFactor: 17, quantizationTableId: 1 }, | ||
{ id: 3, samplingFactor: 17, quantizationTableId: 1 } | ||
] | ||
}); | ||
} else if (item.marker === 0xffda) { | ||
@@ -149,3 +137,3 @@ assert.deepEqual(item.segment, { | ||
spectrumStart: 0, | ||
spectrumEnd: 63, | ||
spectrumEnd: 0, | ||
spectrumSelect: 0 | ||
@@ -152,0 +140,0 @@ }); |
@@ -296,2 +296,34 @@ var assert = require('assert'); | ||
describe('Nest parser', function() { | ||
it('should parse nested parsers', function() { | ||
var nameParser = new Parser() | ||
.string('firstName', { | ||
zeroTerminated: true | ||
}) | ||
.string('lastName', { | ||
zeroTerminated: true | ||
}); | ||
var infoParser = new Parser() | ||
.uint8('age'); | ||
var personParser = new Parser() | ||
.nest('name', { | ||
type: nameParser | ||
}) | ||
.nest('info', { | ||
type: infoParser | ||
}); | ||
var buffer = Buffer.concat([new Buffer('John\0Doe\0'), new Buffer([0x20])]); | ||
assert.deepEqual(personParser.parse(buffer), { | ||
name: { | ||
firstName: 'John', | ||
lastName: 'Doe' | ||
}, | ||
info: { | ||
age: 0x20 | ||
} | ||
}); | ||
}); | ||
}); | ||
describe('Utilities', function() { | ||
@@ -298,0 +330,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
Native code
Supply chain riskContains native code (e.g., compiled binaries or shared libraries). Including native code can obscure malicious behavior.
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
13
1101
252
99335
3
1