utils-fs-read-properties
Version:
Reads the entire contents of a .properties file.
207 lines (145 loc) • 4.97 kB
Markdown
.properties
===
[![NPM version][npm-image]][npm-url] [![Build Status][travis-image]][travis-url] [![Coverage Status][codecov-image]][codecov-url] [![Dependencies][dependencies-image]][dependencies-url]
> Reads the entire contents of a [.properties](https://en.wikipedia.org/wiki/.properties) file.
``` bash
$ npm install utils-fs-read-properties
```
``` javascript
var read = require( 'utils-fs-read-properties' );
```
Reads the entire contents of a [.properties](https://en.wikipedia.org/wiki/.properties) file.
``` javascript
read( '/path/to/data.properties', onData );
function onData( error, data ) {
if ( error ) {
console.error( error );
} else {
console.log( data );
}
}
```
The `function` accepts the same options as [`properties
``` javascript
var opts = {
'sections': true
};
read( '/path/to/data.properties', opts, onData );
function onData( error, data ) {
if ( error ) {
console.error( error );
} else {
console.log( data );
}
}
```
Synchronously reads the contents of an entire [.properties](https://en.wikipedia.org/wiki/.properties) file.
``` javascript
var out = read.sync( '/path/to/data.properties' );
if ( out instanceof Error ) {
throw out;
}
console.log( out );
```
The `function` accepts the same options as [`fs.readFileSync()`](https://nodejs.org/api/fs.html#fs_fs_readfilesync_filename_options) as well as [utils-properties-parse](https://github.com/kgryte/utils-properties-parse/) options.
``` javascript
var path = require( 'path' ),
read = require( 'utils-fs-read-properties' );
var file = path.join( __dirname, 'config.properties' );
/**
* .properties reviver.
*
* @param {String} key - .properties key
* @param {String} value - .properties value
* @param {String} section - .properties section
* @returns {*} revived value
*/
function reviver( key, value ) {
/* jshint validthis:true */
var vals;
// Do not split section lines...
if ( this.isSection ) {
return this.assert();
}
// Split comma-delimited strings...
if ( typeof value === 'string' ){
vals = value.split( ',' );
return ( vals.length === 1 ) ? value : vals;
}
// Do not split the rest of the lines:
return this.assert();
}
// Sync:
var data = read.sync( file, {
'sections': true,
'namespaces': true,
'reviver': reviver
});
// returns <object>
console.log( data instanceof Error );
// returns false
data = read.sync( 'beepboop' );
// returns <error>
console.log( data instanceof Error );
// returns true
// Async:
read( file, {
'sections': true,
'namespaces': true,
'reviver': reviver
}, onRead );
read( 'beepboop', onRead );
function onRead( error, config ) {
if ( error ) {
if ( error.code === 'ENOENT' ) {
console.error( '.properties file does not exist.' );
} else {
throw error;
}
} else {
console.log( 'Port: %s.', config.server.port );
}
}
```
To run the example code from the top-level application directory,
``` bash
$ node ./examples/index.js
```
Unit tests use the [Mocha](http://mochajs.org/) test framework with [Chai](http://chaijs.com) assertions. To run the tests, execute the following command in the top-level application directory:
``` bash
$ make test
```
All new feature development should have corresponding unit tests to validate correct functionality.
This repository uses [Istanbul](https://github.com/gotwarlost/istanbul) as its code coverage tool. To generate a test coverage report, execute the following command in the top-level application directory:
``` bash
$ make test-cov
```
Istanbul creates a `./reports/coverage` directory. To access an HTML version of the report,
``` bash
$ make view-cov
```
---
[](http://opensource.org/licenses/MIT).
Copyright © 2015. Athan Reines.
[]: http://img.shields.io/npm/v/utils-fs-read-properties.svg
[]: https://npmjs.org/package/utils-fs-read-properties
[]: http://img.shields.io/travis/kgryte/utils-fs-read-properties/master.svg
[]: https://travis-ci.org/kgryte/utils-fs-read-properties
[]: https://img.shields.io/codecov/c/github/kgryte/utils-fs-read-properties/master.svg
[]: https://codecov.io/github/kgryte/utils-fs-read-properties?branch=master
[]: http://img.shields.io/david/kgryte/utils-fs-read-properties.svg
[]: https://david-dm.org/kgryte/utils-fs-read-properties
[]: http://img.shields.io/david/dev/kgryte/utils-fs-read-properties.svg
[]: https://david-dm.org/dev/kgryte/utils-fs-read-properties
[]: http://img.shields.io/github/issues/kgryte/utils-fs-read-properties.svg
[]: https://github.com/kgryte/utils-fs-read-properties/issues