-
-
Notifications
You must be signed in to change notification settings - Fork 69
/
format.ts
50 lines (44 loc) · 1.5 KB
/
format.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import {latest} from './reference/latest';
import stringifyPretty from 'json-stringify-pretty-compact';
function sortKeysBy(obj, reference) {
const result = {};
for (const key in reference) {
if (obj[key] !== undefined) {
result[key] = obj[key];
}
}
for (const key in obj) {
if (result[key] === undefined) {
result[key] = obj[key];
}
}
return result;
}
/**
* Format a MapLibre Style. Returns a stringified style with its keys
* sorted in the same order as the reference style.
*
* The optional `space` argument is passed to
* [`JSON.stringify`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify)
* to generate formatted output.
*
* If `space` is unspecified, a default of `2` spaces will be used.
*
* @private
* @param {Object} style a MapLibre Style
* @param {number} [space] space argument to pass to `JSON.stringify`
* @returns {string} stringified formatted JSON
* @example
* var fs = require('fs');
* var format = require('maplibre-gl-style-spec').format;
* var style = fs.readFileSync('./source.json', 'utf8');
* fs.writeFileSync('./dest.json', format(style));
* fs.writeFileSync('./dest.min.json', format(style, 0));
*/
export function format(style, space = 2) {
style = sortKeysBy(style, latest.$root);
if (style.layers) {
style.layers = style.layers.map((layer) => sortKeysBy(layer, latest.layer));
}
return stringifyPretty(style, {indent: space});
}