From e0a28b434e52f576f6d79f8b8bf2b2c2ce31f5ca Mon Sep 17 00:00:00 2001 From: Mike Stegeman Date: Mon, 30 Oct 2017 06:37:16 -0600 Subject: [PATCH] Fix parsing and writing of empty values. Also, fix other parsing issues. --- index.js | 27 +++++++++------------------ 1 file changed, 9 insertions(+), 18 deletions(-) diff --git a/index.js b/index.js index 19ac5c3..c36694d 100644 --- a/index.js +++ b/index.js @@ -36,7 +36,7 @@ function parsePythonConf(str, cb) { if (!line) { return; } - var parts = line.trim().split('='); + var parts = line.split('='); var pykey = parts.shift().trim(); var key = camelCase(pykey); var val = parts.join('=').trim(); @@ -47,13 +47,13 @@ function parsePythonConf(str, cb) { else if ('False' === val) { val = false; } - else if ('None' === val) { + else if ('None' === val || '' === val) { val = null; } else if (/,/.test(val) && !/^"[^"]*"$/.test(val)) { - val = val.split(','); + val = val.split(',').map(function(x) { return x.trim(); }); } - else if (/^[0-9]+$/.test(val)) { + else if (/^-?[0-9]+$/.test(val)) { val = parseInt(val, 10); } @@ -91,7 +91,7 @@ function toPyVal(val) { else if (Array.isArray(val)) { val = val.join(','); if (-1 === val.indexOf(',')) { - val += ','; // disambguates value from array with one element + val += ','; // disambiguates value from array with one element } return val; } @@ -119,9 +119,10 @@ function stringifyPythonConf(obj, cb) { if ('undefined' === typeof pyval) { if ('number' === typeof num) { - obj.__lines[num] = "___DELETE_ME___"; + pyval = ''; + } else { + return; } - return; } if ('number' !== typeof num) { @@ -145,17 +146,7 @@ function stringifyPythonConf(obj, cb) { comment = obj.__lines[num].replace(/.*?(\s*#.*)/, '$1'); } - if ('undefined' === typeof pyval) { - obj.__lines[num] = "___DELETE_ME___"; - } else { - obj.__lines[num] = pykey + ' = ' + pyval + comment; - } - }); - - obj.__lines = obj.__lines.filter(function (line) { - if ("___DELETE_ME___" !== line) { - return true; - } + obj.__lines[num] = pykey + ' = ' + pyval + comment; }); if ('string' === typeof endline) {