Skip to main content
On this page

@std/toml

Overview Jump to heading

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

Supported with warnings see Warning.

Warning

String

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.

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" }]
}
import { parse, stringify } from "@std/toml";
import { assertEquals } from "@std/assert";

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);
assertEquals(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);
assertEquals(tomlObject, obj);

Add to your project Jump to heading

deno add jsr:@std/toml

See all symbols in @std/toml on

什么是 TOML? Jump to heading

TOML(Tom's Obvious, Minimal Language)是一种配置文件格式,因其简单的语法而易于阅读。它支持多种数据类型,如字符串、数字、布尔值、日期、数组和表格,使其在配置中非常灵活。

为什么使用 @std/toml? Jump to heading

该模块提供了一种简单高效的方式,在 JavaScript 中解析和序列化 TOML 数据。可用于读取配置文件或以编程方式生成 TOML 内容。

小贴士 Jump to heading

  • 注意支持的类型:某些 TOML 结构在 JS 中会以字符串形式表示。
  • 对于重复的部分,使用表格数组;保持结构简单以便于理解和维护。

你找到了你需要的东西吗?

编辑此页面
隐私政策