Skip to content
This repository has been archived by the owner on Mar 19, 2024. It is now read-only.

Commit

Permalink
Wrap text and code after 78 chars
Browse files Browse the repository at this point in the history
Personal preference.
  • Loading branch information
sjmulder committed Dec 29, 2016
1 parent c84879e commit 4e3fbd2
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 86 deletions.
36 changes: 24 additions & 12 deletions Changelog.md
Expand Up @@ -13,33 +13,44 @@ Similarly, that's what `writeUncompressed()` as well now:
writeUncompressed({ name: 'Level', value: { ... } });

* Improvement: dropped unused jasmine package dependency.
* Improvement: [#13] read() and write() cycles are now guaranteed to be idempotent.
* Improvement: [#13] read() and write() cycles are now guaranteed to be
idempotent.
* Improvement: [#14] back to consistent structure for parse() result.
* Improvement: [#15] `writer.byteArray()` now accepts both arrays and `Buffer`s.
* Improvement: [#15] `writer.byteArray()` now accepts both arrays and
`Buffer`s.
* Fix: parse() would always throw when used on non-gzipped data.

0.5.0
-----

A merge and slight rework of the terrific improvements by the people at [ProsmarineJS](https://github.com/PrismarineJS/prismarine-nbt).
A merge and slight rework of the terrific improvements by the people at
[ProsmarineJS](https://github.com/PrismarineJS/prismarine-nbt).

* Feature: support for writing archives (to a `Buffer`).
* Improvement: switch to Mocha testing framwork.
* Improvement: `Reader.offset` is now exposed.
* Improvement: 64 bit values now represented as [upper, lower] pairs for better portability.
* Improvement: exposed `nbt.writeUncompressed`, which is sync as opposed to its unzipping counterpart.
* Improvement: lists and compounds now return type information. This creates a symetry because this output can be fed into the writer as-is.
* Improvement: 64 bit values now represented as [upper, lower] pairs for
better portability.
* Improvement: exposed `nbt.writeUncompressed`, which is sync as opposed
to its unzipping counterpart.
* Improvement: lists and compounds now return type information. This creates
a symetry because this output can be fed into the writer as-is.

0.4.0
-----

* Feature: the `Reader` constructor is now public and can be used to read individual NBT elements from a buffer.
* Feature: support for `npm test` using the previously built testing infrastructure.
* Feature: the `Reader` constructor is now public and can be used eo read
individual NBT elements from a buffer.
* Feature: support for `npm test` using the previously built testing
infrastructure.
* Improvement: greatly expanded test coverage.
* Improvement: byte array values are now returned as arrays rather than Buffer objects.
* Improvement: continuous integration with Travis now runs test against the GitHub repository.
* Improvement: byte array values are now returned as arrays rather than
Buffer objects.
* Improvement: continuous integration with Travis now runs test against the
GitHub repository.
* Improvement: added a changelog.
* Fix: the long reader returned incorrect values due to an operator precedence issue.
* Fix: the long reader returned incorrect values due to an operator
precedence issue.

0.3.0
-----
Expand All @@ -55,7 +66,8 @@ A merge and slight rework of the terrific improvements by the people at [Prosmar

* Feature: support for gzipped archives.
* Feature: support for int arrays (type 11).
* Improvement: when the root node is a compound with no name, only the value is returned.
* Improvement: when the root node is a compound with no name, only the value
is returned.
* Improvement: better documentation.
* Improvement: when an error occurs, an Error is thrown rather than as string.

Expand Down
10 changes: 5 additions & 5 deletions License.md
@@ -1,6 +1,6 @@
I, the copyright holder of this work, hereby release it into the public domain.
This applies worldwide.
I, the copyright holder of this work, hereby release it into the public
domain. This applies worldwide.

In case this is not legally possible: I grant anyone the right to use this work
for any purpose, without any conditions, unless such conditions are required by
law.
In case this is not legally possible: I grant anyone the right to use this
work for any purpose, without any conditions, unless such conditions are
required by law.
24 changes: 17 additions & 7 deletions Readme.md
Expand Up @@ -3,13 +3,16 @@ NBT.js [![Build Status](https://travis-ci.org/sjmulder/nbt-js.png?branch=master)

By Sijmen Mulder and a host of wonderful contributors.

NBT.js is a JavaScript parser and serializer for [NBT](http://wiki.vg/NBT) archives, for use with [Node.js](http://nodejs.org/) or the browser.
NBT.js is a JavaScript parser and serializer for [NBT](http://wiki.vg/NBT)
archives, for use with [Node.js](http://nodejs.org/) or the browser.


Usage
-----

After `var nbt = require('nbt')` or `<script src="nbt.js"></script>`, you can use `nbt.parse(data, callback)` to convert NBT data into a regular JavaScript object.
After `var nbt = require('nbt')` or `<script src="nbt.js"></script>`, you can
use `nbt.parse(data, callback)` to convert NBT data into a regular JavaScript
object.

```js
var fs = require('fs'),
Expand All @@ -25,15 +28,19 @@ fs.readFile('bigtest.nbt', function(error, data) {
});
```

If the data is gzipped, it is automatically decompressed first. When running in the browser, `window.zlib` is required for this to work.
If the data is gzipped, it is automatically decompressed first. When running
in the browser, `window.zlib` is required for this to work.

Tag names are copied verbatim, and as some names are not valid JavaScript names, use of the indexer may be required – such as with the nested compound test in the example above.
Tag names are copied verbatim, and as some names are not valid JavaScript
names, use of the indexer may be required – such as with the nested
compound test in the example above.


API documentation
-----------------

The full documentation generated with JSDoc is available in the docs/ directory and online:
The full documentation generated with JSDoc is available in the docs/
directory and online:

http://sjmulder.github.io/nbt-js/

Expand All @@ -51,6 +58,9 @@ make doc # Regenerate the documentation in docs/
Copyright
---------

I, the copyright holder of this work, hereby release it into the public domain. This applies worldwide.
I, the copyright holder of this work, hereby release it into the public
domain. This applies worldwide.

In case this is not legally possible: I grant anyone the right to use this work for any purpose, without any conditions, unless such conditions are required by law.
In case this is not legally possible: I grant anyone the right to use this
work for any purpose, without any conditions, unless such conditions are
required by law.
6 changes: 4 additions & 2 deletions test/nbt-spec.js
Expand Up @@ -70,7 +70,8 @@ describe('nbt.write', function() {

var input = require('../sample/bigtest');
var output = nbt.writeUncompressed(input);
expect(new Uint8Array(output)).to.deep.equal(new Uint8Array(nbtdata));
expect(new Uint8Array(output)).to.deep.equal(
new Uint8Array(nbtdata));
done();
});
});
Expand All @@ -79,6 +80,7 @@ describe('nbt.write', function() {
var input = require('../sample/bigtest');
var output = nbt.writeUncompressed(input);
var decodedOutput = nbt.parseUncompressed(output);
expect(new Uint8Array(decodedOutput)).to.deep.equal(new Uint8Array(input));
expect(new Uint8Array(decodedOutput)).to.deep.equal(
new Uint8Array(input));
});
});
6 changes: 4 additions & 2 deletions test/reader-spec.js
Expand Up @@ -98,8 +98,10 @@ describe('nbt.Reader', function() {
8, 0,0,0,2, 0,5, 0x48,0x65,0x6C,0x6C,0x6F,
0,5, 0x57,0x6F,0x72,0x6C,0x64
]));
expect(reader.list()).to.deep.equal({ type: "byte", value: [1, 2, 3] });
expect(reader.list()).to.deep.equal({ type: "string", value: ['Hello', 'World'] });
expect(reader.list()).to.deep.equal(
{ type: "byte", value: [1, 2, 3] });
expect(reader.list()).to.deep.equal(
{ type: "string", value: ['Hello', 'World'] });
});

it('reads compounds', function() {
Expand Down
129 changes: 71 additions & 58 deletions test/writer-spec.js
Expand Up @@ -14,9 +14,10 @@ describe('nbt.Writer', function() {
writer.byte(127);
writer.byte(-127);

expect(new Uint8Array(writer.getData())).to.deep.equal(new Uint8Array([
0, 127, 129
]));
expect(new Uint8Array(writer.getData())).to.deep.equal(
new Uint8Array([
0, 127, 129
]));
});

it('writes 16-bit shorts', function() {
Expand All @@ -25,11 +26,12 @@ describe('nbt.Writer', function() {
writer.short(255);
writer.short((-127 << 8) | 255);

expect(new Uint8Array(writer.getData())).to.deep.equal(new Uint8Array([
0, 0,
0, 255,
129, 255
]));
expect(new Uint8Array(writer.getData())).to.deep.equal(
new Uint8Array([
0, 0,
0, 255,
129, 255
]));
});

it('writes 32-bit ints', function() {
Expand All @@ -38,11 +40,12 @@ describe('nbt.Writer', function() {
writer.int(255);
writer.int(-127 << 24);

expect(new Uint8Array(writer.getData())).to.deep.equal(new Uint8Array([
0, 0, 0, 0,
0, 0, 0, 255,
129, 0, 0, 0
]));
expect(new Uint8Array(writer.getData())).to.deep.equal(
new Uint8Array([
0, 0, 0, 0,
0, 0, 0, 255,
129, 0, 0, 0
]));
});

it('writes 64-bit longs', function() {
Expand All @@ -51,90 +54,98 @@ describe('nbt.Writer', function() {
writer.long([0, 255]);
writer.long([-127 << 24, 0]);

expect(new Uint8Array(writer.getData())).to.deep.equal(new Uint8Array([
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 255,
129, 0, 0, 0, 0, 0, 0, 0
]));
expect(new Uint8Array(writer.getData())).to.deep.equal(
new Uint8Array([
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 255,
129, 0, 0, 0, 0, 0, 0, 0
]));
});

it('writes 32-bit floats', function() {
var writer = new nbt.Writer();
writer.float(0);
writer.float(1);

expect(new Uint8Array(writer.getData())).to.deep.equal(new Uint8Array([
0x00, 0x00, 0x00, 0x00,
0x3F, 0x80, 0x00, 0x00
]));
expect(new Uint8Array(writer.getData())).to.deep.equal(
new Uint8Array([
0x00, 0x00, 0x00, 0x00,
0x3F, 0x80, 0x00, 0x00
]));
});

it('writes 64-bit doubles', function() {
var writer = new nbt.Writer();
writer.double(0);
writer.double(1);

expect(new Uint8Array(writer.getData())).to.deep.equal(new Uint8Array([
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x3F, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
]));
expect(new Uint8Array(writer.getData())).to.deep.equal(
new Uint8Array([
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x3F, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
]));
});

it('writes 8-bit byte arrays from typed arrays', function() {
var writer = new nbt.Writer();
writer.byteArray(new Uint8Array([1, 2]));
writer.byteArray(new Uint8Array([3,4, 5, 6]));

expect(new Uint8Array(writer.getData())).to.deep.equal(new Uint8Array([
0,0,0,2, 1,2,
0,0,0,4, 3,4,5,6
]));
expect(new Uint8Array(writer.getData())).to.deep.equal(
new Uint8Array([
0,0,0,2, 1,2,
0,0,0,4, 3,4,5,6
]));
});

it('writes 8-bit byte arrays from plain arrays', function() {
var writer = new nbt.Writer();
writer.byteArray([1, 2]);
writer.byteArray([3,4, 5, 6]);

expect(new Uint8Array(writer.getData())).to.deep.equal(new Uint8Array([
0,0,0,2, 1,2,
0,0,0,4, 3,4,5,6
]));
expect(new Uint8Array(writer.getData())).to.deep.equal(
new Uint8Array([
0,0,0,2, 1,2,
0,0,0,4, 3,4,5,6
]));
});

it('writes 32-bit int arrays', function() {
var writer = new nbt.Writer();
writer.intArray([1, 2]);
writer.intArray([3,4, 5, 6]);

expect(new Uint8Array(writer.getData())).to.deep.equal(new Uint8Array([
0,0,0,2, 0,0,0,1, 0,0,0,2,
0,0,0,4, 0,0,0,3, 0,0,0,4, 0,0,0,5, 0,0,0,6,
]));
expect(new Uint8Array(writer.getData())).to.deep.equal(
new Uint8Array([
0,0,0,2, 0,0,0,1, 0,0,0,2,
0,0,0,4, 0,0,0,3, 0,0,0,4, 0,0,0,5, 0,0,0,6,
]));
});

it('writes strings', function() {
var writer = new nbt.Writer();
writer.string('Hello!');
writer.string('こんにちは!');

expect(new Uint8Array(writer.getData())).to.deep.equal(new Uint8Array([
0,6, 0x48,0x65,0x6C,0x6C,0x6F,0x21,
0,16, 0xE3,0x81,0x93,0xE3,0x82,0x93,0xE3,0x81,
0xAB,0xE3,0x81,0xA1,0xE3,0x81,0xAF,0x21
]));
expect(new Uint8Array(writer.getData())).to.deep.equal(
new Uint8Array([
0,6, 0x48,0x65,0x6C,0x6C,0x6F,0x21,
0,16, 0xE3,0x81,0x93,0xE3,0x82,0x93,0xE3,0x81,
0xAB,0xE3,0x81,0xA1,0xE3,0x81,0xAF,0x21
]));
});

it('writes lists', function() {
var writer = new nbt.Writer();
writer.list({ type: "byte", value: [1, 2, 3] });
writer.list({ type: "string", value: ['Hello', 'World'] });

expect(new Uint8Array(writer.getData())).to.deep.equal(new Uint8Array([
1, 0,0,0,3, 1, 2, 3,
8, 0,0,0,2, 0,5, 0x48,0x65,0x6C,0x6C,0x6F,
0,5, 0x57,0x6F,0x72,0x6C,0x64
]));
expect(new Uint8Array(writer.getData())).to.deep.equal(
new Uint8Array([
1, 0,0,0,3, 1, 2, 3,
8, 0,0,0,2, 0,5, 0x48,0x65,0x6C,0x6C,0x6F,
0,5, 0x57,0x6F,0x72,0x6C,0x64
]));
});

it('writes compounds', function() {
Expand All @@ -149,13 +160,14 @@ describe('nbt.Writer', function() {
cc: { type: "byte", value: 2 }
});

expect(new Uint8Array(writer.getData())).to.deep.equal(new Uint8Array([
1, 0,2, 0x61,0x61, 1,
9, 0,2, 0x62,0x62, 1, 0,0,0,3, 1, 2, 3,
0,
1, 0,2, 0x63,0x63, 2,
0
]));
expect(new Uint8Array(writer.getData())).to.deep.equal(
new Uint8Array([
1, 0,2, 0x61,0x61, 1,
9, 0,2, 0x62,0x62, 1, 0,0,0,3, 1, 2, 3,
0,
1, 0,2, 0x63,0x63, 2,
0
]));
});

it('tracks the number of bytes written', function() {
Expand All @@ -172,9 +184,10 @@ describe('nbt.Writer', function() {
writer.offset = 1;
writer.short(0x5678);

expect(new Uint8Array(writer.getData())).to.deep.equal(new Uint8Array([
0x12, 0x56, 0x78
]));
expect(new Uint8Array(writer.getData())).to.deep.equal(
new Uint8Array([
0x12, 0x56, 0x78
]));
});

it('can seek to a location beyond the buffer', function() {
Expand Down

0 comments on commit 4e3fbd2

Please sign in to comment.