import * as mod from "https://deno.land/std@0.180.0/toml/mod.ts";
parse
and stringify
for handling
TOML encoded data. Be sure to read the supported
types as not every spec is supported at the moment and the handling in
TypeScript side is a bit different.
Supported types and handling
- :heavy_check_mark: Keys
- :exclamation: String
- :heavy_check_mark: Multiline String
- :heavy_check_mark: Literal String
- :exclamation: Integer
- :heavy_check_mark: Float
- :heavy_check_mark: Boolean
- :heavy_check_mark: Offset Date-time
- :heavy_check_mark: Local Date-time
- :heavy_check_mark: Local Date
- :exclamation: Local Time
- :heavy_check_mark: Table
- :heavy_check_mark: Inline Table
- :exclamation: Array of Tables
:exclamation: Supported with warnings see Warning.
: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" } } }
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" }]
}
This module is browser compatible.
Examples
Example 1
Example 1
import {
parse,
stringify,
} from "https://deno.land/std@0.180.0/toml/mod.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);
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" } ]
// }