From f145b62b16f62593c5e766839cad654bbbff586d Mon Sep 17 00:00:00 2001 From: Tommaso Allevi Date: Thu, 23 Nov 2023 11:31:27 +0100 Subject: [PATCH] Lint --- src/index.ts | 12 ++++----- tests/index.test.ts | 66 +++++++++++++++++++-------------------------- 2 files changed, 34 insertions(+), 44 deletions(-) diff --git a/src/index.ts b/src/index.ts index 2824a42..7e661fa 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,5 +1,5 @@ -type StrictArrayBuffer = ArrayBuffer & { buffer?: undefined }; +type StrictArrayBuffer = ArrayBuffer & { buffer?: undefined } export interface Ser { index: number @@ -92,7 +92,7 @@ export function createDes (buffer: StrictArrayBuffer): Des { deserializeArray, deserializeIterable, getArrayElements, - unsafeDeserializeUint32Array, + unsafeDeserializeUint32Array } } @@ -172,20 +172,20 @@ function deserializeIterable (this: Des, deserialize: (des: Des) => T): Itera } } -function unsafeSerializeUint32Array(this: Ser, arr: Uint32Array) { +function unsafeSerializeUint32Array (this: Ser, arr: Uint32Array): void { const length = Math.ceil(arr.byteLength / 4) this.uint32Array[this.index++] = length this.uint32Array.set(arr, this.index) this.index += length } -function unsafeDeserializeUint32Array(this: Des): Uint32Array { +function unsafeDeserializeUint32Array (this: Des): Uint32Array { const byteLength = this.uint32Array[this.index++] const d = new Uint32Array(this.buffer, this.index * 4, byteLength) this.index += byteLength return d } -function serializeIndexableArray(this: Ser, arr: T[], serialize: (ser: Ser, t: T) => void): void { +function serializeIndexableArray (this: Ser, arr: T[], serialize: (ser: Ser, t: T) => void): void { const l = arr.length this.uint32Array[this.index++] = l let indexOffsets = this.index @@ -200,7 +200,7 @@ function serializeIndexableArray(this: Ser, arr: T[], serialize: (ser: Ser, t this.uint32Array[indexOffsets++] = offsetEnd - offsetStart } } -function getArrayElements(this: Des, indexes: number[], deserialize: (des: Des, start: number, end: number) => T): T[] { +function getArrayElements (this: Des, indexes: number[], deserialize: (des: Des, start: number, end: number) => T): T[] { const currentIndex = this.index + 1 const l = indexes.length const arr = new Array(l) diff --git a/tests/index.test.ts b/tests/index.test.ts index 4aaec8a..2fe5a26 100644 --- a/tests/index.test.ts +++ b/tests/index.test.ts @@ -269,63 +269,53 @@ await t.test('setBuffer with options', async t => { ser.serializeUInt32(3) ser.serializeUInt32(4) const buff = ser.getBuffer() - + const des = createDes(new ArrayBuffer(0)) - { - des.setBuffer(buff, 0, 4) - assert.equal(des.deserializeUInt32(), 1) - } - { - des.setBuffer(buff, 4, 4) + des.setBuffer(buff, 0, 4) + assert.equal(des.deserializeUInt32(), 1) - assert.equal(des.deserializeUInt32(), 2) - } - { - des.setBuffer(buff, 8, 4) - assert.equal(des.deserializeUInt32(), 3) - } - { - des.setBuffer(buff, 12, 4) - assert.equal(des.deserializeUInt32(), 4) - } - { - des.setBuffer(buff, 4, 12) - assert.equal(des.deserializeUInt32(), 2) - assert.equal(des.deserializeUInt32(), 3) - assert.equal(des.deserializeUInt32(), 4) - } + des.setBuffer(buff, 4, 4) + assert.equal(des.deserializeUInt32(), 2) + + des.setBuffer(buff, 8, 4) + assert.equal(des.deserializeUInt32(), 3) + + des.setBuffer(buff, 12, 4) + assert.equal(des.deserializeUInt32(), 4) + + des.setBuffer(buff, 4, 12) + assert.equal(des.deserializeUInt32(), 2) + assert.equal(des.deserializeUInt32(), 3) + assert.equal(des.deserializeUInt32(), 4) }) await t.test('uint32 & string', async () => { const ser = createSer() ser.serializeUInt32(1) - ser.serializeString("v1") + ser.serializeString('v1') const buff = ser.getBuffer() - + const des = createDes(new ArrayBuffer(0)) - { - des.setBuffer(buff, 0, 12) - assert.equal(des.deserializeUInt32(), 1) - assert.equal(des.deserializeString(), "v1") - } - { - des.setBuffer(buff, 4, 8) - assert.equal(des.deserializeString(), "v1") - } + des.setBuffer(buff, 0, 12) + assert.equal(des.deserializeUInt32(), 1) + assert.equal(des.deserializeString(), 'v1') + + des.setBuffer(buff, 4, 8) + assert.equal(des.deserializeString(), 'v1') }) }) -function serializeItem(ser: Ser, t: { foo: string, bar: number}) { +function serializeItem (ser: Ser, t: { foo: string, bar: number }): void { ser.serializeString(t.foo) ser.serializeUInt32(t.bar) } -function deserializeItem(des: Des): { foo: string, bar: number} { +function deserializeItem (des: Des): { foo: string, bar: number } { const foo = des.deserializeString() const bar = des.deserializeUInt32() return { foo, - bar, + bar } } @@ -359,7 +349,7 @@ await t.test('serialize + getArrayelements + serialize unsafe + deserialize with foo = ser.getBuffer() } - let found: { foo: string, bar: number }[] + let found: Array<{ foo: string, bar: number }> { const des = createDes(foo)