1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-24 08:09:08 -05:00
denoland-deno/encoding
Bert Belder b95f79d74c io: refactor BufReader/Writer interfaces to be more idiomatic (denoland/deno_std#444)
Thanks Vincent Le Goff (@zekth) for porting over the CSV reader
implementation.

Fixes: denoland/deno_std#436

Original: 0ee6334b69
2019-05-29 09:50:12 -07:00
..
testdata TOML: Move to encoding dir (denoland/deno_std#435) 2019-05-23 21:48:54 +03:00
csv.ts io: refactor BufReader/Writer interfaces to be more idiomatic (denoland/deno_std#444) 2019-05-29 09:50:12 -07:00
csv_test.ts io: refactor BufReader/Writer interfaces to be more idiomatic (denoland/deno_std#444) 2019-05-29 09:50:12 -07:00
README.md TOML: Move to encoding dir (denoland/deno_std#435) 2019-05-23 21:48:54 +03:00
test.ts Add encoding/csv (denoland/deno_std#432) 2019-05-24 16:33:42 +03:00
toml.ts TOML: Move to encoding dir (denoland/deno_std#435) 2019-05-23 21:48:54 +03:00
toml_test.ts TOML: Move to encoding dir (denoland/deno_std#435) 2019-05-23 21:48:54 +03:00

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