liudong
2023-05-29 340f156319b863525e50e900c58e59b86ecb3d5e
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
(function (global, factory) {
  // eslint-disable-next-line no-unused-expressions
  typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports)
  // eslint-disable-next-line no-undef
    : typeof define === 'function' && define.amd ? define('jsonlint-formatter', ['exports'], factory)
    // eslint-disable-next-line no-undef
      : (global = global || self, factory(global.jsonlintFormatter = {}))
}(this, function (exports) {
  'use strict'
 
  /**
   * Manual formatter taken straight from https://github.com/umbrae/jsonlintdotcom
   *
   * jsl.format - Provide json reformatting in a character-by-character approach,
   *   so that even invalid JSON may be reformatted (to the best of its ability).
   *
   */
 
  function repeat (s, count) {
    return new Array(count + 1).join(s)
  }
 
  function format (json, indent) {
    var i = 0
    var length = 0
    var indentString = indent !== undefined
      ? typeof indent === 'number'
        ? new Array(indent + 1).join(' ') : indent : '  '
    var outputString = ''
    var indentLevel = 0
    var inString
    var currentChar
 
    for (i = 0, length = json.length; i < length; i += 1) {
      currentChar = json.charAt(i)
      switch (currentChar) {
        case '{':
        case '[':
          if (!inString) {
            outputString += currentChar + '\n' + repeat(indentString, indentLevel + 1)
            indentLevel += 1
          } else {
            outputString += currentChar
          }
          break
        case '}':
        case ']':
          if (!inString) {
            indentLevel -= 1
            outputString += '\n' + repeat(indentString, indentLevel) + currentChar
          } else {
            outputString += currentChar
          }
          break
        case ',':
          if (!inString) {
            outputString += ',\n' + repeat(indentString, indentLevel)
          } else {
            outputString += currentChar
          }
          break
        case ':':
          if (!inString) {
            outputString += ': '
          } else {
            outputString += currentChar
          }
          break
        case ' ':
        case '\n':
        case '\t':
          if (inString) {
            outputString += currentChar
          }
          break
        case '"':
          if (i > 0 && json.charAt(i - 1) !== '\\') {
            inString = !inString
          }
          outputString += currentChar
          break
        default:
          outputString += currentChar
          break
      }
    }
 
    return outputString
  }
 
  exports.format = format
 
  Object.defineProperty(exports, '__esModule', { value: true })
}))