From d13bd878d64cf2c22312562a90f40461173a514a Mon Sep 17 00:00:00 2001 From: RichardDorian Date: Wed, 30 Mar 2022 08:48:43 +0200 Subject: [PATCH] feat: :sparkles: add int arrays --- README.md | 2 +- package.json | 4 ++-- src/index.ts | 37 ++++++++++++++++++++++++++++++++++++- test/intArray.test.js | 40 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 79 insertions(+), 4 deletions(-) create mode 100644 test/intArray.test.js diff --git a/README.md b/README.md index d7bf7cb..da61f0f 100644 --- a/README.md +++ b/README.md @@ -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 ``` diff --git a/package.json b/package.json index 80eb52e..0003726 100644 --- a/package.json +++ b/package.json @@ -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": { @@ -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" } } diff --git a/src/index.ts b/src/index.ts index 2ac576f..fa0fdc7 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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); // + * ``` + */ + 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; + } } diff --git a/test/intArray.test.js b/test/intArray.test.js new file mode 100644 index 0000000..5ba74b7 --- /dev/null +++ b/test/intArray.test.js @@ -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); + }); +});