properties-file 3.7.0 → 5.0.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.
- package/README.md +253 -285
- package/dist/cjs/bundler/bun.d.ts +10 -0
- package/dist/cjs/bundler/bun.js +1 -0
- package/dist/cjs/bundler/esbuild.d.ts +11 -0
- package/dist/cjs/bundler/esbuild.js +1 -0
- package/dist/cjs/bundler/rollup.d.ts +11 -0
- package/dist/cjs/bundler/rollup.js +1 -0
- package/dist/cjs/bundler/webpack.d.ts +12 -0
- package/dist/cjs/bundler/webpack.js +1 -0
- package/dist/cjs/editor/index.d.ts +76 -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 +113 -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/bundler/bun.d.ts +10 -0
- package/dist/esm/bundler/bun.js +1 -0
- package/dist/esm/bundler/esbuild.d.ts +11 -0
- package/dist/esm/bundler/esbuild.js +1 -0
- package/dist/esm/bundler/rollup.d.ts +11 -0
- package/dist/esm/bundler/rollup.js +1 -0
- package/dist/esm/bundler/webpack.d.ts +12 -0
- package/dist/esm/editor/index.d.ts +76 -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 +113 -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 +81 -30
- package/dist/cjs/loader/webpack.d.ts +0 -12
- package/dist/cjs/loader/webpack.js +0 -1
- 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/loader/webpack.d.ts +0 -12
- 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/dist/esm/{loader → bundler}/webpack.js +0 -0
package/README.md
CHANGED
|
@@ -1,285 +1,253 @@
|
|
|
1
|
-
# properties-file
|
|
2
|
-
|
|
3
|
-
[](https://opensource.org/licenses/MIT)
|
|
4
|
-
[](https://www.npmjs.com/package/properties-file)
|
|
5
|
-

|
|
8
|
-
|
|
9
|
-
`.properties` file parser, editor, formatter and
|
|
10
|
-
|
|
11
|
-
## Installation
|
|
12
|
-
|
|
13
|
-
>
|
|
14
|
-
|
|
15
|
-
Add the package as a dependency:
|
|
16
|
-
|
|
17
|
-
```
|
|
18
|
-
npm install properties-file
|
|
19
|
-
```
|
|
20
|
-
|
|
21
|
-
## What's in it for me?
|
|
22
|
-
|
|
23
|
-
- A modern library written entirely in TypeScript that exactly reproduces the [Properties Java implementation](/assets/java-implementation.md).
|
|
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
|
-
-
|
|
31
|
-
-
|
|
32
|
-
-
|
|
33
|
-
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
import {
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
)
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
*
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
import {
|
|
118
|
-
|
|
119
|
-
const properties = new Properties('
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
properties.
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
*
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
})
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
*
|
|
178
|
-
*
|
|
179
|
-
* # This is a
|
|
180
|
-
*
|
|
181
|
-
*
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
-
|
|
246
|
-
-
|
|
247
|
-
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
###
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
| File format | Key/value based | Supports inline comments | Built for localization | Good linguistic tools support |
|
|
256
|
-
| ------------- | ---------------- | ------------------------ | ---------------------- | ----------------------------- |
|
|
257
|
-
| `.properties` | Yes | Yes | Yes (Resource Bundles) | Yes |
|
|
258
|
-
| `JSON` | No (can do more) | No (requires JSON5) | No | Depends on the schema |
|
|
259
|
-
| `YAML` | No (can do more) | Yes | No | Depends on the schema |
|
|
260
|
-
|
|
261
|
-
Having good JavaScript/TypeScript support for `.properties` files offers more internationalization (i18n) options.
|
|
262
|
-
|
|
263
|
-
### How does this package work?
|
|
264
|
-
|
|
265
|
-
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:
|
|
266
|
-
|
|
267
|
-
1. The content is split by lines, creating an array of strings where each line is an element.
|
|
268
|
-
2. All lines are parsed to create a collection of `Property` objects that:
|
|
269
|
-
1. Identify key-value pair lines from the other lines (e.g., comments, blank lines, etc.).
|
|
270
|
-
2. Merge back multiline key-value pairs on single lines by removing trailing backslashes.
|
|
271
|
-
3. Unescape the keys and values.
|
|
272
|
-
|
|
273
|
-
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.
|
|
274
|
-
|
|
275
|
-
## Additional references
|
|
276
|
-
|
|
277
|
-
- Java [Test Sandbox](https://codehs.com/sandbox/id/java-main-FObePj)
|
|
278
|
-
- Java's `Properties` class [documentation](https://docs.oracle.com/javase/9/docs/api/java/util/Properties.html)
|
|
279
|
-
- Java's `PropertyResourceBundle` [documentation](https://docs.oracle.com/javase/9/docs/api/java/util/PropertyResourceBundle.html)
|
|
280
|
-
- Java's Internationalization [Guide](https://docs.oracle.com/en/java/javase/18/intl/internationalization-overview.html)
|
|
281
|
-
- Wikipedia's .properties [page](https://en.wikipedia.org/wiki/.properties)
|
|
282
|
-
|
|
283
|
-
### Special mention
|
|
284
|
-
|
|
285
|
-
Thanks to [@calibr](https://github.com/calibr), the creator of [properties-file version 1.0](https://github.com/calibr/properties-file), for letting us use the [https://www.npmjs.com/package/properties-file](https://www.npmjs.com/package/properties-file) package name. We hope that it will make it easier to find our package.
|
|
1
|
+
# properties-file
|
|
2
|
+
|
|
3
|
+
[](https://opensource.org/licenses/MIT)
|
|
4
|
+
[](https://www.npmjs.com/package/properties-file)
|
|
5
|
+

|
|
6
|
+

|
|
7
|
+

|
|
8
|
+
|
|
9
|
+
`.properties` file parser, editor, formatter and bundler integrations.
|
|
10
|
+
|
|
11
|
+
## Installation
|
|
12
|
+
|
|
13
|
+
> Doing a major version update? Check our [migration guides](./docs/migration/README.md).
|
|
14
|
+
|
|
15
|
+
Add the package as a dependency:
|
|
16
|
+
|
|
17
|
+
```
|
|
18
|
+
npm install properties-file
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## What's in it for me?
|
|
22
|
+
|
|
23
|
+
- A modern library written entirely in TypeScript that exactly reproduces the [Properties Java implementation](/assets/java-implementation.md).
|
|
24
|
+
- Works for both Node.js applications and browsers that support at least [ES5](https://www.w3schools.com/js/js_es5.asp).
|
|
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
|
|
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.
|
|
39
|
+
|
|
40
|
+
### `getProperties` (converting `.properties` to an object)
|
|
41
|
+
|
|
42
|
+
The most common use case for `.properties` files is for Node.js applications that need to read the file's content into a simple key-value pair object. Here is how this can be done with a single API call:
|
|
43
|
+
|
|
44
|
+
```ts
|
|
45
|
+
import { readFileSync } from 'node:fs'
|
|
46
|
+
import { getProperties } from 'properties-file'
|
|
47
|
+
|
|
48
|
+
console.log(getProperties(readFileSync('hello-world.properties')))
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
Output:
|
|
52
|
+
|
|
53
|
+
```js
|
|
54
|
+
{ hello: 'hello', world: 'world' }
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
### `Properties` (lossless parsing with full data model)
|
|
58
|
+
|
|
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.
|
|
60
|
+
|
|
61
|
+
```ts
|
|
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'
|
|
93
|
+
|
|
94
|
+
const properties = new Properties(
|
|
95
|
+
'hello = hello1\nworld = world1\nworld = world2\nhello = hello2\nworld = world3'
|
|
96
|
+
)
|
|
97
|
+
|
|
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(', ')}`)
|
|
102
|
+
})
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Outputs:
|
|
106
|
+
*
|
|
107
|
+
* Key 'hello' appears on lines 1, 4
|
|
108
|
+
* Key 'world' appears on lines 2, 3, 5
|
|
109
|
+
*/
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
#### Normalizing output
|
|
113
|
+
|
|
114
|
+
Passing options to `format()` produces a normalized version of the file with granular control over formatting:
|
|
115
|
+
|
|
116
|
+
```ts
|
|
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
|
+
)
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Outputs:
|
|
135
|
+
*
|
|
136
|
+
* key = updated
|
|
137
|
+
*/
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
### `PropertiesEditor` (editing `.properties` content)
|
|
141
|
+
|
|
142
|
+
The `PropertiesEditor` extends `Properties` with methods to insert, update, delete, and upsert entries while preserving formatting.
|
|
143
|
+
|
|
144
|
+
```ts
|
|
145
|
+
import { PropertiesEditor } from 'properties-file/editor'
|
|
146
|
+
|
|
147
|
+
const properties = new PropertiesEditor('hello = hello\n# This is a comment\nworld = world')
|
|
148
|
+
|
|
149
|
+
properties.insertComment('This is a multiline\ncomment before `newKey3`')
|
|
150
|
+
properties.insert('newKey3', 'This is my third key')
|
|
151
|
+
|
|
152
|
+
properties.insert('newKey1', 'This is my first new key', {
|
|
153
|
+
referenceKey: 'newKey3',
|
|
154
|
+
position: 'before',
|
|
155
|
+
comment: 'Below are the new keys being edited',
|
|
156
|
+
commentDelimiter: '!',
|
|
157
|
+
})
|
|
158
|
+
|
|
159
|
+
properties.insert('newKey2', 'hello', {
|
|
160
|
+
referenceKey: 'newKey1',
|
|
161
|
+
position: 'after',
|
|
162
|
+
escapeUnicode: true,
|
|
163
|
+
})
|
|
164
|
+
|
|
165
|
+
properties.delete('hello')
|
|
166
|
+
properties.update('world', {
|
|
167
|
+
newValue: 'new world',
|
|
168
|
+
})
|
|
169
|
+
console.log(properties.format())
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Outputs:
|
|
173
|
+
*
|
|
174
|
+
* # This is a comment
|
|
175
|
+
* world = new world
|
|
176
|
+
* ! Below are the new keys being edited
|
|
177
|
+
* newKey1 = This is my first new key
|
|
178
|
+
* newKey2 = hello
|
|
179
|
+
* # This is a multiline
|
|
180
|
+
* # comment before `newKey3`
|
|
181
|
+
* newKey3 = This is my third key
|
|
182
|
+
*/
|
|
183
|
+
```
|
|
184
|
+
|
|
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.
|
|
186
|
+
|
|
187
|
+
### Bundler Integrations
|
|
188
|
+
|
|
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**.
|
|
190
|
+
|
|
191
|
+
See [BUNDLER.md](./docs/BUNDLER.md) for setup instructions and examples.
|
|
192
|
+
|
|
193
|
+
By adding these configurations you should now be able to import directly `.properties` files just like this:
|
|
194
|
+
|
|
195
|
+
```ts
|
|
196
|
+
import { properties as helloWorld } from './hello-world.properties'
|
|
197
|
+
|
|
198
|
+
console.dir(helloWorld)
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
Output:
|
|
202
|
+
|
|
203
|
+
```json
|
|
204
|
+
{ "hello": "world" }
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
## Why another `.properties` file package?
|
|
208
|
+
|
|
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:
|
|
210
|
+
|
|
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.
|
|
214
|
+
|
|
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.
|
|
216
|
+
|
|
217
|
+
### So why `.properties` files?
|
|
218
|
+
|
|
219
|
+
While many options exist today to handle configurations, `.properties` files remain one of the best options to store localizable strings (also known as messages). On the Java side, `PropertyResourceBundle` is how most implementations handle localization today. Because of its simplicity and maturity, `.properties` files remain one of the best options today when it comes to internationalization (i18n):
|
|
220
|
+
|
|
221
|
+
| File format | Key/value based | Supports inline comments | Built for localization | Good linguistic tools support |
|
|
222
|
+
| ------------- | ---------------- | ------------------------ | ---------------------- | ----------------------------- |
|
|
223
|
+
| `.properties` | Yes | Yes | Yes (Resource Bundles) | Yes |
|
|
224
|
+
| `JSON` | No (can do more) | No (requires JSON5) | No | Depends on the schema |
|
|
225
|
+
| `YAML` | No (can do more) | Yes | No | Depends on the schema |
|
|
226
|
+
|
|
227
|
+
Having good JavaScript/TypeScript support for `.properties` files offers more internationalization (i18n) options.
|
|
228
|
+
|
|
229
|
+
### How does this package work?
|
|
230
|
+
|
|
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.
|
|
238
|
+
|
|
239
|
+
## Contributing
|
|
240
|
+
|
|
241
|
+
See [CONTRIBUTING.md](./docs/CONTRIBUTING.md) for project principles, architecture, code style, and development commands.
|
|
242
|
+
|
|
243
|
+
## Additional references
|
|
244
|
+
|
|
245
|
+
- Java [Test Sandbox](https://codehs.com/sandbox/id/java-main-FObePj)
|
|
246
|
+
- Java's `Properties` class [documentation](https://docs.oracle.com/javase/9/docs/api/java/util/Properties.html)
|
|
247
|
+
- Java's `PropertyResourceBundle` [documentation](https://docs.oracle.com/javase/9/docs/api/java/util/PropertyResourceBundle.html)
|
|
248
|
+
- Java's Internationalization [Guide](https://docs.oracle.com/en/java/javase/18/intl/internationalization-overview.html)
|
|
249
|
+
- Wikipedia's .properties [page](https://en.wikipedia.org/wiki/.properties)
|
|
250
|
+
|
|
251
|
+
### Special mention
|
|
252
|
+
|
|
253
|
+
Thanks to [@calibr](https://github.com/calibr), the creator of [properties-file version 1.0](https://github.com/calibr/properties-file), for letting us use the [https://www.npmjs.com/package/properties-file](https://www.npmjs.com/package/properties-file) package name. We hope that it will make it easier to find our package.
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { BunPlugin } from 'bun';
|
|
2
|
+
/**
|
|
3
|
+
* Bun plugin for `.properties` files. Works with both `Bun.plugin` (runtime) and `Bun.build`
|
|
4
|
+
* (build-time).
|
|
5
|
+
*/
|
|
6
|
+
declare const bunPlugin: BunPlugin;
|
|
7
|
+
export default bunPlugin;
|
|
8
|
+
|
|
9
|
+
// Enables type recognition for direct `.properties` file imports.
|
|
10
|
+
import '../properties-file.d.ts'
|
|
@@ -0,0 +1 @@
|
|
|
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;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { Plugin } from 'esbuild';
|
|
2
|
+
/**
|
|
3
|
+
* esbuild plugin for `.properties` files.
|
|
4
|
+
*
|
|
5
|
+
* @returns An esbuild plugin that transforms `.properties` imports into JavaScript modules.
|
|
6
|
+
*/
|
|
7
|
+
declare const esbuildPlugin: () => Plugin;
|
|
8
|
+
export default esbuildPlugin;
|
|
9
|
+
|
|
10
|
+
// Enables type recognition for direct `.properties` file imports.
|
|
11
|
+
import '../properties-file.d.ts'
|
|
@@ -0,0 +1 @@
|
|
|
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;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { Plugin } from 'rollup';
|
|
2
|
+
/**
|
|
3
|
+
* Rollup plugin for `.properties` files. Also compatible with Vite and Rolldown.
|
|
4
|
+
*
|
|
5
|
+
* @returns A Rollup plugin that transforms `.properties` imports into JavaScript modules.
|
|
6
|
+
*/
|
|
7
|
+
declare const rollupPlugin: () => Plugin;
|
|
8
|
+
export default rollupPlugin;
|
|
9
|
+
|
|
10
|
+
// Enables type recognition for direct `.properties` file imports.
|
|
11
|
+
import '../properties-file.d.ts'
|
|
@@ -0,0 +1 @@
|
|
|
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;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Webpack file loader for `.properties` files. Also compatible with Rspack.
|
|
3
|
+
*
|
|
4
|
+
* @param content - The content of a `.properties` file.
|
|
5
|
+
*
|
|
6
|
+
* @returns A CommonJS module string exporting the parsed key-value pairs.
|
|
7
|
+
*/
|
|
8
|
+
declare const webpackLoader: (content: string) => string;
|
|
9
|
+
export default webpackLoader;
|
|
10
|
+
|
|
11
|
+
// Enables type recognition for direct `.properties` file imports.
|
|
12
|
+
import '../properties-file.d.ts'
|
|
@@ -0,0 +1 @@
|
|
|
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;
|