UNPKG

@smartimpact-it/json-merge-shopify

Version:
49 lines (48 loc) 1.49 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.formatJson = void 0; /** * Format JSONs similarly to how Shopify does it. */ const formatJson = (source, options = {}) => { const defaultOptions = { doubleEscape: true, fixEmptyObjects: true, fixEmptyArrays: true, addNewLineAtEndOfFile: true, tabSize: 2, }; options = { ...defaultOptions, ...options, }; let formatted; // Double escape forward slashes if (options.doubleEscape) { const replacer = (key, value) => { if (typeof value === 'string') { return value.replace(/\//g, '\\=-=-=\\/'); } return value; }; formatted = JSON.stringify(source, replacer, options.tabSize).replaceAll(`\\\\=-=-=\\\\`, '\\'); } else { formatted = JSON.stringify(source, null, options.tabSize); } // Fix empty objects - {} becomes {\n} if (options.fixEmptyObjects) { formatted = formatted.replace(/\n(\s+)([^{\n]+)\{\}/g, '\n$1$2{\n$1}'); } // Fix empty arrays - {} becomes [\n\n] if (options.fixEmptyArrays) { formatted = formatted.replace(/\n(\s+)([^{\n]+)\[\]/g, '\n$1$2[\n\n$1]'); } // Add new line at the end of the file if (options.addNewLineAtEndOfFile) { formatted += '\n'; } return formatted; }; exports.formatJson = formatJson; exports.default = exports.formatJson;