Skip to content

Commit

Permalink
feat: ✨ add int arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
RichardDorian committed Mar 30, 2022
1 parent 3de9033 commit d13bd87
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ $ npm install @minecraft-js/bufwrapper
And then import it in your JavaScript/TypeScript file

```ts
const BufWrapper = require('@minecraft-js/bufwrapper'); // CommonJS
const BufWrapper = require('@minecraft-js/bufwrapper').default; // CommonJS

import BufWrapper from '@minecraft-js/bufwrapper'; // ES6
```
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@minecraft-js/bufwrapper",
"version": "1.0.0",
"version": "1.0.1",
"description": "Encode and decode data using buffers",
"main": "dist/index.js",
"scripts": {
Expand All @@ -22,13 +22,13 @@
"homepage": "https://github.com/MinecraftJS/BufWrapper#readme",
"devDependencies": {
"@types/node": "^17.0.23",
"@types/varint": "^6.0.0",
"chai": "^4.3.6",
"mocha": "^9.2.2",
"prettier": "^2.6.1",
"typescript": "^4.6.3"
},
"dependencies": {
"@types/varint": "^6.0.0",
"varint": "^6.0.0"
}
}
37 changes: 36 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,10 +180,45 @@ export default class BufWrapper {
*/
public readStringArray(): string[] {
const length = this.readVarint();
const value = [];
const value: string[] = [];
for (let i = 0; i < length; i++) {
value.push(this.readString());
}
return value;
}

/**
* Write an array of ints to the buffer
* @param value The value to write (number[])
* @example
* ```javascript
* const buf = new BufWrapper();
* buf.writeIntArray([1, 2, 3]);
* console.log(buf.buffer); // <Buffer 03 00 00 00 01 00 00 00 02 00 00 00 03>
* ```
*/
public writeIntArray(value: number[]): void {
this.writeVarint(value.length);
value.forEach((v) => this.writeInt(v));
}

/**
* Read an array of ints from the buffer
* @returns The array read from the buffer
* @example
* ```javascript
* const buffer = Buffer.from([ 0x03, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x03 ]);
* const buf = new BufWrapper(buffer);
* const decoded = buf.readIntArray();
* console.log(decoded); // [ 1, 2, 3 ]
* ```
*/
public readIntArray(): number[] {
const length = this.readVarint();
const value: number[] = [];
for (let i = 0; i < length; i++) {
value.push(this.readInt());
}
return value;
}
}
40 changes: 40 additions & 0 deletions test/intArray.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
const { assert } = require('chai');
const BufWrapper = require('../dist').default;

describe('Int Array', () => {
it('Write 1', () => {
const buf = new BufWrapper();
buf.writeIntArray([1, 2, 3]);
assert.equal(buf.buffer.toString('hex'), '03000000010000000200000003');
});

it('Write 2', () => {
const buf = new BufWrapper();
buf.writeIntArray([42, -42]);
assert.equal(buf.buffer.toString('hex'), '020000002affffffd6');
});

it('Read 1', () => {
const buf = new BufWrapper(
Buffer.from([
0x03, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
0x03,
])
);
const decoded = buf.readIntArray();
assert.equal(decoded.length, 3);
assert.equal(decoded[0], 1);
assert.equal(decoded[1], 2);
assert.equal(decoded[2], 3);
});

it('Read 2', () => {
const buf = new BufWrapper(
Buffer.from([0x02, 0x00, 0x00, 0x00, 0x2a, 0xff, 0xff, 0xff, 0xd6])
);
const decoded = buf.readIntArray();
assert.equal(decoded.length, 2);
assert.equal(decoded[0], 42);
assert.equal(decoded[1], -42);
});
});

0 comments on commit d13bd87

Please sign in to comment.