-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
302 lines (246 loc) · 6.73 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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/* eslint-disable quotes */
const escapeQuotes = require('escape-quotes')
const isPlainObject = require('lodash.isplainobject')
const isEmpty = require('lodash.isempty')
const PhpParser = require('php-parser')
const escapeStr = val => {
val = escapeQuotes(val)
val = val.replace(/\n/g, "\\n")
val = val.replace(/"/g, "\\\"")
return val
}
const phpParser = new PhpParser({
parser: {
locations: false,
extractDoc: true
}
})
const phpLexer = {
L_PARENTHESIS: '(',
R_PARENTHESIS: ')',
ARRAY_POINTER: '=>',
ARRAY_KEYWORD: 'array',
NULL_KEYWORD: 'null',
EMPTY_KEYWORD: "''",
COMMA: ','
}
const indentTypes = {
SPACE: ' ',
TAB: '\t'
}
const quoteTypes = {
SINGLE: "'",
DOUBLE: '"'
}
const isString = str => {
return typeof str === 'string'
}
const isArray = arr => {
return typeof arr === 'object' && Array.isArray(arr)
}
const isJSON = json => {
try {
return JSON.parse(json)
} catch (err) {
return false
}
}
const literal = string => {
return {
___$isLiteral: true,
___$string: string
}
}
const arrify = (obj, options, tree = 1) => {
tree = tree < 0 ? 0 : tree
const result = []
const hasIndent = options.indent > 0
const indentChar = options.space ? indentTypes.SPACE : indentTypes.TAB
let objSize = typeof obj === 'object' && obj !== null ? Object.keys(obj).length : 0
const addTabTo = (arr, treeCount) => {
if (!treeCount && isNaN(treeCount)) {
treeCount = tree
}
if (hasIndent) {
const indentChars = []
for (let i = 0; i < options.indent * treeCount; i++) {
indentChars.push(indentChar)
}
arr.push(indentChars.join(''))
}
}
const addNewLineTo = arr => {
if (hasIndent) {
arr.push('\n')
}
}
const addSpaceTo = arr => {
if (hasIndent) {
arr.push(' ')
}
}
if (isPlainObject(obj) && obj.___$isLiteral) {
return obj.___$string
} else if (isPlainObject(obj)) {
result.push(phpLexer.ARRAY_KEYWORD)
result.push(phpLexer.L_PARENTHESIS)
if (Object.keys(obj).length > 0) {
addNewLineTo(result)
}
let index = 0
for (const key in obj) {
if (Object.prototype.hasOwnProperty.call(obj, key)) {
const arrKey = options.quote + escapeStr(key) + options.quote
const arrValue = obj[key]
addTabTo(result)
result.push(arrKey)
addSpaceTo(result)
result.push(phpLexer.ARRAY_POINTER)
addSpaceTo(result)
result.push(arrify(arrValue, options, tree + 1))
index++
}
if ((index < objSize && options.trailingComma === false) || options.trailingComma) {
result.push(phpLexer.COMMA)
}
if (index < objSize) {
addNewLineTo(result)
}
}
if (Object.keys(obj).length > 0) {
addNewLineTo(result)
addTabTo(result, tree - 1)
}
result.push(phpLexer.R_PARENTHESIS)
return result.join('')
} else if (isArray(obj)) {
objSize = obj.length - 1
result.push(phpLexer.ARRAY_KEYWORD)
result.push(phpLexer.L_PARENTHESIS)
obj.forEach((item, index) => {
addNewLineTo(result)
addTabTo(result)
result.push(arrify(item, options, tree + 1))
if ((index < objSize && options.trailingComma === false) || options.trailingComma) {
result.push(phpLexer.COMMA)
}
if (index === objSize) {
addNewLineTo(result)
addTabTo(result, tree - 1)
}
})
result.push(phpLexer.R_PARENTHESIS)
return result.join('')
} else if (isString(obj)) {
return options.quote + escapeStr(obj) + options.quote
} else if ((!obj || objSize === 0) && isPlainObject(obj)) {
result.push(phpLexer.ARRAY_KEYWORD)
result.push(phpLexer.L_PARENTHESIS)
result.push(phpLexer.R_PARENTHESIS)
return result.join('')
} else if (obj === null) {
result.push(phpLexer.NULL_KEYWORD)
return result.join(null)
} else if (isNaN(obj)) {
result.push(phpLexer.EMPTY_KEYWORD)
return result.join('')
}
return obj
}
const applyEmptyRules = (object, emptyRules) => {
for (const key in object) {
if (Object.prototype.hasOwnProperty.call(object, key)) {
if (key in emptyRules) {
if (isEmpty(object[key])) {
object[key] = emptyRules[key]
} else {
object[key] = Object.assign(object[key], applyEmptyRules(object[key], emptyRules[key]))
}
}
}
}
return object
}
exports.quoteTypes = quoteTypes
exports.arrify = (json, options) => {
const defaultOptions = Object.assign({}, {
prettify: false,
indent: 1,
space: false,
trailingComma: false,
quote: quoteTypes.DOUBLE
})
options = Object.assign(defaultOptions, options)
options.indent = (options.prettify === false) ? 0 : options.indent
const validJSON = isJSON(json)
let object = validJSON || {}
object = isPlainObject(json) ? json : object
const phpArray = arrify(object, options)
return `${phpArray};`
}
exports.parse = (codes, options = {}) => {
options = Object.assign({
asObject: true,
emptyRules: {}
}, options)
const AST = phpParser.parseEval(codes)
const iterator = items => {
const normalizeValue = item => {
let mapItems = []
let callArgs = ''
switch (item.value.kind) {
case 'number':
return parseInt(item.value.value, 10)
case 'array':
if (item.value.items.length === 0) {
return []
}
return iterator(item.value.items)
case 'call':
mapItems = item.value.arguments.map(item => {
switch (item.kind) {
case 'string':
return quoteTypes.SINGLE + item.value + quoteTypes.SINGLE
case 'boolean':
return JSON.parse(item.value)
case 'number':
return parseInt(item.value, 10)
default:
return item.value
}
})
callArgs = mapItems.length > 0 ? ` ${mapItems.join(', ')} ` : ''
return literal(`${item.value.what.name}(${callArgs})`)
case 'string':
return item.value.value
case 'boolean':
return JSON.parse(item.value.value)
default:
return JSON.stringify(item.value.value)
}
}
const json = {}
let arry = []
items.forEach(item => {
if (item.key) {
json[item.key.value] = normalizeValue(item)
} else {
arry = arry.concat(normalizeValue(item))
}
})
if (arry.length > 0) {
return arry
}
return json
}
let object = {}
if (AST.kind === 'program') {
object = iterator(AST.children[0].items)
object = applyEmptyRules(object, options.emptyRules)
}
if (options.asObject === false) {
object = JSON.stringify(object)
}
return object
}
exports.literal = literal