Skip to content

Commit

Permalink
docs(msgpack): complete documentation of the package (#4832)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucacasonato authored May 22, 2024
1 parent 315005c commit 0e31686
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 6 deletions.
9 changes: 7 additions & 2 deletions msgpack/decode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,19 @@ import type { ValueType } from "./encode.ts";
/**
* Decode a value from the MessagePack binary format.
*
* @example
* If the input is not in valid message pack format, an error will be thrown.
*
* @example Decode a value from the MessagePack binary format
* ```ts
* import { decode } from "@std/msgpack/decode";
*
* const encoded = Uint8Array.of(1, 2, 3)
* const encoded = new Uint8Array([1, 2, 3])
*
* console.log(decode(encoded))
* ```
*
* @param uint8 Uint8Array containing the MessagePack binary data.
* @returns Decoded value from the MessagePack binary data.
*/
export function decode(uint8: Uint8Array): ValueType {
const pointer = { consumed: 0 };
Expand Down
8 changes: 4 additions & 4 deletions msgpack/encode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ export type ValueType =
* Value map that can be encoded to MessagePack.
*/
export interface ValueMap {
/**
* Value types that can be encoded to MessagePack.
*/
[index: string | number]: ValueType;
}

Expand All @@ -42,7 +39,7 @@ const encoder = new TextEncoder();
/**
* Encode a value to MessagePack binary format.
*
* @example
* @example Encode a value to MessagePack binary format
* ```ts
* import { encode } from "@std/msgpack/encode";
*
Expand All @@ -56,6 +53,9 @@ const encoder = new TextEncoder();
*
* console.log(encode(obj))
* ```
*
* @param object Value to encode to MessagePack binary format.
* @returns Encoded MessagePack binary data.
*/
export function encode(object: ValueType): Uint8Array {
const byteParts: Uint8Array[] = [];
Expand Down
42 changes: 42 additions & 0 deletions msgpack/mod.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,47 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
// This module is browser compatible.

/**
* MessagePack is an efficient binary serialization format that is language
* agnostic. It is like JSON, but generally produces much smaller payloads.
* [Learn more about MessagePack](https://msgpack.org/).
*
* This module provides functions to encode and decode MessagePack.
*
* ```ts
* import { decode, encode } from "@std/msgpack";
* import { assertEquals } from "@std/assert"
*
* const obj = {
* str: "deno",
* arr: [1, 2, 3],
* bool: true,
* nil: null,
* map: {
* foo: "bar"
* }
* };
*
* const encoded = encode(obj);
* console.log(encoded); // Uint8Array(42) [...]
*
* const decoded = decode(encoded);
* assertEquals(decoded, obj);
* ```
*
* MessagePack supports encoding and decoding the following types:
*
* - `number`
* - `bigint`
* - `string`
* - `boolean`
* - `null`
* - `Uint8Array`
* - arrays of values of these types
* - objects with string or number keys, and values of these types
*
* @module
*/

export * from "./decode.ts";
export * from "./encode.ts";

0 comments on commit 0e31686

Please sign in to comment.