-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtest.js
79 lines (73 loc) · 1.73 KB
/
test.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
const { clone, mapKeys } = require('lodash')
const { equal, deepEqual } = require('assert')
const { format, parse } = require('.')
const json = {
_null: null,
_false: false,
_true: true,
_zero: 0,
_one: 1,
_negative: -1,
_float: 3.14,
_big: 1e123,
_string: "...'...'...",
_array0: [],
_array1: [1],
_array2: [1, 2],
_object0: [],
_object1: {
_nested: {
_value: 1,
},
},
_object2: {
'a b': 1,
"...'...'...": 2,
null: 3,
false: 4,
true: 5,
},
}
const lua = `return {
_null = nil,
_false = false,
_true = true,
_zero = 0,
_one = 1,
_negative = -1,
_float = 3.14,
_big = 1e+123,
_string = '...\\'...\\'...',
_array0 = {},
_array1 = {
1,
},
_array2 = {
1,
2,
},
_object0 = {},
_object1 = {
_nested = {
_value = 1,
},
},
_object2 = {
['a b'] = 1,
['...\\'...\\'...'] = 2,
[nil] = 3,
[false] = 4,
[true] = 5,
},
}`
const luaMinified = `return{_null=nil,_false=false,_true=true,_zero=0,_one=1,_negative=-1,_float=3.14,_big=1e+123,_string='...\\'...\\'...',_array0={},_array1={1,},_array2={1,2,},_object0={},_object1={_nested={_value=1,},},_object2={['a b']=1,['...\\'...\\'...']=2,[nil]=3,[false]=4,[true]=5,},}`
equal(format(json), lua)
deepEqual(parse(lua), json)
equal(format(json, { spaces: null }), luaMinified)
deepEqual(parse(luaMinified), json)
const jsonDoubleQuote = clone(json)
jsonDoubleQuote._string = jsonDoubleQuote._string.replace(/'/g, '"')
jsonDoubleQuote._object2 = mapKeys(jsonDoubleQuote._object2, (_, key) => key.replace(/'/g, '"'))
const luaDoubleQuote = lua.replace(/'/g, '"')
equal(format(jsonDoubleQuote, { singleQuote: false }), luaDoubleQuote)
deepEqual(parse(luaDoubleQuote), jsonDoubleQuote)