|
| 1 | +// Closely based on v5.0.0 of stringify-object (BSD-2-Clause licensed), with |
| 2 | +// modifications to simplify it for our use case (simpler obj & regex checks |
| 3 | +// + object key retrieval) and no awkwardly incompatible ESM. |
| 4 | + |
| 5 | +module.exports = (input, options, pad) => { |
| 6 | + const seen = [] |
| 7 | + |
| 8 | + return (function stringify (input, options = {}, pad = '') { |
| 9 | + const indent = options.indent || '\t' |
| 10 | + |
| 11 | + let tokens |
| 12 | + if (options.inlineCharacterLimit === undefined) { |
| 13 | + tokens = { |
| 14 | + newline: '\n', |
| 15 | + newlineOrSpace: '\n', |
| 16 | + pad, |
| 17 | + indent: pad + indent |
| 18 | + } |
| 19 | + } else { |
| 20 | + tokens = { |
| 21 | + newline: '@@__STRINGIFY_OBJECT_NEW_LINE__@@', |
| 22 | + newlineOrSpace: '@@__STRINGIFY_OBJECT_NEW_LINE_OR_SPACE__@@', |
| 23 | + pad: '@@__STRINGIFY_OBJECT_PAD__@@', |
| 24 | + indent: '@@__STRINGIFY_OBJECT_INDENT__@@' |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + const expandWhiteSpace = string => { |
| 29 | + if (options.inlineCharacterLimit === undefined) { |
| 30 | + return string |
| 31 | + } |
| 32 | + |
| 33 | + const oneLined = string |
| 34 | + .replace(new RegExp(tokens.newline, 'g'), '') |
| 35 | + .replace(new RegExp(tokens.newlineOrSpace, 'g'), ' ') |
| 36 | + .replace(new RegExp(tokens.pad + '|' + tokens.indent, 'g'), '') |
| 37 | + |
| 38 | + if (oneLined.length <= options.inlineCharacterLimit) { |
| 39 | + return oneLined |
| 40 | + } |
| 41 | + |
| 42 | + return string |
| 43 | + .replace(new RegExp(tokens.newline + '|' + tokens.newlineOrSpace, 'g'), '\n') |
| 44 | + .replace(new RegExp(tokens.pad, 'g'), pad) |
| 45 | + .replace(new RegExp(tokens.indent, 'g'), pad + indent) |
| 46 | + } |
| 47 | + |
| 48 | + if (seen.includes(input)) { |
| 49 | + return '"[Circular]"' |
| 50 | + } |
| 51 | + |
| 52 | + if ( |
| 53 | + input === null || |
| 54 | + input === undefined || |
| 55 | + typeof input === 'number' || |
| 56 | + typeof input === 'boolean' || |
| 57 | + typeof input === 'function' || |
| 58 | + typeof input === 'symbol' || |
| 59 | + input instanceof RegExp |
| 60 | + ) { |
| 61 | + return String(input) |
| 62 | + } |
| 63 | + |
| 64 | + if (input instanceof Date) { |
| 65 | + return `new Date('${input.toISOString()}')` |
| 66 | + } |
| 67 | + |
| 68 | + if (Array.isArray(input)) { |
| 69 | + if (input.length === 0) { |
| 70 | + return '[]' |
| 71 | + } |
| 72 | + |
| 73 | + seen.push(input) |
| 74 | + |
| 75 | + const returnValue = '[' + tokens.newline + input.map((element, i) => { |
| 76 | + const eol = input.length - 1 === i ? tokens.newline : ',' + tokens.newlineOrSpace |
| 77 | + |
| 78 | + let value = stringify(element, options, pad + indent) |
| 79 | + if (options.transform) { |
| 80 | + value = options.transform(input, i, value) |
| 81 | + } |
| 82 | + |
| 83 | + return tokens.indent + value + eol |
| 84 | + }).join('') + tokens.pad + ']' |
| 85 | + |
| 86 | + seen.pop() |
| 87 | + |
| 88 | + return expandWhiteSpace(returnValue) |
| 89 | + } |
| 90 | + |
| 91 | + if (typeof input === 'object') { |
| 92 | + let objectKeys = Object.keys(input) |
| 93 | + |
| 94 | + if (options.filter) { |
| 95 | + objectKeys = objectKeys.filter(element => options.filter(input, element)) |
| 96 | + } |
| 97 | + |
| 98 | + if (objectKeys.length === 0) { |
| 99 | + return '{}' |
| 100 | + } |
| 101 | + |
| 102 | + seen.push(input) |
| 103 | + |
| 104 | + const returnValue = '{' + tokens.newline + objectKeys.map((element, index) => { |
| 105 | + const eol = objectKeys.length - 1 === index ? tokens.newline : ',' + tokens.newlineOrSpace |
| 106 | + const isSymbol = typeof element === 'symbol' |
| 107 | + const isClassic = !isSymbol && /^[a-z$_][$\w]*$/i.test(element) |
| 108 | + const key = isSymbol || isClassic ? element : stringify(element, options) |
| 109 | + |
| 110 | + let value = stringify(input[element], options, pad + indent) |
| 111 | + if (options.transform) { |
| 112 | + value = options.transform(input, element, value) |
| 113 | + } |
| 114 | + |
| 115 | + return tokens.indent + String(key) + ': ' + value + eol |
| 116 | + }).join('') + tokens.pad + '}' |
| 117 | + |
| 118 | + seen.pop() |
| 119 | + |
| 120 | + return expandWhiteSpace(returnValue) |
| 121 | + } |
| 122 | + |
| 123 | + input = input.replace(/\\/g, '\\\\') |
| 124 | + input = String(input).replace(/[\r\n]/g, x => x === '\n' ? '\\n' : '\\r') |
| 125 | + |
| 126 | + if (options.singleQuotes === false) { |
| 127 | + input = input.replace(/"/g, '\\"') |
| 128 | + return `"${input}"` |
| 129 | + } |
| 130 | + |
| 131 | + input = input.replace(/'/g, '\\\'') |
| 132 | + return `'${input}'` |
| 133 | + })(input, options, pad) |
| 134 | +} |
0 commit comments