Skip to content

Commit 1f4527e

Browse files
committed
initial implementation
0 parents  commit 1f4527e

File tree

5 files changed

+68
-0
lines changed

5 files changed

+68
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
*.sw*

Makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
test:
3+
@node_modules/.bin/mocha --reporter spec
4+
5+
.PHONY: test
6+

index.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2+
module.exports = extract;
3+
4+
var comma = ','.charCodeAt(0);
5+
6+
function extract(buf, key){
7+
for (var i = 0; i < buf.length; i++) {
8+
var match = true;
9+
for (var j = 0; j < key.length; j++) {
10+
if (buf[i + j] != key.charCodeAt(j)) {
11+
match = false;
12+
break;
13+
}
14+
}
15+
if (!match) continue;
16+
17+
var start = i + key.length + 2;
18+
var end;
19+
for (var j = start; j < buf.length; j++) {
20+
if (buf[j] == comma) {
21+
end = j;
22+
break;
23+
}
24+
}
25+
26+
var json = buf.toString('utf8', start, end);
27+
return JSON.parse(json);
28+
}
29+
}
30+

package.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"name": "binary-extract",
3+
"description": "Extract a value from a binary json blob",
4+
"version": "0.0.0",
5+
"repository": "segmentio/binary-extract",
6+
"license": "MIT",
7+
"devDependencies": {
8+
"mocha": "~1.18.0"
9+
}
10+
}

test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
var assert = require('assert');
3+
var equal = assert.equal;
4+
var extract = require('./');
5+
6+
var obj = {
7+
foo: 'bar',
8+
bar: 3,
9+
baz: {
10+
beep: 'boop'
11+
}
12+
}
13+
var buf = Buffer(JSON.stringify(obj));
14+
15+
describe('extract(buf, key)', function(){
16+
it('should extract a value', function(){
17+
equal(extract(buf, 'foo'), 'bar');
18+
})
19+
})
20+

0 commit comments

Comments
 (0)