@decimalturn/toml-patch 3.2.0-lite → 3.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +22 -0
- package/README.md +200 -0
- package/dist/toml-patch.d.ts +547 -0
- package/dist/toml-patch.js +84 -0
- package/package.json +110 -14
- /package/dist/{patch.d.ts → patch-lite.d.ts} +0 -0
- /package/dist/{patch.js → patch-lite.js} +0 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2019–2025 Tim Hall
|
|
4
|
+
Copyright (c) 2025 Martin Leduc
|
|
5
|
+
|
|
6
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
in the Software without restriction, including without limitation the rights
|
|
9
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
furnished to do so, subject to the following conditions:
|
|
12
|
+
|
|
13
|
+
The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
copies or substantial portions of the Software.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
<img src="assets/toml-patch-logo.png" alt="toml-patch logo" width="120"/>
|
|
2
|
+
|
|
3
|
+
# toml-patch
|
|
4
|
+
|
|
5
|
+
[](https://www.npmjs.com/package/@decimalturn/toml-patch)
|
|
6
|
+
[](https://jsr.io/@decimalturn/toml-patch)
|
|
7
|
+
[](https://github.com/DecimalTurn/toml-patch/actions/workflows/test-and-build.yml)
|
|
8
|
+
|
|
9
|
+
Patch, parse, and stringify [TOML](https://toml.io/en/) (v1.1.0) while preserving comments, whitespace and formatting.
|
|
10
|
+
|
|
11
|
+
This project started as a fork of the [original toml-patch](https://github.com/timhall/toml-patch) but has since evolved into a standalone project with significant improvements in reliability and features. We've added TOML v1.1 support, introduced new APIs like `TomlDocument` and `TomlFormat` classes, fixed numerous bugs through increase in testing namely with [toml-test](https://github.com/toml-lang/toml-test).
|
|
12
|
+
|
|
13
|
+
We hope that these improvements can be incorporated upstream one day if the original author returns, but until then, this project is the actively maintained version.
|
|
14
|
+
|
|
15
|
+
## Documentation
|
|
16
|
+
|
|
17
|
+
- [Installation](#installation)
|
|
18
|
+
- [API](#api)
|
|
19
|
+
- [Patch-lite](#patch-lite)
|
|
20
|
+
- [Comment ownership](#comment-ownership)
|
|
21
|
+
- [Date/time handling and Temporal](#datetime-handling--temporal)
|
|
22
|
+
- [Formatting](#formatting)
|
|
23
|
+
- [Changelog](https://github.com/DecimalTurn/toml-patch/blob/v3.2.0/CHANGELOG.md)
|
|
24
|
+
- [Contributing](https://github.com/DecimalTurn/toml-patch/blob/v3.2.0/CONTRIBUTING.md)
|
|
25
|
+
- [MIT License](https://github.com/DecimalTurn/toml-patch/blob/v3.2.0/LICENSE)
|
|
26
|
+
|
|
27
|
+
## Installation
|
|
28
|
+
|
|
29
|
+
toml-patch is dependency-free and can be installed via your favorite package manager.
|
|
30
|
+
|
|
31
|
+
*Example with NPM*
|
|
32
|
+
|
|
33
|
+
```
|
|
34
|
+
$ npm install --save @decimalturn/toml-patch
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
For the standalone lite distribution, install the `lite` npm dist-tag:
|
|
38
|
+
|
|
39
|
+
```sh
|
|
40
|
+
$ npm install --save @decimalturn/toml-patch@lite
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
For browser usage, you can use unpkg:
|
|
44
|
+
|
|
45
|
+
```html
|
|
46
|
+
<script type="module">
|
|
47
|
+
import * as TOML from 'https://unpkg.com/@decimalturn/toml-patch@v3.2.0/dist/toml-patch.js';
|
|
48
|
+
</script>
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
## API
|
|
52
|
+
|
|
53
|
+
overwrite(tomlString: string): void
|
|
54
|
+
toml-patch provides a functional API for one-time operations and a document-oriented API for workflows that need multiple operations on the same TOML document.
|
|
55
|
+
|
|
56
|
+
See the [API reference](https://github.com/DecimalTurn/toml-patch/blob/v3.2.0/docs/API.md) for `patch`, `parse`, `stringify` and the `TomlDocument` class.
|
|
57
|
+
|
|
58
|
+
For a quick start, patch an existing TOML string like this:
|
|
59
|
+
|
|
60
|
+
```js
|
|
61
|
+
import { patch } from '@decimalturn/toml-patch';
|
|
62
|
+
|
|
63
|
+
const existing = 'title = "TOML example"\nowner.name = "Bob"\n';
|
|
64
|
+
const updated = patch(existing, {
|
|
65
|
+
title: 'TOML example',
|
|
66
|
+
owner: { name: 'Tim' }
|
|
67
|
+
});
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
## Patch-lite
|
|
71
|
+
|
|
72
|
+
The `patch-lite` subpath is a smaller, edit-only distribution for applications that only need to update existing TOML values, such as bumping a package version. It exposes the same `patch` name with a two-argument `(existing, updated)` signature:
|
|
73
|
+
|
|
74
|
+
```js
|
|
75
|
+
import { patch } from '@decimalturn/toml-patch/patch-lite';
|
|
76
|
+
|
|
77
|
+
const existing = 'version = "1.0.0"\n';
|
|
78
|
+
const updated = patch(existing, { version: '1.0.1' });
|
|
79
|
+
// updated === 'version = "1.0.1"\n'
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
The lite function edits existing primitive values only and preserves all source text outside the edited value, including comments, whitespace, line endings and a leading BOM. Added or removed keys, array length changes, reordering, renames, primitive/container type changes and unsupported value types throw before any output is produced. Use the full `patch()` for additions, removals, moves, renames and advanced formatting.
|
|
83
|
+
|
|
84
|
+
Unlike the full `patch()`, the lite distribution performs no encoding validation on its output: it does not reject strings containing unpaired UTF-16 surrogates, which cannot be represented as valid TOML/UTF-8. Use the full `patch()` when this validation is required.
|
|
85
|
+
|
|
86
|
+
The [Patch-lite guide](https://github.com/DecimalTurn/toml-patch/blob/v3.2.0/docs/Patch-Lite.md) lists every supported and rejected operation with its error code, the value-encoding rules that differ from the full `patch()`, and the measured size and throughput, so you can check whether the lite distribution covers your use case.
|
|
87
|
+
|
|
88
|
+
The lite distribution is also published as a standalone package under the `lite` npm dist-tag:
|
|
89
|
+
|
|
90
|
+
```sh
|
|
91
|
+
npm install @decimalturn/toml-patch@lite
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
## Comment Ownership
|
|
95
|
+
|
|
96
|
+
When `patch()` removes or reorders an entry, any comment describing it (a same-line trailing
|
|
97
|
+
comment, or a line-comment directly above with no blank line in between) travels along with
|
|
98
|
+
it, instead of being left behind to describe whatever ends up in that spot. This applies to root
|
|
99
|
+
keys, `[table]`/`[[array-of-tables]]` blocks, and elements inside multi-line arrays and inline
|
|
100
|
+
tables.
|
|
101
|
+
|
|
102
|
+
```js
|
|
103
|
+
import * as TOML from '@decimalturn/toml-patch';
|
|
104
|
+
import { strict as assert } from 'assert';
|
|
105
|
+
|
|
106
|
+
const existing = `
|
|
107
|
+
x = 1 # a note about x
|
|
108
|
+
y = 2
|
|
109
|
+
`;
|
|
110
|
+
|
|
111
|
+
const patched = TOML.patch(existing, { y: 2 });
|
|
112
|
+
|
|
113
|
+
assert.strictEqual(patched, `
|
|
114
|
+
y = 2
|
|
115
|
+
`);
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
See the [comment ownership guide](https://github.com/DecimalTurn/toml-patch/blob/v3.2.0/docs/Comment-Ownership.md) for the full behavior, including how a blank line opts a comment out of ownership and current scope limitations.
|
|
119
|
+
|
|
120
|
+
Note that `patch()` does not reorder entries by default.
|
|
121
|
+
To have it match the key order of the object you pass in, enable `updateOrder` in the [formatting options](https://github.com/DecimalTurn/toml-patch/blob/v3.2.0/docs/Formatting.md#updateorder).
|
|
122
|
+
|
|
123
|
+
## Date/Time Handling & Temporal
|
|
124
|
+
|
|
125
|
+
TOML date/time values are parsed into custom `Date` subclasses (`LocalDate`, `LocalTime`, `LocalDateTime`, `OffsetDateTime`) by default. Set `temporal: true` to receive [Temporal](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Temporal) objects instead. `stringify()` and `patch()` auto-detect Temporal objects and serialize them correctly.
|
|
126
|
+
|
|
127
|
+
### Enabling Temporal
|
|
128
|
+
|
|
129
|
+
The `temporal: true` option requires `Temporal` to be available in the runtime:
|
|
130
|
+
|
|
131
|
+
| Runtime | How to enable |
|
|
132
|
+
|---|---|
|
|
133
|
+
| **Node.js 26+** | Built-in — enable with `temporal: true` |
|
|
134
|
+
| **Node.js 20–24** | `node --harmony-temporal` flag |
|
|
135
|
+
| **Node.js 14–26** | [@js-temporal/polyfill](https://www.npmjs.com/package/@js-temporal/polyfill) |
|
|
136
|
+
|
|
137
|
+
```js
|
|
138
|
+
import * as TOML from '@decimalturn/toml-patch';
|
|
139
|
+
|
|
140
|
+
// Node 26+ (native) or Node 20–24 with --harmony-temporal:
|
|
141
|
+
const obj = TOML.parse('d = 2024-01-15\n', { temporal: true });
|
|
142
|
+
// obj.d → Temporal.PlainDate
|
|
143
|
+
|
|
144
|
+
// All Node versions with the polyfill:
|
|
145
|
+
import { Temporal } from '@js-temporal/polyfill';
|
|
146
|
+
globalThis.Temporal = Temporal;
|
|
147
|
+
const obj2 = TOML.parse('d = 2024-01-15\n', { temporal: true });
|
|
148
|
+
// obj2.d → PlainDate (polyfill)
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
> **Note:** Only offset-based timezones (`+05:30`, `Z`) are supported in TOML. IANA timezone annotations (e.g., `[Asia/Kolkata]`) will throw an error.
|
|
152
|
+
|
|
153
|
+
See the [date/time guide](https://github.com/DecimalTurn/toml-patch/blob/v3.2.0/docs/Dates.md) for details and examples.
|
|
154
|
+
|
|
155
|
+
## Formatting
|
|
156
|
+
|
|
157
|
+
The `TomlFormat` class controls how TOML documents are formatted during stringification and patching operations.
|
|
158
|
+
|
|
159
|
+
```typescript
|
|
160
|
+
class TomlFormat {
|
|
161
|
+
newLine: string
|
|
162
|
+
trailingNewline: number
|
|
163
|
+
trailingComma: boolean
|
|
164
|
+
bracketSpacing: boolean
|
|
165
|
+
inlineTableStart?: number
|
|
166
|
+
truncateZeroTimeInDates: boolean
|
|
167
|
+
useTabsForIndentation?: boolean
|
|
168
|
+
indentWidth: number
|
|
169
|
+
minimumDecimals?: number
|
|
170
|
+
leadingBom: boolean
|
|
171
|
+
updateOrder?: boolean
|
|
172
|
+
multilineTable: boolean | number | 'auto' | 'parent'
|
|
173
|
+
multilineArray: boolean | number | 'auto' | 'parent'
|
|
174
|
+
|
|
175
|
+
static default(): TomlFormat
|
|
176
|
+
static autoDetectFormat(tomlString: string): TomlFormat
|
|
177
|
+
}
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
Start with `TomlFormat.default()` and override the options you need. e.g.:
|
|
181
|
+
|
|
182
|
+
```js
|
|
183
|
+
import { stringify, TomlFormat } from '@decimalturn/toml-patch';
|
|
184
|
+
|
|
185
|
+
const format = TomlFormat.default();
|
|
186
|
+
format.newLine = '\r\n';
|
|
187
|
+
format.trailingNewline = 0;
|
|
188
|
+
format.trailingComma = true;
|
|
189
|
+
format.bracketSpacing = false;
|
|
190
|
+
format.indentWidth = 4;
|
|
191
|
+
format.multilineTable = 'parent';
|
|
192
|
+
|
|
193
|
+
const toml = stringify({
|
|
194
|
+
title: 'My App',
|
|
195
|
+
tags: ['dev', 'config'],
|
|
196
|
+
database: { host: 'localhost', port: 5432 }
|
|
197
|
+
}, format);
|
|
198
|
+
|
|
199
|
+
```
|
|
200
|
+
See the [formatting reference](https://github.com/DecimalTurn/toml-patch/blob/v3.2.0/docs/Formatting.md) for the complete list of options, auto-detection behavior, `updateOrder` and more examples.
|