diff --git a/README.md b/README.md index 924b731..4545d89 100644 --- a/README.md +++ b/README.md @@ -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_ diff --git a/lib/read.js b/lib/read.js index b9e37df..889df56 100644 --- a/lib/read.js +++ b/lib/read.js @@ -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]; @@ -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")); + } } } @@ -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= 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