Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Eject encodeAny, decodeAny from buffer - fixes #19 #56

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -200,11 +200,6 @@ broadcastchannel.publish('my events', 'hello from tab B') // => A: 'hello from t
<b><code>buffer.fromBase64</code></b><br>
<b><code>buffer.copyUint8Array(uint8Array: Uint8Array): Uint8Array</code></b><br>
<dd><p>Copy the content of an Uint8Array view to a new ArrayBuffer.</p></dd>
<b><code>buffer.encodeAny(data: any): Uint8Array</code></b><br>
<dd><p>Encode anything as a UInt8Array. It's a pun on typescripts's <code>any</code> type.
See encoding.writeAny for more information.</p></dd>
<b><code>buffer.decodeAny(buf: Uint8Array): any</code></b><br>
<dd><p>Decode an any-encoded value.</p></dd>
</dl>
</details>
<details><summary><b>[lib0/cache]</b> An implementation of a map which has keys that expire.</summary>
Expand Down Expand Up @@ -409,6 +404,8 @@ to the next byte and read it as unsigned integer.</p></dd>
<dd><p>Decoding target.</p></dd>
<b><code>decoding.IntDiffOptRleDecoder#pos: number</code></b><br>
<dd><p>Current decoding position.</p></dd>
<b><code>decoding.decodeAny(buf: Uint8Array): any</code></b><br>
<dd><p>Decode an any-encoded value.</p></dd>
</dl>
</details>
<details><summary><b>[lib0/diff]</b> Efficient diffs.</summary>
Expand Down Expand Up @@ -710,6 +707,9 @@ In practice, when decoding several million small strings, the GC will kick in mo
<b><code>encoding.RleEncoder#bufs: Array&lt;Uint8Array&gt;</code></b><br>
<b><code>encoding.IntDiffEncoder#bufs: Array&lt;Uint8Array&gt;</code></b><br>
<b><code>encoding.RleIntDiffEncoder#bufs: Array&lt;Uint8Array&gt;</code></b><br>
<b><code>encoding.encodeAny(data: any): Uint8Array</code></b><br>
<dd><p>Encode anything as a UInt8Array. It's a pun on typescripts's <code>any</code> type.
See encoding.writeAny for more information.</p></dd>
</dl>
</details>
<details><summary><b>[lib0/map]</b> Isomorphic module to work access the environment (query params, env variables).</summary>
Expand Down
23 changes: 0 additions & 23 deletions buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

import * as string from './string.js'
import * as env from './environment.js'
import * as encoding from './encoding.js'
import * as decoding from './decoding.js'

/**
* @param {number} len
Expand Down Expand Up @@ -92,24 +90,3 @@ export const copyUint8Array = uint8Array => {
newBuf.set(uint8Array)
return newBuf
}

/**
* Encode anything as a UInt8Array. It's a pun on typescripts's `any` type.
* See encoding.writeAny for more information.
*
* @param {any} data
* @return {Uint8Array}
*/
export const encodeAny = data => {
const encoder = encoding.createEncoder()
encoding.writeAny(encoder, data)
return encoding.toUint8Array(encoder)
}

/**
* Decode an any-encoded value.
*
* @param {Uint8Array} buf
* @return {any}
*/
export const decodeAny = buf => decoding.readAny(decoding.createDecoder(buf))
9 changes: 0 additions & 9 deletions buffer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,3 @@ export const testRepeatBase64Encoding = tc => {
}
t.compare(copied, decoded)
}

/**
* @param {t.TestCase} tc
*/
export const testAnyEncoding = tc => {
const obj = { val: 1, arr: [1, 2], str: '409231dtrnä' }
const res = buffer.decodeAny(buffer.encodeAny(obj))
t.compare(obj, res)
}
8 changes: 8 additions & 0 deletions decoding.js
Original file line number Diff line number Diff line change
Expand Up @@ -679,3 +679,11 @@ export class StringDecoder {
return res
}
}

/**
* Decode an any-encoded value.
*
* @param {Uint8Array} buf
* @return {any}
*/
export const decodeAny = buf => readAny(createDecoder(buf))
13 changes: 13 additions & 0 deletions encoding.js
Original file line number Diff line number Diff line change
Expand Up @@ -853,3 +853,16 @@ export class StringEncoder {
return toUint8Array(encoder)
}
}

/**
* Encode anything as a UInt8Array. It's a pun on typescripts's `any` type.
* See encoding.writeAny for more information.
*
* @param {any} data
* @return {Uint8Array}
*/
export const encodeAny = data => {
const encoder = createEncoder()
writeAny(encoder, data)
return toUint8Array(encoder)
}
9 changes: 9 additions & 0 deletions encoding.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -840,3 +840,12 @@ export const testInvalidVarIntEncoding = _tc => {
decoding.readVarUint(decoder)
})
}

/**
* @param {t.TestCase} tc
*/
export const testAnyEncoding = tc => {
const obj = { val: 1, arr: [1, 2], str: '409231dtrnä' }
const res = decoding.decodeAny(encoding.encodeAny(obj))
t.compare(obj, res)
}