1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-29 16:30:56 -05:00
denoland-deno/std/encoding
2020-02-08 14:15:59 -06:00
..
testdata fix: Deno.remove() to properly remove dangling symlinks (#3860) 2020-02-03 08:20:15 -05:00
yaml change copyrights from 2019 to 2020 (#3733) 2020-01-21 10:01:55 -05:00
base32.ts remove non-null assertion operator from std (part1) (#3900) 2020-02-07 02:23:38 -05:00
base32_test.ts std/encoding: add base32 support (#3855) 2020-02-02 16:49:41 -05:00
csv.ts remove non-null assertion operator from std (part2) (#3927) 2020-02-08 14:15:59 -06:00
csv_test.ts remove non-null assertion operator from std (part2) (#3927) 2020-02-08 14:15:59 -06:00
hex.ts Happy new year! (#3578) 2020-01-02 15:13:47 -05:00
hex_test.ts Happy new year! (#3578) 2020-01-02 15:13:47 -05:00
mod.ts feat: Add missing mod.ts files in std (#3509) 2019-12-20 15:21:30 -05:00
README.md std/encoding: add base32 support (#3855) 2020-02-02 16:49:41 -05:00
toml.ts remove non-null assertion operator from std (part2) (#3927) 2020-02-08 14:15:59 -06:00
toml_test.ts fix: Deno.remove() to properly remove dangling symlinks (#3860) 2020-02-03 08:20:15 -05:00
yaml.ts fix(std/encoding/yaml): export parseAll (#3592) 2020-01-04 04:26:57 -05:00
yaml_test.ts Happy new year! (#3578) 2020-01-02 15:13:47 -05:00

Encoding

CSV

  • parseCsv(input: string | BufReader, opt: ParseCsvOptions): Promise<unknown[]>: Read the string/buffer into an

Usage

const string = "a,b,c\nd,e,f";

console.log(
  await parseCsv(string, {
    header: false
  })
);
// output:
// [["a", "b", "c"], ["d", "e", "f"]]

TOML

This module parse TOML files. It follows as much as possible the TOML specs. 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.

Supported types and handling

Supported with warnings see Warning.

⚠️ Warning

String
  • Regex : Due to the spec, there is no flag to detect regex properly in a TOML declaration. So the regex is stored as string.
Integer

For Binary / Octal / Hexadecimal numbers, they are stored as string to be not interpreted as Decimal.

Local Time

Because local time does not exist in JavaScript, the local time is stored as a string.

Inline Table

Inline tables are supported. See below:

animal = { type = { name = "pug" } }
## Output
animal = { type.name = "pug" }
## Output { animal : { type : { name : "pug" } }
animal.as.leaders = "tosin"
## Output { animal: { as: { leaders: "tosin" } } }
"tosin.abasi" = "guitarist"
## Output
"tosin.abasi" : "guitarist"
Array of Tables

At the moment only simple declarations like below are supported:

[[bin]]
name = "deno"
path = "cli/main.rs"

[[bin]]
name = "deno_core"
path = "src/foo.rs"

[[nib]]
name = "node"
path = "not_found"

will output:

{
  "bin": [
    { "name": "deno", "path": "cli/main.rs" },
    { "name": "deno_core", "path": "src/foo.rs" }
  ],
  "nib": [{ "name": "node", "path": "not_found" }]
}

Usage

Parse

import { parse } from "./parser.ts";
import { readFileStrSync } from "../fs/read_file_str.ts";

const tomlObject = parse(readFileStrSync("file.toml"));

const tomlString = 'foo.bar = "Deno"';
const tomlObject22 = parse(tomlString);

Stringify

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);

YAML

YAML parser / dumper for Deno

Heavily inspired from js-yaml

Basic usage

parse parses the yaml string, and stringify dumps the given object to YAML string.

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.

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.

⚠️ Limitations

  • binary type is currently not stable
  • function, regexp, and undefined type are currently not supported

More example

See ./yaml/example folder and js-yaml repository.

base32

RFC4648 base32 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.

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=