Reads the entire contents of a .properties file.
npm install utils-fs-read-properties.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 file.
`` bash`
$ npm install utils-fs-read-properties
` javascript`
var read = require( 'utils-fs-read-properties' );
#### read( path, [ options,] clbk )
Reads the entire contents of a .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#parse, except path is __always__ true.
` 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 );
}
}
`
#### read.sync( path[, options] )
Synchronously reads the contents of an entire .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() as well as 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
console.log( data instanceof Error );
// returns false
data = read.sync( 'beepboop' );
// returns
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 test framework with Chai 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 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
---
Copyright © 2015. Athan Reines.
[npm-image]: http://img.shields.io/npm/v/utils-fs-read-properties.svg
[npm-url]: https://npmjs.org/package/utils-fs-read-properties
[travis-image]: http://img.shields.io/travis/kgryte/utils-fs-read-properties/master.svg
[travis-url]: https://travis-ci.org/kgryte/utils-fs-read-properties
[codecov-image]: https://img.shields.io/codecov/c/github/kgryte/utils-fs-read-properties/master.svg
[codecov-url]: https://codecov.io/github/kgryte/utils-fs-read-properties?branch=master
[dependencies-image]: http://img.shields.io/david/kgryte/utils-fs-read-properties.svg
[dependencies-url]: https://david-dm.org/kgryte/utils-fs-read-properties
[dev-dependencies-image]: http://img.shields.io/david/dev/kgryte/utils-fs-read-properties.svg
[dev-dependencies-url]: https://david-dm.org/dev/kgryte/utils-fs-read-properties
[github-issues-image]: http://img.shields.io/github/issues/kgryte/utils-fs-read-properties.svg
[github-issues-url]: https://github.com/kgryte/utils-fs-read-properties/issues