2020-03-10 15:16:08 -04:00
|
|
|
# encoding
|
|
|
|
|
|
|
|
Helper module for dealing with external data structures.
|
|
|
|
|
|
|
|
- [`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>
|
|
|
|
varnum(b: Uint8Array, o: VarnumOptions = {}): number | Deno.EOF
|
|
|
|
varbig(b: Uint8Array, o: VarbigOptions = {}): bigint | Deno.EOF
|
|
|
|
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
|
|
|
|
|
2019-12-20 15:21:30 -05:00
|
|
|
- **`parseCsv(input: string | BufReader, opt: ParseCsvOptions): Promise<unknown[]>`**:
|
|
|
|
Read the string/buffer into an
|
2019-05-30 09:50:29 -04:00
|
|
|
|
2019-12-20 15:21:30 -05:00
|
|
|
### Usage
|
2019-05-30 09:50:29 -04:00
|
|
|
|
|
|
|
```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(
|
|
|
|
await parseCsv(string, {
|
|
|
|
header: false
|
|
|
|
})
|
|
|
|
);
|
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
|
|
|
```
|
|
|
|
|
|
|
|
## TOML
|
2019-03-28 12:31:15 -04:00
|
|
|
|
|
|
|
This module parse TOML files. It follows as much as possible the
|
|
|
|
[TOML specs](https://github.com/toml-lang/toml). 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-05-30 09:50:29 -04:00
|
|
|
### Supported types and handling
|
2019-03-28 12:31:15 -04:00
|
|
|
|
|
|
|
- :heavy_check_mark: [Keys](https://github.com/toml-lang/toml#string)
|
|
|
|
- :exclamation: [String](https://github.com/toml-lang/toml#string)
|
|
|
|
- :heavy_check_mark:
|
|
|
|
[Multiline String](https://github.com/toml-lang/toml#string)
|
|
|
|
- :heavy_check_mark: [Literal String](https://github.com/toml-lang/toml#string)
|
|
|
|
- :exclamation: [Integer](https://github.com/toml-lang/toml#integer)
|
|
|
|
- :heavy_check_mark: [Float](https://github.com/toml-lang/toml#float)
|
|
|
|
- :heavy_check_mark: [Boolean](https://github.com/toml-lang/toml#boolean)
|
|
|
|
- :heavy_check_mark:
|
|
|
|
[Offset Date-time](https://github.com/toml-lang/toml#offset-date-time)
|
|
|
|
- :heavy_check_mark:
|
|
|
|
[Local Date-time](https://github.com/toml-lang/toml#local-date-time)
|
|
|
|
- :heavy_check_mark: [Local Date](https://github.com/toml-lang/toml#local-date)
|
|
|
|
- :exclamation: [Local Time](https://github.com/toml-lang/toml#local-time)
|
|
|
|
- :heavy_check_mark: [Table](https://github.com/toml-lang/toml#table)
|
2019-10-09 17:22:22 -04:00
|
|
|
- :heavy_check_mark:
|
|
|
|
[Inline Table](https://github.com/toml-lang/toml#inline-table)
|
|
|
|
- :exclamation:
|
|
|
|
[Array of Tables](https://github.com/toml-lang/toml#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" } }
|
2019-05-30 09:50:29 -04:00
|
|
|
## Output
|
2019-03-28 12:31:15 -04:00
|
|
|
animal = { type.name = "pug" }
|
2019-05-30 09:50:29 -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"
|
2019-05-30 09:50:29 -04:00
|
|
|
## Output
|
2019-04-04 06:00:24 -04:00
|
|
|
"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" }]
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2019-05-30 09:50:29 -04:00
|
|
|
### Usage
|
2019-03-28 12:31:15 -04:00
|
|
|
|
2019-05-30 09:50:29 -04:00
|
|
|
#### Parse
|
2019-04-05 00:23:05 -04:00
|
|
|
|
2019-03-28 12:31:15 -04:00
|
|
|
```ts
|
2019-04-25 18:47:18 -04:00
|
|
|
import { parse } from "./parser.ts";
|
|
|
|
import { readFileStrSync } from "../fs/read_file_str.ts";
|
2019-03-28 12:31:15 -04:00
|
|
|
|
2019-04-25 18:47:18 -04:00
|
|
|
const tomlObject = parse(readFileStrSync("file.toml"));
|
2019-03-28 12:31:15 -04:00
|
|
|
|
|
|
|
const tomlString = 'foo.bar = "Deno"';
|
|
|
|
const tomlObject22 = parse(tomlString);
|
|
|
|
```
|
2019-04-05 00:23:05 -04:00
|
|
|
|
2019-05-30 09:50:29 -04:00
|
|
|
#### Stringify
|
2019-04-05 00:23:05 -04:00
|
|
|
|
|
|
|
```ts
|
|
|
|
import { stringify } from "./parser.ts";
|
|
|
|
const obj = {
|
|
|
|
bin: [
|
|
|
|
{ name: "deno", path: "cli/main.rs" },
|
|
|
|
{ name: "deno_core", path: "src/foo.rs" }
|
|
|
|
],
|
|
|
|
nib: [{ name: "node", path: "not_found" }]
|
|
|
|
};
|
|
|
|
const tomlString = stringify(obj);
|
|
|
|
```
|
2019-11-18 09:39:32 -05:00
|
|
|
|
|
|
|
## YAML
|
|
|
|
|
|
|
|
YAML parser / dumper for Deno
|
|
|
|
|
|
|
|
Heavily inspired from [js-yaml]
|
|
|
|
|
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
|
|
|
|
import { parse, stringify } from "https://deno.land/std/encoding/yaml.ts";
|
|
|
|
|
|
|
|
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
|
|
|
|
import { parseAll } from "https://deno.land/std/encoding/yaml.ts";
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
- `binary` type is currently not stable
|
|
|
|
- `function`, `regexp`, and `undefined` type are currently not supported
|
|
|
|
|
2019-12-20 11:32:05 -05:00
|
|
|
### More example
|
2019-11-18 09:39:32 -05:00
|
|
|
|
2019-12-20 11:32:05 -05:00
|
|
|
See [`./yaml/example`](./yaml/example) folder and [js-yaml] repository.
|
2019-11-18 09:39:32 -05:00
|
|
|
|
|
|
|
[js-yaml]: 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
|
|
|
|
for Deno
|
|
|
|
|
|
|
|
### Basic usage
|
|
|
|
|
|
|
|
`encode` encodes a `Uint8Array` to RFC4648 base32 representation, and `decode`
|
|
|
|
decodes the given RFC4648 base32 representation to a `Uint8Array`.
|
|
|
|
|
|
|
|
```ts
|
|
|
|
import { encode, decode } from "https://deno.land/std/encoding/base32.ts";
|
|
|
|
|
|
|
|
const b32Repr = "RC2E6GA=";
|
|
|
|
|
|
|
|
const binaryData = decode(b32Repr);
|
|
|
|
console.log(binaryData);
|
|
|
|
// => Uint8Array [ 136, 180, 79, 24 ]
|
|
|
|
|
|
|
|
console.log(encode(binaryData));
|
|
|
|
// => RC2E6GA=
|
|
|
|
```
|