diff --git a/lib/hjson-stringify.js b/lib/hjson-stringify.js index 20d39cc..95cc2c7 100644 --- a/lib/hjson-stringify.js +++ b/lib/hjson-stringify.js @@ -36,6 +36,7 @@ module.exports = function(data, opt) { var separator = ''; // comma separator var dsfDef = null; var sortProps = false; + var sortOnlyRoot = true; var token = plainToken; if (opt && typeof opt === 'object') { @@ -52,6 +53,7 @@ module.exports = function(data, opt) { separator = opt.separator === true ? token.com[0] : ''; dsfDef = opt.dsf; sortProps = opt.sortProps; + sortOnlyRoot = opt.sortOnlyRoot === false ? false : true; // If the space parameter is a number, make an indent string containing that // many spaces. If it is a string, it will be used as the indent string. @@ -205,6 +207,9 @@ module.exports = function(data, opt) { str = common.forceComment(str); var i, len = str.length; for (i = 0; i < len && str[i] <= ' '; i++) {} + if (trim) { + str = str.replace(/\s{0,}$/,""); + } if (trim && i > 0) str = str.substr(i); if (i < len) return prefix + wrap(token.rem, str); else return str; @@ -269,13 +274,22 @@ module.exports = function(data, opt) { if (comments) { c = comments.a[i]||[]; ca = commentOnThisLine(c[1]); - partial.push(makeComment(c[0], "\n") + eolGap); + // partial.push(makeComment(c[0], "\n") + eolGap); if (cpartial && (c[0] || c[1] || ca)) cpartial = null; } else partial.push(eolGap); wrapLen = 0; v = value[i]; - partial.push(str(v, comments ? ca : false, true) + (setsep ? separator : '')); + const partLine = str(v, comments ? ca : false, true) + (setsep ? separator : ''); + if(partial.length > 0){ + if(partLine.startsWith('{')){ + partial.push('\n' + gap + partLine); + }else{ + partial.push(' ' + partLine); + } + }else{ + partial.push(partLine); + } if (cpartial) { // prepare the condensed version switch (typeof v) { @@ -298,11 +312,11 @@ module.exports = function(data, opt) { // when empty if (comments && comments.e) partial.push(makeComment(comments.e[0], "\n") + eolMind); } - else partial.push(eolMind); + // else partial.push(eolMind); // Join all of the elements together, separated with newline, and wrap them in // brackets. - + if (partial.length === 0) res = wrap(token.arr, ''); else { res = prefix + wrap(token.arr, partial.join('')); @@ -320,18 +334,23 @@ module.exports = function(data, opt) { if (Object.prototype.hasOwnProperty.call(value, k) && commentKeys.indexOf(k) < 0) objectKeys.push(k); } + var keys = commentKeys.concat(objectKeys); if(sortProps) { - objectKeys.sort(); + if(sortOnlyRoot){ + if(isRootObject){ + keys.sort(); + } + }else{ + keys.sort(); + } } - var keys = commentKeys.concat(objectKeys); - for (i = 0, length = keys.length; i < length; i++) { setsep = i < length - 1; k = keys[i]; if (comments) { c = comments.c[k]||[]; ca = commentOnThisLine(c[1]); - partial.push(makeComment(c[0], "\n") + eolGap); + partial.push(makeComment(c[0], "\n" + gap, true) + eolGap); if (cpartial && (c[0] || c[1] || ca)) cpartial = null; } else partial.push(eolGap); diff --git a/package.json b/package.json index 85ea15d..b14f1d0 100644 --- a/package.json +++ b/package.json @@ -4,6 +4,7 @@ "main": "./lib/hjson.js", "author": "Christian Zangl", "version": "3.2.1", + "type": "module", "keywords": [ "json", "comments",