2020-03-10 15:16:08 -04:00
|
|
|
# encoding
|
|
|
|
|
|
|
|
Helper module for dealing with external data structures.
|
|
|
|
|
2020-07-14 14:26:49 -04:00
|
|
|
- [`ascii85`](#ascii85)
|
2020-03-10 15:16:08 -04:00
|
|
|
- [`base32`](#base32)
|
|
|
|
- [`binary`](#binary)
|
|
|
|
- [`csv`](#csv)
|
|
|
|
- [`toml`](#toml)
|
|
|
|
- [`yaml`](#yaml)
|
|
|
|
|
|
|
|
## Binary
|
|
|
|
|
|
|
|
Implements equivalent methods to Go's `encoding/binary` package.
|
|
|
|
|
|
|
|
Available Functions:
|
|
|
|
|
|
|
|
```typescript
|
|
|
|
sizeof(dataType: RawTypes): number
|
|
|
|
getNBytes(r: Deno.Reader, n: number): Promise<Uint8Array>
|
2020-04-28 12:40:43 -04:00
|
|
|
varnum(b: Uint8Array, o: VarnumOptions = {}): number | null
|
|
|
|
varbig(b: Uint8Array, o: VarbigOptions = {}): bigint | null
|
2020-03-10 15:16:08 -04:00
|
|
|
putVarnum(b: Uint8Array, x: number, o: VarnumOptions = {}): number
|
|
|
|
putVarbig(b: Uint8Array, x: bigint, o: VarbigOptions = {}): number
|
|
|
|
readVarnum(r: Deno.Reader, o: VarnumOptions = {}): Promise<number>
|
|
|
|
readVarbig(r: Deno.Reader, o: VarbigOptions = {}): Promise<bigint>
|
|
|
|
writeVarnum(w: Deno.Writer, x: number, o: VarnumOptions = {}): Promise<number>
|
|
|
|
writeVarbig(w: Deno.Writer, x: bigint, o: VarbigOptions = {}): Promise<number>
|
|
|
|
```
|
2019-05-30 09:50:29 -04:00
|
|
|
|
|
|
|
## CSV
|
|
|
|
|
2020-05-31 16:02:39 -04:00
|
|
|
### API
|
|
|
|
|
2020-11-25 09:50:00 -05:00
|
|
|
#### `readMatrix`
|
|
|
|
|
|
|
|
```ts
|
|
|
|
(reader: BufReader, opt: ReadOptions = {
|
|
|
|
comma: ",",
|
|
|
|
trimLeadingSpace: false,
|
|
|
|
lazyQuotes: false,
|
|
|
|
}): Promise<string[][]>
|
|
|
|
```
|
2020-05-31 16:02:39 -04:00
|
|
|
|
|
|
|
Parse the CSV from the `reader` with the options provided and return
|
|
|
|
`string[][]`.
|
|
|
|
|
2020-11-25 09:50:00 -05:00
|
|
|
#### `parse`
|
|
|
|
|
|
|
|
```ts
|
|
|
|
(input: string | BufReader, opt: ParseOptions = { skipFirstRow: false }): Promise<unknown[]>
|
|
|
|
```
|
2020-05-31 16:02:39 -04:00
|
|
|
|
|
|
|
Parse the CSV string/buffer with the options provided. The result of this
|
|
|
|
function is as follows:
|
|
|
|
|
2020-09-27 14:20:46 -04:00
|
|
|
- If you don't provide `opt.skipFirstRow`, `opt.parse`, and `opt.columns`, it
|
|
|
|
returns `string[][]`.
|
|
|
|
- If you provide `opt.skipFirstRow` or `opt.columns` but not `opt.parse`, it
|
|
|
|
returns `object[]`.
|
2020-05-31 16:02:39 -04:00
|
|
|
- If you provide `opt.parse`, it returns an array where each element is the
|
|
|
|
value returned from `opt.parse`.
|
|
|
|
|
|
|
|
##### `ParseOptions`
|
|
|
|
|
2020-09-27 14:20:46 -04:00
|
|
|
- **`skipFirstRow: boolean;`**: If you provide `skipFirstRow: true` and
|
|
|
|
`columns`, the first line will be skipped. If you provide `skipFirstRow: true`
|
|
|
|
but not `columns`, the first line will be skipped and used as header
|
|
|
|
definitions.
|
|
|
|
- **`columns: string[] | HeaderOptions[];`**: If you provide `string[]` or
|
|
|
|
`ColumnOptions[]`, those names will be used for header definition.
|
2020-05-31 16:02:39 -04:00
|
|
|
- **`parse?: (input: unknown) => unknown;`**: Parse function for the row, which
|
|
|
|
will be executed after parsing of all columns. Therefore if you don't provide
|
2020-09-27 14:20:46 -04:00
|
|
|
`skipFirstRow`, `columns`, and `parse` function, input will be `string[]`.
|
2020-05-31 16:02:39 -04:00
|
|
|
|
|
|
|
##### `HeaderOptions`
|
|
|
|
|
|
|
|
- **`name: string;`**: Name of the header to be used as property.
|
|
|
|
- **`parse?: (input: string) => unknown;`**: Parse function for the column. This
|
|
|
|
is executed on each entry of the header. This can be combined with the Parse
|
|
|
|
function of the rows.
|
|
|
|
|
|
|
|
##### `ReadOptions`
|
|
|
|
|
2020-11-25 09:50:00 -05:00
|
|
|
- **`comma?: string;`**: Character which separates values. Default: `","`.
|
|
|
|
- **`comment?: string;`**: Character to start a comment. Default: `"#"`.
|
2020-05-31 16:02:39 -04:00
|
|
|
- **`trimLeadingSpace?: boolean;`**: Flag to trim the leading space of the
|
2020-10-03 16:19:11 -04:00
|
|
|
value. Default: `false`.
|
2020-05-31 16:02:39 -04:00
|
|
|
- **`lazyQuotes?: boolean;`**: Allow unquoted quote in a quoted field or non
|
2020-10-03 16:19:11 -04:00
|
|
|
double quoted quotes in quoted field. Default: `false`.
|
2020-05-31 16:02:39 -04:00
|
|
|
- **`fieldsPerRecord?`**: Enabling the check of fields for each row. If == 0,
|
|
|
|
first row is used as referral for the number of fields.
|
2019-05-30 09:50:29 -04:00
|
|
|
|
2020-11-25 09:50:00 -05:00
|
|
|
#### `stringify`
|
|
|
|
|
|
|
|
```ts
|
|
|
|
(data: DataItem[], columns: Column[], options?: StringifyOptions): Promise<string>
|
|
|
|
```
|
|
|
|
|
|
|
|
- **`data`** is the source data to stringify. It's an array of items which are
|
|
|
|
plain objects or arrays.
|
|
|
|
|
|
|
|
`DataItem: Record<string, unknown> | unknown[]`
|
|
|
|
|
2020-11-26 09:52:45 -05:00
|
|
|
```ts
|
|
|
|
const data = [
|
|
|
|
{
|
|
|
|
name: "Deno",
|
|
|
|
repo: { org: "denoland", name: "deno" },
|
|
|
|
runsOn: ["Rust", "TypeScript"],
|
|
|
|
},
|
|
|
|
];
|
|
|
|
```
|
2020-11-25 09:50:00 -05:00
|
|
|
|
|
|
|
- **`columns`** is a list of instructions for how to target and transform the
|
|
|
|
data for each column of output. This is also where you can provide an explicit
|
|
|
|
header name for the column.
|
|
|
|
|
|
|
|
`Column`:
|
2020-11-26 09:52:45 -05:00
|
|
|
|
2020-11-25 09:50:00 -05:00
|
|
|
- The most essential aspect of a column is accessing the property holding the
|
|
|
|
data for that column on each object in the data array. If that member is at
|
|
|
|
the top level, `Column` can simply be a property accessor, which is either a
|
|
|
|
`string` (if it's a plain object) or a `number` (if it's an array).
|
|
|
|
|
2020-11-26 09:52:45 -05:00
|
|
|
```ts
|
|
|
|
const columns = [
|
|
|
|
"name",
|
|
|
|
];
|
|
|
|
```
|
2020-11-25 09:50:00 -05:00
|
|
|
|
|
|
|
Each property accessor will be used as the header for the column:
|
|
|
|
|
|
|
|
| name |
|
|
|
|
| :--: |
|
|
|
|
| Deno |
|
|
|
|
|
|
|
|
- If the required data is not at the top level (it's nested in other
|
|
|
|
objects/arrays), then a simple property accessor won't work, so an array of
|
|
|
|
them will be required.
|
|
|
|
|
2020-11-26 09:52:45 -05:00
|
|
|
```ts
|
|
|
|
const columns = [
|
|
|
|
["repo", "name"],
|
|
|
|
["repo", "org"],
|
|
|
|
];
|
|
|
|
```
|
2020-11-25 09:50:00 -05:00
|
|
|
|
|
|
|
When using arrays of property accessors, the header names inherit the value
|
|
|
|
of the last accessor in each array:
|
|
|
|
|
|
|
|
| name | org |
|
|
|
|
| :--: | :------: |
|
|
|
|
| deno | denoland |
|
|
|
|
|
|
|
|
- If the data is not already in the required output format, or a different
|
|
|
|
column header is desired, then a `ColumnDetails` object type can be used for
|
|
|
|
each column:
|
|
|
|
|
|
|
|
- **`fn?: (value: any) => string | Promise<string>`** is an optional
|
|
|
|
function to transform the targeted data into the desired format
|
2020-11-26 09:52:45 -05:00
|
|
|
|
2020-11-25 09:50:00 -05:00
|
|
|
- **`header?: string`** is the optional value to use for the column header
|
|
|
|
name
|
2020-11-26 09:52:45 -05:00
|
|
|
|
2020-11-25 09:50:00 -05:00
|
|
|
- **`prop: PropertyAccessor | PropertyAccessor[]`** is the property accessor
|
|
|
|
(`string` or `number`) or array of property accessors used to access the
|
|
|
|
data on each object
|
|
|
|
|
2020-11-26 09:52:45 -05:00
|
|
|
```ts
|
|
|
|
const columns = [
|
|
|
|
"name",
|
|
|
|
{
|
|
|
|
prop: ["runsOn", 0],
|
|
|
|
header: "language 1",
|
|
|
|
fn: (str: string) => str.toLowerCase(),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
prop: ["runsOn", 1],
|
|
|
|
header: "language 2",
|
|
|
|
fn: (str: string) => str.toLowerCase(),
|
|
|
|
},
|
|
|
|
];
|
|
|
|
```
|
2020-11-25 09:50:00 -05:00
|
|
|
|
|
|
|
| name | language 1 | language 2 |
|
|
|
|
| :--: | :--------: | :--------: |
|
|
|
|
| Deno | rust | typescript |
|
|
|
|
|
2020-12-10 14:45:45 -05:00
|
|
|
- **`options`** are options for the delimiter-separated output.
|
2020-11-25 09:50:00 -05:00
|
|
|
|
|
|
|
- **`headers?: boolean`**: Whether or not to include the row of headers.
|
|
|
|
Default: `true`
|
|
|
|
|
|
|
|
- **`separator?: string`**: Delimiter used to separate values. Examples:
|
|
|
|
- `","` _comma_ (Default)
|
|
|
|
- `"\t"` _tab_
|
|
|
|
- `"|"` _pipe_
|
|
|
|
- etc.
|
|
|
|
|
|
|
|
### Basic Usage
|
2019-05-30 09:50:29 -04:00
|
|
|
|
|
|
|
```ts
|
2020-10-04 08:18:36 -04:00
|
|
|
import { parse } from "https://deno.land/std@$STD_VERSION/encoding/csv.ts";
|
2019-12-20 15:21:30 -05:00
|
|
|
const string = "a,b,c\nd,e,f";
|
2019-05-30 09:50:29 -04:00
|
|
|
|
2019-12-20 15:21:30 -05:00
|
|
|
console.log(
|
2020-05-18 06:10:19 -04:00
|
|
|
await parse(string, {
|
2020-10-07 12:01:35 -04:00
|
|
|
skipFirstRow: false,
|
2020-07-14 15:24:17 -04:00
|
|
|
}),
|
2019-12-20 15:21:30 -05:00
|
|
|
);
|
2019-05-30 09:50:29 -04:00
|
|
|
// output:
|
2019-12-20 15:21:30 -05:00
|
|
|
// [["a", "b", "c"], ["d", "e", "f"]]
|
2019-05-30 09:50:29 -04:00
|
|
|
```
|
|
|
|
|
2020-11-25 09:50:00 -05:00
|
|
|
```ts
|
|
|
|
import {
|
|
|
|
Column,
|
|
|
|
stringify,
|
|
|
|
} from "https://deno.land/std@$STD_VERSION/encoding/csv.ts";
|
|
|
|
|
|
|
|
type Character = {
|
|
|
|
age: number;
|
|
|
|
name: {
|
|
|
|
first: string;
|
|
|
|
last: string;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
const data: Character[] = [
|
|
|
|
{
|
|
|
|
age: 70,
|
|
|
|
name: {
|
|
|
|
first: "Rick",
|
|
|
|
last: "Sanchez",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
age: 14,
|
|
|
|
name: {
|
|
|
|
first: "Morty",
|
|
|
|
last: "Smith",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
let columns: Column[] = [
|
|
|
|
["name", "first"],
|
|
|
|
"age",
|
|
|
|
];
|
|
|
|
|
|
|
|
console.log(await stringify(data, columns));
|
|
|
|
// first,age
|
|
|
|
// Rick,70
|
|
|
|
// Morty,14
|
|
|
|
//
|
|
|
|
|
|
|
|
columns = [
|
|
|
|
{
|
|
|
|
prop: "name",
|
|
|
|
fn: (name: Character["name"]) => `${name.first} ${name.last}`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
prop: "age",
|
|
|
|
header: "is_adult",
|
|
|
|
fn: (age: Character["age"]) => String(age >= 18),
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
console.log(await stringify(data, columns, { separator: "\t" }));
|
|
|
|
// name is_adult
|
|
|
|
// Rick Sanchez true
|
|
|
|
// Morty Smith false
|
|
|
|
//
|
|
|
|
```
|
|
|
|
|
2019-05-30 09:50:29 -04:00
|
|
|
## TOML
|
2019-03-28 12:31:15 -04:00
|
|
|
|
|
|
|
This module parse TOML files. It follows as much as possible the
|
2020-07-30 09:56:10 -04:00
|
|
|
[TOML specs](https://toml.io/en/latest). Be sure to read the supported types as
|
|
|
|
not every specs is supported at the moment and the handling in TypeScript side
|
|
|
|
is a bit different.
|
2019-03-28 12:31:15 -04:00
|
|
|
|
2019-05-30 09:50:29 -04:00
|
|
|
### Supported types and handling
|
2019-03-28 12:31:15 -04:00
|
|
|
|
2020-07-30 09:56:10 -04:00
|
|
|
- :heavy_check_mark: [Keys](https://toml.io/en/latest#keys)
|
|
|
|
- :exclamation: [String](https://toml.io/en/latest#string)
|
|
|
|
- :heavy_check_mark: [Multiline String](https://toml.io/en/latest#string)
|
|
|
|
- :heavy_check_mark: [Literal String](https://toml.io/en/latest#string)
|
|
|
|
- :exclamation: [Integer](https://toml.io/en/latest#integer)
|
|
|
|
- :heavy_check_mark: [Float](https://toml.io/en/latest#float)
|
|
|
|
- :heavy_check_mark: [Boolean](https://toml.io/en/latest#boolean)
|
2019-03-28 12:31:15 -04:00
|
|
|
- :heavy_check_mark:
|
2020-07-30 09:56:10 -04:00
|
|
|
[Offset Date-time](https://toml.io/en/latest#offset-date-time)
|
2019-03-28 12:31:15 -04:00
|
|
|
- :heavy_check_mark:
|
2020-07-30 09:56:10 -04:00
|
|
|
[Local Date-time](https://toml.io/en/latest#local-date-time)
|
|
|
|
- :heavy_check_mark: [Local Date](https://toml.io/en/latest#local-date)
|
|
|
|
- :exclamation: [Local Time](https://toml.io/en/latest#local-time)
|
|
|
|
- :heavy_check_mark: [Table](https://toml.io/en/latest#table)
|
|
|
|
- :heavy_check_mark: [Inline Table](https://toml.io/en/latest#inline-table)
|
|
|
|
- :exclamation: [Array of Tables](https://toml.io/en/latest#array-of-tables)
|
2019-03-28 12:31:15 -04:00
|
|
|
|
|
|
|
:exclamation: _Supported with warnings see [Warning](#Warning)._
|
|
|
|
|
2019-05-30 09:50:29 -04:00
|
|
|
#### :warning: Warning
|
2019-03-28 12:31:15 -04:00
|
|
|
|
2019-05-30 09:50:29 -04:00
|
|
|
##### String
|
2019-03-28 12:31:15 -04:00
|
|
|
|
2019-10-09 17:22:22 -04:00
|
|
|
- Regex : Due to the spec, there is no flag to detect regex properly in a TOML
|
|
|
|
declaration. So the regex is stored as string.
|
2019-03-28 12:31:15 -04:00
|
|
|
|
2019-05-30 09:50:29 -04:00
|
|
|
##### Integer
|
2019-03-28 12:31:15 -04:00
|
|
|
|
2019-10-09 17:22:22 -04:00
|
|
|
For **Binary** / **Octal** / **Hexadecimal** numbers, they are stored as string
|
|
|
|
to be not interpreted as Decimal.
|
2019-03-28 12:31:15 -04:00
|
|
|
|
2019-05-30 09:50:29 -04:00
|
|
|
##### Local Time
|
2019-03-28 12:31:15 -04:00
|
|
|
|
2019-10-09 17:22:22 -04:00
|
|
|
Because local time does not exist in JavaScript, the local time is stored as a
|
|
|
|
string.
|
2019-03-28 12:31:15 -04:00
|
|
|
|
2019-05-30 09:50:29 -04:00
|
|
|
##### Inline Table
|
2019-03-28 12:31:15 -04:00
|
|
|
|
2019-04-04 06:00:24 -04:00
|
|
|
Inline tables are supported. See below:
|
2019-03-28 12:31:15 -04:00
|
|
|
|
|
|
|
```toml
|
2019-04-04 06:00:24 -04:00
|
|
|
animal = { type = { name = "pug" } }
|
2020-07-30 09:56:10 -04:00
|
|
|
## Output { animal: { type: { name: "pug" } } }
|
2019-03-28 12:31:15 -04:00
|
|
|
animal = { type.name = "pug" }
|
2020-07-30 09:56:10 -04:00
|
|
|
## Output { animal: { type : { name : "pug" } }
|
2019-04-04 06:00:24 -04:00
|
|
|
animal.as.leaders = "tosin"
|
2019-05-30 09:50:29 -04:00
|
|
|
## Output { animal: { as: { leaders: "tosin" } } }
|
2019-04-04 06:00:24 -04:00
|
|
|
"tosin.abasi" = "guitarist"
|
2020-07-30 09:56:10 -04:00
|
|
|
## Output { tosin.abasi: "guitarist" }
|
2019-03-28 12:31:15 -04:00
|
|
|
```
|
|
|
|
|
2019-05-30 09:50:29 -04:00
|
|
|
##### Array of Tables
|
2019-03-28 12:31:15 -04:00
|
|
|
|
|
|
|
At the moment only simple declarations like below are supported:
|
|
|
|
|
|
|
|
```toml
|
|
|
|
[[bin]]
|
|
|
|
name = "deno"
|
|
|
|
path = "cli/main.rs"
|
|
|
|
|
|
|
|
[[bin]]
|
|
|
|
name = "deno_core"
|
|
|
|
path = "src/foo.rs"
|
|
|
|
|
|
|
|
[[nib]]
|
|
|
|
name = "node"
|
|
|
|
path = "not_found"
|
|
|
|
```
|
|
|
|
|
|
|
|
will output:
|
|
|
|
|
|
|
|
```json
|
|
|
|
{
|
|
|
|
"bin": [
|
|
|
|
{ "name": "deno", "path": "cli/main.rs" },
|
|
|
|
{ "name": "deno_core", "path": "src/foo.rs" }
|
|
|
|
],
|
|
|
|
"nib": [{ "name": "node", "path": "not_found" }]
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2020-06-09 15:08:38 -04:00
|
|
|
### Basic usage
|
2019-04-05 00:23:05 -04:00
|
|
|
|
|
|
|
```ts
|
2020-10-04 08:18:36 -04:00
|
|
|
import {
|
|
|
|
parse,
|
|
|
|
stringify,
|
|
|
|
} from "https://deno.land/std@$STD_VERSION/encoding/toml.ts";
|
2019-04-05 00:23:05 -04:00
|
|
|
const obj = {
|
|
|
|
bin: [
|
|
|
|
{ name: "deno", path: "cli/main.rs" },
|
2020-03-28 13:03:49 -04:00
|
|
|
{ name: "deno_core", path: "src/foo.rs" },
|
2019-04-05 00:23:05 -04:00
|
|
|
],
|
2020-03-28 13:03:49 -04:00
|
|
|
nib: [{ name: "node", path: "not_found" }],
|
2019-04-05 00:23:05 -04:00
|
|
|
};
|
|
|
|
const tomlString = stringify(obj);
|
2020-06-09 15:08:38 -04:00
|
|
|
console.log(tomlString);
|
|
|
|
|
|
|
|
// =>
|
|
|
|
// [[bin]]
|
|
|
|
// name = "deno"
|
|
|
|
// path = "cli/main.rs"
|
|
|
|
|
|
|
|
// [[bin]]
|
|
|
|
// name = "deno_core"
|
|
|
|
// path = "src/foo.rs"
|
|
|
|
|
|
|
|
// [[nib]]
|
|
|
|
// name = "node"
|
|
|
|
// path = "not_found"
|
|
|
|
|
|
|
|
const tomlObject = parse(tomlString);
|
|
|
|
console.log(tomlObject);
|
|
|
|
|
|
|
|
// =>
|
|
|
|
// {
|
|
|
|
// bin: [
|
|
|
|
// { name: "deno", path: "cli/main.rs" },
|
|
|
|
// { name: "deno_core", path: "src/foo.rs" }
|
|
|
|
// ],
|
|
|
|
// nib: [ { name: "node", path: "not_found" } ]
|
|
|
|
// }
|
2019-04-05 00:23:05 -04:00
|
|
|
```
|
2019-11-18 09:39:32 -05:00
|
|
|
|
|
|
|
## YAML
|
|
|
|
|
2020-10-03 16:19:11 -04:00
|
|
|
YAML parser / dumper for Deno.
|
2019-11-18 09:39:32 -05:00
|
|
|
|
2020-11-25 09:50:00 -05:00
|
|
|
Heavily inspired from [`js-yaml`](https://github.com/nodeca/js-yaml).
|
2019-11-18 09:39:32 -05:00
|
|
|
|
2019-12-20 11:32:05 -05:00
|
|
|
### Basic usage
|
2019-11-18 09:39:32 -05:00
|
|
|
|
2019-12-20 11:32:05 -05:00
|
|
|
`parse` parses the yaml string, and `stringify` dumps the given object to YAML
|
|
|
|
string.
|
|
|
|
|
|
|
|
```ts
|
2020-10-04 08:18:36 -04:00
|
|
|
import {
|
|
|
|
parse,
|
|
|
|
stringify,
|
|
|
|
} from "https://deno.land/std@$STD_VERSION/encoding/yaml.ts";
|
2019-12-20 11:32:05 -05:00
|
|
|
|
|
|
|
const data = parse(`
|
|
|
|
foo: bar
|
|
|
|
baz:
|
|
|
|
- qux
|
|
|
|
- quux
|
|
|
|
`);
|
|
|
|
console.log(data);
|
|
|
|
// => { foo: "bar", baz: [ "qux", "quux" ] }
|
|
|
|
|
|
|
|
const yaml = stringify({ foo: "bar", baz: ["qux", "quux"] });
|
|
|
|
console.log(yaml);
|
|
|
|
// =>
|
|
|
|
// foo: bar
|
|
|
|
// baz:
|
|
|
|
// - qux
|
|
|
|
// - quux
|
|
|
|
```
|
|
|
|
|
|
|
|
If your YAML contains multiple documents in it, you can use `parseAll` for
|
|
|
|
handling it.
|
|
|
|
|
|
|
|
```ts
|
2020-10-04 08:18:36 -04:00
|
|
|
import { parseAll } from "https://deno.land/std@$STD_VERSION/encoding/yaml.ts";
|
2019-12-20 11:32:05 -05:00
|
|
|
|
|
|
|
const data = parseAll(`
|
|
|
|
---
|
|
|
|
id: 1
|
|
|
|
name: Alice
|
|
|
|
---
|
|
|
|
id: 2
|
|
|
|
name: Bob
|
|
|
|
---
|
|
|
|
id: 3
|
|
|
|
name: Eve
|
|
|
|
`);
|
|
|
|
console.log(data);
|
|
|
|
// => [ { id: 1, name: "Alice" }, { id: 2, name: "Bob" }, { id: 3, name: "Eve" } ]
|
|
|
|
```
|
|
|
|
|
|
|
|
### API
|
|
|
|
|
|
|
|
#### `parse(str: string, opts?: ParserOption): unknown`
|
|
|
|
|
|
|
|
Parses the YAML string with a single document.
|
|
|
|
|
|
|
|
#### `parseAll(str: string, iterator?: Function, opts?: ParserOption): unknown`
|
|
|
|
|
|
|
|
Parses the YAML string with multiple documents. If the iterator is given, it's
|
|
|
|
applied to every document instead of returning the array of parsed objects.
|
|
|
|
|
|
|
|
#### `stringify(obj: object, opts?: DumpOption): string`
|
|
|
|
|
|
|
|
Serializes `object` as a YAML document.
|
2019-11-18 09:39:32 -05:00
|
|
|
|
|
|
|
### :warning: Limitations
|
|
|
|
|
2020-10-03 16:19:11 -04:00
|
|
|
- `binary` type is currently not stable.
|
|
|
|
- `function`, `regexp`, and `undefined` type are currently not supported.
|
2019-11-18 09:39:32 -05:00
|
|
|
|
2019-12-20 11:32:05 -05:00
|
|
|
### More example
|
2019-11-18 09:39:32 -05:00
|
|
|
|
2020-10-03 16:19:11 -04:00
|
|
|
See: https://github.com/nodeca/js-yaml
|
2020-02-02 16:49:41 -05:00
|
|
|
|
|
|
|
## base32
|
|
|
|
|
|
|
|
[RFC4648 base32](https://tools.ietf.org/html/rfc4648#section-6) encoder/decoder
|
2020-10-03 16:19:11 -04:00
|
|
|
for Deno.
|
2020-02-02 16:49:41 -05:00
|
|
|
|
|
|
|
### Basic usage
|
|
|
|
|
|
|
|
`encode` encodes a `Uint8Array` to RFC4648 base32 representation, and `decode`
|
|
|
|
decodes the given RFC4648 base32 representation to a `Uint8Array`.
|
|
|
|
|
|
|
|
```ts
|
2020-10-04 08:18:36 -04:00
|
|
|
import {
|
|
|
|
decode,
|
|
|
|
encode,
|
|
|
|
} from "https://deno.land/std@$STD_VERSION/encoding/base32.ts";
|
2020-02-02 16:49:41 -05:00
|
|
|
|
|
|
|
const b32Repr = "RC2E6GA=";
|
|
|
|
|
|
|
|
const binaryData = decode(b32Repr);
|
|
|
|
console.log(binaryData);
|
|
|
|
// => Uint8Array [ 136, 180, 79, 24 ]
|
|
|
|
|
|
|
|
console.log(encode(binaryData));
|
|
|
|
// => RC2E6GA=
|
|
|
|
```
|
2020-07-14 14:26:49 -04:00
|
|
|
|
|
|
|
## ascii85
|
|
|
|
|
2020-10-03 16:19:11 -04:00
|
|
|
Ascii85/base85 encoder and decoder with support for multiple standards.
|
2020-07-14 14:26:49 -04:00
|
|
|
|
|
|
|
### Basic usage
|
|
|
|
|
|
|
|
`encode` encodes a `Uint8Array` to a ascii85 representation, and `decode`
|
|
|
|
decodes the given ascii85 representation to a `Uint8Array`.
|
|
|
|
|
|
|
|
```ts
|
2020-10-04 08:18:36 -04:00
|
|
|
import {
|
|
|
|
decode,
|
|
|
|
encode,
|
|
|
|
} from "https://deno.land/std@$STD_VERSION/encoding/ascii85.ts";
|
2020-07-14 14:26:49 -04:00
|
|
|
|
|
|
|
const a85Repr = "LpTqp";
|
|
|
|
|
|
|
|
const binaryData = decode(a85Repr);
|
|
|
|
console.log(binaryData);
|
|
|
|
// => Uint8Array [ 136, 180, 79, 24 ]
|
|
|
|
|
|
|
|
console.log(encode(binaryData));
|
|
|
|
// => LpTqp
|
|
|
|
```
|
|
|
|
|
2020-12-10 14:45:45 -05:00
|
|
|
### Specifying a standard and delimiter
|
2020-07-14 14:26:49 -04:00
|
|
|
|
|
|
|
By default all functions are using the most popular Adobe version of ascii85 and
|
2020-12-10 14:45:45 -05:00
|
|
|
not adding any delimiter. However, there are three more standards supported -
|
|
|
|
btoa (different delimiter and additional compression of 4 bytes equal to 32),
|
2020-07-14 14:26:49 -04:00
|
|
|
[Z85](https://rfc.zeromq.org/spec/32/) and
|
|
|
|
[RFC 1924](https://tools.ietf.org/html/rfc1924). It's possible to use a
|
|
|
|
different encoding by specifying it in `options` object as a second parameter.
|
|
|
|
|
2020-12-10 14:45:45 -05:00
|
|
|
Similarly, it's possible to make `encode` add a delimiter (`<~` and `~>` for
|
|
|
|
Adobe, `xbtoa Begin` and `xbtoa End` with newlines between the delimiters and
|
|
|
|
encoded data for btoa. Checksums for btoa are not supported. Delimiters are not
|
2020-07-14 14:26:49 -04:00
|
|
|
supported by other encodings.)
|
|
|
|
|
|
|
|
encoding examples:
|
|
|
|
|
|
|
|
```ts
|
2020-10-04 08:18:36 -04:00
|
|
|
import {
|
|
|
|
decode,
|
|
|
|
encode,
|
|
|
|
} from "https://deno.land/std@$STD_VERSION/encoding/ascii85.ts";
|
2020-07-14 14:26:49 -04:00
|
|
|
const binaryData = new Uint8Array([136, 180, 79, 24]);
|
|
|
|
console.log(encode(binaryData));
|
|
|
|
// => LpTqp
|
2020-12-10 14:45:45 -05:00
|
|
|
console.log(encode(binaryData, { standard: "Adobe", delimiter: true }));
|
2020-07-14 14:26:49 -04:00
|
|
|
// => <~LpTqp~>
|
2020-12-10 14:45:45 -05:00
|
|
|
console.log(encode(binaryData, { standard: "btoa", delimiter: true }));
|
2020-07-14 14:26:49 -04:00
|
|
|
/* => xbtoa Begin
|
|
|
|
LpTqp
|
|
|
|
xbtoa End */
|
|
|
|
console.log(encode(binaryData, { standard: "RFC 1924" }));
|
|
|
|
// => h_p`_
|
|
|
|
console.log(encode(binaryData, { standard: "Z85" }));
|
|
|
|
// => H{P}{
|
|
|
|
```
|