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

Commit

Permalink
Finished replace functions and test cases.
Browse files Browse the repository at this point in the history
  • Loading branch information
roeldev committed Apr 18, 2015
1 parent 4d86cd1 commit a3e91c0
Show file tree
Hide file tree
Showing 3 changed files with 364 additions and 92 deletions.
29 changes: 16 additions & 13 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ var Confirge = function($source)
}
else if (_.isString($source))
{
$result = Confirge.readFile($source);
$result = Confirge.read($source);
}

return $result;
Expand All @@ -40,30 +40,35 @@ var Confirge = function($source)
* @param {string} $file - File path to read.
* @return {object|boolean}
*/
Confirge.readFile = function($file)
Confirge.read = function($file)
{
var $result = false;

try
if (_.isFunction($file))
{
$file = FileSystem.readFileSync($file, 'utf8');
$result = Yaml.safeLoad($file);
$result = Confirge.read($file());
}
catch ($e)
else
{
try
{
$file = FileSystem.readFileSync($file, 'utf8');
$result = Yaml.safeLoad($file);
}
catch ($e)
{
}
}

return $result;
};

/**
* Replace vars in obj values.
*
* @param {object} $obj - Obj where values are checked and replaced.
* @param {object} $vars - Vars to replace, key: value.
* @param {object} $obj - Object where values are checked and replaced.
* @param {object} $vars - Vars to replace, { varName: value }.
* @return {object}
*/
Confirge.replaceVars = function($obj, $vars)
Confirge.replace = function($obj, $vars)
{
var $key,
$value;
Expand All @@ -87,8 +92,6 @@ Confirge.replaceVars = function($obj, $vars)
$obj[$key] = Utils.replaceVars($value, $vars);
}

console.log($obj);

return $obj;
};

Expand Down
140 changes: 118 additions & 22 deletions lib/utils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* confirge | lib/utils.js
* file version: 0.00.001
* file version: 0.00.002
*/
'use strict';

Expand All @@ -10,14 +10,73 @@ var _ = require('underscore');

var Utils =
{
'needsReplacement': function($str)
/**
* Search for replacements and return a array with each replacement string.
* Returns `false` when no replacements are found.
*
* @param {string} $str - The String to search in.
* @return {array|boolean}
*/
'findReplacements': function($str)
{
var $result = /\%([0-9a-zA-Z\-_]+)\%/g.exec($str);
return (!_.isNull($result) ? $result[1] : false);
var $result = {},
$found = false,
$regexp = /\%([0-9a-zA-Z\-_]+)\%/g,
$match;

while (!!($match = $regexp.exec($str)))
{
$result[$match[1]] = true;
$found = true;
}

return ($found ? _.keys($result) : false);
},

'prepareVar': function($result, $vars, $search, $replace)
/**
* Prepares the given var $search. If it's value contains a replacement, it
* will be prepared first and it's value will be replaced for the current
* var.
*
* @param {object} $result - Object with regexps and replaced vars
* @param {object} $vars - Object with all available vars
* @param {string} $search - The var to prepare
* @return {object} Returns the result object so it can be passed down.
*/
'prepareVar': function($result, $vars, $search)
{
var $replace = $vars[$search];
if (_.isUndefined($replace))
{
$result[$search] = false;
return $result;
}

var $replacements = Utils.findReplacements($replace),
$replacement;

if ($replacements !== false)
{
// loop through replacements and prepare them. by doing this we can
// use the result for the current search's replace value
for (var $i = 0, $iL = $replacements.length; $i < $iL; $i++)
{
$replacement = $replacements[$i];
if (_.isUndefined($result[$replacement]))
{
$result = Utils.prepareVar($result, $vars, $replacement);
}

// replace the found var inside the current var's replace value
$replacement = $result[$replacement];
if($replacement !== false)
{
$replace = $replace.replace($replacement.regexp,
$replacement.replace);
}
}
}

$result[$search] =
{
'regexp': new RegExp('%'+ $search +'%', 'g'),
Expand All @@ -27,43 +86,80 @@ var Utils =
return $result;
},

/**
* Prepare all given vars. If the value of a var contains another var, it
* will be processed first. The function will return an object where all
* vars will have a regexp to replace it's value and ofcourse the value
* itself.
*
* @param {object|function} $vars - Object with vars, { varName: value }
* @return {object|boolean}
*/
'prepareVars': function($vars)
{
var $result = {},
$regexp;
if (_.isFunction($vars))
{
return Utils.prepareVars($vars());
}

for(var $search in $vars)
var $result = false;

if (_.isObject($vars) && !_.isArray($vars))
{
if ($vars.hasOwnProperty($search) &&
_.isUndefined($result[$search]))
$result = {};

for(var $search in $vars)
{
$result = Utils.prepareVar($result,
$vars,
$search,
$vars[$search]);
if ($vars.hasOwnProperty($search) &&
_.isUndefined($result[$search]))
{
$result = Utils.prepareVar($result,
$vars,
$search);
}
}
}

return $result;
},

/**
* Replace the vars inside the string.
*
* @param {string} $str - String to search and replace the vars in.
* @param {object} $vars - The prepared vars to use for the replacements.
* @return {string}
*/
'replaceVars': function($str, $vars)
{
var $regexp,
$replace;
if (!_.isObject($vars) || _.isArray($vars) || _.isFunction($vars))
{
return $str;
}

var $replacements = Utils.findReplacements($str),
$replacement,
$var;

for(var $search in $vars)
if ($replacements !== false)
{
if ($vars.hasOwnProperty($search))
// when replacements are found... try to replace them
for (var $i = 0, $iL = $replacements.length; $i < $iL; $i++)
{
$regexp = new RegExp('%'+ $search +'%', 'g');
$replace = $vars[$search];
$str = $str.replace($regexp, $replace);
$replacement = $replacements[$i];
$var = $vars[$replacement];

// check if the replacement is a var inside the prepared vars
// object, only then can we replace the value
if (!_.isUndefined($var))
{
$str = $str.replace($var.regexp, $var.replace);
}
}
}

return $str;
}
}
};

module.exports = Utils;
Loading

0 comments on commit a3e91c0

Please sign in to comment.