Skip to content

Commit

Permalink
Adding support for keys such as :main.sub.sub for the patterns - issue
Browse files Browse the repository at this point in the history
  • Loading branch information
jcrawford committed Mar 9, 2018
1 parent aa87775 commit da3e82e
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,12 @@ function replace(pattern, data, options){
var ret = {};

for (var i = 0, key; key = keys[i++];) {
var val = data[key];
var val = '';
if(key.indexOf('.') > -1) {
val = deref(data, key);
} else {
val = data[key];
}
if (val == null) return null;
if (val instanceof Date) {
ret[key] = options.date(val);
Expand All @@ -197,6 +202,13 @@ function replace(pattern, data, options){
return substitute(pattern, ret);
}

function deref(obj, s) {
var i = 0;
s = s.split('.');
while (obj && i < s.length)
obj = obj[s[i++]];
}

/**
* Get the params from a `pattern` string.
*
Expand All @@ -205,10 +217,10 @@ function replace(pattern, data, options){
*/

function params(pattern){
var matcher = /:(\w+)/g;
var matcher = /:(\w)+(.\w+)?/g;
var ret = [];
var m;
while (m = matcher.exec(pattern)) ret.push(m[1]);
while (m = matcher.exec(pattern)) ret.push(m[0]);
return ret;
}

Expand Down

0 comments on commit da3e82e

Please sign in to comment.