mirror of
https://github.com/denoland/std.git
synced 2024-11-22 04:59:05 +00:00
4543b563a9
Make warnings fail |
||
---|---|---|
.. | ||
testdata | ||
parser_test.ts | ||
parser.ts | ||
README.md | ||
test.ts |
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
- ✔️ Keys
- ❗ String
- ✔️ Multiline String
- ✔️ Literal String
- ❗ Integer
- ✔️ Float
- ✔️ Boolean
- ✔️ Offset Date-time
- ✔️ Local Date-time
- ✔️ Local Date
- ❗ Local Time
- ✔️ Table
- ✔️ Inline Table
- ❗ Array of Tables
❗ 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 { parseFile, parse } from "./parser.ts";
const tomlObject = parseFile("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);