3
3
This is a small library that provides utilities to parse and
4
4
manipulate [ Java properties] ( https://docs.oracle.com/javase/9/docs/api/java/util/Properties.html ) files.
5
5
6
- Intended mostly for the tools that need to modify existing property file, without reformatting the contents.
7
- That is achieved by using string array as a backing storage. If you want only to read the properties,
8
- you should convert it to an object or a ` Map ` using ` toObject(...) ` or ` toMap(...) ` function, respectively.
6
+ Intended mostly for the tools that need to modify an existing property file without reformatting the contents. This is
7
+ achieved by using a string array as a backing storage. If you want only to read the properties, you should convert it to
8
+ an object or a ` Map ` using ` toObject(...) ` or ` toMap(...) ` function, respectively.
9
9
10
10
## Usage
11
11
@@ -15,6 +15,12 @@ You can install this library using NPM:
15
15
npm install js-java-properties
16
16
```
17
17
18
+ ### Types
19
+
20
+ - ` Properties ` represent lines in the properties file. A single property can span multiple lines.
21
+ It is a part of the API, and it may be extended in future versions.
22
+ - ` KeyValuePair ` parsed key and value. Used by ` listProperties ` .
23
+
18
24
### Parsing
19
25
20
26
Parses correctly file contents as a string into lines.
@@ -60,22 +66,25 @@ they are returned here as well.
60
66
import * as properties from ' js-java-properties'
61
67
62
68
const props = properties .empty ()
69
+ props .lines .push (' # comment' )
63
70
props .lines .push (' key1=value1' , ' key2 = value2' , ' key3: value3' )
71
+ props .lines .push (' key3: duplicate' )
64
72
65
73
for (const {key, value} of properties .listProperties (props )) {
66
74
console .log (` ${key }=${value } ` )
67
75
// key1=value1
68
76
// key2=value2
69
77
// key3=value3
78
+ // key3=duplicate
70
79
}
71
80
```
72
81
73
82
### Getting a value by key
74
83
75
84
Note that this method has ` O(n) ` complexity for every operation.
76
- Use ` toObject ` or ` toMap ` methods to convert it into readable object.
85
+ Use ` toObject ` or ` toMap ` methods to convert it into a readable object.
77
86
78
- In case there are duplicate keys, last one is returned.
87
+ In case there are duplicate keys, the last one is returned.
79
88
80
89
``` ts
81
90
import * as properties from ' js-java-properties'
@@ -85,10 +94,16 @@ props.lines.push('key1=value1', 'key2 = value2', 'key3: value3')
85
94
86
95
console .log (properties .getProperty (props , ' key2' ))
87
96
// 'value2'
97
+
98
+ props .lines .push (' key2 = duplicate' )
99
+ console .log (properties .getProperty (props , ' key2' ))
100
+ // 'duplicate'
88
101
```
89
102
90
103
### Converting to object or map
91
104
105
+ In case there are duplicate keys, the last one is returned.
106
+
92
107
``` ts
93
108
import * as properties from ' js-java-properties'
94
109
@@ -104,10 +119,11 @@ console.log(properties.toMap(props))
104
119
105
120
### Setting a value
106
121
107
- Adds or replaces given key and value. If value is undefined, it is removed.
108
- Empty string still counts as a value.
122
+ This method adds or replaces the given key- value pair . If the value is undefined, the key is removed.
123
+ Note that an empty string is still considered a value.
109
124
110
- If there are duplicate keys in the list, all but first one are removed.
125
+ If there are multiple occurrences of the same key in the list, only the first one is kept and
126
+ all other occurrences are removed.
111
127
112
128
``` ts
113
129
import * as properties from ' js-java-properties'
@@ -116,25 +132,26 @@ const props = properties.empty()
116
132
props .lines .push (' key1=value1' , ' key2 = value2' , ' key3: value3' )
117
133
118
134
properties .setProperty (props , ' key2' , ' new-value' )
119
- console .log (properties . stringify ( props ) )
120
- // 'key1=value1\nkey2 = new-value\nkey3 : value3\n'
135
+ console .log (props )
136
+ // { lines: [ 'key1=value1', 'key2 = new-value', 'key3 : value3' ] }
121
137
122
138
properties .setProperty (props , ' new-key' , ' new-value' )
123
- console .log (properties . stringify ( props ) )
124
- // 'key1=value1\nkey2 = new-value\nkey3 : value3\nnew -key= new-value\n'
139
+ console .log (props )
140
+ // { lines: [ 'key1=value1', 'key2 = new-value', 'key3 : value3', 'new -key: new-value' ] }
125
141
126
- properties .setProperty (props , ' new-key' , ' new-value' , {separator: ' : ' })
127
- console .log (properties . stringify ( props ) )
128
- // 'key1=value1\nkey2 = new-value\nkey3 : value3\nnew -key: new-value\n'
142
+ properties .setProperty (props , ' new-key' , ' new-value' , {separator: ' = ' })
143
+ console .log (props )
144
+ // { lines: [ 'key1=value1', 'key2 = new-value', 'key3 : value3', 'new -key = new-value' ] }
129
145
130
146
properties .setProperty (props , ' key3' , undefined )
131
- console .log (properties . stringify ( props ) )
132
- // 'key1=value1\nkey2 = new-value\n'
147
+ console .log (props )
148
+ // { lines: [ 'key1=value1', 'key2 = new-value', 'new-key = new-value' ] }
133
149
```
134
150
135
151
### Removing a value
136
152
137
- Removes given key and value. If there are duplicate keys in the list, all are removed.
153
+ Removes the given key and its associated value. If there are duplicate keys with the same name,
154
+ all values associated with the given key are removed.
138
155
139
156
``` ts
140
157
import * as properties from ' js-java-properties'
@@ -143,8 +160,8 @@ const props = properties.empty()
143
160
props .lines .push (' key1=value1' , ' key2 = value2' , ' key3: value3' )
144
161
145
162
properties .removeProperty (props , ' key2' )
146
- console .log (properties . stringify ( props ) )
147
- // 'key1=value1\nkey3 : value3\n'
163
+ console .log (props )
164
+ // { lines: [ 'key1=value1', 'key3 : value3' ] }
148
165
```
149
166
150
167
## Development
@@ -156,7 +173,8 @@ console.log(properties.stringify(props))
156
173
### Publishing
157
174
158
175
Releases are generated using [ Release Please] ( https://github.com/googleapis/release-please ) .
159
- Package is automatically published to a [ npm registry] ( https://www.npmjs.com/package/js-java-properties ) when release is created.
176
+ Package is automatically published to a [ npm registry] ( https://www.npmjs.com/package/js-java-properties ) ,
177
+ when release is created.
160
178
161
179
## Contributing
162
180
0 commit comments