properties-file 4.0.0 → 5.0.1

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 (57) hide show
  1. package/README.md +94 -97
  2. package/dist/cjs/bundler/bun.js +1 -1
  3. package/dist/cjs/bundler/esbuild.js +1 -1
  4. package/dist/cjs/bundler/rollup.js +1 -1
  5. package/dist/cjs/bundler/webpack.js +1 -1
  6. package/dist/cjs/editor/index.d.ts +100 -93
  7. package/dist/cjs/editor/index.js +1 -1
  8. package/dist/cjs/escape/index.js +1 -1
  9. package/dist/cjs/index.d.ts +0 -1
  10. package/dist/cjs/index.js +1 -1
  11. package/dist/cjs/package.json +1 -1
  12. package/dist/cjs/parse-properties.d.ts +22 -0
  13. package/dist/cjs/parse-properties.js +1 -0
  14. package/dist/cjs/parser/index.d.ts +3 -0
  15. package/dist/cjs/parser/index.js +1 -0
  16. package/dist/cjs/parser/nodes.d.ts +156 -0
  17. package/dist/cjs/parser/nodes.js +1 -0
  18. package/dist/cjs/parser/normalize.d.ts +12 -0
  19. package/dist/cjs/parser/normalize.js +1 -0
  20. package/dist/cjs/parser/parse.d.ts +23 -0
  21. package/dist/cjs/parser/parse.js +1 -0
  22. package/dist/cjs/parser/properties.d.ts +93 -0
  23. package/dist/cjs/parser/properties.js +1 -0
  24. package/dist/cjs/unescape/index.d.ts +4 -0
  25. package/dist/cjs/unescape/index.js +1 -1
  26. package/dist/esm/editor/index.d.ts +100 -93
  27. package/dist/esm/editor/index.js +1 -1
  28. package/dist/esm/escape/index.js +1 -1
  29. package/dist/esm/index.d.ts +0 -1
  30. package/dist/esm/index.js +1 -1
  31. package/dist/esm/parse-properties.d.ts +22 -0
  32. package/dist/esm/parse-properties.js +1 -0
  33. package/dist/esm/parser/index.d.ts +3 -0
  34. package/dist/esm/parser/index.js +1 -0
  35. package/dist/esm/parser/nodes.d.ts +156 -0
  36. package/dist/esm/parser/nodes.js +1 -0
  37. package/dist/esm/parser/normalize.d.ts +12 -0
  38. package/dist/esm/parser/normalize.js +1 -0
  39. package/dist/esm/parser/parse.d.ts +23 -0
  40. package/dist/esm/parser/parse.js +1 -0
  41. package/dist/esm/parser/properties.d.ts +93 -0
  42. package/dist/esm/parser/properties.js +1 -0
  43. package/dist/esm/unescape/index.d.ts +4 -0
  44. package/dist/esm/unescape/index.js +1 -1
  45. package/package.json +33 -21
  46. package/dist/cjs/properties.d.ts +0 -96
  47. package/dist/cjs/properties.js +0 -1
  48. package/dist/cjs/property-line.d.ts +0 -22
  49. package/dist/cjs/property-line.js +0 -1
  50. package/dist/cjs/property.d.ts +0 -81
  51. package/dist/cjs/property.js +0 -1
  52. package/dist/esm/properties.d.ts +0 -96
  53. package/dist/esm/properties.js +0 -1
  54. package/dist/esm/property-line.d.ts +0 -22
  55. package/dist/esm/property-line.js +0 -1
  56. package/dist/esm/property.d.ts +0 -81
  57. package/dist/esm/property.js +0 -1
package/README.md CHANGED
@@ -2,15 +2,15 @@
2
2
 
3
3
  [![License](https://img.shields.io/npm/l/make-coverage-badge.svg?color=brightgreen)](https://opensource.org/licenses/MIT)
4
4
  [![Download Stats](https://img.shields.io/npm/dw/properties-file.svg?color=brightgreen)](https://www.npmjs.com/package/properties-file)
5
- ![Coverage](https://img.shields.io/badge/Coverage-100%25-brightgreen.svg)
6
- [![Package Size](https://deno.bundlejs.com/badge?q=properties-file@latest&treeshake=[*])](https://bundlejs.com/?q=properties-file@latest&treeshake=[*])
5
+ ![Coverage](https://img.shields.io/badge/coverage-100%25-brightgreen.svg)
6
+ ![Package Size](https://img.shields.io/badge/min%2Bgzip-1.1%20kB-brightgreen)
7
7
  ![Dependencies](https://img.shields.io/badge/dependencies-0-brightgreen)
8
8
 
9
9
  `.properties` file parser, editor, formatter and bundler integrations.
10
10
 
11
- ## Installation 💻
11
+ ## Installation
12
12
 
13
- > In April 2023, we released version 3 of this package, which includes breaking changes. Please refer to the [upgrade guide](./V2-TO-V3-UPGRADE-GUIDE.md) before upgrading.
13
+ > Doing a major version update? Check our [migration guides](./docs/migration/README.md).
14
14
 
15
15
  Add the package as a dependency:
16
16
 
@@ -18,21 +18,22 @@ Add the package as a dependency:
18
18
  npm install properties-file
19
19
  ```
20
20
 
21
- ## What's in it for me? 🤔
21
+ ## What's in it for me?
22
22
 
23
23
  - A modern library written entirely in TypeScript that exactly reproduces the [Properties Java implementation](/assets/java-implementation.md).
24
24
  - Works for both Node.js applications and browsers that support at least [ES5](https://www.w3schools.com/js/js_es5.asp).
25
- - Flexible APIs:
26
- - `getProperties` converts the content of `.properties` files to a key-value pair object.
27
- - A `Properties` class provides insights into parsing data.
28
- - A `PropertiesEditor` class enables the addition, edition, and removal of entries.
29
- - `escapeKey` and `escapeValue` allow the conversion of any content to a `.properties` compatible format.
30
- - Bundler integrations for Webpack, Rollup/Vite, esbuild, and Bun to import `.properties` files directly. See [BUNDLER.md](./BUNDLER.md).
31
- - [Tiny](https://bundlejs.com/?q=properties-file%40latest&treeshake=%5B*%5D) with 0 dependencies.
32
- - 100% test coverage based on the output from a Java implementation.
33
- - Active maintenance (many popular `.properties` packages have been inactive for years).
34
-
35
- ## Usage 🎬
25
+ - Flexible, tree-shakable APIs — import only what you need, and your bundler will exclude the rest:
26
+ - `getProperties` converts `.properties` content to a key-value pair object.
27
+ - `Properties` provides lossless parsing with a full data model — every element (properties, comments, blank lines, whitespace, duplicate keys) is preserved and can be round-tripped exactly or normalized via `format()` options.
28
+ - `PropertiesEditor` enables insertion, edition, and removal of entries while preserving formatting.
29
+ - `escapeKey` and `escapeValue` convert any content to `.properties` compatible format.
30
+ - Bundler integrations for Webpack, Rollup/Vite, esbuild, and Bun to import `.properties` files directly. See [BUNDLER.md](./docs/BUNDLER.md).
31
+ - **Tiny with 0 dependencies** — `getProperties` is only 1.1 kB min+gzip.
32
+ - **Runs everywhere** compiled to ES5, works in any browser and on Node.js all the way back to v0.10 (2013). [Verified via Docker](./tests/node-compat/).
33
+ - **100% test coverage** based on the output from a Java implementation.
34
+ - Active maintenance (many popular `.properties` packages have been inactive for years). See our [detailed comparison](./docs/COMPARISON.md) with other packages.
35
+
36
+ ## Usage
36
37
 
37
38
  We have put a lot of effort into incorporating [TSDoc](https://tsdoc.org/) into all our APIs. If you are unsure about how to use certain APIs provided in our examples, please check directly in your IDE.
38
39
 
@@ -53,103 +54,97 @@ Output:
53
54
  { hello: 'hello', world: 'world' }
54
55
  ```
55
56
 
56
- ### `Properties` (using parsing metadata)
57
+ ### `Properties` (lossless parsing with full data model)
57
58
 
58
- The `Properties` object is what makes `getProperties` work under the hood, but when using it directly, you can access granular parsing metadata. Here is an example of how the object can be used to find key collisions:
59
+ The `Properties` class parses a `.properties` file into a lossless data model where every element — properties, comments, blank lines is preserved in order. This is useful when you need to inspect, analyze, or transform `.properties` files while retaining their exact structure.
59
60
 
60
61
  ```ts
61
- import { Properties } from 'properties-file'
62
+ import { readFileSync } from 'node:fs'
63
+ import { PropertiesNodeType, Properties } from 'properties-file/parser'
64
+
65
+ const properties = new Properties(readFileSync('example.properties'))
66
+
67
+ // Access all nodes in file order (properties, comments, blank lines).
68
+ for (const node of properties.nodes) {
69
+ switch (node.type) {
70
+ case PropertiesNodeType.PROPERTY:
71
+ console.log(`${node.key} = ${node.value}`)
72
+ break
73
+ case PropertiesNodeType.COMMENT:
74
+ console.log(`Comment: ${node.delimiter}${node.body}`)
75
+ break
76
+ case PropertiesNodeType.BLANK:
77
+ console.log('(blank line)')
78
+ break
79
+ }
80
+ }
81
+
82
+ // Get a simple key-value object (last-wins for duplicate keys).
83
+ console.log(properties.toObject())
84
+
85
+ // Lossless round-trip: format() reproduces the exact original content.
86
+ console.log(properties.format() === readFileSync('example.properties', 'utf8')) // true
87
+ ```
88
+
89
+ #### Finding key collisions
90
+
91
+ ```ts
92
+ import { Properties } from 'properties-file/parser'
62
93
 
63
94
  const properties = new Properties(
64
95
  'hello = hello1\nworld = world1\nworld = world2\nhello = hello2\nworld = world3'
65
96
  )
66
- console.log(properties.format())
67
-
68
- /**
69
- * Outputs:
70
- *
71
- * hello = hello1
72
- * world = world1
73
- * world = world2
74
- * hello = hello2
75
- * world = world3
76
- */
77
-
78
- properties.collection.forEach((property) => {
79
- console.log(`${property.key} = ${property.value}`)
80
- })
81
-
82
- /**
83
- * Outputs:
84
- *
85
- * hello = hello2
86
- * world = world3
87
- */
88
-
89
- const keyCollisions = properties.getKeyCollisions()
90
97
 
91
- keyCollisions.forEach((keyCollision) => {
92
- console.warn(
93
- `Found a key collision for key '${
94
- keyCollision.key
95
- }' on lines ${keyCollision.startingLineNumbers.join(
96
- ', '
97
- )} (will use the value at line ${keyCollision.getApplicableLineNumber()}).`
98
- )
98
+ const collisions = properties.getKeyCollisions()
99
+ collisions.forEach((collision) => {
100
+ const lines = collision.nodes.map((node) => node.startingLineNumber)
101
+ console.log(`Key '${collision.key}' appears on lines ${lines.join(', ')}`)
99
102
  })
100
103
 
101
104
  /**
102
105
  * Outputs:
103
106
  *
104
- * Found a key collision for key 'hello' on lines 1, 4 (will use the value at line 4).
105
- * Found a key collision for key 'world' on lines 2, 3, 5 (will use the value at line 5).
107
+ * Key 'hello' appears on lines 1, 4
108
+ * Key 'world' appears on lines 2, 3, 5
106
109
  */
107
110
  ```
108
111
 
109
- For purposes where you require more parsing metadata, such as building a syntax highlighter, it is recommended that you access the `Property` objects included in the `Properties.collection`. These objects provide comprehensive information about each key-value pair.
112
+ #### Normalizing output
110
113
 
111
- ### `PropertiesEditor` (editing `.properties` content)
112
-
113
- In certain scenarios, it may be necessary to modify the content of the `.properties` key-value pair objects. This can be achieved easily using the `Properties` object, with the assistance of the `escapeKey` and `escapeValue` APIs, as demonstrated below:
114
+ Passing options to `format()` produces a normalized version of the file with granular control over formatting:
114
115
 
115
116
  ```ts
116
- import { Properties } from 'properties-file'
117
- import { escapeKey, escapeValue } from 'properties-file/escape'
118
-
119
- const properties = new Properties('hello = hello\n# This is a comment\nworld = world')
120
- const newProperties: string[] = []
121
-
122
- properties.collection.forEach((property) => {
123
- const key = property.key === 'world' ? 'new world' : property.key
124
- const value = property.value === 'world' ? 'new world' : property.value
125
- newProperties.push(`${escapeKey(key)} = ${escapeValue(value)}`)
126
- })
127
-
128
- console.log(newProperties.join('\n'))
117
+ import { Properties } from 'properties-file/parser'
118
+
119
+ const properties = new Properties('# comment\n\n key : value\n key : updated')
120
+
121
+ console.log(
122
+ properties.format({
123
+ removeComments: true, // Strip all comments
124
+ removeBlankLines: true, // Strip all blank lines
125
+ removeLeadingWhitespace: true, // Strip indentation
126
+ deduplicateKeys: true, // Keep only last occurrence
127
+ separatorChar: '=', // Standardize separator
128
+ separatorLeading: ' ', // Space before =
129
+ separatorTrailing: ' ', // Space after =
130
+ })
131
+ )
129
132
 
130
133
  /**
131
134
  * Outputs:
132
135
  *
133
- * hello = hello
134
- * new\ world = new world
136
+ * key = updated
135
137
  */
136
138
  ```
137
139
 
138
- The limitation of this approach is that its output contains only valid keys, without any comments or whitespace. However, if you require a more advanced editor that preserves these original elements, then the `PropertiesEditor` object is exactly what you need.
140
+ ### `PropertiesEditor` (editing `.properties` content)
141
+
142
+ The `PropertiesEditor` extends `Properties` with methods to insert, update, delete, and upsert entries while preserving formatting.
139
143
 
140
144
  ```ts
141
145
  import { PropertiesEditor } from 'properties-file/editor'
142
146
 
143
147
  const properties = new PropertiesEditor('hello = hello\n# This is a comment\nworld = world')
144
- console.log(properties.format())
145
-
146
- /**
147
- * Outputs:
148
- *
149
- * hello = hello
150
- * # This is a comment
151
- * world = world
152
- */
153
148
 
154
149
  properties.insertComment('This is a multiline\ncomment before `newKey3`')
155
150
  properties.insert('newKey3', 'This is my third key')
@@ -161,7 +156,7 @@ properties.insert('newKey1', 'This is my first new key', {
161
156
  commentDelimiter: '!',
162
157
  })
163
158
 
164
- properties.insert('newKey2', 'こんにちは', {
159
+ properties.insert('newKey2', 'hello', {
165
160
  referenceKey: 'newKey1',
166
161
  position: 'after',
167
162
  escapeUnicode: true,
@@ -180,20 +175,20 @@ console.log(properties.format())
180
175
  * world = new world
181
176
  * ! Below are the new keys being edited
182
177
  * newKey1 = This is my first new key
183
- * newKey2 = \u3053\u3093\u306b\u3061\u306f
178
+ * newKey2 = hello
184
179
  * # This is a multiline
185
180
  * # comment before `newKey3`
186
181
  * newKey3 = This is my third key
187
182
  */
188
183
  ```
189
184
 
190
- For convenience, we also added an `upsert` method that allows updating a key if it exists or adding it at the end, when it doesn't. Make sure to check in your IDE for all available methods and options in our TSDoc.
185
+ The editor also provides `upsert` (update or insert) and `deleteAll` (remove all occurrences of a duplicate key). Check your IDE for all available methods and options via TSDoc.
191
186
 
192
187
  ### Bundler Integrations
193
188
 
194
189
  If you would like to import `.properties` directly using `import`, this package provides integrations for all major bundlers: **Webpack/Rspack**, **Rollup/Vite/Rolldown**, **esbuild**, and **Bun**.
195
190
 
196
- See [BUNDLER.md](./BUNDLER.md) for setup instructions and examples.
191
+ See [BUNDLER.md](./docs/BUNDLER.md) for setup instructions and examples.
197
192
 
198
193
  By adding these configurations you should now be able to import directly `.properties` files just like this:
199
194
 
@@ -211,11 +206,11 @@ Output:
211
206
 
212
207
  ## Why another `.properties` file package?
213
208
 
214
- There are probably over 20 similar packages available, but:
209
+ There are over 20 similar packages available, but most are abandoned, incomplete, or not compliant with the Java specification. See our [detailed comparison](./docs/COMPARISON.md) for benchmarks, compliance tests, and a feature matrix against the top 5 packages. The short version:
215
210
 
216
- - Many of the most popular packages have had no activity for over 5 years.
217
- - Most packages will not replicate the current Java implementation.
218
- - No package offers the same capabilities as this one.
211
+ - **100% Java spec compliance** the only package (alongside `properties-parser`) to pass all test cases.
212
+ - **3–7x faster** than alternatives on a 10,000-entry file.
213
+ - **Lossless data model** — no other package preserves comments, blank lines, whitespace, and duplicate keys for round-trip editing.
219
214
 
220
215
  Unfortunately, the `.properties` file specification is not well-documented. One reason for this is that it was originally used in Java to store configurations. Today, most applications handle this using JSON, YAML, or other modern formats because these formats are more flexible.
221
216
 
@@ -233,15 +228,17 @@ Having good JavaScript/TypeScript support for `.properties` files offers more in
233
228
 
234
229
  ### How does this package work?
235
230
 
236
- Basically, our goal was to offer parity with the Java implementation, which is the closest thing to a specification for `.properties` files. Here is the logic behind this package in a nutshell:
231
+ Our goal is to offer parity with the Java implementation, which is the closest thing to a specification for `.properties` files. The package provides two parsing paths:
232
+
233
+ 1. **`getProperties`** — a fast, functional parser optimized for the common case of converting `.properties` content to a key-value object. Uses `charCodeAt`-based scanning with zero-copy optimizations.
234
+
235
+ 2. **`Properties`** — a lossless parser that produces an ordered array of typed nodes (`PropertyNode`, `CommentNode`, `BlankLineNode`). Every element in the file is preserved, enabling exact round-trip reconstruction via `format()` and flexible normalization by passing options to `format()`.
236
+
237
+ Both parsers are fully compliant with the Java `Properties` specification and produce identical key-value output. Just like Java, if a Unicode-escaped character (`\u`) is malformed, an error will be thrown.
237
238
 
238
- 1. The content is split by lines, creating an array of strings where each line is an element.
239
- 2. All lines are parsed to create a collection of `Property` objects that:
240
- 1. Identify key-value pair lines from the other lines (e.g., comments, blank lines, etc.).
241
- 2. Merge back multiline key-value pairs on single lines by removing trailing backslashes.
242
- 3. Unescape the keys and values.
239
+ ## Contributing
243
240
 
244
- Just like Java, if a Unicode-escaped character (`\u`) is malformed, an error will be thrown. However, we do not recommend using Unicode-escaped characters, but rather using UTF-8 encoding that supports more characters.
241
+ See [CONTRIBUTING.md](./docs/CONTRIBUTING.md) for project principles, architecture, code style, and development commands.
245
242
 
246
243
  ## Additional references
247
244
 
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var node_fs_1=require("node:fs"),__1=require(".."),bunPlugin={name:"properties-file",setup:function(e){e.onLoad({filter:/\.properties$/},function(e){var r=e.path;return{exports:{properties:(0,__1.getProperties)((0,node_fs_1.readFileSync)(r,"utf8"))},loader:"object"}})}};exports.default=bunPlugin;
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0}),Object.defineProperty(exports,"default",{enumerable:!0,get:function(){return _default}});var _nodefs=require("node:fs"),_index=require("../index.js"),bunPlugin={name:"properties-file",setup:function(e){e.onLoad({filter:/\.properties$/},function(e){var r=e.path;return{exports:{properties:(0,_index.getProperties)((0,_nodefs.readFileSync)(r,"utf8"))},loader:"object"}})}},_default=bunPlugin;
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var node_fs_1=require("node:fs"),__1=require(".."),esbuildPlugin=function(){return{name:"properties-file",setup:function(e){e.onLoad({filter:/\.properties$/},function(e){var r=e.path;return{contents:"export const properties = ".concat(JSON.stringify((0,__1.getProperties)((0,node_fs_1.readFileSync)(r,"utf8"))),";"),loader:"js"}})}}};exports.default=esbuildPlugin;
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0}),Object.defineProperty(exports,"default",{enumerable:!0,get:function(){return _default}});var _nodefs=require("node:fs"),_index=require("../index.js"),esbuildPlugin=function(){return{name:"properties-file",setup:function(e){e.onLoad({filter:/\.properties$/},function(e){var t=e.path;return{contents:"export const properties = ".concat(JSON.stringify((0,_index.getProperties)((0,_nodefs.readFileSync)(t,"utf8"))),";"),loader:"js"}})}}},_default=esbuildPlugin;
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var __1=require(".."),PROPERTIES_EXTENSION=".properties",rollupPlugin=function(){return{name:"properties-file",transform:function(e,r){return-1===r.indexOf(PROPERTIES_EXTENSION,r.length-PROPERTIES_EXTENSION.length)?null:{code:"export const properties = ".concat(JSON.stringify((0,__1.getProperties)(e)),";"),map:null}}}};exports.default=rollupPlugin;
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0}),Object.defineProperty(exports,"default",{enumerable:!0,get:function(){return _default}});var _index=require("../index.js"),PROPERTIES_EXTENSION=".properties",rollupPlugin=function(){return{name:"properties-file",transform:function(e,r){return-1===r.indexOf(PROPERTIES_EXTENSION,r.length-PROPERTIES_EXTENSION.length)?null:{code:"export const properties = ".concat(JSON.stringify((0,_index.getProperties)(e)),";"),map:null}}}},_default=rollupPlugin;
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var __1=require(".."),webpackLoader=function(e){return"exports.properties = ".concat(JSON.stringify((0,__1.getProperties)(e)),";")};exports.default=webpackLoader;
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0}),Object.defineProperty(exports,"default",{enumerable:!0,get:function(){return _default}});var _index=require("../index.js"),webpackLoader=function(e){return"exports.properties = ".concat(JSON.stringify((0,_index.getProperties)(e)),";")},_default=webpackLoader;
@@ -1,154 +1,161 @@
1
- import { KeyValuePairObject } from '..';
2
- import { Properties } from '../properties';
3
- /** The default separator between keys and values. */
4
- export declare const DEFAULT_SEPARATOR = "=";
5
- /** The default character used as comment delimiter. */
6
- export declare const DEFAULT_COMMENT_DELIMITER = "#";
1
+ import { Properties } from '../parser/properties.js';
2
+ import type { PropertyNode } from '../parser/nodes.js';
7
3
  /** Characters that can be used as key-value pair separators. */
8
- export type KeyValuePairSeparator = ' ' | ':' | '=';
4
+ export type KeyValuePairSeparator = '=' | ':' | ' ';
9
5
  /** Characters that can be used as comment delimiters. */
10
6
  export type CommentDelimiter = '#' | '!';
11
- /** Options on the `Properties.insert` method. */
7
+ /** Options for {@link PropertiesEditor.insert}. */
12
8
  export type InsertOptions = {
13
- /** The name of the key to insert before or after. If the key not found, the new property will not be inserted. */
9
+ /**
10
+ * Insert relative to this key (last occurrence). If the key is not found,
11
+ * the property is appended at the end.
12
+ */
14
13
  referenceKey?: string;
15
- /** The position of the insertion related to the `referenceKey` (default is `after`) */
14
+ /** Position relative to the reference key. Default: `'after'`. */
16
15
  position?: 'before' | 'after';
17
- /** Escape unicode characters into ISO-8859-1 compatible encoding? */
16
+ /** If `true`, escape non-ASCII characters as `\\uXXXX` sequences. Default: `false`. */
18
17
  escapeUnicode?: boolean;
19
- /** The key/value separator character. */
18
+ /** Separator character to use between key and value. Default: `'='`. */
20
19
  separator?: KeyValuePairSeparator;
21
- /** A comment to insert before. */
20
+ /**
21
+ * Comment text to prepend before the property. Supports multi-line: newlines
22
+ * in the string create separate comment nodes. Empty lines within the text
23
+ * become blank line nodes.
24
+ */
22
25
  comment?: string;
23
- /** The comment's delimiter. */
26
+ /** Delimiter character for the comment. Default: `'#'`. */
24
27
  commentDelimiter?: CommentDelimiter;
25
28
  };
26
- /** Options on the `Properties.insertComment` method. */
29
+ /** Options for {@link PropertiesEditor.insertComment}. */
27
30
  export type InsertCommentOptions = {
28
- /** The name of the key to insert before or after. If the key not found, the new property will not be inserted. */
31
+ /**
32
+ * Insert relative to this key (last occurrence). If the key is not found,
33
+ * the comment is appended at the end.
34
+ */
29
35
  referenceKey?: string;
30
- /** The position of the insertion related to the `referenceKey` (default is `after`) */
36
+ /** Position relative to the reference key. Default: `'after'`. */
31
37
  position?: 'before' | 'after';
32
- /** The comment's delimiter. */
38
+ /** Delimiter character for the comment. Default: `'#'`. */
33
39
  commentDelimiter?: CommentDelimiter;
34
40
  };
35
- /** Options on the `Properties.update` method. */
41
+ /** Options for {@link PropertiesEditor.insertBlankLine}. */
42
+ export type InsertBlankLineOptions = {
43
+ /**
44
+ * Insert relative to this key (last occurrence). If the key is not found,
45
+ * the blank line is appended at the end.
46
+ */
47
+ referenceKey?: string;
48
+ /** Position relative to the reference key. Default: `'after'`. */
49
+ position?: 'before' | 'after';
50
+ };
51
+ /** Options for {@link PropertiesEditor.update}. */
36
52
  export type UpdateOptions = {
37
- /** Optionally replace the existing value with a new value. */
53
+ /** Replacement value. When not set, the original value is preserved. */
38
54
  newValue?: string;
39
- /** Optionally replace the existing key with a new key name. */
55
+ /** Replacement key (rename). When not set, the original key is preserved. */
40
56
  newKey?: string;
41
- /** Escape unicode characters into ISO-8859-1 compatible encoding? */
57
+ /** If `true`, escape non-ASCII characters as `\\uXXXX` sequences. Default: `false`. */
42
58
  escapeUnicode?: boolean;
43
- /** A key/value separator character. */
44
- separator?: ' ' | ':' | '=';
45
- /** Optionally insert a new comment, or replace the existing one (including white-space characters). */
59
+ /** New separator character. When not set, the original separator is preserved. */
60
+ separator?: KeyValuePairSeparator;
61
+ /**
62
+ * Replacement comment text. When set, all comment and blank line nodes immediately
63
+ * preceding the property (up to the previous property) are removed and replaced
64
+ * with the new comment. Supports multi-line via newlines in the string.
65
+ */
46
66
  newComment?: string;
47
- /** The comment's delimiter. */
67
+ /** Delimiter character for the new comment. Default: `'#'`. */
48
68
  commentDelimiter?: CommentDelimiter;
49
69
  };
50
- /** Options on the `Properties.upsert` method. */
70
+ /** Options for {@link PropertiesEditor.upsert}. */
51
71
  export type UpsertOptions = {
52
- /** Escape unicode characters into ISO-8859-1 compatible encoding? */
72
+ /** If `true`, escape non-ASCII characters as `\\uXXXX` sequences. Default: `false`. */
53
73
  escapeUnicode?: boolean;
54
- /** The key/value separator character. */
74
+ /** Separator character. Default: `'='`. */
55
75
  separator?: KeyValuePairSeparator;
56
- /** A comment to insert before. */
76
+ /**
77
+ * Comment text. When inserting a new property, this is prepended as a comment.
78
+ * When updating an existing property, this replaces the leading comment nodes.
79
+ */
57
80
  comment?: string;
58
- /** The comment's delimiter. */
81
+ /** Delimiter character for the comment. Default: `'#'`. */
59
82
  commentDelimiter?: CommentDelimiter;
60
83
  };
84
+ /** Options for {@link PropertiesEditor.delete}. */
85
+ export type DeleteOptions = {
86
+ /**
87
+ * If `false`, only the property node itself is removed. If `true` (default),
88
+ * all comment and blank line nodes immediately preceding the property (up to
89
+ * the previous property) are also removed.
90
+ */
91
+ deleteLeadingNodes?: boolean;
92
+ };
61
93
  /**
62
- * A .properties file editor.
94
+ * An editor for `.properties` files that extends the lossless {@link Properties}
95
+ * parser with insert, update, delete, and upsert operations.
63
96
  */
64
97
  export declare class PropertiesEditor extends Properties {
65
- /** Is line parsing required to re-async the object's properties? */
66
- private needsLineParsing;
67
- /**
68
- * Create `PropertiesEditor` object.
69
- *
70
- * @param content - The content of a `.properties` file.
71
- */
72
- constructor(content: string);
73
98
  /**
74
- * Find the last occurrence of a property by key (iterates backward for performance).
99
+ * Find the index of the last property node with the given key.
75
100
  *
76
- * @param key - The property key to search for.
101
+ * @param key - The unescaped key to search for.
77
102
  *
78
- * @returns The last matching property, or undefined if not found.
79
- */
80
- private findLastPropertyByKey;
81
- /**
82
- * Parse the `.properties` content line by line only when needed.
103
+ * @returns The index in `this.nodes`, or `-1` if not found.
83
104
  */
84
- private parseLinesIfNeeded;
105
+ private findLastPropertyIndex;
85
106
  /**
86
- * Insert a new property in the existing object (by default it will be at the end).
107
+ * Insert a new property.
87
108
  *
88
- * @param key - A property key (unescaped).
89
- * @param value - A property value (unescaped).
90
- * @param options - Additional options.
91
- *
92
- * @returns True if the key was inserted, otherwise false.
109
+ * @param key - The unescaped key.
110
+ * @param value - The unescaped value.
111
+ * @param options - Insert options.
93
112
  */
94
- insert(key: string, value: string, options?: InsertOptions): boolean;
113
+ insert(key: string, value: string, options?: InsertOptions): void;
95
114
  /**
96
- * Insert a new comment in the existing object (by default it will be at the end).
97
- *
98
- * @param comment - The comment to add.
99
- * @param options - Additional options.
115
+ * Insert a comment.
100
116
  *
101
- * @returns True if the comment was inserted, otherwise false.
117
+ * @param comment - The comment text (may contain newlines).
118
+ * @param options - Insert comment options.
102
119
  */
103
- insertComment(comment: string, options?: InsertCommentOptions): boolean;
120
+ insertComment(comment: string, options?: InsertCommentOptions): void;
104
121
  /**
105
- * Delete the last occurrence of a given key from the existing object.
122
+ * Insert a blank line.
106
123
  *
107
- * @param key - The name of the key to delete.
108
- * @param deleteCommentsAndWhiteSpace - By default, comments and white-space characters before the key will be deleted.
109
- *
110
- * @returns True if the key was deleted, otherwise false.
124
+ * @param options - Insert blank line options.
111
125
  */
112
- delete(key: string, deleteCommentsAndWhiteSpace?: boolean): boolean;
126
+ insertBlankLine(options?: InsertBlankLineOptions): void;
113
127
  /**
114
- * Restore the original newline characters of a key.
128
+ * Update an existing property.
115
129
  *
116
- * @param property - A property object.
130
+ * @param key - The unescaped key to update (uses last occurrence).
131
+ * @param options - Update options.
117
132
  *
118
- * @returns The key with its original newlines characters restored.
133
+ * @returns `true` if the property was found and updated, `false` otherwise.
119
134
  */
120
- private getKeyWithNewlines;
135
+ update(key: string, options: UpdateOptions): boolean;
121
136
  /**
122
- * Restore the original newline characters of a value.
123
- *
124
- * @param property - A property object.
137
+ * Update a property if it exists, or insert it if it doesn't.
125
138
  *
126
- * @returns The value with its original newlines characters restored.
139
+ * @param key - The unescaped key.
140
+ * @param value - The unescaped value.
141
+ * @param options - Upsert options.
127
142
  */
128
- private getValueWithNewlines;
143
+ upsert(key: string, value: string, options?: UpsertOptions): void;
129
144
  /**
130
- * Update the last occurrence of a given key from the existing object.
145
+ * Delete the last occurrence of a property (the effective value in last-wins semantics).
131
146
  *
132
- * @param key - The name of the key to update.
133
- * @param options - Additional options.
147
+ * @param key - The unescaped key to delete.
148
+ * @param options - Delete options.
134
149
  *
135
- * @returns True if the key was updated, otherwise false.
150
+ * @returns The deleted {@link PropertyNode}, or `undefined` if the key was not found.
136
151
  */
137
- update(key: string, options?: UpdateOptions): boolean;
152
+ delete(key: string, options?: DeleteOptions): PropertyNode | undefined;
138
153
  /**
139
- * Update a key if it exist, otherwise add it at the end.
154
+ * Delete all occurrences of a key.
140
155
  *
141
- * @param key - A property key (unescaped).
142
- * @param value - A property value (unescaped).
143
- * @param options - Additional options.
144
- *
145
- * @returns True if the key was updated or inserted, otherwise false.
146
- */
147
- upsert(key: string, value: string, options?: UpsertOptions): boolean;
148
- /**
149
- * Get the key/value object representing the properties.
156
+ * @param key - The unescaped key to delete.
150
157
  *
151
- * @returns A key/value object representing the properties.
158
+ * @returns An array of the deleted {@link PropertyNode} instances.
152
159
  */
153
- toObject(): KeyValuePairObject;
160
+ deleteAll(key: string): PropertyNode[];
154
161
  }