ig-serialize 1.0.0 → 1.0.1

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 CHANGED
@@ -1,2 +1,15 @@
1
- # serilize.js
2
- Extended JSON serilization
1
+ # serilize.js: Extended JSON serilization
2
+
3
+ This extends the default JSON serialization adding the following:
4
+ - Recursive data structure serialization
5
+ - `undefined`/`NaN` serialization
6
+ - `Set`/`Map` serialization
7
+ - Function serialization (off by default)
8
+ - Deep and partial-deep cleen object copy
9
+
10
+
11
+
12
+ ## Motivation
13
+
14
+
15
+
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ig-serialize",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "experimental extended json serializaion...",
5
5
  "main": "serialize.js",
6
6
  "scripts": {
package/serialize.js CHANGED
@@ -117,7 +117,7 @@ function(obj, path=[], seen=new Map(), indent, depth=0, functions){
117
117
  seen.set(obj, path)
118
118
  // if functions array is given add function to it and store its
119
119
  // index in the serialized data...
120
- if(functions != null){
120
+ if(functions instanceof Array){
121
121
  functions.push(obj)
122
122
  obj = functions.length-1 }
123
123
  var s = '('+ obj.toString() +')'
@@ -543,6 +543,7 @@ module.eJSON = {
543
543
  // NOTE: this uses eval(..) so care must be taken when enabling this...
544
544
  func: function(state, path, match, str, i, line){
545
545
  if(state.functions == null){
546
+ console.log('---', state)
546
547
  this.error('Deserializing functions disabled.', str, i, line) }
547
548
 
548
549
  debug.lex('function', str, i, line)
@@ -558,6 +559,9 @@ module.eJSON = {
558
559
  if(state.functions instanceof Array){
559
560
  var [n, i, line] = this.number(state, path, str[i+1], str, i+1, line)
560
561
  res = state.functions[n]
562
+ if(str[i] != ')'){
563
+ this.error('Expected ")" got "'+ str[i] +'"', str, i, line) }
564
+ i++
561
565
  // func code...
562
566
  } else {
563
567
  var code = str.slice(i, i+l)
@@ -626,15 +630,15 @@ function(str, functions){
626
630
 
627
631
  var deepCopy =
628
632
  module.deepCopy =
629
- function(obj){
633
+ function(obj, functions){
630
634
  return deserialize(
631
- serialize(obj)) }
635
+ serialize(obj, null, 0, functions),
636
+ functions) }
632
637
 
633
638
 
634
- var semiDeepCopy =
635
- module.semiDeepCopy =
636
- function(obj){
637
- var funcs = []
639
+ var partialDeepCopy =
640
+ module.partialDeepCopy =
641
+ function(obj, funcs=[]){
638
642
  return deserialize(
639
643
  serialize(obj, null, 0, funcs),
640
644
  funcs) }
package/test.js CHANGED
@@ -152,7 +152,42 @@ test.Tests({
152
152
  var obj = eJSON.deserialize(setup, true)
153
153
  assert(
154
154
  JSON.stringify(obj) == eJSON.serialize(obj) ) },
155
+
156
+ 'deep-copy': function(assert, [setup]){
157
+ var obj = eJSON.deserialize(setup, true)
158
+ var copy = eJSON.deepCopy(obj, true)
159
+
160
+ assert(eJSON.serialize(obj) == eJSON.serialize(copy))
161
+
162
+ // XXX check if all non-atoms are distinct...
163
+ // XXX
164
+ },
165
+
166
+ //* XXX ERR
167
+ 'partial-deep-copy': function(assert, [setup]){
168
+ var obj = eJSON.deserialize(setup, true)
169
+ var funcs = []
170
+ var copy = eJSON.partialDeepCopy(obj, funcs)
171
+
172
+ assert(eJSON.serialize(obj) == eJSON.serialize(copy))
173
+
174
+ // XXX check if all non-atoms are distinct and functions are the same...
175
+ // XXX
176
+ },
177
+ //*/
178
+ })
179
+
180
+ /* XXX these break things...
181
+ test.Cases({
182
+ 'deep-copy-function': function(assert, [setup]){
183
+ // XXX check function isolation...
184
+ },
185
+
186
+ 'partial-deep-copy-function': function(assert, [setup]){
187
+ // XXX check function isolation...
188
+ },
155
189
  })
190
+ //*/
156
191
 
157
192
 
158
193
  //---------------------------------------------------------------------