Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: sortProps not working #51

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 27 additions & 8 deletions lib/hjson-stringify.js
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand All @@ -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.
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -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(''));
Expand All @@ -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);
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"main": "./lib/hjson.js",
"author": "Christian Zangl",
"version": "3.2.1",
"type": "module",
"keywords": [
"json",
"comments",
Expand Down