stringify.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. const util = require('./util')
  2. module.exports = function stringify (value, replacer, space) {
  3. const stack = []
  4. let indent = ''
  5. let propertyList
  6. let replacerFunc
  7. let gap = ''
  8. let quote
  9. if (
  10. replacer != null &&
  11. typeof replacer === 'object' &&
  12. !Array.isArray(replacer)
  13. ) {
  14. space = replacer.space
  15. quote = replacer.quote
  16. replacer = replacer.replacer
  17. }
  18. if (typeof replacer === 'function') {
  19. replacerFunc = replacer
  20. } else if (Array.isArray(replacer)) {
  21. propertyList = []
  22. for (const v of replacer) {
  23. let item
  24. if (typeof v === 'string') {
  25. item = v
  26. } else if (
  27. typeof v === 'number' ||
  28. v instanceof String ||
  29. v instanceof Number
  30. ) {
  31. item = String(v)
  32. }
  33. if (item !== undefined && propertyList.indexOf(item) < 0) {
  34. propertyList.push(item)
  35. }
  36. }
  37. }
  38. if (space instanceof Number) {
  39. space = Number(space)
  40. } else if (space instanceof String) {
  41. space = String(space)
  42. }
  43. if (typeof space === 'number') {
  44. if (space > 0) {
  45. space = Math.min(10, Math.floor(space))
  46. gap = ' '.substr(0, space)
  47. }
  48. } else if (typeof space === 'string') {
  49. gap = space.substr(0, 10)
  50. }
  51. return serializeProperty('', {'': value})
  52. function serializeProperty (key, holder) {
  53. let value = holder[key]
  54. if (value != null) {
  55. if (typeof value.toJSON5 === 'function') {
  56. value = value.toJSON5(key)
  57. } else if (typeof value.toJSON === 'function') {
  58. value = value.toJSON(key)
  59. }
  60. }
  61. if (replacerFunc) {
  62. value = replacerFunc.call(holder, key, value)
  63. }
  64. if (value instanceof Number) {
  65. value = Number(value)
  66. } else if (value instanceof String) {
  67. value = String(value)
  68. } else if (value instanceof Boolean) {
  69. value = value.valueOf()
  70. }
  71. switch (value) {
  72. case null: return 'null'
  73. case true: return 'true'
  74. case false: return 'false'
  75. }
  76. if (typeof value === 'string') {
  77. return quoteString(value, false)
  78. }
  79. if (typeof value === 'number') {
  80. return String(value)
  81. }
  82. if (typeof value === 'object') {
  83. return Array.isArray(value) ? serializeArray(value) : serializeObject(value)
  84. }
  85. return undefined
  86. }
  87. function quoteString (value) {
  88. const quotes = {
  89. "'": 0.1,
  90. '"': 0.2,
  91. }
  92. const replacements = {
  93. "'": "\\'",
  94. '"': '\\"',
  95. '\\': '\\\\',
  96. '\b': '\\b',
  97. '\f': '\\f',
  98. '\n': '\\n',
  99. '\r': '\\r',
  100. '\t': '\\t',
  101. '\v': '\\v',
  102. '\0': '\\0',
  103. '\u2028': '\\u2028',
  104. '\u2029': '\\u2029',
  105. }
  106. let product = ''
  107. for (let i = 0; i < value.length; i++) {
  108. const c = value[i]
  109. switch (c) {
  110. case "'":
  111. case '"':
  112. quotes[c]++
  113. product += c
  114. continue
  115. case '\0':
  116. if (util.isDigit(value[i + 1])) {
  117. product += '\\x00'
  118. continue
  119. }
  120. }
  121. if (replacements[c]) {
  122. product += replacements[c]
  123. continue
  124. }
  125. if (c < ' ') {
  126. let hexString = c.charCodeAt(0).toString(16)
  127. product += '\\x' + ('00' + hexString).substring(hexString.length)
  128. continue
  129. }
  130. product += c
  131. }
  132. const quoteChar = quote || Object.keys(quotes).reduce((a, b) => (quotes[a] < quotes[b]) ? a : b)
  133. product = product.replace(new RegExp(quoteChar, 'g'), replacements[quoteChar])
  134. return quoteChar + product + quoteChar
  135. }
  136. function serializeObject (value) {
  137. if (stack.indexOf(value) >= 0) {
  138. throw TypeError('Converting circular structure to JSON5')
  139. }
  140. stack.push(value)
  141. let stepback = indent
  142. indent = indent + gap
  143. let keys = propertyList || Object.keys(value)
  144. let partial = []
  145. for (const key of keys) {
  146. const propertyString = serializeProperty(key, value)
  147. if (propertyString !== undefined) {
  148. let member = serializeKey(key) + ':'
  149. if (gap !== '') {
  150. member += ' '
  151. }
  152. member += propertyString
  153. partial.push(member)
  154. }
  155. }
  156. let final
  157. if (partial.length === 0) {
  158. final = '{}'
  159. } else {
  160. let properties
  161. if (gap === '') {
  162. properties = partial.join(',')
  163. final = '{' + properties + '}'
  164. } else {
  165. let separator = ',\n' + indent
  166. properties = partial.join(separator)
  167. final = '{\n' + indent + properties + ',\n' + stepback + '}'
  168. }
  169. }
  170. stack.pop()
  171. indent = stepback
  172. return final
  173. }
  174. function serializeKey (key) {
  175. if (key.length === 0) {
  176. return quoteString(key, true)
  177. }
  178. const firstChar = String.fromCodePoint(key.codePointAt(0))
  179. if (!util.isIdStartChar(firstChar)) {
  180. return quoteString(key, true)
  181. }
  182. for (let i = firstChar.length; i < key.length; i++) {
  183. if (!util.isIdContinueChar(String.fromCodePoint(key.codePointAt(i)))) {
  184. return quoteString(key, true)
  185. }
  186. }
  187. return key
  188. }
  189. function serializeArray (value) {
  190. if (stack.indexOf(value) >= 0) {
  191. throw TypeError('Converting circular structure to JSON5')
  192. }
  193. stack.push(value)
  194. let stepback = indent
  195. indent = indent + gap
  196. let partial = []
  197. for (let i = 0; i < value.length; i++) {
  198. const propertyString = serializeProperty(String(i), value)
  199. partial.push((propertyString !== undefined) ? propertyString : 'null')
  200. }
  201. let final
  202. if (partial.length === 0) {
  203. final = '[]'
  204. } else {
  205. if (gap === '') {
  206. let properties = partial.join(',')
  207. final = '[' + properties + ']'
  208. } else {
  209. let separator = ',\n' + indent
  210. let properties = partial.join(separator)
  211. final = '[\n' + indent + properties + ',\n' + stepback + ']'
  212. }
  213. }
  214. stack.pop()
  215. indent = stepback
  216. return final
  217. }
  218. }