ig-serialize 1.0.5 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -0
- package/package.json +1 -1
- package/serialize.js +15 -14
- package/test.js +25 -9
package/README.md
CHANGED
package/package.json
CHANGED
package/serialize.js
CHANGED
|
@@ -51,9 +51,9 @@ var NAN = 'NaN'
|
|
|
51
51
|
var INFINITY = 'Infinity'
|
|
52
52
|
var NEG_INFINITY = '-Infinity'
|
|
53
53
|
|
|
54
|
-
var
|
|
54
|
+
var REFERENCE = '<REF%>'
|
|
55
55
|
|
|
56
|
-
var FUNCTION = '<
|
|
56
|
+
var FUNCTION = '<FUNC[%]>'
|
|
57
57
|
|
|
58
58
|
|
|
59
59
|
|
|
@@ -77,7 +77,7 @@ var debug = {
|
|
|
77
77
|
|
|
78
78
|
//---------------------------------------------------------------------
|
|
79
79
|
|
|
80
|
-
module.STRING_LENGTH_REF =
|
|
80
|
+
module.STRING_LENGTH_REF = REFERENCE.length * 8
|
|
81
81
|
|
|
82
82
|
|
|
83
83
|
//
|
|
@@ -148,11 +148,11 @@ function(obj, path=[], seen=new Map(), indent, depth=0, options={}){
|
|
|
148
148
|
options.string_length_ref
|
|
149
149
|
?? module.STRING_LENGTH_REF
|
|
150
150
|
|
|
151
|
-
//
|
|
151
|
+
// reference...
|
|
152
152
|
var p = seen.get(obj)
|
|
153
153
|
if(p != null){
|
|
154
154
|
// NOTE: _serialize(..) is always printed flat here, regardless of indent/depth...
|
|
155
|
-
return
|
|
155
|
+
return REFERENCE.replace('%', _serialize(p)) }
|
|
156
156
|
|
|
157
157
|
// functions...
|
|
158
158
|
// NOTE: we are storing function length to avoid parsing the function...
|
|
@@ -260,7 +260,7 @@ function(obj, indent, depth=0, options){
|
|
|
260
260
|
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
|
261
261
|
|
|
262
262
|
// XXX better error handling...
|
|
263
|
-
// XXX try and make this single stage (see notes for : .
|
|
263
|
+
// XXX try and make this single stage (see notes for : .reference(..))
|
|
264
264
|
var eJSON =
|
|
265
265
|
module.eJSON = {
|
|
266
266
|
chars: {
|
|
@@ -287,9 +287,9 @@ module.eJSON = {
|
|
|
287
287
|
NaN: NaN,
|
|
288
288
|
|
|
289
289
|
'<empty>': 'empty',
|
|
290
|
-
'<
|
|
290
|
+
'<REF': 'reference',
|
|
291
291
|
|
|
292
|
-
'<
|
|
292
|
+
'<FUNC[': 'func',
|
|
293
293
|
},
|
|
294
294
|
|
|
295
295
|
|
|
@@ -413,7 +413,7 @@ module.eJSON = {
|
|
|
413
413
|
var mode = 'dec'
|
|
414
414
|
if(str[j] == '0'
|
|
415
415
|
&& 'xXbBoO'.includes(str[j+1])){
|
|
416
|
-
mode = str[j+1]
|
|
416
|
+
mode = str[j+1].toLowerCase()
|
|
417
417
|
j++ }
|
|
418
418
|
j++
|
|
419
419
|
while(j < str.length
|
|
@@ -432,6 +432,7 @@ module.eJSON = {
|
|
|
432
432
|
// exponent...
|
|
433
433
|
|| str[j] == 'e'
|
|
434
434
|
|| str[j] == 'E')){
|
|
435
|
+
// exponent sign...
|
|
435
436
|
if('eE'.includes(str[j])
|
|
436
437
|
&& '+-'.includes(str[j+1])){
|
|
437
438
|
j++ }
|
|
@@ -613,15 +614,15 @@ module.eJSON = {
|
|
|
613
614
|
// directly reference that -- this would eliminate the need for
|
|
614
615
|
// stage two... (XXX TEST)
|
|
615
616
|
// ...need to use serialized paths as keys...
|
|
616
|
-
|
|
617
|
-
debug.lex('
|
|
617
|
+
reference: function(state, path, match, str, i, line){
|
|
618
|
+
debug.lex('reference', str, i, line)
|
|
618
619
|
return this.sequence(
|
|
619
620
|
state, path, str, i+match.length, line,
|
|
620
621
|
'>',
|
|
621
622
|
function(res, index, str, i, line){
|
|
622
623
|
var obj
|
|
623
624
|
;[obj, i, line] = this.array(state, [...path, index], '[', str, i, line)
|
|
624
|
-
var rec = state.
|
|
625
|
+
var rec = state.reference ??= []
|
|
625
626
|
rec.push([path, obj])
|
|
626
627
|
return [{}, i, line] }) },
|
|
627
628
|
|
|
@@ -694,8 +695,8 @@ module.eJSON = {
|
|
|
694
695
|
var state = {functions: options.functions}
|
|
695
696
|
var res = this.value(state, [], str)[0]
|
|
696
697
|
|
|
697
|
-
// stage 2: link the
|
|
698
|
-
for(var [a, b] of state.
|
|
698
|
+
// stage 2: link the reference structures...
|
|
699
|
+
for(var [a, b] of state.reference ?? []){
|
|
699
700
|
this.setItem(res, a, this.getItem(res, b)) }
|
|
700
701
|
|
|
701
702
|
return res },
|
package/test.js
CHANGED
|
@@ -49,6 +49,8 @@ var setups = test.Setups({
|
|
|
49
49
|
// return ['+123', json] },
|
|
50
50
|
'number-exp': function(assert){
|
|
51
51
|
return ['1e+100', json] },
|
|
52
|
+
'number-exp-neg': function(assert){
|
|
53
|
+
return ['1e-100', json] },
|
|
52
54
|
'number-zero': function(assert){
|
|
53
55
|
return ['0', json] },
|
|
54
56
|
'float-a': function(assert){
|
|
@@ -98,19 +100,19 @@ var setups = test.Setups({
|
|
|
98
100
|
return ['Map([])'] },
|
|
99
101
|
|
|
100
102
|
'function': function(assert){
|
|
101
|
-
return ['<
|
|
103
|
+
return ['<FUNC[14,(function(){})]>'] },
|
|
102
104
|
|
|
103
105
|
// recursive...
|
|
104
106
|
'array-recursive': function(assert){
|
|
105
|
-
return ['[<
|
|
107
|
+
return ['[<REF[]>]'] },
|
|
106
108
|
'object-recursive': function(assert){
|
|
107
|
-
return ['{"r":<
|
|
109
|
+
return ['{"r":<REF[]>}'] },
|
|
108
110
|
'set-recursive': function(assert){
|
|
109
|
-
return ['Set([<
|
|
111
|
+
return ['Set([<REF[]>])'] },
|
|
110
112
|
'map-recursive-key': function(assert){
|
|
111
|
-
return ['Map([[<
|
|
113
|
+
return ['Map([[<REF[]>,"value"]])'] },
|
|
112
114
|
'map-recursive-value': function(assert){
|
|
113
|
-
return ['Map([["key",<
|
|
115
|
+
return ['Map([["key",<REF[]>]])'] },
|
|
114
116
|
})
|
|
115
117
|
|
|
116
118
|
test.Modifiers({
|
|
@@ -204,19 +206,33 @@ test.Cases({
|
|
|
204
206
|
['123.', '123'],
|
|
205
207
|
['+123', '123'],
|
|
206
208
|
['123e100', '123e+100'],
|
|
209
|
+
['123E100', '123e+100'],
|
|
207
210
|
['0xff', '255'],
|
|
211
|
+
['0Xff', '255'],
|
|
212
|
+
['0o77', '63'],
|
|
213
|
+
['0O77', '63'],
|
|
214
|
+
['0b11', '3'],
|
|
215
|
+
['0B11', '3'],
|
|
216
|
+
// XXX
|
|
217
|
+
//['010', '8'],
|
|
218
|
+
//['080', '80'],
|
|
208
219
|
|
|
209
220
|
// string quotes...
|
|
210
221
|
["'abc'", '"abc"'],
|
|
211
222
|
['`abc`', '"abc"'],
|
|
223
|
+
/* XXX test new lines...
|
|
224
|
+
[`\`a
|
|
225
|
+
b
|
|
226
|
+
c\``, '"a\\nb\\nc"'],
|
|
227
|
+
//*/
|
|
212
228
|
|
|
213
229
|
// arrays...
|
|
214
230
|
['[1,2,]', '[1,2]'],
|
|
215
231
|
|
|
216
232
|
// sparse arrays...
|
|
217
|
-
['[
|
|
218
|
-
['[1,2
|
|
219
|
-
['[1,2,<empty
|
|
233
|
+
['[,]', '[<empty>]'],
|
|
234
|
+
['[1,2,,]', '[1,2,<empty>]'],
|
|
235
|
+
['[1,2,<empty>,]', '[1,2,<empty>]'],
|
|
220
236
|
],
|
|
221
237
|
'syntax-simplifications': function(assert){
|
|
222
238
|
var aa, bb
|