From 323a353cfd5bae2254be3e2c302d39888f3ecdbe Mon Sep 17 00:00:00 2001 From: Cedric Kalista Date: Sun, 10 Jan 2016 16:05:48 +0000 Subject: [PATCH 1/4] Add support for tolerant_reader --- lib/read.js | 49 +++++++++++++++++++++++++--- test/parse.js | 22 +++++++++++++ test/variables-tolerant-substitution | 31 ++++++++++++++++++ 3 files changed, 97 insertions(+), 5 deletions(-) create mode 100644 test/variables-tolerant-substitution diff --git a/lib/read.js b/lib/read.js index b9e37df..1d1112d 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,13 +130,44 @@ var namespaceKey = function (o, key, value){ o[n[n.length - 1]] = value; }; +var splitByDotWithVariables = function(section) { + var splittedNamespaces = []; + 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; + splittedNamespaces.push(section.substring(startingDotpointer +1, i + 1)); + } + } else if (currentChar === "." && openedVariables === 0) { + if (startingDotpointer >= 0 && previousChar !== "}") { + splittedNamespaces.push(section.substring(startingDotpointer + 1, i)); + } + startingDotpointer = i; + } + previousChar = currentChar; + } + return splittedNamespaces; +} var namespaceSection = function (o, section){ - var n = section.split ("."); + + + var n = splitByDotWithVariables(section); var str; - + for (var i=0; i Date: Mon, 11 Jan 2016 11:38:51 +0000 Subject: [PATCH 2/4] Add condition to use simple split when possible --- lib/read.js | 42 ++++++++++++++++++++++++------------------ test/parse.js | 4 ++-- 2 files changed, 26 insertions(+), 20 deletions(-) diff --git a/lib/read.js b/lib/read.js index 1d1112d..00c08a1 100644 --- a/lib/read.js +++ b/lib/read.js @@ -131,13 +131,14 @@ var namespaceKey = function (o, key, value){ }; var splitByDotWithVariables = function(section) { - var splittedNamespaces = []; + var splitNamespaces = []; var previousChar; var currentChar; - var startingDotpointer = -1; + 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 === "{") { @@ -147,35 +148,40 @@ var splitByDotWithVariables = function(section) { if (openedVariables == closedVariables) { openedVariables = 0; closedVariables = 0; - splittedNamespaces.push(section.substring(startingDotpointer +1, i + 1)); + splitNamespaces.push(section.substring(startingDotPointer +1, i + 1)); } } else if (currentChar === "." && openedVariables === 0) { - if (startingDotpointer >= 0 && previousChar !== "}") { - splittedNamespaces.push(section.substring(startingDotpointer + 1, i)); + if (startingDotPointer >= 0 && previousChar !== "}") { + splitNamespaces.push(section.substring(startingDotPointer + 1, i)); } - startingDotpointer = i; + startingDotPointer = i; } previousChar = currentChar; } - return splittedNamespaces; + return splitNamespaces; } -var namespaceSection = function (o, section){ +var namespaceSection = function(o, section, tolerant_substitution){ - var n = splitByDotWithVariables(section); - var str; + var n; + if (tolerant_substitution){ + n = splitByDotWithVariables(section); + } else{ + n = section.split("."); + } - for (var i=0; i Date: Mon, 11 Jan 2016 11:47:39 +0000 Subject: [PATCH 3/4] Add documentation --- README.md | 2 ++ 1 file changed, 2 insertions(+) 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_ From 04e03d3c9bcbe78f91b32eb2a6cce444ad62bf9d Mon Sep 17 00:00:00 2001 From: Cedric Kalista Date: Mon, 11 Jan 2016 11:49:46 +0000 Subject: [PATCH 4/4] Fix indentation --- lib/read.js | 43 ++++++++++++++++++++++--------------------- 1 file changed, 22 insertions(+), 21 deletions(-) diff --git a/lib/read.js b/lib/read.js index 00c08a1..889df56 100644 --- a/lib/read.js +++ b/lib/read.js @@ -49,10 +49,10 @@ var expand = function (o, str, options, cb){ }else if (c === "}"){ holder = section !== null ? searchValue (o, section, true) : o; if (!holder){ - if(options.tolerant_substitution) { + if (options.tolerant_substitution){ holder = o; - } else{ - return cb (new Error ("The section \"" + section + "\" does not " + + }else{ + return cb (new Error("The section \"" + section + "\" does not " + "exist")); } } @@ -65,10 +65,10 @@ var expand = function (o, str, options, cb){ : options._vars[key] if (v === undefined){ - if(options.tolerant_substitution) { + if (options.tolerant_substitution){ return cb (null, str); - } else { - return cb(new Error("The property \"" + key + "\" does not " + + }else{ + return cb (new Error("The property \"" + key + "\" does not " + "exist")); } } @@ -130,7 +130,7 @@ var namespaceKey = function (o, key, value){ o[n[n.length - 1]] = value; }; -var splitByDotWithVariables = function(section) { +var splitByDotWithVariables = function (section){ var splitNamespaces = []; var previousChar; var currentChar; @@ -139,19 +139,19 @@ var splitByDotWithVariables = function(section) { var closedVariables = 0; section = "." + section + "."; - for (var i = 0; i < section.length; i++) { + for (var i=0; i= 0 && previousChar !== "}") { + }else if (currentChar === "." && openedVariables === 0){ + if (startingDotPointer >= 0 && previousChar !== "}"){ splitNamespaces.push(section.substring(startingDotPointer + 1, i)); } startingDotPointer = i; @@ -159,25 +159,26 @@ var splitByDotWithVariables = function(section) { previousChar = currentChar; } return splitNamespaces; -} +}; -var namespaceSection = function(o, section, tolerant_substitution){ +var namespaceSection = function (o, section, tolerant_substitution){ var n; if (tolerant_substitution){ n = splitByDotWithVariables(section); - } else{ + }else{ n = section.split("."); } var str; - for (var i = 0; i < n.length; i++){ + + for (var i=0; i