stringify.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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 (const c of value) {
  108. switch (c) {
  109. case "'":
  110. case '"':
  111. quotes[c]++
  112. product += c
  113. continue
  114. }
  115. if (replacements[c]) {
  116. product += replacements[c]
  117. continue
  118. }
  119. if (c < ' ') {
  120. let hexString = c.charCodeAt(0).toString(16)
  121. product += '\\x' + ('00' + hexString).substring(hexString.length)
  122. continue
  123. }
  124. product += c
  125. }
  126. const quoteChar = quote || Object.keys(quotes).reduce((a, b) => (quotes[a] < quotes[b]) ? a : b)
  127. product = product.replace(new RegExp(quoteChar, 'g'), replacements[quoteChar])
  128. return quoteChar + product + quoteChar
  129. }
  130. function serializeObject (value) {
  131. if (stack.indexOf(value) >= 0) {
  132. throw TypeError('Converting circular structure to JSON5')
  133. }
  134. stack.push(value)
  135. let stepback = indent
  136. indent = indent + gap
  137. let keys = propertyList || Object.keys(value)
  138. let partial = []
  139. for (const key of keys) {
  140. const propertyString = serializeProperty(key, value)
  141. if (propertyString !== undefined) {
  142. let member = serializeKey(key) + ':'
  143. if (gap !== '') {
  144. member += ' '
  145. }
  146. member += propertyString
  147. partial.push(member)
  148. }
  149. }
  150. let final
  151. if (partial.length === 0) {
  152. final = '{}'
  153. } else {
  154. let properties
  155. if (gap === '') {
  156. properties = partial.join(',')
  157. final = '{' + properties + '}'
  158. } else {
  159. let separator = ',\n' + indent
  160. properties = partial.join(separator)
  161. final = '{\n' + indent + properties + ',\n' + stepback + '}'
  162. }
  163. }
  164. stack.pop()
  165. indent = stepback
  166. return final
  167. }
  168. function serializeKey (key) {
  169. if (key.length === 0) {
  170. return quoteString(key, true)
  171. }
  172. const firstChar = String.fromCodePoint(key.codePointAt(0))
  173. if (!util.isIdStartChar(firstChar)) {
  174. return quoteString(key, true)
  175. }
  176. for (let i = firstChar.length; i < key.length; i++) {
  177. if (!util.isIdContinueChar(String.fromCodePoint(key.codePointAt(i)))) {
  178. return quoteString(key, true)
  179. }
  180. }
  181. return key
  182. }
  183. function serializeArray (value) {
  184. if (stack.indexOf(value) >= 0) {
  185. throw TypeError('Converting circular structure to JSON5')
  186. }
  187. stack.push(value)
  188. let stepback = indent
  189. indent = indent + gap
  190. let partial = []
  191. for (let i = 0; i < value.length; i++) {
  192. const propertyString = serializeProperty(String(i), value)
  193. partial.push((propertyString !== undefined) ? propertyString : 'null')
  194. }
  195. let final
  196. if (partial.length === 0) {
  197. final = '[]'
  198. } else {
  199. if (gap === '') {
  200. let properties = partial.join(',')
  201. final = '[' + properties + ']'
  202. } else {
  203. let separator = ',\n' + indent
  204. let properties = partial.join(separator)
  205. final = '[\n' + indent + properties + ',\n' + stepback + ']'
  206. }
  207. }
  208. stack.pop()
  209. indent = stepback
  210. return final
  211. }
  212. }