diff --git a/README.md b/README.md index 7522921..3555bbe 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ config = confirge('config.yml'); // load from a file, returned by a function config = confirge(function() { - return 'config.json' + return 'config.json'; }); // extend objects @@ -34,6 +34,7 @@ config = confirge.extend(config, }); // will replace vars inside the config obj, eg. %var1%, %var2% +// this will result in { 'example': 'value1 and value2' } config = confirge.replace(config, { 'var1': 'value1', diff --git a/lib/index.js b/lib/index.js index 16bf48e..5490202 100644 --- a/lib/index.js +++ b/lib/index.js @@ -7,27 +7,11 @@ var _ = require('underscore'), DeepExtend = require('deep-extend'), FileSystem = require('graceful-fs'), + Utils = require('./utils.js'), Yaml = require('js-yaml'); //////////////////////////////////////////////////////////////////////////////// -function varsReplace($str, $vars) -{ - var $replace; - for(var $search in $vars) - { - if ($vars.hasOwnProperty($search)) - { - $replace = $vars[$search]; - $str = $str.replace('%'+ $search +'%', $replace); - } - } - - return $str; -} - -//------------------------------------------------------------------------------ - var Confirge = function($source) { var $result = false; @@ -79,12 +63,13 @@ Confirge.readFile = function($file) * @param {object} $vars - Vars to replace, key: value. * @return {object} */ -Confirge.replace = function($obj, $vars) +Confirge.replaceVars = function($obj, $vars) { var $key, $value; - $obj = Confirge($obj); + $obj = Confirge($obj); + $vars = Utils.prepareVars($vars); for($key in $obj) { @@ -99,9 +84,11 @@ Confirge.replace = function($obj, $vars) continue; } - $obj[$key] = varsReplace($value, $vars); + $obj[$key] = Utils.replaceVars($value, $vars); } + console.log($obj); + return $obj; }; diff --git a/lib/utils.js b/lib/utils.js new file mode 100644 index 0000000..01f192c --- /dev/null +++ b/lib/utils.js @@ -0,0 +1,69 @@ +/** + * confirge | lib/utils.js + * file version: 0.00.001 + */ +'use strict'; + +var _ = require('underscore'); + +//////////////////////////////////////////////////////////////////////////////// + +var Utils = +{ + 'needsReplacement': function($str) + { + var $result = /\%([0-9a-zA-Z\-_]+)\%/g.exec($str); + return (!_.isNull($result) ? $result[1] : false); + }, + + 'prepareVar': function($result, $vars, $search, $replace) + { + $result[$search] = + { + 'regexp': new RegExp('%'+ $search +'%', 'g'), + 'replace': $replace + }; + + return $result; + }, + + 'prepareVars': function($vars) + { + var $result = {}, + $regexp; + + for(var $search in $vars) + { + if ($vars.hasOwnProperty($search) && + _.isUndefined($result[$search])) + { + $result = Utils.prepareVar($result, + $vars, + $search, + $vars[$search]); + } + } + + return $result; + }, + + 'replaceVars': function($str, $vars) + { + var $regexp, + $replace; + + for(var $search in $vars) + { + if ($vars.hasOwnProperty($search)) + { + $regexp = new RegExp('%'+ $search +'%', 'g'); + $replace = $vars[$search]; + $str = $str.replace($regexp, $replace); + } + } + + return $str; + } +} + +module.exports = Utils; diff --git a/test/main.js b/test/main.js index 678f94d..9ddf356 100644 --- a/test/main.js +++ b/test/main.js @@ -1,16 +1,26 @@ /** * confirge | test/main.js - * file version: 0.00.001 + * file version: 0.00.002 */ 'use strict'; var Assert = require('assert'), Confirge = require('../lib/index.js'), - Path = require('path'); + Path = require('path'), + Utils = require('../lib/utils.js'); -var OBJ_PLAIN = { 'title': 'test js obj', 'success': true }, - OBJ_YAML = { 'title': 'test yaml', 'success': true }, - OBJ_JSON = { 'title': 'test json', 'success': true }; +var OBJ_PLAIN = { 'title': 'test obj', 'success': true }, + RESULT_YAML = { 'title': 'test yaml', 'success': true }, + RESULT_JSON = { 'title': 'test json', 'success': true }, + + RESULT_VARS = + { + 'var1': + { + 'regexp': new RegExp('%var1%', 'g'), + 'replace': 'value1' + } + }; //////////////////////////////////////////////////////////////////////////////// @@ -30,7 +40,7 @@ describe('Confirge()', function() it('should execute the function, read the file, and return an object', function() { - Assert.deepEqual(OBJ_YAML, Confirge(function() + Assert.deepEqual(RESULT_YAML, Confirge(function() { return getFixtureFile('success.yml'); }) ); @@ -38,7 +48,7 @@ describe('Confirge()', function() it('should execute the function and return an object', function() { - Assert.deepEqual(OBJ_YAML, Confirge(function() + Assert.deepEqual(RESULT_YAML, Confirge(function() { return Confirge.readFile(getFixtureFile('success.yml')); }) ); @@ -58,13 +68,13 @@ describe('Confirge.readFile()', function() it('should read the yaml file and return an object', function() { var $result = Confirge.readFile( getFixtureFile('success.yml') ); - Assert.deepEqual(OBJ_YAML, $result); + Assert.deepEqual(RESULT_YAML, $result); }); it('should read the json file and return an object', function() { var $result = Confirge.readFile( getFixtureFile('success.json') ); - Assert.deepEqual(OBJ_JSON, $result); + Assert.deepEqual(RESULT_JSON, $result); }); it('should fail reading the yaml file', function() @@ -85,3 +95,96 @@ describe('Confirge.readFile()', function() Assert.equal(false, $result); }); }); + +/*describe('Confirge.replaceVars()', function() +{ + it('should replace a string var', function() + { + var $input = { 'str': '%var1%' }, + $output = { 'str': 'value1' }, + $vars = { 'var1': 'value1' }; + + Assert.deepEqual($output, Confirge.replaceVars($input, $vars)) + }); + + it('should replace a string var, twice', function() + { + var $input = { 'str': '%var1% is equal to %var1%' }, + $output = { 'str': 'value1 is equal to value1' }, + $vars = { 'var1': 'value1' }; + + Assert.deepEqual($output, Confirge.replaceVars($input, $vars)) + }); +});*/ + +describe('Utils.needsReplacement()', function() +{ + it('has a var and should be replaced [1]', function() + { + var $result = Utils.needsReplacement('%test%!'); + Assert($result, 'test'); + }); + + it('has a var and should be replaced [2]', function() + { + var $result = Utils.needsReplacement('bla bla.. %test-test%'); + Assert.equal($result, 'test-test'); + }); + + it('has a var and should be replaced [3]', function() + { + var $result = Utils.needsReplacement('jada, %test_test% na na!'); + Assert.equal($result, 'test_test'); + }); + + it('does not have a var and should not be replaced [1]', function() + { + Assert(!Utils.needsReplacement('%test %test')); + }); + + it('does not have a var and should not be replaced [2]', function() + { + Assert(!Utils.needsReplacement('test% test%')); + }); + + it('does not have a var and should not be replaced [3]', function() + { + Assert(!Utils.needsReplacement('% test % test')); + }); +}); + +describe('Utils.prepareVar()', function() +{ + it('should return an object with regexp and replace value', function() + { + Assert.deepEqual(Utils.prepareVar({}, {}, 'var1', 'value1'), RESULT_VARS); + }); +}); + +describe('Utils.prepareVars()', function() +{ + it('should return an object with regexp and replace value', function() + { + Assert.deepEqual(Utils.prepareVars({ 'var1': 'value1' }), RESULT_VARS); + }); + + it('should return an object with regexp and prepared replace values', function() + { + var $input = + { + 'var1': 'value1 %var3%', + 'var2': 'value2', + 'var3': 'value3 %var2%' + }, + + $result = + { + 'var2': { 'regexp': new RegExp('%var2%', 'g'), 'replace': 'value2' }, + 'var3': { 'regexp': new RegExp('%var3%', 'g'), 'replace': 'value3 value2' }, + 'var1': { 'regexp': new RegExp('%var1%', 'g'), 'replace': 'value1 value3 value2' } + }; + + //Assert.deepEqual(Utils.prepareVars($input), $result); + }); +}); +