diff --git a/gulpfile.js b/gulpfile.js index d14cdc5..ec0c617 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -1,6 +1,6 @@ /** * confirge | gulpfile.js - * file version: 0.00.001 + * file version: 0.00.002 */ 'use strict'; @@ -23,7 +23,7 @@ Gulp.task('lint', function() Gulp.task('test', function() { return Gulp.src('test/*.js', { 'read': false }) - .pipe( GulpMocha() ); + .pipe( GulpMocha({ 'reporter': 'list' }) ); }); Gulp.task('dev', function() @@ -40,3 +40,8 @@ Gulp.task('watch', function() { Gulp.watch(JS_SRC, ['dev']); }); + +Gulp.task('watch:lint', function() +{ + Gulp.watch(JS_SRC, ['lint']); +}); diff --git a/lib/index.js b/lib/index.js index 6f7e8e6..1b88340 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,6 +1,6 @@ /** * confirge | lib/index.js - * file version: 0.00.002 + * file version: 0.00.003 */ 'use strict'; @@ -64,35 +64,25 @@ Confirge.read = function($file) /** * Replace vars in obj values. * - * @param {object} $obj - Object where values are checked and replaced. + * @param {object|array} $source - Object/Array where values are replaced. * @param {object} $vars - Vars to replace, { varName: value }. * @return {object} */ -Confirge.replace = function($obj, $vars) +Confirge.replace = function($source, $vars) { - var $key, - $value; + $source = Confirge($source); + $vars = Utils.prepareVars($vars); - $obj = Confirge($obj); - $vars = Utils.prepareVars($vars); - - for($key in $obj) + if (_.isArray($source)) { - if (!$obj.hasOwnProperty($key)) - { - continue; - } - - $value = $obj[$key]; - if (!_.isString($value)) - { - continue; - } - - $obj[$key] = Utils.replaceVars($value, $vars); + $source = Utils.handleArray($source, $vars); + } + else if (_.isObject($source)) + { + $source = Utils.handleObject($source, $vars); } - return $obj; + return $source; }; // copy of deep-extend diff --git a/lib/utils.js b/lib/utils.js index 3d501d2..d3d490b 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -1,6 +1,6 @@ /** * confirge | lib/utils.js - * file version: 0.00.002 + * file version: 0.00.003 */ 'use strict'; @@ -21,7 +21,7 @@ var Utils = { var $result = {}, $found = false, - $regexp = /\%([0-9a-zA-Z\-_]+)\%/g, + $regexp = /\%([0-9a-zA-Z\-_\.]+)\%/g, $match; while (!!($match = $regexp.exec($str))) @@ -108,7 +108,7 @@ var Utils = { $result = {}; - for(var $search in $vars) + for (var $search in $vars) { if ($vars.hasOwnProperty($search) && _.isUndefined($result[$search])) @@ -132,11 +132,6 @@ var Utils = */ 'replaceVars': function($str, $vars) { - if (!_.isObject($vars) || _.isArray($vars) || _.isFunction($vars)) - { - return $str; - } - var $replacements = Utils.findReplacements($str), $replacement, $var; @@ -159,6 +154,74 @@ var Utils = } return $str; + }, + + /** + * Handle an item by passing it to either the `replaceVars()`, + * `handleArray()` or `handleObject()` functions, or by returning the + * original input value. + * + * @param {mixed} $value - The item value to handle. + * @param {object} $vars - Object with prepared vars. + * @return {mixed} + */ + 'handleItem': function($value, $vars) + { + if (_.isString($value)) + { + $value = Utils.replaceVars($value, $vars); + } + else if (_.isArray($value)) + { + $value = Utils.handleArray($value, $vars); + } + else if (_.isObject($value)) + { + $value = Utils.handleObject($value, $vars); + } + + return $value; + }, + + /** + * Handle an array by looping through it's values and passing them to + * `handleItem()`. + * + * @param {array} $array - The array to loop through. + * @param {object} $vars - Object with prepared vars. + * @return {array} + */ + 'handleArray': function($array, $vars) + { + for (var $i = 0, $iL = $array.length; $i < $iL; $i++) + { + $array[$i] = Utils.handleItem($array[$i], $vars); + } + + return $array; + }, + + /** + * Handle an object by looping through it's values and passing them to + * `handleItem()`. + * + * @param {object} $obj - The object to loop through. + * @param {object} $vars - Object with prepared vars. + * @return {object} + */ + 'handleObject': function($obj, $vars) + { + for (var $key in $obj) + { + if (!$obj.hasOwnProperty($key)) + { + continue; + } + + $obj[$key] = Utils.handleItem($obj[$key], $vars); + } + + return $obj; } }; diff --git a/test/main.js b/test/main.js index 98baee3..7031181 100644 --- a/test/main.js +++ b/test/main.js @@ -1,6 +1,6 @@ /** * confirge | test/main.js - * file version: 0.00.002 + * file version: 0.00.003 */ 'use strict'; @@ -9,7 +9,7 @@ var Assert = require('assert'), Path = require('path'), Utils = require('../lib/utils.js'); -var OBJ_PLAIN = { 'title': 'test obj', 'success': true }, +var OBJ_PLAIN = { 'title': 'test obj', 'success': true }, EXPECTED_YAML = { 'title': 'test yaml', 'success': true }, EXPECTED_JSON = { 'title': 'test json', 'success': true }, @@ -159,6 +159,53 @@ describe('Confirge.replace()', function() var $input = ['test', '123', function(){ return false; }]; Assert.deepEqual(Confirge.replace($input, INPUT_VARS), $input); }); + + it('should replace the vars deep inside the object', function() + { + var $input = + { + 'key1': + { + 'key1-2': 'replace me! %var1%' + }, + + 'key2': + { + 'key2-2': + [ + 'replace me too! %var1%', + 'skip me! %var2%', + + [ + 'down the rabbit hole', + 'last %var1% replacement' + ] + ] + } + }; + + Assert.deepEqual(Confirge.replace($input, INPUT_VARS), + { + 'key1': + { + 'key1-2': 'replace me! value1' + }, + + 'key2': + { + 'key2-2': + [ + 'replace me too! value1', + 'skip me! %var2%', + + [ + 'down the rabbit hole', + 'last value1 replacement' + ] + ] + } + }); + }); }); //------------------------------------------------------------------------------ @@ -179,8 +226,8 @@ describe('Utils.findReplacements()', function() it('found multiple replacements [1]', function() { - var $actual = Utils.findReplacements('%test%, %test_test% na na!'); - Assert.deepEqual($actual, ['test', 'test_test']); + var $actual = Utils.findReplacements('%test.me%, %test_test% na na!'); + Assert.deepEqual($actual, ['test.me', 'test_test']); }); it('found multiple replacements [2]', function() @@ -343,7 +390,7 @@ describe('Utils.replaceVars()', function() Assert.equal($actual, 'value1value2'); }); - it('should replace the nested var', function() + it('should replace the replaced var', function() { var $input = { 'var1': 'value1 %var2%', 'var2': 'value2' }, $vars = Utils.prepareVars($input), @@ -377,3 +424,117 @@ describe('Utils.replaceVars()', function() Assert.equal($actual, $input); }); }); + +describe('Utils.handleItem()', function() +{ + it('should replace the var and return a string', function() + { + var $input = 'the value is %var1%', + $vars = Utils.prepareVars(INPUT_VARS); + + Assert.equal(Utils.handleItem($input, $vars), 'the value is value1'); + }); + + it('should handle the array', function() + { + var $input = ['test 1 = %var1%', 'test 2 = %var2%'], + $vars = Utils.prepareVars(INPUT_VARS); + + Assert.deepEqual(Utils.handleItem($input, $vars), + [ + 'test 1 = value1', + 'test 2 = %var2%' + ]); + }); + + it('should handle the object', function() + { + var $input = + { + 'test1': 'test 1 = %var1%', + 'test2': 'test 2 = %var2%' + }, + + $vars = Utils.prepareVars(INPUT_VARS); + + Assert.deepEqual(Utils.handleItem($input, $vars), + { + 'test1': 'test 1 = value1', + 'test2': 'test 2 = %var2%' + }); + }); + + it('should return the exact input value [1]', function() + { + var $input = true; + Assert.equal(Utils.handleItem($input, {}), $input); + }); + + it('should return the exact input value [2]', function() + { + var $input = function() {}; + Assert.equal(Utils.handleItem($input, {}), $input); + }); + + it('should return the exact input value [3]', function() + { + var $input = 123; + Assert.equal(Utils.handleItem($input, {}), $input); + }); +}); + +describe('Utils.handleArray()', function() +{ + it('should handle the array', function() + { + var $input = ['test 1 = %var1%', 'test 2 = %var2%'], + $vars = Utils.prepareVars(INPUT_VARS); + + Assert.deepEqual(Utils.handleItem($input, $vars), + [ + 'test 1 = value1', + 'test 2 = %var2%' + ]); + }); +}); + +describe('Utils.handleObject()', function() +{ + it('should handle the object', function() + { + var $input = + { + 'test1': 'test 1 = %var1%', + 'test2': 'test 2 = %var2%' + }, + + $vars = Utils.prepareVars(INPUT_VARS); + + Assert.deepEqual(Utils.handleItem($input, $vars), + { + 'test1': 'test 1 = value1', + 'test2': 'test 2 = %var2%' + }); + }); + + it('should only handle the objects own property', function() + { + /* jshint ignore:start */ + Object.prototype.confirge_test = true; + /* jshint ignore:end */ + + var $input = + { + 'test1': 'test 1 = %var1%', + 'test2': 'test 2 = %var2%' + }, + + $vars = Utils.prepareVars(INPUT_VARS); + + Assert.deepEqual(Utils.handleItem($input, $vars), + { + 'test1': 'test 1 = value1', + 'test2': 'test 2 = %var2%' + }); + }); +});