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

Commit

Permalink
Started working on util functions
Browse files Browse the repository at this point in the history
  • Loading branch information
roeldev committed Apr 16, 2015
1 parent 7568769 commit 4d86cd1
Show file tree
Hide file tree
Showing 4 changed files with 190 additions and 30 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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',
Expand Down
27 changes: 7 additions & 20 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
{
Expand All @@ -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;
};

Expand Down
69 changes: 69 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
@@ -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;
121 changes: 112 additions & 9 deletions test/main.js
Original file line number Diff line number Diff line change
@@ -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'
}
};

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

Expand All @@ -30,15 +40,15 @@ 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');
}) );
});

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'));
}) );
Expand All @@ -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()
Expand All @@ -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);
});
});

0 comments on commit 4d86cd1

Please sign in to comment.