Skip to content

rainbowatcher/toml-edit-js

Repository files navigation

NPM Version GitHub License GitHub Actions Workflow Status NPM Downloads

Toml edit for JavaScript

This repo is built on top of the toml-edit crate. It brings toml-edit to the JavaScript world through WebAssembly.

Usage

npm install @rainbowatcher/toml-edit-js
import init, {
    edit, initSync, parse, stringify,
} from "@rainbowatcher/toml-edit-js"

const toml = `
[package]
rand = "1"

[profile.release]
strip = "symbols"
lto = true
codegen-units = 1
`

await init()
// or initSync()
const parsed = parse(toml)
/*
the parsed will be a js object as follow
{
    "package": {
        "rand": "1"
    },
    "profile": {
        "release": {
            "strip": "symbols",
            "lto": true,
            "codegen-units": 1
        }
    }
}
*/

const edited = edit(toml, "package.rand", { version: "1.0" })
/*
the edited will be a string as follow

[package]
rand = { version = "1.0" }

[profile.release]
strip = "symbols"
lto = true
codegen-units = 1
*/

const str = stringify(parsed)
/* same as const toml */

API

function parse(input: string): any
function stringify(input: any): string
function edit(input: string, path: string, value: any, option?: IEditOptions): string

Options

edit method can receive a options

type IEditOptions = {
    /**
     * whether add the final newline
     */
    finalNewline?: boolean

    /**
     * Write data in InlineTable format when the value to be written is a object type and inline is set to true
     * @default true
     */
    inline?: boolean
}

License

MIT.

About

brings toml-edit to the JavaScript world

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published