Skip to content

Commit 745dd79

Browse files
authored
feat(bytes-iter): allow back side iteration (#14)
1 parent 6bf288a commit 745dd79

File tree

3 files changed

+38
-19
lines changed

3 files changed

+38
-19
lines changed

src/bytes-iter/bytes-iter.spec.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,11 @@ describe('BytesIter', () => {
1414
expect(iter.nextByte()).toEqual('0xad')
1515
expect(iter.nextBytes(2)).toEqual('0xbeef')
1616
})
17+
18+
it('should iterate in reverse ', () => {
19+
const iter = BytesIter.String('0xdeadbeef')
20+
expect(iter.nextByte(BytesIter.SIDE.Back)).toEqual('0xef')
21+
expect(iter.nextByte(BytesIter.SIDE.Back)).toEqual('0xbe')
22+
expect(iter.nextBytes(2, BytesIter.SIDE.Back)).toEqual('0xdead')
23+
})
1724
})

src/bytes-iter/bytes-iter.ts

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@ import assert from 'assert'
22
import {isHexBytes} from '../validations'
33
import {add0x} from '../utils'
44

5+
enum Side {
6+
Front,
7+
Back
8+
}
9+
510
/**
611
* Class to iterate through bytes string by parsing individual bytes
712
*
@@ -12,6 +17,8 @@ import {add0x} from '../utils'
1217
* iter.nextBytes(2) == BigInt(0xbeef)
1318
*/
1419
export class BytesIter<T> {
20+
public static SIDE = Side
21+
1522
private bytes: string
1623

1724
private constructor(
@@ -42,11 +49,11 @@ export class BytesIter<T> {
4249
return this.bytes.length === 0
4350
}
4451

45-
public nextByte(): T {
46-
return this.nextBytes(1)
52+
public nextByte(side = Side.Front): T {
53+
return this.nextBytes(1, side)
4754
}
4855

49-
public nextBytes(n: number): T {
56+
public nextBytes(n: number, side = Side.Front): T {
5057
const cnt = n * 2
5158

5259
if (this.bytes.length < cnt) {
@@ -55,38 +62,42 @@ export class BytesIter<T> {
5562
)
5663
}
5764

58-
const bytes = this.bytes.slice(0, cnt)
65+
const isFront = side === Side.Front
66+
67+
const bytes = isFront
68+
? this.bytes.slice(0, cnt)
69+
: this.bytes.slice(-cnt)
5970

60-
this.bytes = this.bytes.slice(cnt)
71+
this.bytes = isFront ? this.bytes.slice(cnt) : this.bytes.slice(0, -cnt)
6172

6273
return this.ResultType(add0x(bytes))
6374
}
6475

65-
public nextUint8(): T {
66-
return this.nextByte()
76+
public nextUint8(side = Side.Front): T {
77+
return this.nextByte(side)
6778
}
6879

69-
public nextUint16(): T {
70-
return this.nextBytes(2)
80+
public nextUint16(side = Side.Front): T {
81+
return this.nextBytes(2, side)
7182
}
7283

73-
public nextUint24(): T {
74-
return this.nextBytes(3)
84+
public nextUint24(side = Side.Front): T {
85+
return this.nextBytes(3, side)
7586
}
7687

77-
public nextUint32(): T {
78-
return this.nextBytes(4)
88+
public nextUint32(side = Side.Front): T {
89+
return this.nextBytes(4, side)
7990
}
8091

81-
public nextUint128(): T {
82-
return this.nextBytes(16)
92+
public nextUint128(side = Side.Front): T {
93+
return this.nextBytes(16, side)
8394
}
8495

85-
public nextUint160(): T {
86-
return this.nextBytes(20)
96+
public nextUint160(side = Side.Front): T {
97+
return this.nextBytes(20, side)
8798
}
8899

89-
public nextUint256(): T {
90-
return this.nextBytes(32)
100+
public nextUint256(side = Side.Front): T {
101+
return this.nextBytes(32, side)
91102
}
92103
}

tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"ES2021"
88
],
99
"target": "ES2021",
10+
"removeComments": false
1011
},
1112
"include": ["./src"]
1213
}

0 commit comments

Comments
 (0)