-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathstringify-js-object.js
More file actions
134 lines (105 loc) · 3.79 KB
/
stringify-js-object.js
File metadata and controls
134 lines (105 loc) · 3.79 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// Closely based on v5.0.0 of stringify-object (BSD-2-Clause licensed), with
// modifications to simplify it for our use case (simpler obj & regex checks
// + object key retrieval) and no awkwardly incompatible ESM.
module.exports = (input, options, pad) => {
const seen = []
return (function stringify (input, options = {}, pad = '') {
const indent = options.indent || '\t'
let tokens
if (options.inlineCharacterLimit === undefined) {
tokens = {
newline: '\n',
newlineOrSpace: '\n',
pad,
indent: pad + indent
}
} else {
tokens = {
newline: '@@__STRINGIFY_OBJECT_NEW_LINE__@@',
newlineOrSpace: '@@__STRINGIFY_OBJECT_NEW_LINE_OR_SPACE__@@',
pad: '@@__STRINGIFY_OBJECT_PAD__@@',
indent: '@@__STRINGIFY_OBJECT_INDENT__@@'
}
}
const expandWhiteSpace = string => {
if (options.inlineCharacterLimit === undefined) {
return string
}
const oneLined = string
.replace(new RegExp(tokens.newline, 'g'), '')
.replace(new RegExp(tokens.newlineOrSpace, 'g'), ' ')
.replace(new RegExp(tokens.pad + '|' + tokens.indent, 'g'), '')
if (oneLined.length <= options.inlineCharacterLimit) {
return oneLined
}
return string
.replace(new RegExp(tokens.newline + '|' + tokens.newlineOrSpace, 'g'), '\n')
.replace(new RegExp(tokens.pad, 'g'), pad)
.replace(new RegExp(tokens.indent, 'g'), pad + indent)
}
if (seen.includes(input)) {
return '"[Circular]"'
}
if (
input === null ||
input === undefined ||
typeof input === 'number' ||
typeof input === 'boolean' ||
typeof input === 'function' ||
typeof input === 'symbol' ||
input instanceof RegExp
) {
return String(input)
}
if (input instanceof Date) {
return `new Date('${input.toISOString()}')`
}
if (Array.isArray(input)) {
if (input.length === 0) {
return '[]'
}
seen.push(input)
const returnValue = '[' + tokens.newline + input.map((element, i) => {
const eol = input.length - 1 === i ? tokens.newline : ',' + tokens.newlineOrSpace
let value = stringify(element, options, pad + indent)
if (options.transform) {
value = options.transform(input, i, value)
}
return tokens.indent + value + eol
}).join('') + tokens.pad + ']'
seen.pop()
return expandWhiteSpace(returnValue)
}
if (typeof input === 'object') {
let objectKeys = Object.keys(input)
if (options.filter) {
objectKeys = objectKeys.filter(element => options.filter(input, element))
}
if (objectKeys.length === 0) {
return '{}'
}
seen.push(input)
const returnValue = '{' + tokens.newline + objectKeys.map((element, index) => {
const eol = objectKeys.length - 1 === index ? tokens.newline : ',' + tokens.newlineOrSpace
const isSymbol = typeof element === 'symbol'
const isClassic = !isSymbol && /^[a-z$_][$\w]*$/i.test(element)
const key = isSymbol || isClassic ? element : stringify(element, options)
let value = stringify(input[element], options, pad + indent)
if (options.transform) {
value = options.transform(input, element, value)
}
return tokens.indent + String(key) + ': ' + value + eol
}).join('') + tokens.pad + '}'
seen.pop()
return expandWhiteSpace(returnValue)
}
input = input.replace(/\\/g, '\\\\')
input = String(input).replace(/[\r\n]/g, x => x === '\n' ? '\\n' : '\\r')
if (options.singleQuotes === false) {
input = input.replace(/"/g, '\\"')
return `"${input}"`
}
input = input.replace(/'/g, '\\\'')
return `'${input}'`
})(input, options, pad)
}