@decimalturn/toml-patch 3.1.2-dev → 3.1.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 (70) hide show
  1. package/LICENSE +22 -0
  2. package/README.md +169 -0
  3. package/dist/toml-patch.js +56 -0
  4. package/package.json +93 -11
  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 -660
  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 -59
  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 -196
  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.js +0 -269
  20. package/dist/dev/diff.js.map +0 -1
  21. package/dist/dev/escape-preference.js +0 -144
  22. package/dist/dev/escape-preference.js.map +0 -1
  23. package/dist/dev/find-by-path.js +0 -111
  24. package/dist/dev/find-by-path.js.map +0 -1
  25. package/dist/dev/formatter.js +0 -298
  26. package/dist/dev/formatter.js.map +0 -1
  27. package/dist/dev/generate.js +0 -451
  28. package/dist/dev/generate.js.map +0 -1
  29. package/dist/dev/inline-comma-space.js +0 -13
  30. package/dist/dev/inline-comma-space.js.map +0 -1
  31. package/dist/dev/inline-format.js +0 -106
  32. package/dist/dev/inline-format.js.map +0 -1
  33. package/dist/dev/inline-layout.js +0 -107
  34. package/dist/dev/inline-layout.js.map +0 -1
  35. package/dist/dev/line-ending-backslash.js +0 -320
  36. package/dist/dev/line-ending-backslash.js.map +0 -1
  37. package/dist/dev/location.js +0 -104
  38. package/dist/dev/location.js.map +0 -1
  39. package/dist/dev/parse-error.js +0 -27
  40. package/dist/dev/parse-error.js.map +0 -1
  41. package/dist/dev/parse-js.js +0 -107
  42. package/dist/dev/parse-js.js.map +0 -1
  43. package/dist/dev/parse-string.js +0 -122
  44. package/dist/dev/parse-string.js.map +0 -1
  45. package/dist/dev/parse-toml.js +0 -714
  46. package/dist/dev/parse-toml.js.map +0 -1
  47. package/dist/dev/patch.js +0 -2540
  48. package/dist/dev/patch.js.map +0 -1
  49. package/dist/dev/to-js.js +0 -303
  50. package/dist/dev/to-js.js.map +0 -1
  51. package/dist/dev/to-toml.js +0 -874
  52. package/dist/dev/to-toml.js.map +0 -1
  53. package/dist/dev/tokenizer.js +0 -225
  54. package/dist/dev/tokenizer.js.map +0 -1
  55. package/dist/dev/toml-document.js +0 -220
  56. package/dist/dev/toml-document.js.map +0 -1
  57. package/dist/dev/toml-format.js +0 -476
  58. package/dist/dev/toml-format.js.map +0 -1
  59. package/dist/dev/toml-patch.js +0 -94
  60. package/dist/dev/toml-patch.js.map +0 -1
  61. package/dist/dev/traverse.js +0 -61
  62. package/dist/dev/traverse.js.map +0 -1
  63. package/dist/dev/truncate.js +0 -64
  64. package/dist/dev/truncate.js.map +0 -1
  65. package/dist/dev/update-order.js +0 -235
  66. package/dist/dev/update-order.js.map +0 -1
  67. package/dist/dev/utils.js +0 -223
  68. package/dist/dev/utils.js.map +0 -1
  69. package/dist/dev/writer.js +0 -758
  70. package/dist/dev/writer.js.map +0 -1
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,169 @@
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 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
+ - [Comment ownership](#comment-ownership)
20
+ - [Date/time handling and Temporal](#datetime-handling--temporal)
21
+ - [Formatting](#formatting)
22
+ - [Changelog](https://github.com/DecimalTurn/toml-patch/blob/v3.1.2/CHANGELOG.md)
23
+ - [Contributing](https://github.com/DecimalTurn/toml-patch/blob/v3.1.2/CONTRIBUTING.md)
24
+ - [MIT License](https://github.com/DecimalTurn/toml-patch/blob/v3.1.2/LICENSE)
25
+
26
+ ## Installation
27
+
28
+ toml-patch is dependency-free and can be installed via your favorite package manager.
29
+
30
+ *Example with NPM*
31
+
32
+ ```
33
+ $ npm install --save @decimalturn/toml-patch
34
+ ```
35
+
36
+ For browser usage, you can use unpkg:
37
+
38
+ ```html
39
+ <script type="module">
40
+ import * as TOML from 'https://unpkg.com/@decimalturn/toml-patch@v3.1.2/dist/toml-patch.js';
41
+ </script>
42
+ ```
43
+
44
+ ## API
45
+
46
+ overwrite(tomlString: string): void
47
+ 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.
48
+
49
+ See the [API reference](https://github.com/DecimalTurn/toml-patch/blob/v3.1.2/docs/API.md) for `patch`, `parse`, `stringify` and the `TomlDocument` class.
50
+
51
+ For a quick start, patch an existing TOML string like this:
52
+
53
+ ```js
54
+ import { patch } from '@decimalturn/toml-patch';
55
+
56
+ const existing = 'title = "TOML example"\nowner.name = "Bob"\n';
57
+ const updated = patch(existing, {
58
+ title: 'TOML example',
59
+ owner: { name: 'Tim' }
60
+ });
61
+ ```
62
+
63
+ ## Comment Ownership
64
+
65
+ When `patch()` removes or reorders an entry, any comment describing it (a same-line trailing
66
+ comment, or a line-comment directly above with no blank line in between) travels along with
67
+ it, instead of being left behind to describe whatever ends up in that spot. This applies to root
68
+ keys, `[table]`/`[[array-of-tables]]` blocks, and elements inside multi-line arrays and inline
69
+ tables.
70
+
71
+ ```js
72
+ import * as TOML from '@decimalturn/toml-patch';
73
+ import { strict as assert } from 'assert';
74
+
75
+ const existing = `
76
+ x = 1 # a note about x
77
+ y = 2
78
+ `;
79
+
80
+ const patched = TOML.patch(existing, { y: 2 });
81
+
82
+ assert.strictEqual(patched, `
83
+ y = 2
84
+ `);
85
+ ```
86
+
87
+ See the [comment ownership guide](https://github.com/DecimalTurn/toml-patch/blob/v3.1.2/docs/Comment-Ownership.md) for the full behavior, including how a blank line opts a comment out of ownership and current scope limitations.
88
+
89
+ Note that `patch()` does not reorder entries by default.
90
+ 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.1.2/docs/Formatting.md#updateorder).
91
+
92
+ ## Date/Time Handling & Temporal
93
+
94
+ 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.
95
+
96
+ ### Enabling Temporal
97
+
98
+ The `temporal: true` option requires `Temporal` to be available in the runtime:
99
+
100
+ | Runtime | How to enable |
101
+ |---|---|
102
+ | **Node.js 26+** | Built-in — enable with `temporal: true` |
103
+ | **Node.js 20–24** | `node --harmony-temporal` flag |
104
+ | **Node.js 14–26** | [@js-temporal/polyfill](https://www.npmjs.com/package/@js-temporal/polyfill) |
105
+
106
+ ```js
107
+ import * as TOML from '@decimalturn/toml-patch';
108
+
109
+ // Node 26+ (native) or Node 20–24 with --harmony-temporal:
110
+ const obj = TOML.parse('d = 2024-01-15\n', { temporal: true });
111
+ // obj.d → Temporal.PlainDate
112
+
113
+ // All Node versions with the polyfill:
114
+ import { Temporal } from '@js-temporal/polyfill';
115
+ globalThis.Temporal = Temporal;
116
+ const obj2 = TOML.parse('d = 2024-01-15\n', { temporal: true });
117
+ // obj2.d → PlainDate (polyfill)
118
+ ```
119
+
120
+ > **Note:** Only offset-based timezones (`+05:30`, `Z`) are supported in TOML. IANA timezone annotations (e.g., `[Asia/Kolkata]`) will throw an error.
121
+
122
+ See the [date/time guide](https://github.com/DecimalTurn/toml-patch/blob/v3.1.2/docs/Dates.md) for details and examples.
123
+
124
+ ## Formatting
125
+
126
+ The `TomlFormat` class controls how TOML documents are formatted during stringification and patching operations.
127
+
128
+ ```typescript
129
+ class TomlFormat {
130
+ newLine: string
131
+ trailingNewline: number
132
+ trailingComma: boolean
133
+ bracketSpacing: boolean
134
+ inlineTableStart?: number
135
+ truncateZeroTimeInDates: boolean
136
+ useTabsForIndentation?: boolean
137
+ indentWidth: number
138
+ minimumDecimals?: number
139
+ leadingBom: boolean
140
+ updateOrder?: boolean
141
+ multilineTable: boolean | number | 'auto' | 'parent'
142
+ multilineArray: boolean | number | 'auto' | 'parent'
143
+
144
+ static default(): TomlFormat
145
+ static autoDetectFormat(tomlString: string): TomlFormat
146
+ }
147
+ ```
148
+
149
+ Start with `TomlFormat.default()` and override the options you need. e.g.:
150
+
151
+ ```js
152
+ import { stringify, TomlFormat } from '@decimalturn/toml-patch';
153
+
154
+ const format = TomlFormat.default();
155
+ format.newLine = '\r\n';
156
+ format.trailingNewline = 0;
157
+ format.trailingComma = true;
158
+ format.bracketSpacing = false;
159
+ format.indentWidth = 4;
160
+ format.multilineTable = 'parent';
161
+
162
+ const toml = stringify({
163
+ title: 'My App',
164
+ tags: ['dev', 'config'],
165
+ database: { host: 'localhost', port: 5432 }
166
+ }, format);
167
+
168
+ ```
169
+ See the [formatting reference](https://github.com/DecimalTurn/toml-patch/blob/v3.1.2/docs/Formatting.md) for the complete list of options, auto-detection behavior, `updateOrder` and more examples.