Skip to content

About

Use JSON paths to replace values in TOML

Resources

Contributing

Stars

0 stars

Watchers

1 watching

Forks

Repository files navigation

toml-json-path-replacer

Use JSON paths to set or remove values in TOML strings, while keeping comments and formatting.

Installation

npm i toml-json-path-replacer

Usage

import { tomlJSONPathReplacer } from 'toml-json-path-replacer';
// or: const { tomlJSONPathReplacer } = require('toml-json-path-replacer');

const toml = `
name = "my-worker" # worker name
`;
const updatedTOML = tomlJSONPathReplacer(
  toml, // the TOML string
  ['name'], // the JSON path to set
  'my-updated-worker', // the new value, or undefined to remove the path
);
// name = "my-updated-worker" # worker name

tomlJSONPathReplacer returns a new string. It does not change its arguments.

Examples

const toml = `
workers_dev = false
route = { pattern = "example.org/*", zone_name = "example.org" } # my route

kv_namespaces = [
  { binding = "<MY_NAMESPACE>", id = "<KV_ID>" } # my kv
]

[limits]
cpu_ms = 100

[env.staging]
# Overrides above
route = { pattern = "staging.example.org/*", zone_name = "example.org" }
`;

tomlJSONPathReplacer(toml, ['workers_dev'], true);
// workers_dev = true

tomlJSONPathReplacer(toml, ['route', 'pattern'], 'api.example.org/*');
// route = { pattern = "api.example.org/*", zone_name = "example.org" } # my route

tomlJSONPathReplacer(toml, ['limits', 'cpu_ms'], 50);
// [limits]
// cpu_ms = 50

tomlJSONPathReplacer(toml, ['kv_namespaces', 0, 'id'], 'new-id');
// kv_namespaces = [
//   { binding = "<MY_NAMESPACE>", id = "new-id" } # my kv
// ]

tomlJSONPathReplacer(toml, ['env', 'staging', 'route', 'pattern'], 'api.staging.example.org/*');
// [env.staging]
// # Overrides above
// route = { pattern = "api.staging.example.org/*", zone_name = "example.org" }

tomlJSONPathReplacer(toml, ['env', 'staging', 'route'], {
  pattern: 'api.staging.example.com/*',
  zone_name: 'api.staging.example.com',
});
// [env.staging]
// # Overrides above
// route = { pattern = "api.staging.example.com/*", zone_name = "api.staging.example.com" }

tomlJSONPathReplacer(toml, ['env', 'production'], {
  route: {
    pattern: 'example.com/*',
    zone_name: 'example.com',
  },
});
// adds this to the end of the file:
// [env.production]
// route = { pattern = "example.com/*", zone_name = "example.com" }

tomlJSONPathReplacer(toml, ['env', 'staging'], undefined);
// removes the [env.staging] table

Behavior

  • Paths. Each path segment is a key or an array index. A number, or a digit string such as "0", is an index when it points into an array. On an existing table it is a plain key. A number that is not a non-negative integer, such as -1 or 1.5, throws. An empty path throws.
  • Missing paths. A missing path is created. New tables go to the end of the file. New tables inside a table array element go after that element's sub-tables. New top-level values go to the top of the file, where no table header can claim them. They go after any #: directive lines, such as #:schema.
  • Arrays. You can replace any element or append one at the end. Skipping past the end throws Cannot skip array elements when inserting.
  • Type changes. Setting a new type replaces the old value, its sub-tables included. A table array element that is set to a non-table turns the table array into an inline array. The other elements keep their values as written.
  • Removals. Pass undefined to remove a path and everything below it. A [table] section stays in place, empty, when you remove its last key. A table that only dotted keys define, such as a in a.b = 1, goes away with its last key. So does a table array with its last element. Removing a path that does not exist returns the input unchanged.
  • Formatting. Comments and layout outside the changed value stay as they are. This includes line endings and a byte order mark. Edits inside arrays and inline tables keep the text and comments of the other items. A replaced table keeps its header line. A new value is written in a standard form, so 0x10 set to 16 becomes 16.
  • Rewrites. A change that the current form cannot express rewrites the whole value. One example is a table array that becomes an inline array. Comments inside the rewritten value are lost.
  • Values. Strings, numbers, booleans, bigint, Date, arrays, and plain objects are supported. Object properties set to undefined are skipped. These throw: null, undefined inside an array (holes in a sparse array included), a bigint outside the 64-bit range, an invalid Date or one with a year outside 0 to 9999, a value that contains itself, and any other object, such as a Map or a class instance.
  • TOML versions. The input can use TOML 1.0 or TOML 1.1, such as inline tables over several lines.
  • Errors. Invalid input TOML throws a parse error. The function never returns invalid TOML: if an update would produce it, the function throws instead.

Compatibility

  • Node.js 20.19 or later, 22.13 or later, and 24 or later.
  • Cloudflare Workers.
  • Browsers, through a bundler or a CDN that serves ES modules.

The package ships ES modules and CommonJS, with TypeScript types for both.

About

Use JSON paths to replace values in TOML

Resources

Contributing

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages