-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add optional offset param to varint.decode (#201)
- Loading branch information
Showing
2 changed files
with
31 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* globals describe, it */ | ||
|
||
import { varint } from 'multiformats' | ||
import chai from 'chai' | ||
import chaiAsPromised from 'chai-as-promised' | ||
|
||
chai.use(chaiAsPromised) | ||
const { assert } = chai | ||
|
||
const UTF8 = new TextEncoder() | ||
|
||
describe('varint', () => { | ||
it('can decode with offset', () => { | ||
const message = UTF8.encode('hello-world') | ||
const outerTag = 0x55 | ||
const innerTag = 0xe3 | ||
const outerTagSize = varint.encodingLength(outerTag) | ||
const innerTagSize = varint.encodingLength(innerTag) | ||
|
||
const bytes = new Uint8Array(message.byteLength + outerTagSize + innerTagSize) | ||
varint.encodeTo(outerTag, bytes) | ||
varint.encodeTo(innerTag, bytes, outerTagSize) | ||
bytes.set(message, outerTagSize + innerTagSize) | ||
|
||
assert.deepStrictEqual(varint.decode(bytes), [outerTag, outerTagSize]) | ||
assert.deepStrictEqual(varint.decode(bytes, outerTagSize), [innerTag, innerTagSize]) | ||
}) | ||
}) |