From 4992c861442545fe0176a0aee373754e1b5d5cfb Mon Sep 17 00:00:00 2001 From: GoonNahm Date: Fri, 5 Jun 2020 15:18:11 +0900 Subject: [PATCH 1/5] fix: sortProps not working I tried to use sortProps option to sort properties, but it didn't work. So I've changed code as the commit to make it work. Please review my changes, if something more needed, just let me know. Regards. --- lib/hjson-stringify.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/hjson-stringify.js b/lib/hjson-stringify.js index 20d39cc..b2a658f 100644 --- a/lib/hjson-stringify.js +++ b/lib/hjson-stringify.js @@ -320,10 +320,11 @@ 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(); + keys.sort(); } - var keys = commentKeys.concat(objectKeys); for (i = 0, length = keys.length; i < length; i++) { setsep = i < length - 1; From 1691d0e46b403aaf2a9365884b8d5807d076b9ce Mon Sep 17 00:00:00 2001 From: GoonNahm Date: Fri, 5 Jun 2020 15:24:48 +0900 Subject: [PATCH 2/5] feat: add sortOnlyRoot option Add sortOnlyRoot option to stringify function to allow restrict sorting only root properties. --- lib/hjson-stringify.js | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/hjson-stringify.js b/lib/hjson-stringify.js index b2a658f..fdde682 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. @@ -320,10 +322,15 @@ 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) { - keys.sort(); + if(sortOnlyRoot){ + if(isRootObject){ + keys.sort(); + } + }else{ + keys.sort(); + } } for (i = 0, length = keys.length; i < length; i++) { From 871cce1514e218a885c4785ab5e5c14160044578 Mon Sep 17 00:00:00 2001 From: GoonNahm Date: Fri, 5 Jun 2020 18:48:13 +0900 Subject: [PATCH 3/5] fix: add gap prefix to and remove trailing spaces from comment on stringify function --- lib/hjson-stringify.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/hjson-stringify.js b/lib/hjson-stringify.js index fdde682..3518d56 100644 --- a/lib/hjson-stringify.js +++ b/lib/hjson-stringify.js @@ -207,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; @@ -332,14 +335,13 @@ module.exports = function(data, opt) { keys.sort(); } } - 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); From ed5057e501f443798c799594b7372459ca227adf Mon Sep 17 00:00:00 2001 From: GoonNahm Date: Wed, 18 May 2022 21:28:28 +0900 Subject: [PATCH 4/5] =?UTF-8?q?fix(stringify):=20=EB=B0=B0=EC=97=B4?= =?UTF-8?q?=EC=9D=98=20=EA=B0=9C=EC=B2=B4=EB=93=A4=EC=9D=B4=20=EB=AC=B4?= =?UTF-8?q?=EC=A1=B0=EA=B1=B4=20new=20line=20=EC=9C=BC=EB=A1=9C=20?= =?UTF-8?q?=EC=8B=9C=EC=9E=91=ED=95=98=EB=8D=98=EA=B2=83=EC=9D=84=20?= =?UTF-8?q?=EC=98=A4=EB=B8=8C=EC=A0=9D=ED=8A=B8=EC=9D=BC=20=EA=B2=BD?= =?UTF-8?q?=EC=9A=B0=EB=A1=9C=EB=A7=8C=20=EC=A0=9C=ED=95=9C=ED=95=A8.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/hjson-stringify.js | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/lib/hjson-stringify.js b/lib/hjson-stringify.js index 3518d56..95cc2c7 100644 --- a/lib/hjson-stringify.js +++ b/lib/hjson-stringify.js @@ -274,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) { @@ -303,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('')); From ebf9b1ec0d2bb4e4d0457cdf23cc4bf6edbabc1f Mon Sep 17 00:00:00 2001 From: GoonNahm Seo Date: Wed, 22 Jan 2025 13:54:15 +0900 Subject: [PATCH 5/5] chore: try to support importing as esm module --- package.json | 1 + 1 file changed, 1 insertion(+) 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",