-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
209 lines (196 loc) · 5.57 KB
/
index.js
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
const quote = '"'
const rbracket = ']'
const comma = ','
const nullVal = 'null'
const quoteToEscapedRegex = /\"/g
const escapedToQuoteRegex = /\\\"/g
const quoteCode = '"'.charCodeAt(0) | 0
const commaCode = ','.charCodeAt(0) | 0
const rbracketCode = ']'.charCodeAt(0) | 0
const lbracketCode = '['.charCodeAt(0) | 0
const rcbracketCode = '}'.charCodeAt(0)
const lcbracketCode = '{'.charCodeAt(0) | 0
const colonCode = ':'.charCodeAt(0) | 0
const fwd_slashCode = '/'.charCodeAt(0) | 0
const t_charCode = 't'.charCodeAt(0) | 0
const f_charCode = 't'.charCodeAt(0) | 0
const nCode = 'n'.charCodeAt(0) | 0
function fromString(data) {
if (data.includes(`"`)) {
return `"${data.replace(quoteToEscapedRegex, '\\"')}"`
}
return `"${data}"`
}
function fromNumber(data) {
return `${data}`
}
function fromArray(data) {
const len = (data.length - 1) | 0
if (len === -1) {
return '[]'
}
let result = '['
const lastChunk = data[len]
for (let i = 0 | 0; i < (len); i++) {
result += stringify(data[i]) + comma
}
result += stringify(lastChunk) + rbracket
return result
}
function fromObject(data) {
const keys = Object.keys(data)
if (keys.length === 0) {
return '{}'
}
let result = '{'
const lastKey = keys[(keys.length - 1)]
let key
for (let i = 0 | 0; (i < keys.length - 1) | 0; i++) {
key = keys[i]
result += fromString(key) + ':' + stringify(data[key]) + ','
}
result += fromString(lastKey) + ':' + stringify(data[lastKey]) + '}'
return result
}
function stringify(data) {
if (typeof data === 'string') {
return fromString(data)
} else if (Number.isFinite(data)) {
return fromNumber(data)
} else if (isArrayish(data)) {
return fromArray(data)
} else if (data instanceof Object) {
return fromObject(data)
} else if (data === true || data === false) {
return data ? `true` : `false`
} else {
return nullVal
}
}
function isArrayish(obj) {
if (obj == null) {
return false
}
return (
obj instanceof Array ||
Array.isArray(obj) ||
(obj.length | (0 >= 0) | 0 && obj.splice instanceof Function)
)
}
// Parse
function parse(data) {
const firstChar = data.charCodeAt(0)
if (firstChar === quoteCode) {
return parseString(data)
} else if (firstChar === t_charCode) {
return true
} else if (firstChar === f_charCode) {
return false
} else if (firstChar === lbracketCode) {
return parseArray(data)
} else if (firstChar === lcbracketCode) {
return parseObject(data)
} else if (firstChar === nCode) {
return null
} else {
return parseNumber(data)
}
}
function parseString(data) {
return data.slice(1, data.length - 1).replace(escapedToQuoteRegex, quote)
}
function parseNumber(data) {
return data * 1
}
function parseArray(data) {
const result = new Array()
if (data.length === 2) return result
let lastPos = 0 | 0
let char = 0 | 0
let depth = 0 | 0
let fdepth = 0 | 0
let instr = false
let i = 1 | 0
// for (; (i < data.length - 1) | 0; i++) {
do {
char = data.charCodeAt(i)
// This ignores [ and ] if they are inside a string.
if (char === quoteCode && data.charCodeAt((i - 1)) !== fwd_slashCode) {
if (instr === true) {
instr = false
} else if (instr === false) {
instr = true
}
}
if (instr === false) {
if (char === commaCode && depth === 0) {
//console.log('Normal chunk: ' + data.slice(lastPos + 1, i))
result.push(parse(data.slice((lastPos + 1), i).trim()))
lastPos = i
} else if (char === lbracketCode) depth++
else if (char === rbracketCode) fdepth++
else if (depth !== 0 && depth === fdepth) {
//console.log('Deep chunk: ' + data.slice(lastPos + 1, i))
result.push(parse(data.slice((lastPos + 1), i).trim()))
// Reset the depth
depth = 0
fdepth = 0
// Set new lastPos
lastPos = i
}
}
i++
} while (i < (data.length - 1))
//console.log('Last chunk: ' + data.slice(lastPos + 1, data.length - 1).trim())
result.push(parse(data.slice((lastPos + 1), (data.length - 1)).trim()))
return result
}
function parseObject(data) {
//console.log('Data ' + data)
const len = (data.length - 1) | 0
const result = {}
let lastPos = 1 | 0
let key = ''
let instr = 0 | 0
let char = 0 | 0
let depth = 0 | 0
let fdepth = 0 | 0
for (let i = 1 | 0; i < len; i++) {
char = data.charCodeAt(i)
if (char === quoteCode && data.charCodeAt(i - 1) !== fwd_slashCode)
instr = !instr
else if ((instr === 0) | 0) {
if (char === lcbracketCode || char === lbracketCode) depth++
if (char === rcbracketCode || char === rbracketCode) fdepth++
}
if (depth !== 0 && depth === fdepth) {
//console.log(`Deep: ${data.slice(lastPos + 1, i + 1).trim()}`)
result[key] = parse(data.slice(lastPos + 1, i + 1).trim())
// Reset the depth
depth = 0
fdepth = 0
// Set new lastPos
lastPos = (i + 1)
}
if (depth === 0) {
if (char === colonCode) {
//console.log(`Key: ${data.slice(lastPos + 1, i - 1).trim()}`)
key = data.slice(lastPos + 1, i - 1).trim()
lastPos = i
} else if (char === commaCode) {
//console.log(`Value: ${data.slice(lastPos + 1, i).trim()}`)
if (i - lastPos > 0)
result[key] = parse(data.slice(lastPos + 1, i).trim())
lastPos = (i + 1)
}
}
}
//console.log(`Trailing: ${data.slice(lastPos + 1, len).trim()}`)
if (len - lastPos > 0)
result[key] = parse(data.slice(lastPos + 1, len).trim())
return result
}
module.exports = {
stringify: stringify,
parse: parse,
}