Skip to content

Commit 38571bb

Browse files
committed
docs: added actual docs to README
1 parent 297f752 commit 38571bb

File tree

1 file changed

+134
-8
lines changed

1 file changed

+134
-8
lines changed

README.md

+134-8
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,152 @@
11
# js-java-properties
22

3-
Java properties file parser and formatter for Javascript.
3+
This is a small library that provides utilities to parse and manipulate Java properties files.
44

55
Intended mostly for the tools that need to modify existing property file, without reformatting the contents.
66
That is achieved by using string array as a backing storage. If you want only to read the properties,
7-
you might convert it to `Map` using `toMap(...)` function.
8-
9-
_Warning: Currently this parser does not support UTF-8 character escaping. If you want that feature, please open an
10-
issue._
7+
you should convert it to an object or a `Map` using `toObject(...)` or `toMap(...)` function, respectively.
118

129
## Usage
1310

14-
TODO
11+
You can install this library using NPM:
1512

16-
```sh
13+
```shell
1714
npm install js-java-properties
1815
```
1916

17+
### Parsing
18+
19+
Parses correctly file contents as a string into lines.
20+
21+
```ts
22+
import properties from 'js-java-properties';
23+
24+
const props = properties.parse('key1=value1\nkey2 = value2\nkey3: value3');
25+
console.log(props);
26+
// { lines: [ 'key1=value1', 'key2 = value2', 'key3: value3' ] }
27+
```
28+
29+
### Stringify
30+
31+
Formats property lines into string.
32+
33+
```ts
34+
import properties from 'js-java-properties';
35+
36+
const props = properties.empty();
37+
props.lines.push('key1=value1', 'key2 = value2', 'key3: value3');
38+
39+
const output = properties.stringify(props);
40+
console.log(output);
41+
// 'key1=value1\nkey2 = value2\nkey3: value3\n'
42+
```
43+
44+
### Listing key-value pairs
45+
46+
Iterate over every key-value pair. Note that if file contains duplicate keys,
47+
they are returned here as well.
48+
49+
```ts
50+
import properties from 'js-java-properties';
51+
52+
const props = properties.empty()
53+
props.lines.push('key1=value1', 'key2 = value2', 'key3: value3')
54+
55+
for (const {key, value} of properties.list(props)) {
56+
console.log(`${key}=${value}`)
57+
// key1=value1
58+
// key2=value2
59+
// key3=value3
60+
}
61+
```
62+
63+
### Getting a value by key
64+
65+
Note that this method has `O(n)` complexity for every operation.
66+
Use `toObject` or `toMap` methods to convert it into readable object.
67+
68+
In case there are duplicate keys, last one is returned.
69+
70+
```ts
71+
import properties from 'js-java-properties';
72+
73+
const props = properties.empty();
74+
props.lines.push('key1=value1', 'key2 = value2', 'key3: value3');
75+
76+
console.log(properties.get(props, 'key2'));
77+
// 'value2'
78+
```
79+
80+
### Converting to object or map
81+
82+
```ts
83+
import properties from 'js-java-properties';
84+
85+
const props = properties.empty();
86+
props.lines.push('key1=value1', 'key2 = value2', 'key3: value3');
87+
88+
console.log(properties.toObject(props));
89+
// { key1: 'value1', key2: 'value2', key3: 'value3' }
90+
91+
console.log(properties.toMap(props));
92+
// Map(3) { 'key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3' }
93+
```
94+
95+
### Setting a value
96+
97+
Adds or replaces given key and value. If value is undefined, it is removed.
98+
Empty string still counts as a value.
99+
100+
If there are duplicate keys in the list, all but first one are removed.
101+
102+
```ts
103+
import properties from 'js-java-properties';
104+
105+
const props = properties.empty();
106+
props.lines.push('key1=value1', 'key2 = value2', 'key3: value3');
107+
108+
properties.set(props, 'key2', 'new-value');
109+
console.log(properties.stringify(props));
110+
// 'key1=value1\nkey2 = new-value\nkey3: value3\n'
111+
112+
properties.set(props, 'new-key', 'new-value');
113+
console.log(properties.stringify(props));
114+
// 'key1=value1\nkey2 = new-value\nkey3: value3\nnew-key=new-value\n'
115+
116+
properties.set(props, 'new-key', 'new-value', {separator: ':'});
117+
console.log(properties.stringify(props));
118+
// 'key1=value1\nkey2 = new-value\nkey3: value3\nnew-key:new-value\n'
119+
120+
properties.set(props, 'key3', undefined);
121+
console.log(properties.stringify(props));
122+
// 'key1=value1\nkey2 = new-value\n'
123+
```
124+
125+
### Removing a value
126+
127+
Removes given key and value. If there are duplicate keys in the list, all are removed.
128+
129+
```ts
130+
import properties from 'js-java-properties';
131+
132+
const props = properties.empty();
133+
props.lines.push('key1=value1', 'key2 = value2', 'key3: value3');
134+
135+
properties.remove(props, 'key2');
136+
console.log(properties.stringify(props));
137+
// 'key1=value1\nkey3: value3\n'
138+
```
139+
20140
## Development
21141

22-
TODO
142+
- Commits must follow [Conventional Commits](https://www.conventionalcommits.org) standard
143+
- Code must conform to eslint and prettier rules
144+
- 100% test coverage is required
23145

24146
### Publishing
25147

26148
TODO
149+
150+
## Contributing
151+
152+
If you would like to contribute to this library, feel free to submit a pull request on GitHub.

0 commit comments

Comments
 (0)