-
Notifications
You must be signed in to change notification settings - Fork 0
/
stringEncoder.js
224 lines (190 loc) · 6.34 KB
/
stringEncoder.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
//
// Convert something to string, and back to data, using encode and decode functions
//
// Adds a hungarian notation prefix to the string when it encodes, so it doesn't lose what the datatype was of the original.
// It needs to do this because otherwise
//
// 'false' and false would be the same thing if encoded and back
//
import { getType } from '@/scripts/util/helpers.js'
const typeChars = {
string: 's',
boolean: 'b',
number: 'n',
null: 'N',
undefined: 'u',
Object: 'o',
objectlike: 'o',
Array: 'a',
arraylike: 'a',
}
export const stringEncoder = {
encode (data) {
const type = getType(data)
const prefixChar = typeChars[type]
if(typeChars[type] === undefined) {
console.error('type: ' + type + " not yet supported in stringEncoder.encode!")
}
switch(prefixChar) {
case 's': // string
case 'b': // boolean
case 'n': // number
case 'N': // null
case 'u': // undefined
return prefixChar + String(data)
case 'a': {
// 1. encode the sub keys
const step1 = data.map(stringEncoder.encode)
// 2. encode JSON
return 'a' + JSON.stringify(step1)
}
case 'o': {
// 1. encode the sub keys
const step1 = {}
for(let k in data) {
const v = data[k]
const encodedV = stringEncoder.encode(v)
step1[k] = encodedV
}
// 2. encode JSON
return 'o' + JSON.stringify(step1)
}
}
},
decode (dataStr) {
if(dataStr == null) return null
let prefixChar = dataStr.charAt(0)
let data = dataStr.slice(1)
let output
switch(prefixChar) {
case 's':
output = data
break
case 'b':
output = data === 'true'
? true
: false
break
case 'n':
output = Number(data)
break
case 'N':
output = null
break
case 'u':
output = undefined
break
case 'a': {
// 1. parse JSON
const step1 = JSON.parse(data)
// 2. decode subkeys
const step2 = step1.map(stringEncoder.decode)
return step2
}
case 'o': {
// 1. parse JOSN
let step1 = JSON.parse(data)
// 2. decode subkeys
const step2 = {}
for(let k in step1) {
const v = step1[k]
const decodedV = stringEncoder.decode(v)
step2[k] = decodedV
}
return step2
}
default:
console.error('prefixChar: ' + prefixChar + " not yet supported in stringEncoder.decode")
}
return output
}
}
import { objDeepClone } from '@/scripts/util/helpers.js'
export const stringEncoderTests = (function() {
const obj = {
encode: [
// Strings
{
input: 'Hello',
exout: 'sHello',
},
{
input: '',
exout: 's',
},
{
input: '12~`-=(%)37ya\rsdfh zxcj😀k﷽v,n2ツ13"k/\\\t\n',
exout: 's12~`-=(%)37ya\rsdfh zxcj😀k﷽v,n2ツ13"k/\\\t\n',
},
// Booleans
{
input: true,
exout: 'btrue',
},
{
input: false,
exout: 'bfalse',
},
// Numbers
{
input: 0,
exout: 'n0'
},
{
input: NaN,
exout: 'nNaN'
},
{
input: Number.MAX_SAFE_INTEGER,
exout: 'n9007199254740991'
},
{
input: Number.MIN_SAFE_INTEGER,
exout: 'n-9007199254740991'
},
{
input: 1e100,
exout: 'n1e+100'
},
// null
{
input: null,
exout: 'Nnull',
},
// undefined
{
input: undefined,
exout: 'uundefined',
},
{
input: 'uundefined',
exout: 'suundefined',
},
// Objects
{
input: {k: 24, k2: 'Hello', 'Yay"bab e': 'a",s:"key65":"n23"', ' ss s s ': null},
exout: 'o{"k":"n24","k2":"sHello","Yay\\"bab e":"sa\\",s:\\"key65\\":\\"n23\\""," ss s s ":"Nnull"}'
},
// Arrays
{
input: ['k,e\'y,"1', true, NaN, 42220, undefined, null, [1,',\\\\,3,"24",,',"25"]],
exout: 'a["sk,e\'y,\\"1","btrue","nNaN","n42220","uundefined","Nnull","a[\\"n1\\",\\"s,\\\\\\\\\\\\\\\\,3,\\\\\\"24\\\\\\",,\\",\\"s25\\"]"]'
},
// Object ./. Arrays
{
input: [{key1: [1,2,null, undefined, 1 / 0], key2: -1 / 0}, 'sNaN', 0.015625, {'String Based Key': true}],
exout: 'a["o{\\"key1\\":\\"a[\\\\\\"n1\\\\\\",\\\\\\"n2\\\\\\",\\\\\\"Nnull\\\\\\",\\\\\\"uundefined\\\\\\",\\\\\\"nInfinity\\\\\\"]\\",\\"key2\\":\\"n-Infinity\\"}","ssNaN","n0.015625","o{\\"String Based Key\\":\\"btrue\\"}"]',
}
],
decode: []
}
// create the decodes by flipping the encodes
obj.encode.forEach(encodeItem => {
const decodeItem = {
input: encodeItem.exout,
exout: objDeepClone(encodeItem.input)
}
obj.decode.push(decodeItem)
})
return obj
})()