java-props ·
Read/Parse Java .properties files (using the same specification)
in Javascript / Node.js.
Note for TypeScript users:
This module build and provide its TypeScript declarations files (.d.ts).
Getting Started
Node.js · Install the module with:
npm i --save java-props
Quick Example
# file.properties
a = Hello World
b : Node.js\u00AE
c value
d=foo\
bar
const javaProps = require('java-props');
javaProps.parseFile('./file.properties').then((props) => {
console.log(props);
// { a: 'Hello World', b: 'Node.js®', c: 'value', d: 'foobar' }
}).catch((err) => {
console.error(err)
});
Documentation
javaProps
Example
const javaProps = require('java-props');
javaProps.parse(str) ⇒ Object
Parses a .properties string, constructing a corresponding JavaScript object.
Returns: Object
- The Object corresponding to the given string
Param | Type | Description |
---|
str | String | The string to parse as .properties |
Example
const props = javaProps.parse('foo=Hello\nbar=World');
console.log(props.foo + ' ' + props.bar);
javaProps.parseFile(path, [encoding]) ⇒ Promise.<Object>
Parses a .properties file, constructing a corresponding JavaScript object.
Returns: Promise.<Object>
- The Object corresponding to the given string
Param | Type | Default | Description |
---|
path | String | Buffer | URL | number | | Filename or file descriptor |
[encoding] | String | utf8 | File encoding |
Example
javaProps.parseFile('./foobar.properties').then((props) => {
console.log(props.foo + ' ' + props.bar);
}).catch((err) => {
console.error(err);
});
- or with async/await -
async function fct() {
try {
const props = await javaProps.parseFile('./foobar.properties');
console.log(props.foo + ' ' + props.bar);
} catch (err) {
console.error(err);
}
}
javaProps.stringify(props) ⇒ String
Convert a JavaScript object to the corresponding .properties string.
Returns: String
- The .properties string corresponding to the given JavaScript object
Param | Type | Description |
---|
props | Object | The JavaScript object to convert |
Example
const str = javaProps.stringify({'foo': 'Hello', 'bar': 'World'});
console.log(str);
Building
This project uses TypeScript. To create javascript sources run:
npm run build
To generate the documentation, edit documentation.js
then run:
jsdoc2md --partial doc/scope.hbs --files doc/documentation.js --heading-depth 3 | xclip -selection c
and copy the result inside this README.
Testing
Run the unit tests (no need to run build before, they use the typescript files):
npm run test
Contributing
Contributions are welcome. Unfortunately there is no documentation on the
codestyle yet, so look at the existing sources and do the same.
The goal is to keep a simple project without unnecessary (non essential)
features.
Don't hesitate to open an issue before to discuss about your idea.
Versioning
We use SemVer for versioning. For the versions available,
see the tags on this repository.
Authors
See also the list of contributors
who participated in this project.
License
This project is licensed under the MIT License - see the LICENSE
file for details.