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

Commit

Permalink
Added support for deep (multi level) objects and arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
roeldev committed Apr 19, 2015
1 parent 18521de commit 293ff06
Show file tree
Hide file tree
Showing 4 changed files with 256 additions and 37 deletions.
9 changes: 7 additions & 2 deletions gulpfile.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* confirge | gulpfile.js
* file version: 0.00.001
* file version: 0.00.002
*/
'use strict';

Expand All @@ -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()
Expand All @@ -40,3 +40,8 @@ Gulp.task('watch', function()
{
Gulp.watch(JS_SRC, ['dev']);
});

Gulp.task('watch:lint', function()
{
Gulp.watch(JS_SRC, ['lint']);
});
34 changes: 12 additions & 22 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* confirge | lib/index.js
* file version: 0.00.002
* file version: 0.00.003
*/
'use strict';

Expand Down Expand Up @@ -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
Expand Down
79 changes: 71 additions & 8 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.002
* file version: 0.00.003
*/
'use strict';

Expand All @@ -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)))
Expand Down Expand Up @@ -108,7 +108,7 @@ var Utils =
{
$result = {};

for(var $search in $vars)
for (var $search in $vars)
{
if ($vars.hasOwnProperty($search) &&
_.isUndefined($result[$search]))
Expand All @@ -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;
Expand All @@ -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;
}
};

Expand Down

0 comments on commit 293ff06

Please sign in to comment.