Skip to content

Commit

Permalink
[encoding] add encode helper function
Browse files Browse the repository at this point in the history
  • Loading branch information
dmonad committed May 25, 2023
1 parent 3c8ce61 commit 79c5082
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
9 changes: 9 additions & 0 deletions encoding.js
Expand Up @@ -53,6 +53,15 @@ export class Encoder {
*/
export const createEncoder = () => new Encoder()

/**
* @param {function(Encoder):void} f
*/
export const encode = (f) => {
const encoder = createEncoder()
f(encoder)
return toUint8Array(encoder)
}

/**
* The current length of the encoded data.
*
Expand Down
4 changes: 1 addition & 3 deletions encoding.test.js
Expand Up @@ -110,9 +110,7 @@ function test (testname, write, read, val, doLog = true) {
* @param {string} s
*/
const testVarString = s => {
const encoder = encoding.createEncoder()
encoding.writeVarString(encoder, s)
const decoder = decoding.createDecoder(encoding.toUint8Array(encoder))
const decoder = decoding.createDecoder(encoding.encode(encoder => { encoding.writeVarString(encoder, s) }))
const peeked = decoding.peekVarString(decoder)
const result = decoding.readVarString(decoder)
t.compareStrings(s, result)
Expand Down

0 comments on commit 79c5082

Please sign in to comment.