diff --git a/src/bytes-iter/bytes-iter.spec.ts b/src/bytes-iter/bytes-iter.spec.ts index 9422486..dc84974 100644 --- a/src/bytes-iter/bytes-iter.spec.ts +++ b/src/bytes-iter/bytes-iter.spec.ts @@ -14,4 +14,11 @@ describe('BytesIter', () => { expect(iter.nextByte()).toEqual('0xad') expect(iter.nextBytes(2)).toEqual('0xbeef') }) + + it('should iterate in reverse ', () => { + const iter = BytesIter.String('0xdeadbeef') + expect(iter.nextByte(BytesIter.SIDE.Back)).toEqual('0xef') + expect(iter.nextByte(BytesIter.SIDE.Back)).toEqual('0xbe') + expect(iter.nextBytes(2, BytesIter.SIDE.Back)).toEqual('0xdead') + }) }) diff --git a/src/bytes-iter/bytes-iter.ts b/src/bytes-iter/bytes-iter.ts index e06488c..213bf61 100644 --- a/src/bytes-iter/bytes-iter.ts +++ b/src/bytes-iter/bytes-iter.ts @@ -2,6 +2,11 @@ import assert from 'assert' import {isHexBytes} from '../validations' import {add0x} from '../utils' +enum Side { + Front, + Back +} + /** * Class to iterate through bytes string by parsing individual bytes * @@ -12,6 +17,8 @@ import {add0x} from '../utils' * iter.nextBytes(2) == BigInt(0xbeef) */ export class BytesIter { + public static SIDE = Side + private bytes: string private constructor( @@ -42,11 +49,11 @@ export class BytesIter { return this.bytes.length === 0 } - public nextByte(): T { - return this.nextBytes(1) + public nextByte(side = Side.Front): T { + return this.nextBytes(1, side) } - public nextBytes(n: number): T { + public nextBytes(n: number, side = Side.Front): T { const cnt = n * 2 if (this.bytes.length < cnt) { @@ -55,38 +62,42 @@ export class BytesIter { ) } - const bytes = this.bytes.slice(0, cnt) + const isFront = side === Side.Front + + const bytes = isFront + ? this.bytes.slice(0, cnt) + : this.bytes.slice(-cnt) - this.bytes = this.bytes.slice(cnt) + this.bytes = isFront ? this.bytes.slice(cnt) : this.bytes.slice(0, -cnt) return this.ResultType(add0x(bytes)) } - public nextUint8(): T { - return this.nextByte() + public nextUint8(side = Side.Front): T { + return this.nextByte(side) } - public nextUint16(): T { - return this.nextBytes(2) + public nextUint16(side = Side.Front): T { + return this.nextBytes(2, side) } - public nextUint24(): T { - return this.nextBytes(3) + public nextUint24(side = Side.Front): T { + return this.nextBytes(3, side) } - public nextUint32(): T { - return this.nextBytes(4) + public nextUint32(side = Side.Front): T { + return this.nextBytes(4, side) } - public nextUint128(): T { - return this.nextBytes(16) + public nextUint128(side = Side.Front): T { + return this.nextBytes(16, side) } - public nextUint160(): T { - return this.nextBytes(20) + public nextUint160(side = Side.Front): T { + return this.nextBytes(20, side) } - public nextUint256(): T { - return this.nextBytes(32) + public nextUint256(side = Side.Front): T { + return this.nextBytes(32, side) } } diff --git a/tsconfig.json b/tsconfig.json index 54ecce5..c5d631b 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -7,6 +7,7 @@ "ES2021" ], "target": "ES2021", + "removeComments": false }, "include": ["./src"] }