Skip to content

Commit dd190c7

Browse files
committed
Update README
1 parent edece35 commit dd190c7

File tree

1 file changed

+44
-27
lines changed

1 file changed

+44
-27
lines changed

README.md

Lines changed: 44 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ and [binary](https://github.com/substack/node-binary).
4040

4141
```javascript
4242
// Module import
43-
var Parser = require("binary-parser").Parser;
43+
const Parser = require("binary-parser").Parser;
4444

4545
// Build an IP packet header Parser
46-
var ipHeader = new Parser()
46+
const ipHeader = new Parser()
4747
.endianess("big")
4848
.bit4("version")
4949
.bit4("headerLength")
@@ -65,12 +65,22 @@ var ipHeader = new Parser()
6565
});
6666

6767
// Prepare buffer to parse.
68-
var buf = Buffer.from("450002c5939900002c06ef98adc24f6c850186d1", "hex");
68+
const buf = Buffer.from("450002c5939900002c06ef98adc24f6c850186d1", "hex");
6969

7070
// Parse buffer and show result
7171
console.log(ipHeader.parse(buf));
7272
```
7373

74+
## Installation
75+
76+
You can install `binary-parser ` via npm:
77+
78+
```bash
79+
npm install binary-parser
80+
```
81+
82+
The npm package provides entry points for both CommonJS and ES modules.
83+
7484
## API
7585

7686
### new Parser()
@@ -98,7 +108,7 @@ returned by the 64 bit is `bigint`.
98108
greater. Lower version will throw a runtime error.
99109

100110
```javascript
101-
var parser = new Parser()
111+
const parser = new Parser()
102112
// Signed 32-bit integer (little endian)
103113
.int32le("a")
104114
// Unsigned 8-bit integer
@@ -119,7 +129,7 @@ Parse bytes as a floating-point value and stores it to a variable named
119129
`name`.
120130

121131
```javascript
122-
var parser = new Parser()
132+
const parser = new Parser()
123133
// 32-bit floating value (big endian)
124134
.floatbe("a")
125135
// 64-bit floating value (little endian)
@@ -182,7 +192,7 @@ keys:
182192
object. If function it reads until the function returns true.
183193

184194
```javascript
185-
var parser = new Parser()
195+
const parser = new Parser()
186196
// Statically sized array
187197
.array("data", {
188198
type: "int32",
@@ -254,11 +264,11 @@ an object which can have the following keys:
254264
`choices`, this parser is used.
255265

256266
```javascript
257-
var parser1 = ...;
258-
var parser2 = ...;
259-
var parser3 = ...;
267+
const parser1 = ...;
268+
const parser2 = ...;
269+
const parser3 = ...;
260270

261-
var parser = new Parser().uint8("tagValue").choice("data", {
271+
const parser = new Parser().uint8("tagValue").choice("data", {
262272
tag: "tagValue",
263273
choices: {
264274
1: parser1, // if tagValue == 1, execute parser1
@@ -294,7 +304,7 @@ when called after another function which would advance the internal buffer
294304
offset.
295305

296306
```javascript
297-
var parser = new Parser()
307+
const parser = new Parser()
298308
// this call advances the buffer offset by
299309
// a variable (i.e. unknown to us) number of bytes
300310
.string("name", {
@@ -324,7 +334,7 @@ Define what endianess to use in this parser. `endianess` can be either
324334
`"little"` or `"big"`. The default endianess of `Parser` is set to big-endian.
325335

326336
```javascript
327-
var parser = new Parser()
337+
const parser = new Parser()
328338
.endianess("little")
329339
// You can specify endianess explicitly
330340
.uint16be("a")
@@ -342,9 +352,9 @@ to have an instance of it.
342352
Especially, the parser may reference itself:
343353

344354
```javascript
345-
var stop = new Parser();
355+
const stop = new Parser();
346356

347-
var parser = new Parser()
357+
const parser = new Parser()
348358
.namely("self") // use 'self' to refer to the parser itself
349359
.uint8("type")
350360
.choice("data", {
@@ -372,7 +382,7 @@ var parser = new Parser()
372382
// /
373383
// 0
374384

375-
var buffer = Buffer.from([
385+
const buffer = Buffer.from([
376386
2,
377387
/* left -> */ 3,
378388
/* one -> */ 1, /* -> */ 0,
@@ -405,11 +415,11 @@ An example of referencing other patches:
405415
```javascript
406416
// the line below registers the name "self", so we will be able to use it in
407417
// `twoCells` as a reference
408-
var parser = Parser.start().namely("self");
418+
const parser = Parser.start().namely("self");
409419

410-
var stop = Parser.start().namely("stop");
420+
const stop = Parser.start().namely("stop");
411421

412-
var twoCells = Parser.start()
422+
const twoCells = Parser.start()
413423
.namely("twoCells")
414424
.nest("left", { type: "self" })
415425
.nest("right", { type: "stop" });
@@ -423,7 +433,7 @@ parser.uint8("type").choice("data", {
423433
}
424434
});
425435

426-
var buffer = Buffer.from([2, /* left */ 1, 1, 0, /* right */ 0]);
436+
const buffer = Buffer.from([2, /* left */ 1, 1, 0, /* right */ 0]);
427437

428438
parser.parse(buffer);
429439
```
@@ -447,12 +457,12 @@ will pass it on to a parser for further processing.
447457
```javascript
448458
const zlib = require("zlib");
449459
// A parser to run on the data returned by the wrapper
450-
var textParser = Parser.start()
460+
const textParser = Parser.start()
451461
.string("text", {
452462
zeroTerminated: true,
453463
});
454464

455-
var mainParser = Parser.start()
465+
const mainParser = Parser.start()
456466
// Read length of the data to wrap
457467
.uint32le("length")
458468
// Read wrapped data
@@ -486,7 +496,7 @@ These options can be used in all parsers.
486496
- `formatter` - Function that transforms the parsed value into a more desired
487497
form.
488498
```javascript
489-
var parser = new Parser().array("ipv4", {
499+
const parser = new Parser().array("ipv4", {
490500
type: uint8,
491501
length: "4",
492502
formatter: function(arr) {
@@ -504,12 +514,12 @@ These options can be used in all parsers.
504514

505515
```javascript
506516
// simple maginc number validation
507-
var ClassFile = Parser.start()
517+
const ClassFile = Parser.start()
508518
.endianess("big")
509519
.uint32("magic", { assert: 0xcafebabe });
510520

511521
// Doing more complex assertion with a predicate function
512-
var parser = new Parser()
522+
const parser = new Parser()
513523
.int16le("a")
514524
.int16le("b")
515525
.int16le("c", {
@@ -545,7 +555,7 @@ you need to call `.useContextVars()` to enable it.
545555
- `$root` - This field references the root structure.
546556

547557
```javascript
548-
var parser = new Parser()
558+
const parser = new Parser()
549559
.useContextVars()
550560
.nest("header", {
551561
type: new Parser().uint32("length"),
@@ -566,7 +576,7 @@ you need to call `.useContextVars()` to enable it.
566576
variable will be available only when using the `length` mode for arrays.
567577

568578
```javascript
569-
var parser = new Parser()
579+
const parser = new Parser()
570580
.useContextVars()
571581
.nest("header", {
572582
type: new Parser().uint32("length"),
@@ -587,9 +597,16 @@ you need to call `.useContextVars()` to enable it.
587597
```
588598

589599
## Examples
600+
590601
See `example/` for real-world examples.
591602

592-
## Support
603+
## Benchmarks
604+
605+
A benchmark script to compare the parsing performance with binparse, structron
606+
and destruct.js is available under `benchmark/`.
607+
608+
## Contributing
609+
593610
Please report issues to the
594611
[issue tracker](https://github.com/keichi/binary-parser/issues) if you have
595612
any difficulties using this module, found a bug, or request a new feature.

0 commit comments

Comments
 (0)