"use strict";Object.defineProperty(exports, "__esModule", {value: true});// src/sortJsonc.ts var _commentjson = require('comment-json'); function sortJsonc(jsoncString, options) { const { parseReviver, removeComments, spaces, sort } = options || {}; const parsed = _commentjson.parse.call(void 0, jsoncString, parseReviver || void 0, removeComments || void 0); const sorted = sortDeepWithSymbols(parsed, getCompareFn(sort)); return _commentjson.stringify.call(void 0, sorted, parseReviver || void 0, spaces || 2); } function getCompareFn(sortOption) { if (Array.isArray(sortOption)) { return createOrderCompareFn(sortOption); } if (typeof sortOption === "function") { return sortOption; } return createIntlCompareFn(); } function sortDeepWithSymbols(initial, compareFn) { const result = { sorted: initial }; const stack = [[result, "sorted"]]; while (stack.length) { const [parent, keyOnParent] = stack.shift(); const current = parent[keyOnParent]; const sorted = Array.isArray(current) ? [] : {}; const keys = []; for (const key of Reflect.ownKeys(current)) { const value = current[key]; if (typeof key === "symbol") { sorted[key] = value; continue; } keys.push(key); } if (!Array.isArray(current)) { keys.sort(compareFn); } for (const key of keys) { const value = current[key]; sorted[key] = value; if (typeof value === "object" && value !== null) { stack.push([sorted, key]); } } parent[keyOnParent] = sorted; } return result.sorted; } function createIntlCompareFn() { return new Intl.Collator("en").compare; } function createOrderCompareFn(order) { const intlCompare = createIntlCompareFn(); const orderMap = new Map(order.map((key, index) => [key, index + 1])); const max = order.length + 1; return (a, b) => { const aWeight = orderMap.get(a) || max; const bWeight = orderMap.get(b) || max; if (aWeight === bWeight) { return intlCompare(a, b); } return aWeight - bWeight; }; } exports.sortJsonc = sortJsonc; //# sourceMappingURL=index.cjs.map