@apigames/json
Advanced tools
Comparing version 1.0.37 to 1.0.38
@@ -17,3 +17,3 @@ export declare function isArray(value: any): boolean; | ||
export declare function clone(value: any, shouldRedactUndefinedValues?: boolean): any; | ||
export declare function stringify(obj: any, indent?: number, linePrefix?: string, quoteFieldNames?: boolean): string; | ||
export declare function stringify(obj: any, indent?: number, linePrefix?: string, quoteFieldNames?: boolean, quoteChar?: string): string; | ||
export declare function extractAndRedact(document: any, propertyName: string): any; |
@@ -125,3 +125,3 @@ "use strict"; | ||
exports.clone = clone; | ||
function stringify(obj, indent = 4, linePrefix = '', quoteFieldNames = true) { | ||
function stringify(obj, indent = 4, linePrefix = '', quoteFieldNames = true, quoteChar = '"') { | ||
const lines = JSON.stringify(obj, null, indent).split('\n'); | ||
@@ -142,2 +142,21 @@ if (linePrefix !== '') { | ||
} | ||
if (quoteChar !== '"') { | ||
for (let i = 1; i < lines.length - 1; i++) { | ||
if ((lines[i].match(/:/g) || []).length > 0) { | ||
const [firstPart, ...subsequentParts] = lines[i].split(':'); | ||
let first = firstPart; | ||
let second = subsequentParts.join(':'); | ||
const namePrefixLength = first.length - first.trimStart().length; | ||
const namePrefix = Array(namePrefixLength).fill(' ').join(''); | ||
first = namePrefix + first.trim().replace(/(^")|(("$)|("(?=,$)))/g, quoteChar); | ||
second = ` ${second.trim().replace(/(^")|(("$)|("(?=,$)))/g, quoteChar)}`; | ||
lines[i] = `${first}:${second}`; | ||
} | ||
else { | ||
const namePrefixLength = lines[i].length - lines[i].trimStart().length; | ||
const namePrefix = Array(namePrefixLength).fill(' ').join(''); | ||
lines[i] = namePrefix + lines[i].trim().replace(/(^")|(("$)|("(?=,$)))/g, quoteChar); | ||
} | ||
} | ||
} | ||
return lines.join('\n'); | ||
@@ -144,0 +163,0 @@ } |
@@ -6,3 +6,3 @@ { | ||
"license": "MIT", | ||
"version": "1.0.37", | ||
"version": "1.0.38", | ||
"main": "lib/index.js", | ||
@@ -9,0 +9,0 @@ "types": "lib/index.d.ts", |
Sorry, the diff of this file is not supported yet
14497
213