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.
- package/README.md +94 -97
- package/dist/cjs/bundler/bun.js +1 -1
- package/dist/cjs/bundler/esbuild.js +1 -1
- package/dist/cjs/bundler/rollup.js +1 -1
- package/dist/cjs/bundler/webpack.js +1 -1
- package/dist/cjs/editor/index.d.ts +100 -93
- package/dist/cjs/editor/index.js +1 -1
- package/dist/cjs/escape/index.js +1 -1
- package/dist/cjs/index.d.ts +0 -1
- package/dist/cjs/index.js +1 -1
- package/dist/cjs/package.json +1 -1
- package/dist/cjs/parse-properties.d.ts +22 -0
- package/dist/cjs/parse-properties.js +1 -0
- package/dist/cjs/parser/index.d.ts +3 -0
- package/dist/cjs/parser/index.js +1 -0
- package/dist/cjs/parser/nodes.d.ts +156 -0
- package/dist/cjs/parser/nodes.js +1 -0
- package/dist/cjs/parser/normalize.d.ts +12 -0
- package/dist/cjs/parser/normalize.js +1 -0
- package/dist/cjs/parser/parse.d.ts +23 -0
- package/dist/cjs/parser/parse.js +1 -0
- package/dist/cjs/parser/properties.d.ts +93 -0
- package/dist/cjs/parser/properties.js +1 -0
- package/dist/cjs/unescape/index.d.ts +4 -0
- package/dist/cjs/unescape/index.js +1 -1
- package/dist/esm/editor/index.d.ts +100 -93
- package/dist/esm/editor/index.js +1 -1
- package/dist/esm/escape/index.js +1 -1
- package/dist/esm/index.d.ts +0 -1
- package/dist/esm/index.js +1 -1
- package/dist/esm/parse-properties.d.ts +22 -0
- package/dist/esm/parse-properties.js +1 -0
- package/dist/esm/parser/index.d.ts +3 -0
- package/dist/esm/parser/index.js +1 -0
- package/dist/esm/parser/nodes.d.ts +156 -0
- package/dist/esm/parser/nodes.js +1 -0
- package/dist/esm/parser/normalize.d.ts +12 -0
- package/dist/esm/parser/normalize.js +1 -0
- package/dist/esm/parser/parse.d.ts +23 -0
- package/dist/esm/parser/parse.js +1 -0
- package/dist/esm/parser/properties.d.ts +93 -0
- package/dist/esm/parser/properties.js +1 -0
- package/dist/esm/unescape/index.d.ts +4 -0
- package/dist/esm/unescape/index.js +1 -1
- package/package.json +33 -21
- package/dist/cjs/properties.d.ts +0 -96
- package/dist/cjs/properties.js +0 -1
- package/dist/cjs/property-line.d.ts +0 -22
- package/dist/cjs/property-line.js +0 -1
- package/dist/cjs/property.d.ts +0 -81
- package/dist/cjs/property.js +0 -1
- package/dist/esm/properties.d.ts +0 -96
- package/dist/esm/properties.js +0 -1
- package/dist/esm/property-line.d.ts +0 -22
- package/dist/esm/property-line.js +0 -1
- package/dist/esm/property.d.ts +0 -81
- package/dist/esm/property.js +0 -1
package/README.md
CHANGED
|
@@ -2,15 +2,15 @@
|
|
|
2
2
|
|
|
3
3
|
[](https://opensource.org/licenses/MIT)
|
|
4
4
|
[](https://www.npmjs.com/package/properties-file)
|
|
5
|
-

|
|
6
|
+

|
|
7
7
|

|
|
8
8
|
|
|
9
9
|
`.properties` file parser, editor, formatter and bundler integrations.
|
|
10
10
|
|
|
11
|
-
## Installation
|
|
11
|
+
## Installation
|
|
12
12
|
|
|
13
|
-
>
|
|
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
|
|
27
|
-
-
|
|
28
|
-
-
|
|
29
|
-
- `escapeKey` and `escapeValue`
|
|
30
|
-
- Bundler integrations for Webpack, Rollup/Vite, esbuild, and Bun to import `.properties` files directly. See [BUNDLER.md](./BUNDLER.md).
|
|
31
|
-
-
|
|
32
|
-
-
|
|
33
|
-
-
|
|
34
|
-
|
|
35
|
-
|
|
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` (
|
|
57
|
+
### `Properties` (lossless parsing with full data model)
|
|
57
58
|
|
|
58
|
-
The `Properties`
|
|
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 {
|
|
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
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
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
|
-
*
|
|
105
|
-
*
|
|
107
|
+
* Key 'hello' appears on lines 1, 4
|
|
108
|
+
* Key 'world' appears on lines 2, 3, 5
|
|
106
109
|
*/
|
|
107
110
|
```
|
|
108
111
|
|
|
109
|
-
|
|
112
|
+
#### Normalizing output
|
|
110
113
|
|
|
111
|
-
|
|
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
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
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
|
-
*
|
|
134
|
-
* new\ world = new world
|
|
136
|
+
* key = updated
|
|
135
137
|
*/
|
|
136
138
|
```
|
|
137
139
|
|
|
138
|
-
|
|
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 =
|
|
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
|
-
|
|
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
|
|
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
|
-
-
|
|
217
|
-
-
|
|
218
|
-
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
241
|
+
See [CONTRIBUTING.md](./docs/CONTRIBUTING.md) for project principles, architecture, code style, and development commands.
|
|
245
242
|
|
|
246
243
|
## Additional references
|
|
247
244
|
|
package/dist/cjs/bundler/bun.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var
|
|
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
|
|
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
|
+
"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
|
+
"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 {
|
|
2
|
-
import {
|
|
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
|
|
7
|
+
/** Options for {@link PropertiesEditor.insert}. */
|
|
12
8
|
export type InsertOptions = {
|
|
13
|
-
/**
|
|
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
|
-
/**
|
|
14
|
+
/** Position relative to the reference key. Default: `'after'`. */
|
|
16
15
|
position?: 'before' | 'after';
|
|
17
|
-
/**
|
|
16
|
+
/** If `true`, escape non-ASCII characters as `\\uXXXX` sequences. Default: `false`. */
|
|
18
17
|
escapeUnicode?: boolean;
|
|
19
|
-
/**
|
|
18
|
+
/** Separator character to use between key and value. Default: `'='`. */
|
|
20
19
|
separator?: KeyValuePairSeparator;
|
|
21
|
-
/**
|
|
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
|
-
/**
|
|
26
|
+
/** Delimiter character for the comment. Default: `'#'`. */
|
|
24
27
|
commentDelimiter?: CommentDelimiter;
|
|
25
28
|
};
|
|
26
|
-
/** Options
|
|
29
|
+
/** Options for {@link PropertiesEditor.insertComment}. */
|
|
27
30
|
export type InsertCommentOptions = {
|
|
28
|
-
/**
|
|
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
|
-
/**
|
|
36
|
+
/** Position relative to the reference key. Default: `'after'`. */
|
|
31
37
|
position?: 'before' | 'after';
|
|
32
|
-
/**
|
|
38
|
+
/** Delimiter character for the comment. Default: `'#'`. */
|
|
33
39
|
commentDelimiter?: CommentDelimiter;
|
|
34
40
|
};
|
|
35
|
-
/** Options
|
|
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
|
-
/**
|
|
53
|
+
/** Replacement value. When not set, the original value is preserved. */
|
|
38
54
|
newValue?: string;
|
|
39
|
-
/**
|
|
55
|
+
/** Replacement key (rename). When not set, the original key is preserved. */
|
|
40
56
|
newKey?: string;
|
|
41
|
-
/**
|
|
57
|
+
/** If `true`, escape non-ASCII characters as `\\uXXXX` sequences. Default: `false`. */
|
|
42
58
|
escapeUnicode?: boolean;
|
|
43
|
-
/**
|
|
44
|
-
separator?:
|
|
45
|
-
/**
|
|
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
|
-
/**
|
|
67
|
+
/** Delimiter character for the new comment. Default: `'#'`. */
|
|
48
68
|
commentDelimiter?: CommentDelimiter;
|
|
49
69
|
};
|
|
50
|
-
/** Options
|
|
70
|
+
/** Options for {@link PropertiesEditor.upsert}. */
|
|
51
71
|
export type UpsertOptions = {
|
|
52
|
-
/**
|
|
72
|
+
/** If `true`, escape non-ASCII characters as `\\uXXXX` sequences. Default: `false`. */
|
|
53
73
|
escapeUnicode?: boolean;
|
|
54
|
-
/**
|
|
74
|
+
/** Separator character. Default: `'='`. */
|
|
55
75
|
separator?: KeyValuePairSeparator;
|
|
56
|
-
/**
|
|
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
|
-
/**
|
|
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
|
-
*
|
|
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
|
|
99
|
+
* Find the index of the last property node with the given key.
|
|
75
100
|
*
|
|
76
|
-
* @param key - The
|
|
101
|
+
* @param key - The unescaped key to search for.
|
|
77
102
|
*
|
|
78
|
-
* @returns The
|
|
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
|
|
105
|
+
private findLastPropertyIndex;
|
|
85
106
|
/**
|
|
86
|
-
* Insert a new property
|
|
107
|
+
* Insert a new property.
|
|
87
108
|
*
|
|
88
|
-
* @param key -
|
|
89
|
-
* @param value -
|
|
90
|
-
* @param 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):
|
|
113
|
+
insert(key: string, value: string, options?: InsertOptions): void;
|
|
95
114
|
/**
|
|
96
|
-
* Insert a
|
|
97
|
-
*
|
|
98
|
-
* @param comment - The comment to add.
|
|
99
|
-
* @param options - Additional options.
|
|
115
|
+
* Insert a comment.
|
|
100
116
|
*
|
|
101
|
-
* @
|
|
117
|
+
* @param comment - The comment text (may contain newlines).
|
|
118
|
+
* @param options - Insert comment options.
|
|
102
119
|
*/
|
|
103
|
-
insertComment(comment: string, options?: InsertCommentOptions):
|
|
120
|
+
insertComment(comment: string, options?: InsertCommentOptions): void;
|
|
104
121
|
/**
|
|
105
|
-
*
|
|
122
|
+
* Insert a blank line.
|
|
106
123
|
*
|
|
107
|
-
* @param
|
|
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
|
-
|
|
126
|
+
insertBlankLine(options?: InsertBlankLineOptions): void;
|
|
113
127
|
/**
|
|
114
|
-
*
|
|
128
|
+
* Update an existing property.
|
|
115
129
|
*
|
|
116
|
-
* @param
|
|
130
|
+
* @param key - The unescaped key to update (uses last occurrence).
|
|
131
|
+
* @param options - Update options.
|
|
117
132
|
*
|
|
118
|
-
* @returns
|
|
133
|
+
* @returns `true` if the property was found and updated, `false` otherwise.
|
|
119
134
|
*/
|
|
120
|
-
|
|
135
|
+
update(key: string, options: UpdateOptions): boolean;
|
|
121
136
|
/**
|
|
122
|
-
*
|
|
123
|
-
*
|
|
124
|
-
* @param property - A property object.
|
|
137
|
+
* Update a property if it exists, or insert it if it doesn't.
|
|
125
138
|
*
|
|
126
|
-
* @
|
|
139
|
+
* @param key - The unescaped key.
|
|
140
|
+
* @param value - The unescaped value.
|
|
141
|
+
* @param options - Upsert options.
|
|
127
142
|
*/
|
|
128
|
-
|
|
143
|
+
upsert(key: string, value: string, options?: UpsertOptions): void;
|
|
129
144
|
/**
|
|
130
|
-
*
|
|
145
|
+
* Delete the last occurrence of a property (the effective value in last-wins semantics).
|
|
131
146
|
*
|
|
132
|
-
* @param key - The
|
|
133
|
-
* @param options -
|
|
147
|
+
* @param key - The unescaped key to delete.
|
|
148
|
+
* @param options - Delete options.
|
|
134
149
|
*
|
|
135
|
-
* @returns
|
|
150
|
+
* @returns The deleted {@link PropertyNode}, or `undefined` if the key was not found.
|
|
136
151
|
*/
|
|
137
|
-
|
|
152
|
+
delete(key: string, options?: DeleteOptions): PropertyNode | undefined;
|
|
138
153
|
/**
|
|
139
|
-
*
|
|
154
|
+
* Delete all occurrences of a key.
|
|
140
155
|
*
|
|
141
|
-
* @param key -
|
|
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
|
|
158
|
+
* @returns An array of the deleted {@link PropertyNode} instances.
|
|
152
159
|
*/
|
|
153
|
-
|
|
160
|
+
deleteAll(key: string): PropertyNode[];
|
|
154
161
|
}
|