|
8 | 8 | const {Object,JSON,WeakMap,ReferenceError,TypeError,RangeError}=window |
9 | 9 | const {keys,getOwnPropertyDescriptor:describe}=Object |
10 | 10 | const {stringify:str,parse}=JSON, CACHE=new WeakMap() |
| 11 | + const {toStringTag,toPrimitive}=Symbol |
11 | 12 |
|
12 | 13 |
|
13 | | - |
| 14 | + //determine if a value is a date or not |
| 15 | + const test_date=new Date(), test_date_num=Number(test_date) |
| 16 | + function isDate(d){ |
| 17 | + return d? d[toPrimitive]?.bind(test_date)('number')===test_date_num: false; |
| 18 | + } |
| 19 | + function datesEqual(d1,d2){ |
| 20 | + return d1[toPrimitive]('number')===d2[toPrimitive]('number') |
| 21 | + } |
14 | 22 | //see if an enumerable property(of key) exists(in obj) |
15 | 23 | function includes(obj,key){ |
16 | 24 | if(!obj) return false; |
|
21 | 29 | //see if 2 objects are the "same"(to determine if to overwrite or not) |
22 | 30 | function same(obj1,obj2){ |
23 | 31 | if(obj1===obj2) return true; |
| 32 | + if(isDate(obj1)&&isDate(obj2)&&datesEqual(obj1,obj2)) return true; |
24 | 33 | if(typeof obj1==="symbol"&&typeof obj2==="symbol") |
25 | 34 | return obj1.description===obj2.description; |
26 | 35 | let condition1=typeof(obj1)===typeof(obj2) |
27 | 36 | let condition2=(obj1 instanceof Array)===(obj2 instanceof Array) |
28 | | - let condition3=(obj1?obj1[Symbol.toStringTag]:null) === (obj2?obj2[Symbol.toStringTag]:null) |
| 37 | + let condition3=(obj1?obj1[toStringTag]:null) === (obj2?obj2[toStringTag]:null) |
29 | 38 | let conditionAll=condition1&&condition2&&condition3&&typeof obj1==="object" |
30 | | - if(conditionAll && obj1[Symbol.toStringTag]?.includes('Array')) |
| 39 | + if(conditionAll && obj1[toStringTag]?.includes('Array')) |
31 | 40 | return obj1.length===obj2.length; |
32 | 41 | return conditionAll |
33 | 42 | } |
|
46 | 55 | } |
47 | 56 | const nonjson={ |
48 | 57 | __proto__:null, |
49 | | - undefined: function(data){return undefined}, |
| 58 | + undefined: function(){return undefined}, |
| 59 | + Date: function(data){return new Date(data)}, |
50 | 60 | Uint8Array: function(data){return new Uint8Array(data)}, |
51 | 61 | Uint8ClampedArray: function(data){return new Uint8ClampedArray(data)}, |
52 | 62 | Uint16Array: function(data){return new Uint16Array(data)}, |
|
57 | 67 | Int32Array: function(data){return new Int32Array(data)}, |
58 | 68 | Int64Array: function(data){return new Int64Array(data)}, |
59 | 69 | BigInt64Array: function(data){return new BigInt64Array(data.split(','))}, |
| 70 | + Float32Array: function(data){return new Float32Array(data)}, |
| 71 | + Float64Array: function(data){return new Float64Array(data)}, |
60 | 72 | BigInt: function(data){return BigInt(data)}, |
61 | 73 | Symbol: function(data){return Symbol(data)} |
62 | 74 | } |
|
69 | 81 | } |
70 | 82 | function casingOf(item,forClone){ |
71 | 83 | if(item===undefined || item===null) return forClone? item: null; |
72 | | - let tag=item[Symbol.toStringTag]; |
| 84 | + if(isDate(item)) |
| 85 | + return forClone? new Date(item[toPrimitive]('number')): item[toPrimitive]('number'); |
| 86 | + let tag=item[toStringTag]; |
73 | 87 | if(tag){ |
74 | 88 | if(tag==="Symbol") |
75 | 89 | return forClone? Symbol(item.description): item.description; |
|
85 | 99 | for(let i=0;i<KEYS.length;i++){ |
86 | 100 | let ITEM=item[KEYS[i]] |
87 | 101 | if(typeof ITEM==="bigint" || ITEM===undefined) continue; |
88 | | - if(!ITEM || (!ITEM[Symbol.toStringTag] && typeof ITEM!=="object")) |
| 102 | + if(!ITEM || (!ITEM[toStringTag] && typeof ITEM!=="object")) |
89 | 103 | shell[KEYS[i]] = ITEM; |
90 | 104 | } |
91 | 105 | return shell |
92 | 106 | } |
93 | 107 | /* |
94 | 108 | the value in ONE index(part) of an objToString array are 1 of the following types: |
95 | 109 | |
| 110 | + [path] //delete |
96 | 111 | [path,data] //value |
97 | 112 | [path,refPath,num] //reference |
98 | | - [path] //delete |
| 113 | + [path,data,0,tag] //custom datatype value |
99 | 114 | |
100 | 115 | - path is array of strings to represent a location |
101 | 116 | - data is an instance of a datatype to represent a value |
102 | 117 | - refPath is an index to a referred path located in another index(part) or the path array itself |
103 | 118 | - num is a number which can be 3 options: 0=not mentioned, 1=mentioned as path, 2=mentioned as reference |
| 119 | + - tag is the [Symbol.toStringTag] property of a value and is used for TypedArray, BigInt, Symbol and undefined and newly Date instances (the latter 2 which have no [Symbol.toStringTag] but isn't JSON) |
104 | 120 | */ |
105 | 121 | function recursivelyDetatch(map,cloned){ |
106 | 122 | const orig=map.get(cloned) |
|
115 | 131 | map.delete(orig) |
116 | 132 | map.delete(cloned) |
117 | 133 | } |
118 | | - function recurse(obj,clone,map,list,PATH,level,RECURSED,isTop){ |
119 | | - if(level>128) throw new RangeError("Given object goes too many levels inward (>128)"); |
| 134 | + function recurse(obj,clone,map,list,PATH,RECURSED,isTop){ |
120 | 135 | let KEYS=keys(obj), KEYS1=keys(clone), data=map.get(obj) |
121 | 136 | if(isTop) RECURSED.set(obj,true); |
122 | 137 | if(obj instanceof Array){ |
|
132 | 147 | for(let i=0;i<KEYS.length;i++){ |
133 | 148 | let key=KEYS[i], item=obj[key] |
134 | 149 | if(includes(clone,key)&&item===clone[key]) continue; |
135 | | - if(typeof item==="function"){ |
| 150 | + if(typeof item==="function"||(typeof item==="number"&&isNaN(item))){ |
136 | 151 | if(includes(clone,key)){ |
137 | 152 | delete clone[key] |
138 | 153 | list.push([ [...PATH,key] ]) //delete BECAUSE datatype is function |
139 | 154 | } |
140 | 155 | continue; |
141 | 156 | } |
142 | | - let Path=[...PATH,key], path=data[1]<list.length&&PATH.length>=2? [data[1],key]: Path; |
| 157 | + let Path=[...PATH,key], path=data[2]>0&&(data[1]<list.length&&PATH.length>=2)? [data[1],key]: Path; |
143 | 158 |
|
144 | 159 | let notSame=!same( obj[key],clone[key] ), temp=map.get(item) |
145 | 160 | if((typeof obj[key]!=="object"||!obj[key]) && (typeof clone[key]==="object"&&clone[key])) |
|
159 | 174 | list.push([ path,refPath,mentioned ]) //refer |
160 | 175 | } |
161 | 176 | else{ |
162 | | - if(item===null || (item===undefined?false:!item[Symbol.toStringTag])) |
| 177 | + if(item===null || (item===undefined?false:!item[toStringTag]&&!isDate(item))) |
163 | 178 | list.push([ path,casingOf(item) ]); //write |
164 | 179 | else{ |
165 | 180 | if(item && typeof item!=="bigint") RECURSED.set(item,true); |
166 | | - let datatype=(item||typeof item==="bigint")? item[Symbol.toStringTag]: "undefined"; |
| 181 | + let datatype=(item||typeof item==="bigint")? item[toStringTag]||"Date": "undefined"; |
167 | 182 | list.push([ path,casingOf(item),0,datatype ]); //write(for nonjson data types) |
168 | 183 | } |
169 | 184 | } |
|
177 | 192 | } |
178 | 193 | if(!RECURSED.get(item)){ |
179 | 194 | RECURSED.set(item,true); |
180 | | - recurse(obj[key],clone[key],map,list,Path,level+1,RECURSED); |
| 195 | + recurse(obj[key],clone[key],map,list,Path,RECURSED); |
181 | 196 | } |
182 | 197 | } |
183 | 198 | } |
|
195 | 210 | var {clone,map}=CACHE.get(obj)||{}; |
196 | 211 |
|
197 | 212 | const list=[ [[],!map?casingOf(obj):{}] ], path=[] |
198 | | - const tag=obj?obj[Symbol.toStringTag]||null:null |
| 213 | + const tag=obj?obj[toStringTag]||null:null |
199 | 214 | if(nonjson[tag]) list[0].push(0,tag); |
200 | 215 |
|
201 | 216 | if(!map){ |
|
206 | 221 | //mentioned is if part of path was already referenced in outgoing string |
207 | 222 | //0 means no(do nothing), 1 means path is mentioned in part[0], 2 means the same for part[1] |
208 | 223 | map.set(obj,[path,0,1,clone,1]) //see temp description in recurse function above |
209 | | - recurse(obj,clone,map,list,path,1,new WeakMap(),true) |
| 224 | + recurse(obj,clone,map,list,path,new WeakMap(),true) |
210 | 225 | return str(list) |
211 | 226 | } |
212 | 227 |
|
|
0 commit comments