Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,8 @@ Options:
Parses dot separated keys as JavaScript objects. Look at the [namespaces](#namespaces) section for further details.
- __variables__ - _Boolean_
Allows you to read the value of a key while the file is being parsed. Look at the [variables](#variables) section for further details.
- __tolerant_substitution__ - _Boolean_
This option can be used with the `variables` option. if true, it allows properties to be partially parsed instead of returning an error when a variable is not set.
- __vars__ - _Boolean_
External variables can be passed to the file if the variables option is enabled. Look at the [variables](#variables) section for further details.
- __include__ - _Boolean_
Expand Down
62 changes: 54 additions & 8 deletions lib/read.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,12 @@ var expand = function (o, str, options, cb){
}else if (c === "}"){
holder = section !== null ? searchValue (o, section, true) : o;
if (!holder){
return cb (new Error ("The section \"" + section + "\" does not " +
if (options.tolerant_substitution){
holder = o;
}else{
return cb (new Error("The section \"" + section + "\" does not " +
"exist"));
}
}

v = options.namespaces ? searchValue (holder, key) : holder[key];
Expand All @@ -61,8 +65,12 @@ var expand = function (o, str, options, cb){
: options._vars[key]

if (v === undefined){
return cb (new Error ("The property \"" + key + "\" does not " +
if (options.tolerant_substitution){
return cb (null, str);
}else{
return cb (new Error("The property \"" + key + "\" does not " +
"exist"));
}
}
}

Expand Down Expand Up @@ -122,10 +130,48 @@ var namespaceKey = function (o, key, value){
o[n[n.length - 1]] = value;
};

var namespaceSection = function (o, section){
var n = section.split (".");
var splitByDotWithVariables = function (section){
var splitNamespaces = [];
var previousChar;
var currentChar;
var startingDotPointer = -1;
var openedVariables = 0;
var closedVariables = 0;
section = "." + section + ".";

for (var i=0; i<section.length; i++){
currentChar = section[i];
if (previousChar === "$" && currentChar === "{"){
openedVariables++;
}else if (currentChar === "}"){
closedVariables++;
if (openedVariables == closedVariables){
openedVariables = 0;
closedVariables = 0;
splitNamespaces.push(section.substring(startingDotPointer + 1, i + 1));
}
}else if (currentChar === "." && openedVariables === 0){
if (startingDotPointer >= 0 && previousChar !== "}"){
splitNamespaces.push(section.substring(startingDotPointer + 1, i));
}
startingDotPointer = i;
}
previousChar = currentChar;
}
return splitNamespaces;
};

var namespaceSection = function (o, section, tolerant_substitution){

var n;
if (tolerant_substitution){
n = splitByDotWithVariables(section);
}else{
n = section.split(".");
}

var str;

for (var i=0; i<n.length; i++){
str = n[i];
if (o[str] === undefined){
Expand All @@ -136,7 +182,7 @@ var namespaceSection = function (o, section){
}
o = o[str];
}

return o;
};

Expand Down Expand Up @@ -328,7 +374,7 @@ var build = function (data, options, dirname, cb){
if (add){
if (options.namespaces){
try{
currentSection = namespaceSection (n, section);
currentSection = namespaceSection (n, section, options.tolerant_substitution);
}catch (error){
abort (error);
}
Expand All @@ -344,7 +390,7 @@ var build = function (data, options, dirname, cb){
currentSectionStr = section;
if (options.namespaces){
try{
currentSection = namespaceSection (n, section);
currentSection = namespaceSection (n, section, options.tolerant_substitution);
}catch (error){
abort (error);
}
Expand Down
22 changes: 22 additions & 0 deletions test/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,28 @@ var tests = {
done ();
});
},
"variables supporting tolerant substitution": function(done){
var options = {variables: true, path: true, sections: true, namespaces: true, tolerant_substitution: true};

properties.parse("variables-tolerant-substitution", options, function(error, p){
assert.ifError(error);

assert.deepEqual(p, {
"a": 1,
"b": 1,
"foo": "${bar}",
"section": {"c": 3, "d": 3, "e": "${section_foo|bar}"},
"s1": {"e2": "ee"},
"s${bar}": {"e2": "ee"},
"namespace": {"a": {"b": "c"}},
"c": {"a": {"b": {"x": 1}, "e": 1}},
"${namespace|a.b.bar}": {"a": {"b": {"x": 1}}},
"${namespace|a.${h}.bar}": {"afgd": {"bsada": {"${bar}": {"foo": {"bar": "foo"}}}}}
});

done();
});
},
"namespaces": function (done){
var options = { path: true, variables: true, namespaces: true };

Expand Down
31 changes: 31 additions & 0 deletions test/variables-tolerant-substitution
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#simple substitution
a = 1
b = ${a}
foo=${bar}

#section substitution
[section]
c = 3
d = ${section|c}
e = ${section_foo|bar}

[s${a}]
e2 = ee

[s${bar}]
e2 = ee

#namespace substitution
[namespace]
a.b c

[${namespace|a.b}.a]
b.x 1

e ${c.a|b.x}

[${namespace|a.b.bar}.a]
b.x 1

[${namespace|a.${h}.bar}.afgd.bsada.${bar}.foo]
bar=foo