-
Notifications
You must be signed in to change notification settings - Fork 18
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
feat: export encode/decode -Options #113
Conversation
These options are being exported so they can be used with cborg's API, specifically the `decodeFirst` and `encodedLength` functions.
Ah ok, I imagined you'd do a |
@@ -181,4 +183,23 @@ describe('dag-cbor', () => { | |||
const encoded = bytes.fromHex('a3636261720363666f6f0163666f6f02') | |||
assert.throws(() => decode(encoded), /CBOR decode error: found repeat map key "foo"/) | |||
}) | |||
|
|||
test('determine encoded length of obj', () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These are both really nice examples of using this, I think they would go well on the README, would you mind adding something there that demonstrates the ability to do encodedLength
and decodeFirst
?
README.md
Outdated
|
||
// encode/decode options are exported for use with cborg's encodedLength and decodeFirst | ||
import { encodeOptions, decodeOptions } from '@ipld/dag-cbor' | ||
import { encodedLength } from 'cborg/length' | ||
import { decodeFirst } from 'cborg' | ||
|
||
// dag-cbor encoded length of obj in bytes | ||
const byteLength = encodedLength(obj, encodeOptions) | ||
byteLength // 104 | ||
|
||
|
||
// concatenate two dag-cbor encoded obj | ||
const concatenatedData = new Uint8Array(data.length * 2) | ||
concatenatedData.set(data) | ||
concatenatedData.set(data, data.length) | ||
|
||
// returns dag-cbor decoded obj at the beginning of the buffer as well as the remaining bytes | ||
const [first, remainder] = decodeFirst(concatenatedData, decodeOptions) | ||
assert.deepEqual(first, obj) // true | ||
assert.deepEqual(remainder, data) // true |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've added usage to readme inside of the current usage code block.
2a94b4e
to
a2bf381
Compare
awesome, expect a semver-minor release soon with this, thanks! |
## [9.1.0](v9.0.8...v9.1.0) (2024-01-30) ### Features * export encode/decode options ([#113](#113)) ([d2094ca](d2094ca))
These options are being exported so they can be used with
cborg's API, specifically the
decodeFirst
andencodedLength
functions.Closes #112