Last active
December 31, 2015 05:49
-
-
Save discretepackets/7943811 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
var Dissolve = require("dissolve"); | |
var parser = Dissolve().loop(function(end) { | |
this.uint8("numHouses") | |
.tap(function() { | |
this.housesCount = 0; | |
}) | |
.loop("houses", function(end) { | |
this.uint8("numWindows") | |
.tap(function() { | |
this.windowsCount = 0; | |
}) | |
.loop("windows", function(end) { | |
this.uint8("window"); | |
if (++this.windowsCount == this.vars.numWindows) return end(); | |
}) | |
if (++this.housesCount == this.vars.numHouses) return end(); | |
}) | |
.tap(function() { | |
this.push(this.vars); | |
this.vars = Object.create(null); | |
}); | |
}); | |
parser.on("readable", function() { | |
var e; | |
while (e = parser.read()) { | |
console.log(e); | |
} | |
}); | |
/* | |
{ | |
houses: [ | |
{ | |
windows: [ | |
{ window: 1 }, | |
{ window: 2 }, | |
{ window: 3 } | |
], | |
doors: [ | |
{ door: 1 } | |
] | |
}, | |
{ | |
windows: [ | |
{ window: 1 } | |
], | |
doors: [ | |
{ door: 1 }, | |
{ door: 2 } | |
] | |
} | |
] | |
} | |
*/ | |
parser.write(new Buffer([ | |
0x02, // Number of houses - 2 houses | |
0x03, // Number of windows - 3 windows ** House 1 | |
0xa1, // window 1 | |
0xa2, // window 2 | |
0xa3, // window 3 | |
0x01, // Number of doors - 1 door | |
0xd1, // door 1 | |
0x01, // Number of windows - 1 window ** House 2 | |
0xa1, // window 1 | |
0x02, // Number of doors - 2 doors | |
0xd1, // door 1 | |
0xd2 // door 2 | |
])); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment