MCPcopy Index your code
hub / github.com/json5/json5 / quoteString

Function quoteString

lib/stringify.js:104–162  ·  view source on GitHub ↗
(value)

Source from the content-addressed store, hash-verified

102 }
103
104 function quoteString (value) {
105 const quotes = {
106 "'": 0.1,
107 '"': 0.2,
108 }
109
110 const replacements = {
111 "'": "\\'",
112 '"': '\\"',
113 '\\': '\\\\',
114 '\b': '\\b',
115 '\f': '\\f',
116 '\n': '\\n',
117 '\r': '\\r',
118 '\t': '\\t',
119 '\v': '\\v',
120 '\0': '\\0',
121 '\u2028': '\\u2028',
122 '\u2029': '\\u2029',
123 }
124
125 let product = ''
126
127 for (let i = 0; i < value.length; i++) {
128 const c = value[i]
129 switch (c) {
130 case "'":
131 case '"':
132 quotes[c]++
133 product += c
134 continue
135
136 case '\0':
137 if (util.isDigit(value[i + 1])) {
138 product += '\\x00'
139 continue
140 }
141 }
142
143 if (replacements[c]) {
144 product += replacements[c]
145 continue
146 }
147
148 if (c < ' ') {
149 let hexString = c.charCodeAt(0).toString(16)
150 product += '\\x' + ('00' + hexString).substring(hexString.length)
151 continue
152 }
153
154 product += c
155 }
156
157 const quoteChar = quote || Object.keys(quotes).reduce((a, b) => (quotes[a] < quotes[b]) ? a : b)
158
159 product = product.replace(new RegExp(quoteChar, 'g'), replacements[quoteChar])
160
161 return quoteChar + product + quoteChar

Callers 2

serializePropertyFunction · 0.85
serializeKeyFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…