Skip to content

Commit

Permalink
Lint
Browse files Browse the repository at this point in the history
  • Loading branch information
allevo committed Nov 23, 2023
1 parent 17aadaf commit f145b62
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 44 deletions.
12 changes: 6 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

type StrictArrayBuffer = ArrayBuffer & { buffer?: undefined };
type StrictArrayBuffer = ArrayBuffer & { buffer?: undefined }

export interface Ser {
index: number
Expand Down Expand Up @@ -92,7 +92,7 @@ export function createDes (buffer: StrictArrayBuffer): Des {
deserializeArray,
deserializeIterable,
getArrayElements,
unsafeDeserializeUint32Array,
unsafeDeserializeUint32Array
}
}

Expand Down Expand Up @@ -172,20 +172,20 @@ function deserializeIterable<T> (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<T>(this: Ser, arr: T[], serialize: (ser: Ser, t: T) => void): void {
function serializeIndexableArray<T> (this: Ser, arr: T[], serialize: (ser: Ser, t: T) => void): void {
const l = arr.length
this.uint32Array[this.index++] = l
let indexOffsets = this.index
Expand All @@ -200,7 +200,7 @@ function serializeIndexableArray<T>(this: Ser, arr: T[], serialize: (ser: Ser, t
this.uint32Array[indexOffsets++] = offsetEnd - offsetStart
}
}
function getArrayElements<T>(this: Des, indexes: number[], deserialize: (des: Des, start: number, end: number) => T): T[] {
function getArrayElements<T> (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)
Expand Down
66 changes: 28 additions & 38 deletions tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit f145b62

Please sign in to comment.