Skip to content

DecimalTurn/toml-patch

 
 

Repository files navigation

toml-patch

NPM Version JSR Version GitHub branch status

Patch, parse, and stringify TOML while preserving comments.

Note that this is a maintenance fork of the original toml-patch package. This fork aims at addressing existing issues from the original project and perform dev-dependencies updates. Hopefully, the work done here can go upstream one day if timhall returns, but until then, welcome aboard!1

Installation

toml-patch is dependency-free and can be installed via npm or yarn.

$ npm install --save @decimalturn/toml-patch

For browser usage, you can use unpkg:

<script src="https://unpkg.com/@decimalturn/toml-patch"></script>

API

# patch(existing, updated)

Patch an existing TOML string with the given updated JS/JSON value, while attempting to retain the format of the existing document, including comments, indentation, and structure.

const TOML = require('@decimalturn/toml-patch');
const assert = require('assert');

const existing = `
# This is a TOML document

title = "TOML example"
owner.name = "Bob"
`;
const patched = TOML.patch(existing, {
  title: 'TOML example',
  owner: {
    name: 'Tim'
  }
});

assert.strictEqual(
  patched,
  `
# This is a TOML document

title = "TOML example"
owner.name = "Tim"
`
);

# parse(value)

Parse a TOML string into a JS/JSON value.

const TOML = require('@decimalturn/toml-patch');
const assert = require('assert');

const parsed = TOML.parse(`
# This is a TOML document.

title = "TOML Example"

[owner]
name = "Tim"`);

assert.deepStrictEqual(parsed, {
  title: 'TOML Example',
  owner: {
    name: 'Tim'
  }
});

# stringify(value[, options])

Convert a JS/JSON value to a TOML string. options can be provided for high-level formatting guidelines that follows prettier's configuration.

options

  • [printWidth = 80] - (coming soon)
  • [trailingComma = false] - Add trailing comma to inline tables
  • [bracketSpacing = true] - true: { key = "value" }, false: {key = "value"}
const TOML = require('@decimalturn/toml-patch');
const assert = require('assert');

const toml = TOML.stringify({
  title: 'TOML Example',
  owner: {
    name: 'Tim'
  }
});

assert.strictEqual(
  toml,
  `title = "TOML Example"

[owner]
name = "Tim"`
);

Development

  1. Update submodules: git submodule update --remote
  2. Typecheck: npm run typecheck
  3. Build: npm run build
  4. Test: npm test
  5. Specs compliance: npm run specs
  6. Benchmark: npm run benchmark [<filter>] [--help] [--example] [--reference]

Footnotes

  1. Tim Hall has been inactive on most of his open source projects for more than 3 years. The sentence wording was inspired by the npm-run-all2 project.

About

Patch, parse, and stringify TOML while preserving comments and formatting.

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published

Languages

  • TypeScript 97.6%
  • JavaScript 2.4%