Skip to content
This repository has been archived by the owner on Apr 25, 2019. It is now read-only.

Commit

Permalink
Merge branch 'release/0.1.2'
Browse files Browse the repository at this point in the history
  • Loading branch information
roeldev committed May 1, 2015
2 parents 8cf6278 + d34b38f commit ad89e33
Show file tree
Hide file tree
Showing 8 changed files with 474 additions and 158 deletions.
58 changes: 58 additions & 0 deletions .jscsrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
{
"disallowEmptyBlocks": true,
"disallowImplicitTypeConversion": ["numeric", "binary", "string"],
"disallowKeywords": ["with"],
"disallowMixedSpacesAndTabs": true,
"disallowMultipleLineStrings": true,
"disallowMultipleVarDecl": "exceptUndefined",
"disallowOperatorBeforeLineBreak": ["."],
"disallowPaddingNewlinesInBlocks": true,
"disallowSpaceAfterObjectKeys": true,
"disallowSpaceAfterPrefixUnaryOperators": true,
"disallowSpaceBeforePostfixUnaryOperators": true,
"disallowSpacesInCallExpression": true,
"disallowSpacesInsideArrayBrackets":
{ "allExcept": ["[", "]", "{", "}"] },
"disallowSpacesInsideBrackets":
{ "allExcept": ["[", "]", "{", "}"] },
"disallowTrailingComma": true,
"disallowTrailingWhitespace": true,
"disallowYodaConditions": true,
"maximumLineLength":
{ "value": 80, "allowUrlComments": true, "allowRegex": true },
"requireBlocksOnNewline": true,
"requireCamelCaseOrUpperCaseIdentifiers": true,
"requireCapitalizedConstructors": true,
"requireCommaBeforeLineBreak": true,
"requireCurlyBraces": true,
"requireDotNotation": true,
"requireKeywordsOnNewLine": ["else"],
"requireLineBreakAfterVariableAssignment": true,
"requireLineFeedAtFileEnd": true,
"requireNewlineBeforeBlockStatements": true,
"requireOperatorBeforeLineBreak": true,
//"requirePaddingNewLineAfterVariableDeclaration": true,
//"requirePaddingNewLinesAfterBlocks": true,
"requirePaddingNewlinesBeforeKeywords":
["do", "case", "try", "void", "while", "function"],
//"requirePaddingNewLinesInObjects": true,
"requireParenthesesAroundIIFE": true,
"requireQuotedKeysInObjects": true,
"requireSemicolons": true,
"requireSpaceAfterBinaryOperators": true,
"requireSpaceAfterKeywords":
["do", "for", "if", "else", "switch", "case", "try", "catch", "void", "while", "with", "return", "typeof"],
"requireSpaceAfterLineComment":
{ "allExcept": ["#", "=",
"------------------------------------------------------------------------------", "//////////////////////////////////////////////////////////////////////////////"] },
"requireSpaceBeforeBinaryOperators": true,
"requireSpaceBeforeObjectValues": true,
"requireSpaceBetweenArguments": true,
"requireSpacesInConditionalExpression": true,
"requireSpacesInForStatement": true,
"requireSpacesInsideObjectBrackets": "all",
"validateIndentation": 4,
"validateParameterSeparator": ", ",
"validateQuoteMarks": "'",
"excludeFiles": ["node_modules/**"]
}
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
.gitattributes
.gitignore
.gitkeep
.jscsrc
.jshintrc

.travis.yml
Expand Down
51 changes: 47 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Confirge
# confirge

[![NPM Version][npm-img]][npm-url]
[![Linux Build][travis-img]][travis-url]
Expand All @@ -15,11 +15,10 @@ npm install --save confirge

## How to use
```js
var confirge = require('confirge'),
config;
var confirge = require('confirge');

// basic usage, load from a file
config = confirge('config.yml');
var config = confirge('config.yml');

// load from a file, returned by a function
config = confirge(function()
Expand All @@ -42,6 +41,45 @@ config = confirge.replace(config,
});
```

## API
- [confirge()][api-confirge]
- [confirge.read()][api-confirge-read]
- [confirge.replace()][api-confirge-replace]
- [confirge.extend()][api-confirge-extend]


### confirge(source)
Handles a string (file path), function, or object source and returns an object.

#### `source`
> Type: `string`, `function` or `object`

### confirge.read(file)
Read file and return object. Returns `false` on failure.
When a function is passed, it is assumed this function returns the path to a file wich should be read.

#### `file`
> Type: `string` or `function`

### confirge.replace(source, vars)
Loops through all (nested) source values and replaces any found variables.

#### `source`
> Type: `object` or `array`
#### `vars`
> Type: `object`

### confirge.extend(source...)
Extend a base object with the given sources. These sources are handled by the main `confirge` function and are only used if objects are returned.

#### `source`
> Type: `string`, `function` or `object`

[npm-img]: https://badge.fury.io/js/confirge.svg
[npm-url]: https://www.npmjs.com/package/confirge
[travis-img]: https://img.shields.io/travis/roeldev/confirge/master.svg?label=linux
Expand All @@ -52,3 +90,8 @@ config = confirge.replace(config,
[coveralls-url]: https://coveralls.io/r/roeldev/confirge?branch=master
[david-img]: https://david-dm.org/roeldev/confirge.svg
[david-url]: https://david-dm.org/roeldev/confirge

[api-confirge]: #confirgesource
[api-confirge-read]: #confirgereadfile
[api-confirge-replace]: #confirgereplacesource-vars
[api-confirge-extend]: #confirgeextendsource
25 changes: 14 additions & 11 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@
*/
'use strict';

var Gulp = require('gulp'),
GulpJsHint = require('gulp-jshint'),
GulpMocha = require('gulp-mocha'),
RunSequence = require('run-sequence');
var Gulp = require('gulp');
var GulpJsHint = require('gulp-jshint');
var GulpJsCs = require('gulp-jscs');
var GulpJsCsStylish = require('gulp-jscs-stylish');
var GulpMocha = require('gulp-mocha');
var RunSequence = require('run-sequence');
var Utils = require('./lib/utils.js');

//------------------------------------------------------------------------------

var JS_SRC = ['gulpfile.js', 'lib/**/*.js', 'test/*.js'];

Expand All @@ -17,23 +22,21 @@ Gulp.task('lint', function()
{
return Gulp.src(JS_SRC)
.pipe( GulpJsHint() )
.pipe( GulpJsCs() ).on('error', Utils.noop)
.pipe( GulpJsCsStylish.combineWithHintResults() )
.pipe( GulpJsHint.reporter('jshint-stylish') );
});

Gulp.task('test', function()
{
return Gulp.src('test/*.js', { 'read': false })
.pipe( GulpMocha({ 'reporter': 'list' }) );
.pipe( GulpMocha({ 'reporter': 'spec' }) );
});

Gulp.task('dev', function()
{
for(var $i = 0; $i < 30; $i++)
{
console.log('');
}

RunSequence('lint', 'test');
process.stdout.write('\u001b[2J');
RunSequence('test', 'lint');
});

Gulp.task('watch', function()
Expand Down
96 changes: 73 additions & 23 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,36 @@
/**
* confirge | lib/index.js
* file version: 0.00.004
* file version: 0.00.007
*/
'use strict';

var _ = require('underscore'),
DeepExtend = require('deep-extend'),
FileSystem = require('graceful-fs'),
Utils = require('./utils.js'),
Yaml = require('js-yaml');
var _ = require('underscore');
var DeepExtend = require('deep-extend');
var FileSystem = require('graceful-fs');
var Path = require('path');
var Utils = require('./utils.js');
var Yaml = require('js-yaml');

////////////////////////////////////////////////////////////////////////////////

var Confirge = function($source)
/**
* Handles a string (file path), function, or object source and returns an
* object.
*
* @param {string|function|object} $source
* @return {object|boolean} Returns `false` on failure.
*/
function Confirge($source)
{
var $result = false;

// assume the string is a file path, read the file
if (_.isString($source))
{
$result = Confirge.read($source);
}
// execute function and return result
if (_.isFunction($source))
else if (_.isFunction($source))
{
$result = Confirge($source());
}
Expand All @@ -26,29 +39,33 @@ var Confirge = function($source)
{
$result = $source;
}
else if (_.isString($source))
{
$result = Confirge.read($source);
}

return $result;
};
}

/**
* Read file and return object.
* Read file and return object. Returns `false` on failure.
* When a function is passed, it is assumed this function returns the path to a
* file wich should be read.
*
* @param {string} $file - File path to read.
* @return {object|boolean}
* @param {string|function} $file - File path to read.
* @return {object|boolean} Returns `false` on failure.
*/
Confirge.read = function($file)
Confirge.read = function confirgeRead($file)
{
var $result = false;

if (_.isFunction($file))
{
$result = Confirge.read($file());
}
else
{
if (!Path.isAbsolute($file))
{
$file = Path.resolve(process.cwd(), $file);
}

try
{
$file = FileSystem.readFileSync($file, 'utf8');
Expand All @@ -58,20 +75,22 @@ Confirge.read = function($file)
{
}
}

return $result;
};

/**
* Replace vars in obj values.
* Loops through all source values and replaces any used variables wich are
* defined in the vars object.
*
* @param {object|array} $source - Object/Array where values are replaced.
* @param {object} $vars - Vars to replace, { varName: value }.
* @return {object}
*/
Confirge.replace = function($source, $vars)
Confirge.replace = function confirgeReplace($source, $vars)
{
$source = Confirge($source);
$vars = Utils.prepareVars($vars);
$vars = Utils.prepareHandleVars($vars);

if (_.isArray($source))
{
Expand All @@ -85,11 +104,42 @@ Confirge.replace = function($source, $vars)
return $source;
};

// copy of deep-extend
Confirge.extend = DeepExtend;
/**
* Extend a base object with the given sources. These sources are handled by
* the main `confirge` function and are only used if objects are returned.
*
* @param {string|function|object} $source
* @return {object|boolean} Returns `false` on failure.
*/
Confirge.extend = function confirgeExtend($source)
{
var $result = false;
var $sources = [];

for (var $i = 0, $iL = arguments.length; $i < $iL; $i++)
{
$source = Confirge(arguments[$i]);

if ($source !== false)
{
$sources.push($source);
}
}

if ($sources.length === 1)
{
$result = $sources[0];
}
else if ($sources.length >= 2)
{
$result = DeepExtend.apply($sources[0], $sources);
}

return $result;
};

/*
Confirge
confirge
Copyright (c) 2015 Roel Schut (roelschut.nl)
This program is free software; you can redistribute it and/or modify
Expand Down
Loading

0 comments on commit ad89e33

Please sign in to comment.