@decimalturn/toml-patch 3.3.2-dev → 3.3.2

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.
Files changed (82) hide show
  1. package/LICENSE +22 -0
  2. package/README.md +203 -0
  3. package/dist/toml-patch.js +84 -0
  4. package/package.json +103 -16
  5. package/dist/dev/comment-alignment.js +0 -372
  6. package/dist/dev/comment-alignment.js.map +0 -1
  7. package/dist/dev/comment-ownership.js +0 -673
  8. package/dist/dev/comment-ownership.js.map +0 -1
  9. package/dist/dev/cst-source.js +0 -118
  10. package/dist/dev/cst-source.js.map +0 -1
  11. package/dist/dev/cst.js +0 -68
  12. package/dist/dev/cst.js.map +0 -1
  13. package/dist/dev/cursor.js +0 -66
  14. package/dist/dev/cursor.js.map +0 -1
  15. package/dist/dev/date-format.js +0 -287
  16. package/dist/dev/date-format.js.map +0 -1
  17. package/dist/dev/decode-utf8.js +0 -27
  18. package/dist/dev/decode-utf8.js.map +0 -1
  19. package/dist/dev/diff-lite.js +0 -108
  20. package/dist/dev/diff-lite.js.map +0 -1
  21. package/dist/dev/diff.js +0 -265
  22. package/dist/dev/diff.js.map +0 -1
  23. package/dist/dev/encode-lite.js +0 -210
  24. package/dist/dev/encode-lite.js.map +0 -1
  25. package/dist/dev/escape-preference.js +0 -166
  26. package/dist/dev/escape-preference.js.map +0 -1
  27. package/dist/dev/find-by-path.js +0 -115
  28. package/dist/dev/find-by-path.js.map +0 -1
  29. package/dist/dev/formatter.js +0 -298
  30. package/dist/dev/formatter.js.map +0 -1
  31. package/dist/dev/generate.js +0 -480
  32. package/dist/dev/generate.js.map +0 -1
  33. package/dist/dev/inline-comma-space.js +0 -13
  34. package/dist/dev/inline-comma-space.js.map +0 -1
  35. package/dist/dev/inline-format.js +0 -106
  36. package/dist/dev/inline-format.js.map +0 -1
  37. package/dist/dev/inline-layout.js +0 -107
  38. package/dist/dev/inline-layout.js.map +0 -1
  39. package/dist/dev/line-ending-backslash.js +0 -320
  40. package/dist/dev/line-ending-backslash.js.map +0 -1
  41. package/dist/dev/literal-string.js +0 -46
  42. package/dist/dev/literal-string.js.map +0 -1
  43. package/dist/dev/location.js +0 -104
  44. package/dist/dev/location.js.map +0 -1
  45. package/dist/dev/parse-error.js +0 -27
  46. package/dist/dev/parse-error.js.map +0 -1
  47. package/dist/dev/parse-js.js +0 -150
  48. package/dist/dev/parse-js.js.map +0 -1
  49. package/dist/dev/parse-string.js +0 -120
  50. package/dist/dev/parse-string.js.map +0 -1
  51. package/dist/dev/parse-toml.js +0 -714
  52. package/dist/dev/parse-toml.js.map +0 -1
  53. package/dist/dev/patch-lite.js +0 -4
  54. package/dist/dev/patch-lite2.js +0 -97
  55. package/dist/dev/patch-lite2.js.map +0 -1
  56. package/dist/dev/patch.js +0 -3128
  57. package/dist/dev/patch.js.map +0 -1
  58. package/dist/dev/smol-toml-date.js +0 -61
  59. package/dist/dev/smol-toml-date.js.map +0 -1
  60. package/dist/dev/to-js.js +0 -308
  61. package/dist/dev/to-js.js.map +0 -1
  62. package/dist/dev/to-toml.js +0 -874
  63. package/dist/dev/to-toml.js.map +0 -1
  64. package/dist/dev/tokenizer.js +0 -225
  65. package/dist/dev/tokenizer.js.map +0 -1
  66. package/dist/dev/toml-document.js +0 -221
  67. package/dist/dev/toml-document.js.map +0 -1
  68. package/dist/dev/toml-format.js +0 -549
  69. package/dist/dev/toml-format.js.map +0 -1
  70. package/dist/dev/toml-patch.js +0 -94
  71. package/dist/dev/toml-patch.js.map +0 -1
  72. package/dist/dev/traverse.js +0 -61
  73. package/dist/dev/traverse.js.map +0 -1
  74. package/dist/dev/truncate.js +0 -64
  75. package/dist/dev/truncate.js.map +0 -1
  76. package/dist/dev/update-order.js +0 -235
  77. package/dist/dev/update-order.js.map +0 -1
  78. package/dist/dev/utils.js +0 -296
  79. package/dist/dev/utils.js.map +0 -1
  80. package/dist/dev/writer.js +0 -758
  81. package/dist/dev/writer.js.map +0 -1
  82. package/dist/patch-lite.d.ts +0 -18
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,203 @@
1
+ <img src="assets/toml-patch-logo.png" alt="toml-patch logo" width="120"/>
2
+
3
+ # toml-patch
4
+
5
+ [![NPM Version](https://img.shields.io/npm/v/%40decimalturn%2Ftoml-patch?logo=npm&logoColor=white&labelColor=red&color=blue)](https://www.npmjs.com/package/@decimalturn/toml-patch)
6
+ [![JSR Version](https://img.shields.io/jsr/v/%40decimalturn/toml-patch?logo=jsr&color=blue)](https://jsr.io/@decimalturn/toml-patch)
7
+ [![GitHub branch status](https://img.shields.io/github/check-runs/DecimalTurn/toml-patch/latest)](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.3.2/CHANGELOG.md)
24
+ - [Contributing](https://github.com/DecimalTurn/toml-patch/blob/v3.3.2/CONTRIBUTING.md)
25
+ - [MIT License](https://github.com/DecimalTurn/toml-patch/blob/v3.3.2/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.3.2/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.3.2/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
+ `patch-lite` 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
+ It is not part of the main package: the main package ships the full API only. Install the lite build from the `lite` npm dist-tag, then import it with the same specifier:
75
+
76
+ ```sh
77
+ npm install --save @decimalturn/toml-patch@lite
78
+ ```
79
+
80
+ ```js
81
+ import { patch } from '@decimalturn/toml-patch';
82
+
83
+ const existing = 'version = "1.0.0"\n';
84
+ const updated = patch(existing, { version: '1.0.1' });
85
+ // updated === 'version = "1.0.1"\n'
86
+ ```
87
+
88
+ The dist-tag tarball maps its root export to the lite build, so there is no `patch-lite` subpath: `import { patch } from '@decimalturn/toml-patch/patch-lite'` fails with `ERR_PACKAGE_PATH_NOT_EXPORTED`. Switching between the two is an install change, never a code change.
89
+
90
+ 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. Date/time values may be given as the custom `Date` subclasses or as `Temporal` objects; both are serialized into the existing row's shape. Use the full `patch()` for additions, removals, moves, renames and advanced formatting.
91
+
92
+ 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.
93
+
94
+ The [Patch-lite guide](https://github.com/DecimalTurn/toml-patch/blob/v3.3.2/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.
95
+
96
+ ## Comment Ownership
97
+
98
+ When `patch()` removes or reorders an entry, any comment describing it (a same-line trailing
99
+ comment, or a line-comment directly above with no blank line in between) travels along with
100
+ it, instead of being left behind to describe whatever ends up in that spot. This applies to root
101
+ keys, `[table]`/`[[array-of-tables]]` blocks, and elements inside multi-line arrays and inline
102
+ tables.
103
+
104
+ ```js
105
+ import * as TOML from '@decimalturn/toml-patch';
106
+ import { strict as assert } from 'assert';
107
+
108
+ const existing = `
109
+ x = 1 # a note about x
110
+ y = 2
111
+ `;
112
+
113
+ const patched = TOML.patch(existing, { y: 2 });
114
+
115
+ assert.strictEqual(patched, `
116
+ y = 2
117
+ `);
118
+ ```
119
+
120
+ See the [comment ownership guide](https://github.com/DecimalTurn/toml-patch/blob/v3.3.2/docs/Comment-Ownership.md) for the full behavior, including how a blank line opts a comment out of ownership and current scope limitations.
121
+
122
+ Note that `patch()` does not reorder entries by default.
123
+ 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.3.2/docs/Formatting.md#updateorder).
124
+
125
+ ## Date/Time Handling & Temporal
126
+
127
+ 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. Fractional seconds keep the source precision, sub-millisecond digits included.
128
+
129
+ ### Enabling Temporal
130
+
131
+ The `temporal: true` option requires `Temporal` to be available in the runtime:
132
+
133
+ | Runtime | How to enable |
134
+ |---|---|
135
+ | **Node.js 26+** | Built-in (no config needed) |
136
+ | **Node.js 20–24** | `node --harmony-temporal` flag |
137
+ | **Node.js 14–26** | [@js-temporal/polyfill](https://www.npmjs.com/package/@js-temporal/polyfill) |
138
+
139
+ ```js
140
+ import * as TOML from '@decimalturn/toml-patch';
141
+
142
+ // Node 26+ (native) or Node 20–24 with --harmony-temporal:
143
+ const obj = TOML.parse('d = 2024-01-15\n', { temporal: true });
144
+ // obj.d → Temporal.PlainDate
145
+
146
+ // All Node versions with the polyfill:
147
+ import { Temporal } from '@js-temporal/polyfill';
148
+ globalThis.Temporal = Temporal;
149
+ const obj2 = TOML.parse('d = 2024-01-15\n', { temporal: true });
150
+ // obj2.d → PlainDate (polyfill)
151
+ ```
152
+
153
+ > **Note:** Only offset-based timezones (`+05:30`, `Z`) are supported in TOML. IANA timezone annotations (e.g., `[Asia/Kolkata]`) will throw an error.
154
+
155
+ See the [date/time guide](https://github.com/DecimalTurn/toml-patch/blob/v3.3.2/docs/Dates.md) for details and examples.
156
+
157
+ ## Formatting
158
+
159
+ The `TomlFormat` class controls how TOML documents are formatted during stringification and patching operations.
160
+
161
+ ```typescript
162
+ class TomlFormat {
163
+ newLine: string
164
+ trailingNewline: number
165
+ trailingComma: boolean
166
+ bracketSpacing: boolean
167
+ inlineTableStart?: number
168
+ truncateZeroTimeInDates: boolean
169
+ useTabsForIndentation?: boolean
170
+ indentWidth: number
171
+ minimumDecimals?: number
172
+ minimumTimeDecimals?: number
173
+ leadingBom: boolean
174
+ updateOrder?: boolean
175
+ multilineTable: boolean | number | 'auto' | 'parent'
176
+ multilineArray: boolean | number | 'auto' | 'parent'
177
+
178
+ static default(): TomlFormat
179
+ static autoDetectFormat(tomlString: string): TomlFormat
180
+ }
181
+ ```
182
+
183
+ Start with `TomlFormat.default()` and override the options you need. e.g.:
184
+
185
+ ```js
186
+ import { stringify, TomlFormat } from '@decimalturn/toml-patch';
187
+
188
+ const format = TomlFormat.default();
189
+ format.newLine = '\r\n';
190
+ format.trailingNewline = 0;
191
+ format.trailingComma = true;
192
+ format.bracketSpacing = false;
193
+ format.indentWidth = 4;
194
+ format.multilineTable = 'parent';
195
+
196
+ const toml = stringify({
197
+ title: 'My App',
198
+ tags: ['dev', 'config'],
199
+ database: { host: 'localhost', port: 5432 }
200
+ }, format);
201
+
202
+ ```
203
+ See the [formatting reference](https://github.com/DecimalTurn/toml-patch/blob/v3.3.2/docs/Formatting.md) for the complete list of options, auto-detection behavior, `updateOrder` and more examples.