@decimalturn/toml-patch 0.3.7 → 0.4.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.
@@ -1,10 +1,248 @@
1
- //! @decimalturn/toml-patch v0.3.7 - https://github.com/DecimalTurn/toml-patch - @license: MIT
2
- interface Format {
3
- printWidth?: number;
4
- tabWidth?: number;
5
- useTabs?: boolean;
6
- trailingComma?: boolean;
7
- bracketSpacing?: boolean;
1
+ //! @decimalturn/toml-patch v0.4.0 - https://github.com/DecimalTurn/toml-patch - @license: MIT
2
+ interface Location {
3
+ start: Position;
4
+ end: Position;
5
+ }
6
+ interface Position {
7
+ line: number;
8
+ column: number;
9
+ }
10
+
11
+ declare enum NodeType {
12
+ Document = "Document",
13
+ Table = "Table",
14
+ TableKey = "TableKey",
15
+ /**
16
+ * Array of Tables node
17
+ * More info: https://toml.io/en/latest#array-of-tables
18
+ */
19
+ TableArray = "TableArray",
20
+ TableArrayKey = "TableArrayKey",
21
+ KeyValue = "KeyValue",
22
+ Key = "Key",
23
+ String = "String",
24
+ Integer = "Integer",
25
+ Float = "Float",
26
+ Boolean = "Boolean",
27
+ DateTime = "DateTime",
28
+ InlineArray = "InlineArray",
29
+ InlineItem = "InlineItem",
30
+ InlineTable = "InlineTable",
31
+ /**
32
+ * Comment node
33
+ * More info: https://toml.io/en/latest#comment
34
+ */
35
+ Comment = "Comment"
36
+ }
37
+ interface Table extends TreeNode {
38
+ type: NodeType.Table;
39
+ key: TableKey;
40
+ items: RowItem[];
41
+ }
42
+ interface TableKey extends TreeNode {
43
+ type: NodeType.TableKey;
44
+ item: Key;
45
+ }
46
+ interface TableArray extends TreeNode {
47
+ type: NodeType.TableArray;
48
+ key: TableArrayKey;
49
+ items: RowItem[];
50
+ }
51
+ interface TableArrayKey extends TreeNode {
52
+ type: NodeType.TableArrayKey;
53
+ item: Key;
54
+ }
55
+ interface KeyValue extends TreeNode {
56
+ type: NodeType.KeyValue;
57
+ key: Key;
58
+ value: Value;
59
+ equals: number;
60
+ }
61
+ interface Key extends TreeNode {
62
+ type: NodeType.Key;
63
+ raw: string;
64
+ value: string[];
65
+ }
66
+ interface String extends TreeNode {
67
+ type: NodeType.String;
68
+ raw: string;
69
+ value: string;
70
+ }
71
+ interface Integer extends TreeNode {
72
+ type: NodeType.Integer;
73
+ raw: string;
74
+ value: number;
75
+ }
76
+ interface Float extends TreeNode {
77
+ type: NodeType.Float;
78
+ raw: string;
79
+ value: number;
80
+ }
81
+ interface Boolean extends TreeNode {
82
+ type: NodeType.Boolean;
83
+ value: boolean;
84
+ }
85
+ interface DateTime extends TreeNode {
86
+ type: NodeType.DateTime;
87
+ raw: string;
88
+ value: Date;
89
+ }
90
+ interface InlineArray<TItem = TreeNode> extends TreeNode {
91
+ type: NodeType.InlineArray;
92
+ items: InlineArrayItem<TItem>[];
93
+ }
94
+ interface InlineItem<TItem = TreeNode> extends TreeNode {
95
+ type: NodeType.InlineItem;
96
+ item: TItem;
97
+ comma: boolean;
98
+ }
99
+ interface InlineArrayItem<TItem = TreeNode> extends InlineItem<TItem> {
100
+ }
101
+ interface InlineTable extends TreeNode {
102
+ type: NodeType.InlineTable;
103
+ items: InlineTableItem[];
104
+ }
105
+ interface InlineTableItem extends InlineItem<KeyValue> {
106
+ }
107
+ interface Comment extends TreeNode {
108
+ type: NodeType.Comment;
109
+ raw: string;
110
+ }
111
+ /**
112
+ * RowItem represents items that can appear inside Table and TableArray sections.
113
+ * These are the items that form the "rows" of content within table structures.
114
+ *
115
+ * Unlike Block items (which include Table and TableArray), RowItems can only be
116
+ * KeyValue pairs and Comments - you cannot have nested tables within a table section.
117
+ */
118
+ type RowItem = KeyValue | Comment;
119
+ /**
120
+ * Block represents items that can appear at the root level (Document level) in TOML.
121
+ *
122
+ * Context and Usage:
123
+ * - Block items are the fundamental top-level constructs in a TOML document
124
+ * - They appear directly in Document containers and regular Table sections
125
+ * - This is in contrast to InlineItems, which appear within inline containers
126
+ *
127
+ * Important Distinction:
128
+ * - Table and TableArray can ONLY exist as Block items (they cannot appear inside inline containers)
129
+ * - KeyValue and Comment can exist as BOTH Block items AND as InlineItems:
130
+ * * As Block: When they appear at root level or inside regular Table sections
131
+ * * As InlineItem: When they appear inside InlineTable or InlineArray containers
132
+ *
133
+ * Examples:
134
+ * ```toml
135
+ * # These are Block items at root level:
136
+ * name = "value" # KeyValue as Block
137
+ * # This is a comment # Comment as Block
138
+ * [table] # Table as Block
139
+ * [[array]] # TableArray as Block
140
+ *
141
+ * # These are Block items inside a Table:
142
+ * [config]
143
+ * setting = "value" # KeyValue as Block (inside Table)
144
+ * # comment here # Comment as Block (inside Table)
145
+ *
146
+ * # These are InlineItems (NOT Block items):
147
+ * array = [ "a", "b" ] # "a", "b" are InlineItems
148
+ * table = { key = "value" } # key="value" is InlineItem
149
+ * ```
150
+ *
151
+ * Type Safety:
152
+ * This distinction is crucial for the AST structure because:
153
+ * - Document.items: Block[]
154
+ * - Table.items: RowItem[] (KeyValue | Comment)
155
+ * - TableArray.items: RowItem[] (KeyValue | Comment)
156
+ * - InlineArray.items: InlineArrayItem[] (which extends InlineItem)
157
+ * - InlineTable.items: InlineTableItem[] (which extends InlineItem)
158
+ */
159
+ type Block = KeyValue | Table | TableArray | Comment;
160
+ type Value<TInlineArrayItem = TreeNode> = String | Integer | Float | Boolean | DateTime | InlineArray<TInlineArrayItem> | InlineTable;
161
+ interface TreeNode {
162
+ type: NodeType;
163
+ loc: Location;
164
+ }
165
+
166
+ declare class TomlFormat {
167
+ /**
168
+ * The line ending character(s) to use in the output TOML.
169
+ * This option affects only the stringification process, not the internal representation (AST).
170
+ *
171
+ * @example
172
+ * - '\n' for Unix/Linux line endings
173
+ * - '\r\n' for Windows line endings
174
+ */
175
+ newLine: string;
176
+ /**
177
+ * The number of trailing newlines to add at the end of the TOML document.
178
+ * This option affects only the stringification process, not the internal representation (AST).
179
+ *
180
+ * @example
181
+ * - 0: No trailing newline
182
+ * - 1: One trailing newline (standard)
183
+ * - 2: Two trailing newlines (adds extra spacing)
184
+ */
185
+ trailingNewline: number;
186
+ /**
187
+ * Whether to add trailing commas after the last element in arrays and inline tables.
188
+ *
189
+ * @example
190
+ * - true: [1, 2, 3,] and { x = 1, y = 2, }
191
+ * - false: [1, 2, 3] and { x = 1, y = 2 }
192
+ */
193
+ trailingComma: boolean;
194
+ /**
195
+ * Whether to add spaces after opening brackets/braces and before closing brackets/braces
196
+ * in arrays and inline tables.
197
+ *
198
+ * @example
199
+ * - true: [ 1, 2, 3 ] and { x = 1, y = 2 }
200
+ * - false: [1, 2, 3] and {x = 1, y = 2}
201
+ */
202
+ bracketSpacing: boolean;
203
+ /**
204
+ * The nesting depth at which new tables should start being formatted as inline tables.
205
+ * When adding new tables during patching or stringifying objects:
206
+ * - Tables at depth >= inlineTableStart will be formatted as inline tables
207
+ * - Tables at depth < inlineTableStart will be formatted as separate table sections
208
+ *
209
+ * @example
210
+ * - 0: All tables are inline tables including top-level tables (root level)
211
+ * - 1: Top-level tables as sections, nested tables as inline (default)
212
+ * - 2: Two levels as sections, deeper nesting as inline
213
+ */
214
+ inlineTableStart?: number;
215
+ constructor(newLine?: string, trailingNewline?: number, trailingComma?: boolean, bracketSpacing?: boolean, inlineTableStart?: number);
216
+ /**
217
+ * Creates a new TomlFormat instance with default formatting preferences.
218
+ *
219
+ * @returns A new TomlFormat instance with default values:
220
+ * - newLine: '\n'
221
+ * - trailingNewline: 1
222
+ * - trailingComma: false
223
+ * - bracketSpacing: true
224
+ * - inlineTableStart: 1
225
+ */
226
+ static default(): TomlFormat;
227
+ /**
228
+ * Auto-detects formatting preferences from an existing TOML string.
229
+ *
230
+ * This method analyzes the provided TOML string to determine formatting
231
+ * preferences such as line endings, trailing newlines, and comma usage.
232
+ *
233
+ * @param tomlString - The TOML string to analyze for formatting patterns
234
+ * @returns A new TomlFormat instance with detected formatting preferences
235
+ *
236
+ * @example
237
+ * ```typescript
238
+ * const toml = 'array = ["a", "b", "c",]\ntable = { x = 1, y = 2, }';
239
+ * const format = TomlFormat.autoDetectFormat(toml);
240
+ * // format.trailingComma will be true
241
+ * // format.newLine will be '\n'
242
+ * // format.trailingNewline will be 0 (no trailing newline)
243
+ * ```
244
+ */
245
+ static autoDetectFormat(tomlString: string): TomlFormat;
8
246
  }
9
247
 
10
248
  /**
@@ -20,11 +258,54 @@ interface Format {
20
258
  * @param format - Optional formatting options to apply to new or modified sections
21
259
  * @returns A new TOML string with the changes applied
22
260
  */
23
- declare function patch(existing: string, updated: any, format?: Format): string;
261
+ declare function patch(existing: string, updated: any, format?: Partial<TomlFormat> | TomlFormat): string;
262
+
263
+ /**
264
+ * TomlDocument encapsulates a TOML AST and provides methods to interact with it.
265
+ */
266
+ declare class TomlDocument {
267
+ #private;
268
+ /**
269
+ * Initializes the TomlDocument with a TOML string, parsing it into an AST.
270
+ * @param tomlString - The TOML string to parse
271
+ */
272
+ constructor(tomlString: string);
273
+ get toTomlString(): string;
274
+ /**
275
+ * Returns the JavaScript object representation of the TOML document.
276
+ */
277
+ get toJsObject(): any;
278
+ /**
279
+ * Returns the internal AST (for testing purposes).
280
+ * @internal
281
+ */
282
+ get ast(): Block[];
283
+ /**
284
+ * Applies a patch to the current AST using a modified JS object.
285
+ * Updates the internal AST. Use toTomlString getter to retrieve the updated TOML string.
286
+ * @param updatedObject - The modified JS object to patch with
287
+ * @param format - Optional formatting options
288
+ */
289
+ patch(updatedObject: any, format?: Partial<TomlFormat> | TomlFormat): void;
290
+ /**
291
+ * Updates the internal document by supplying a modified tomlString.
292
+ * Use toJsObject getter to retrieve the updated JS object representation.
293
+ * @param tomlString - The modified TOML string to update with
294
+ */
295
+ update(tomlString: string): void;
296
+ /**
297
+ * Overwrites the internal AST by fully re-parsing the supplied tomlString.
298
+ * This is simpler but slower than update() which uses incremental parsing.
299
+ * @param tomlString - The TOML string to overwrite with
300
+ */
301
+ overwrite(tomlString: string): void;
302
+ }
24
303
 
25
304
  /**
26
305
  * Parses a TOML string into a JavaScript object.
27
306
  * The function converts TOML syntax to its JavaScript equivalent.
307
+ * This proceeds in two steps: first, it parses the TOML string into an AST,
308
+ * and then it converts the AST into a JavaScript object.
28
309
  *
29
310
  * @param value - The TOML string to parse
30
311
  * @returns The parsed JavaScript object
@@ -37,6 +318,6 @@ declare function parse(value: string): any;
37
318
  * @param format - Optional formatting options for the resulting TOML
38
319
  * @returns The stringified TOML representation
39
320
  */
40
- declare function stringify(value: any, format?: Format): string;
321
+ declare function stringify(value: any, format?: Partial<TomlFormat> | TomlFormat): string;
41
322
 
42
- export { parse, patch, stringify };
323
+ export { TomlDocument, TomlFormat, parse, patch, stringify };