ig-serialize 1.0.6 → 1.2.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.
Files changed (4) hide show
  1. package/README.md +27 -0
  2. package/package.json +2 -2
  3. package/serialize.js +26 -20
  4. package/test.js +17 -10
package/README.md CHANGED
@@ -7,6 +7,9 @@ This extends the default JSON serialization adding the following:
7
7
  - Function serialization (off by default)
8
8
  - Deep and partial-deep cleen object copy
9
9
 
10
+ Possible differences to JSON output:
11
+ - Repeating long strings and BigInts can be referenced instead of
12
+ reincluded in the output.
10
13
 
11
14
 
12
15
  ## Motivation
@@ -20,6 +23,13 @@ and tooling design, basic parsing, among others.
20
23
 
21
24
  ## Installation
22
25
 
26
+ ```shell
27
+ $ npm install ig-serilaize
28
+ ```
29
+
30
+ Or just download and drop [serialize.js](serialize.js) into your code.
31
+
32
+
23
33
 
24
34
  ## Introduction
25
35
 
@@ -38,6 +48,7 @@ loosing:
38
48
  Thus, care must be taken when serializing structures containing function.
39
49
 
40
50
 
51
+
41
52
  ## API
42
53
 
43
54
  ### `serialize(..)` / `eJSON.stringify(..)`
@@ -49,6 +60,20 @@ Thus, care must be taken when serializing structures containing function.
49
60
  ### `partialDeepCopy(..)`
50
61
 
51
62
 
63
+ ### `MIN_LENGTH_REF` / `<options>.min_length_ref`
64
+
65
+ Defines the default minimum length of repeating string or bin-int to
66
+ include as a reference in the output.
67
+
68
+ If set to `0`, referencing will be disabled.
69
+
70
+ Default: 96
71
+
72
+
73
+ ### `DEBUG`
74
+
75
+
76
+
52
77
  ## Format
53
78
 
54
79
  The output of `.serialize(..)` is a strict superset of [standard JSON](https://www.json.org/json-en.html),
@@ -65,6 +90,8 @@ Extensions to JSON:
65
90
 
66
91
  ### Recursion
67
92
 
93
+ If an object is encountered
94
+
68
95
  ### null types
69
96
 
70
97
  ### BigInt
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ig-serialize",
3
- "version": "1.0.6",
3
+ "version": "1.2.0",
4
4
  "description": "experimental extended json serializaion...",
5
5
  "main": "serialize.js",
6
6
  "scripts": {
@@ -10,7 +10,7 @@
10
10
  },
11
11
  "repository": {
12
12
  "type": "git",
13
- "url": "git+https://github.com/flynx/serialize.js"
13
+ "url": "git+https://github.com/flynx/serialize.js.git"
14
14
  },
15
15
  "keywords": [
16
16
  "JSON",
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 RECURSIVE = '<RECURSIVE%>'
54
+ var REFERENCE = '<REF%>'
55
55
 
56
- var FUNCTION = '<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 = RECURSIVE.length * 8
80
+ module.MIN_LENGTH_REF = REFERENCE.length * 16
81
81
 
82
82
 
83
83
  //
@@ -144,15 +144,19 @@ module.STRING_LENGTH_REF = RECURSIVE.length * 8
144
144
  var _serialize =
145
145
  module._serialize =
146
146
  function(obj, path=[], seen=new Map(), indent, depth=0, options={}){
147
- var string_length_ref =
148
- options.string_length_ref
149
- ?? module.STRING_LENGTH_REF
150
-
151
- // recursive...
147
+ var min_length_ref =
148
+ options.min_length_ref
149
+ ?? module.MIN_LENGTH_REF
150
+ min_length_ref =
151
+ min_length_ref <= 0 ?
152
+ Infinity
153
+ : min_length_ref
154
+
155
+ // reference...
152
156
  var p = seen.get(obj)
153
157
  if(p != null){
154
158
  // NOTE: _serialize(..) is always printed flat here, regardless of indent/depth...
155
- return RECURSIVE.replace('%', _serialize(p)) }
159
+ return REFERENCE.replace('%', _serialize(p)) }
156
160
 
157
161
  // functions...
158
162
  // NOTE: we are storing function length to avoid parsing the function...
@@ -171,12 +175,14 @@ function(obj, path=[], seen=new Map(), indent, depth=0, options={}){
171
175
  // long strings...
172
176
  // NOTE: this saves on output size...
173
177
  if(typeof(obj) == 'string'
174
- && obj.length > string_length_ref){
178
+ && obj.length > min_length_ref){
175
179
  seen.set(obj, path) }
176
180
  // BigInt...
177
181
  if(typeof(obj) == 'bigint'){
178
- seen.set(obj, path)
179
- return obj.toString() +'n' }
182
+ var res = obj.toString() +'n'
183
+ if(res.length > min_length_ref){
184
+ seen.set(obj, path) }
185
+ return res }
180
186
 
181
187
  // atomics...
182
188
  // NOTE: these are not stored in seen thus are not re-referenced...
@@ -260,7 +266,7 @@ function(obj, indent, depth=0, options){
260
266
  // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
261
267
 
262
268
  // XXX better error handling...
263
- // XXX try and make this single stage (see notes for : .recursive(..))
269
+ // XXX try and make this single stage (see notes for : .reference(..))
264
270
  var eJSON =
265
271
  module.eJSON = {
266
272
  chars: {
@@ -287,9 +293,9 @@ module.eJSON = {
287
293
  NaN: NaN,
288
294
 
289
295
  '<empty>': 'empty',
290
- '<RECURSIVE': 'recursive',
296
+ '<REF': 'reference',
291
297
 
292
- '<FUNCTION[': 'func',
298
+ '<FUNC[': 'func',
293
299
  },
294
300
 
295
301
 
@@ -614,15 +620,15 @@ module.eJSON = {
614
620
  // directly reference that -- this would eliminate the need for
615
621
  // stage two... (XXX TEST)
616
622
  // ...need to use serialized paths as keys...
617
- recursive: function(state, path, match, str, i, line){
618
- debug.lex('recursive', str, i, line)
623
+ reference: function(state, path, match, str, i, line){
624
+ debug.lex('reference', str, i, line)
619
625
  return this.sequence(
620
626
  state, path, str, i+match.length, line,
621
627
  '>',
622
628
  function(res, index, str, i, line){
623
629
  var obj
624
630
  ;[obj, i, line] = this.array(state, [...path, index], '[', str, i, line)
625
- var rec = state.recursive ??= []
631
+ var rec = state.reference ??= []
626
632
  rec.push([path, obj])
627
633
  return [{}, i, line] }) },
628
634
 
@@ -695,8 +701,8 @@ module.eJSON = {
695
701
  var state = {functions: options.functions}
696
702
  var res = this.value(state, [], str)[0]
697
703
 
698
- // stage 2: link the recursive structures...
699
- for(var [a, b] of state.recursive ?? []){
704
+ // stage 2: link the reference structures...
705
+ for(var [a, b] of state.reference ?? []){
700
706
  this.setItem(res, a, this.getItem(res, b)) }
701
707
 
702
708
  return res },
package/test.js CHANGED
@@ -100,19 +100,19 @@ var setups = test.Setups({
100
100
  return ['Map([])'] },
101
101
 
102
102
  'function': function(assert){
103
- return ['<FUNCTION[14,(function(){})]>'] },
103
+ return ['<FUNC[14,(function(){})]>'] },
104
104
 
105
105
  // recursive...
106
106
  'array-recursive': function(assert){
107
- return ['[<RECURSIVE[]>]'] },
107
+ return ['[<REF[]>]'] },
108
108
  'object-recursive': function(assert){
109
- return ['{"r":<RECURSIVE[]>}'] },
109
+ return ['{"r":<REF[]>}'] },
110
110
  'set-recursive': function(assert){
111
- return ['Set([<RECURSIVE[]>])'] },
111
+ return ['Set([<REF[]>])'] },
112
112
  'map-recursive-key': function(assert){
113
- return ['Map([[<RECURSIVE[]>,"value"]])'] },
113
+ return ['Map([[<REF[]>,"value"]])'] },
114
114
  'map-recursive-value': function(assert){
115
- return ['Map([["key",<RECURSIVE[]>]])'] },
115
+ return ['Map([["key",<REF[]>]])'] },
116
116
  })
117
117
 
118
118
  test.Modifiers({
@@ -213,19 +213,26 @@ test.Cases({
213
213
  ['0O77', '63'],
214
214
  ['0b11', '3'],
215
215
  ['0B11', '3'],
216
+ // XXX
217
+ //['010', '8'],
218
+ //['080', '80'],
216
219
 
217
220
  // string quotes...
218
- // XXX test new lines...
219
221
  ["'abc'", '"abc"'],
220
222
  ['`abc`', '"abc"'],
223
+ /* XXX test new lines...
224
+ [`\`a
225
+ b
226
+ c\``, '"a\\nb\\nc"'],
227
+ //*/
221
228
 
222
229
  // arrays...
223
230
  ['[1,2,]', '[1,2]'],
224
231
 
225
232
  // sparse arrays...
226
- ['[<empty>]', '[,]'],
227
- ['[1,2,<empty>]', '[1,2,,]'],
228
- ['[1,2,<empty>]', '[1,2,<empty>,]'],
233
+ ['[,]', '[<empty>]'],
234
+ ['[1,2,,]', '[1,2,<empty>]'],
235
+ ['[1,2,<empty>,]', '[1,2,<empty>]'],
229
236
  ],
230
237
  'syntax-simplifications': function(assert){
231
238
  var aa, bb