This library brings the power of Rust's toml_edit
to the JavaScript ecosystem through WebAssembly. It allows you to edit TOML files while preserving all comments, spacing, and formatting. It also provides parse
and stringify
functions for standard TOML handling.
- Format-Preserving Editing: Modify specific values in your TOML data while keeping your TOML file's formatting, comments, and whitespace intact.
- Standard TOML Parsing: Parses TOML text into a JavaScript object.
- Standard TOML Stringifying: Converts a JavaScript object back into a TOML string.
- Synchronous & Asynchronous API: Supports both sync and async initialization for flexible integration.
- High Performance: Built with Rust and WebAssembly for great performance. Check out the benchmarks.
npm install @rainbowatcher/toml-edit-js
First, you need to initialize the WebAssembly module. You can do this either asynchronously or synchronously.
import init, {
edit,
initSync,
parse,
stringify,
} from "@rainbowatcher/toml-edit-js"
// Asynchronous initialization
await init()
// Or synchronous initialization (e.g., in a CommonJS environment)
// initSync();
const tomlString = `
# Main package configuration
[package]
name = "my-app"
version = "0.1.0" # Initial version
# Release profile settings
[profile.release]
strip = "symbols"
lto = true
codegen-units = 1
`
const parsed = parse(tomlString)
console.log(parsed)
/*
Output:
{
"package": {
"name": "my-app",
"version": "0.1.0"
},
"profile": {
"release": {
"strip": "symbols",
"lto": true,
"codegen-units": 1
}
}
}
*/
The edit
function allows you to change a value at a specific path. The original formatting and comments are preserved.
const originalToml = `
# Main package configuration
[package]
name = "my-app"
version = "0.1.0" # Initial version
# Release profile settings
[profile.release]
strip = "symbols"
lto = true
codegen-units = 1
`
const updatedToml = edit(originalToml, "package.rand", { version: "1.0" })
console.log(updatedToml)
/*
Output:
# Main package configuration
[package]
name = "my-app"
version = "1.0" # Initial version
# Release profile settings
[profile.release]
strip = "symbols"
lto = true
codegen-units = 1
*/
You can convert a JavaScript object back into a TOML string.
const data = {
database: {
ip: "192.168.1.1",
ports: [8001, 8002],
},
}
const tomlStr = stringify(data)
console.log(tomlStr)
/*
Output:
[database]
ip = "192.168.1.1"
ports = [8001, 8002]
*/
For more examples, please see the tests.
Asynchronously initializes the WebAssembly module.
Synchronously initializes the WebAssembly module.
Parses a TOML string into a JavaScript object.
Stringifies a JavaScript object into a TOML string.
Edits a TOML string at a given path with a new value, preserving formatting.
Options for the edit
function.
type IEditOptions = {
/**
* Whether to add a final newline to the output.
* @default true
*/
finalNewline?: boolean
/**
* When the value to be written is an object, write it as an inline table.
* @default true
*/
inline?: boolean
}
Options for the stringify
function.
type IStringifyOptions = {
/**
* Whether to add a final newline to the output.
* @default true
*/
finalNewline?: boolean
/**
* Prefer using inline tables for all tables.
* @default false
*/
inline?: boolean
}
This project is licensed under the MIT License.