From 9cb6902f80422d3c193c5949b036f9adbe29fdbe Mon Sep 17 00:00:00 2001 From: Vladimir Agafonkin Date: Thu, 26 Jul 2018 15:51:09 +0300 Subject: [PATCH] produce nicer style formatting --- src/style-spec/format.js | 10 ++++------ src/style-spec/package.json | 1 + 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/src/style-spec/format.js b/src/style-spec/format.js index 3a9b27e776c..5f73534fbc4 100644 --- a/src/style-spec/format.js +++ b/src/style-spec/format.js @@ -1,6 +1,7 @@ import reference from './reference/latest.js'; import sortObject from 'sort-object'; +import stringifyPretty from 'json-stringify-pretty-compact'; function sameOrderAs(reference) { const keyOrder = {}; @@ -38,17 +39,14 @@ function sameOrderAs(reference) { * fs.writeFileSync('./dest.json', format(style)); * fs.writeFileSync('./dest.min.json', format(style, 0)); */ -function format(style, space) { - if (space === undefined) space = 2; +function format(style, space = 2) { style = sortObject(style, sameOrderAs(reference.$root)); if (style.layers) { - style.layers = style.layers.map((layer) => { - return sortObject(layer, sameOrderAs(reference.layer)); - }); + style.layers = style.layers.map((layer) => sortObject(layer, sameOrderAs(reference.layer))); } - return JSON.stringify(style, null, space); + return stringifyPretty(style, {indent: space}); } export default format; diff --git a/src/style-spec/package.json b/src/style-spec/package.json index c2e950fc568..c4e32b69a1d 100644 --- a/src/style-spec/package.json +++ b/src/style-spec/package.json @@ -30,6 +30,7 @@ "@mapbox/jsonlint-lines-primitives": "~2.0.2", "@mapbox/unitbezier": "^0.0.0", "csscolorparser": "~1.0.2", + "json-stringify-pretty-compact": "^1.2.0", "minimist": "0.0.8", "rw": "^1.3.3", "sort-object": "^0.3.2"