serialize-anything
Version:
serialize and de-serialize any JavaScript data
1 lines • 13.6 kB
JavaScript
!function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{("undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this).SerAny=e()}}(function(){return function(){return function e(t,r,a){function n(o,s){if(!r[o]){if(!t[o]){var l="function"==typeof require&&require;if(!s&&l)return l(o,!0);if(i)return i(o,!0);var y=new Error("Cannot find module '"+o+"'");throw y.code="MODULE_NOT_FOUND",y}var u=r[o]={exports:{}};t[o][0].call(u.exports,function(e){return n(t[o][1][e]||e)},u,u.exports,e,t,r,a)}return r[o].exports}for(var i="function"==typeof require&&require,o=0;o<a.length;o++)n(a[o]);return n}}()({1:[function(e,t,r){const a=e("deep-copy-all"),n={maxDepth:20,pretty:!1};function i(e){let t="undefined"!=typeof global&&global[e]||"undefined"!=typeof window&&window[e]||"undefined"!=typeof WorkerGlobalScope&&WorkerGlobalScope[e];return"function"!=typeof t&&(t=null),t}const o=(e,t,r,a)=>{const n=a.iterate;if(!n)return;const i=a.setValue;n(e,a=>{const n=a.type,o=u[n],l=o.deserialize;o.iterate?a.value=s(a.value,t,r):l&&(a.value=l(a.value,t),i(e,a))})};function s(e,t,r){r++;let a=l(e),n=u[a];const i=n.deserialize;return i&&(e=i(e,t),a=l(e),n=u[a]),o(e,t,r,n),e}const l=e=>{let t=null;const r=e&&e.constructor&&e.constructor.name;return t=(e=>{let t=typeof e;return"number"===t||"string"===t||"boolean"===t||"symbol"===t||null===e})(e)||!e instanceof Object?"primitive":void 0===e?"undef":"bigint"==typeof e?"BigInt":e._SAType&&e._SAType.includes("_SACustom")?e._SAType:void 0!==e._SAType?e._SAType+"_Serialized":"string"==typeof r&&r.length&&u[r]?r:e instanceof Array&&"Array"!==r?"CustomArray":e instanceof Object&&"Object"!==r?"CustomObject":e instanceof Array?"CustomObject":"Object"},y=(e,t)=>{const r=e.length;for(let a=0;a<r;a++){const r=e[a];t({key:a,value:r,type:l(r)})}},u={Array:{type:Array,serialize:e=>"Array"!==e.constructor.name?{_SAType:"_SACustomArray",_SAconstructorName:e.constructor.name,_SAvalues:Array.from(e)}:e,iterate:y,setValue:(e,t)=>{e[t.key]=t.value}},Date:{type:Date,serialize:e=>({_SAType:"Date",_SAtimestamp:e.getTime()})},Date_Serialized:{deserialize:e=>new Date(e._SAtimestamp)},RegExp:{type:RegExp,serialize:e=>({_SAType:"RegExp",_SAsource:e.source,_SAflags:e.flags})},RegExp_Serialized:{deserialize:e=>new RegExp(e._SAsource,e._SAflags||"")},Function:{type:Function,serialize:e=>({_SAType:"Function",_SAfunction:e.toString()})},Function_Serialized:{deserialize:e=>new Function("return "+e._SAfunction)()},undef:{serialize:()=>({_SAType:"undef"})},undef_Serialized:{deserialize:()=>{}}};"undefined"!=typeof BigInt&&Object.assign(u,{BigInt:{type:BigInt,serialize:e=>({_SAType:"BigInt",_SAnum:e.toString()})},BigInt_Serialized:{deserialize:e=>BigInt(e._SAnum)}}),"undefined"!=typeof Int8Array&&Object.assign(u,{Int8Array:{type:Int8Array,serialize:e=>({_SAType:"Int8Array",_SAvalues:Array.from(e)})},Int8Array_Serialized:{deserialize:e=>Int8Array.from(e._SAvalues)}}),"undefined"!=typeof Uint8Array&&Object.assign(u,{Uint8Array:{type:Uint8Array,serialize:e=>({_SAType:"Uint8Array",_SAvalues:Array.from(e)})},Uint8Array_Serialized:{deserialize:e=>Uint8Array.from(e._SAvalues)}}),"undefined"!=typeof Uint8ClampedArray&&Object.assign(u,{Uint8ClampedArray:{type:Uint8ClampedArray,serialize:e=>({_SAType:"Uint8ClampedArray",_SAvalues:Array.from(e)})},Uint8ClampedArray_Serialized:{deserialize:e=>Uint8ClampedArray.from(e._SAvalues)}}),"undefined"!=typeof Int16Array&&Object.assign(u,{Int16Array:{type:Int16Array,serialize:e=>({_SAType:"Int16Array",_SAvalues:Array.from(e)})},Int16Array_Serialized:{deserialize:e=>Int16Array.from(e._SAvalues)}}),"undefined"!=typeof Uint16Array&&Object.assign(u,{Uint16Array:{type:Uint16Array,serialize:e=>({_SAType:"Uint16Array",_SAvalues:Array.from(e)})},Uint16Array_Serialized:{deserialize:e=>Uint16Array.from(e._SAvalues)}}),"undefined"!=typeof Int32Array&&Object.assign(u,{Int32Array:{type:Int32Array,serialize:e=>({_SAType:"Int32Array",_SAvalues:Array.from(e)})},Int32Array_Serialized:{deserialize:e=>Int32Array.from(e._SAvalues)}}),"undefined"!=typeof Uint32Array&&Object.assign(u,{Uint32Array:{type:Uint32Array,serialize:e=>({_SAType:"Uint32Array",_SAvalues:Array.from(e)})},Uint32Array_Serialized:{deserialize:e=>Uint32Array.from(e._SAvalues)}}),"undefined"!=typeof Float32Array&&Object.assign(u,{Float32Array:{type:Float32Array,serialize:e=>({_SAType:"Float32Array",_SAvalues:Array.from(e)})},Float32Array_Serialized:{deserialize:e=>Float32Array.from(e._SAvalues)}}),"undefined"!=typeof Float64Array&&Object.assign(u,{Float64Array:{type:Float64Array,serialize:e=>{let t=[];return e.forEach(e=>t.push(e.toString())),{_SAType:"Float64Array",_SAvalues:t}}},Float64Array_Serialized:{deserialize:e=>Float64Array.from(e._SAvalues)}}),"undefined"!=typeof BigInt64Array&&Object.assign(u,{BigInt64Array:{type:BigInt64Array,serialize:e=>{let t=[];return e.forEach(e=>t.push(e.toString())),{_SAType:"BigInt64Array",_SAvalues:t}}},BigInt64Array_Serialized:{deserialize:e=>BigInt64Array.from(e._SAvalues)}}),"undefined"!=typeof BigUint64Array&&Object.assign(u,{BigUint64Array:{type:BigUint64Array,serialize:e=>{let t=[];return e.forEach(e=>t.push(e.toString())),{_SAType:"BigUint64Array",_SAvalues:t}}},BigUint64Array_Serialized:{deserialize:e=>BigUint64Array.from(e._SAvalues)}}),"undefined"!=typeof ArrayBuffer&&Object.assign(u,{ArrayBuffer:{type:ArrayBuffer,serialize:e=>{const t=new Uint8Array(e);let r=[];return t.forEach(e=>{r.push(e)}),{_SAType:"ArrayBuffer",_SAvalues:r}}},ArrayBuffer_Serialized:{deserialize:e=>{const t=e._SAvalues,r=new ArrayBuffer(t.length);return new Uint8Array(r).set(t),r}}}),"undefined"!=typeof Map&&Object.assign(u,{Map:{type:Map,serialize:e=>{let t=[];return e.forEach((e,r)=>{t.push([r,e])}),{_SAType:"Map",_SAkvPairs:t}},iterate:(e,t)=>{e.forEach((e,r)=>{const a={key:r,value:e,type:l(e)};t(a)})},setValue:(e,t)=>{e.set(t.key,t.value)}},Map_Serialized:{deserialize:e=>{const t=e._SAkvPairs,r=new Map;return t.forEach(([e,t])=>{r.set(e,t)}),r}}}),"undefined"!=typeof Set&&Object.assign(u,{Set:{type:Set,serialize:e=>{let t=[];return e.forEach(e=>t.push(e)),{_SAType:"Set",_SAvalues:t}},iterate:(e,t)=>{e.forEach(e=>{const r={key:null,value:e,originalValue:e,type:l(e)};t(r)})},setValue:(e,t)=>{e.delete(t.originalValue),e.add(t.value),t.originalValue=t.value}},Set_Serialized:{deserialize:e=>new Set(e._SAvalues)}}),"undefined"!=typeof WeakSet&&Object.assign(u,{WeakSet:{type:WeakSet,serialize:()=>{throw"Error: serialize WeakSet not supported"}}}),"undefined"!=typeof WeakMap&&Object.assign(u,{WeakMap:{type:WeakMap,serialize:()=>{throw"Error: serialize WeakMap not supported"}}}),"undefined"!=typeof Buffer&&Object.assign(u,{Buffer:{type:Buffer,serialize:e=>({_SAType:"Buffer",_SAutf8String:e.toString()})},Buffer_Serialized:{deserialize:e=>Buffer.from(e._SAutf8String)}}),"undefined"!=typeof Error&&Object.assign(u,{Error:{type:Error,serialize:e=>({_SAType:"Error",_SAmessage:e.message,_SAstack:e.stack})},Error_Serialized:{deserialize:e=>{const t=Error(e._SAmessage);return t.stack=e._SAstack,t}}});const p=(e,t)=>{const r=Object.keys(e),a=r.length;for(let n=0;n<a;n++){const a=r[n],i=e[a];t({key:a,value:i,type:l(i)})}};Object.assign(u,{CustomArray:{serialize:e=>({_SAType:"_SACustomArray",_SAconstructorName:e.constructor.name,_SAvalues:Array.from(e)}),iterate:y,setValue:(e,t)=>{e[t.key]=t.value}},CustomObject:{serialize:e=>({_SAType:"_SACustomObject",_SAconstructorName:e.constructor.name,_SAobject:Object.assign({},e)}),iterate:p,setValue:(e,t)=>{e[t.key]=t.value}},Object:{type:Object,iterate:p,setValue:(e,t)=>{e[t.key]=t.value}},_SACustomObject:{deserialize:(e,t)=>{const r=e._SAconstructorName,a=e._SAobject,n=i(r);let o;if(n)o=new n;else{if(!t)throw"Error: deserialize _SACustomObject - getCustomObject missing";o=t(r)}if(!o)throw'Error: unable to deserialize - "'+r+'" undefined';return Object.assign(o,a),o}},_SACustomArray:{deserialize:(e,t)=>{const r=e._SAconstructorName;let a=e._SAvalues;const n=i(r);let o;if(n)o=new n(r);else{if(!t)throw"Error: deserialize _SACustomArray - getCustomObject missing";o=t(r)}if(!o)throw'Error: deserialize _SACustomArray - "'+r+'" undefined';return o=o.concat(o,a)}},unknown:{},primitive:{}}),t.exports={serialize:function(e,t){void 0===(t=t||n).maxDepth&&(t.maxDepth=n.maxDepth),void 0===t.pretty&&(t.pretty=n.pretty);let r=a(e);const i={_Serialize_Any_Encoded:!0,_SA_Content:r=function e(t,r,a){if(++a>r.maxDepth)throw"Error maximum depth exceeded - possible circular reference";let n=" ";for(let e=0;e<a;e++)n+=" ";const i=l(t),o=u[i],s=o.serialize,y=o.iterate,p=o.setValue;return y&&y(t,n=>{const i=n.type,o=u[i],s=o.serialize,l=o.iterate,y=n.value;l?n.value=e(n.value,r,a):s&&(n.value=s(n.value)),n.value!==y&&p(t,n)}),s&&(t=s(t)),t}(r,t,0)};return t.pretty?JSON.stringify(i,null,2):JSON.stringify(i)},deserialize:function(e,t){let r=JSON.parse(e);if("Object"!==l(r)||!r._Serialize_Any_Encoded)throw"Error: object was not serialized by serialize-any";return s(r=r._SA_Content,t,0)}}},{"deep-copy-all":2}],2:[function(e,t,r){"use strict";const[a,n,i]=e("./object-library.js"),o={goDeep:!0,includeNonEnumerable:!1,detectCircular:!0,maxDepth:20};class s{constructor(){this._seenMap=new WeakMap}setAsCopied(e,t){e instanceof Object&&this._seenMap.set(e,t)}wasCopied(e){return e instanceof Object&&this._seenMap.has(e)}getCopy(e){return this._seenMap.get(e)}}const l=(e,t,r)=>{const{destObject:a,srcType:n,watcher:o,options:s}=t,y=s.detectCircular;!function(e,t){if(e>=t)throw`Error max depth of ${t} levels exceeded, possible circular reference`}(++r,s.maxDepth);const u=i(n);if(!u.mayDeepCopy)return;const p=u.addElement;u.iterate(e,s.includeNonEnumerable,e=>{const t=e.value,n=e.type,u=i(n);let c,f=!1;y&&o.wasCopied(t)?(c=o.getCopy(t),f=!0):c=function(e,t,r){const{options:a,watcher:n}=r;let i;return t.mayDeepCopy?(i=t.makeEmpty(e),a.detectCircular&&n.setAsCopied(e,i)):i=t.makeShallow(e),i}(t,u,{options:s,watcher:o}),p(a,e.key,c,e.descriptor),u.mayDeepCopy&&!f&&l(t,{destObject:c,srcType:n,watcher:o,options:s},r)})};t.exports=function(e,t=o){if(function(e){Object.keys(o).forEach(t=>{void 0===e[t]&&(e[t]=o[t])})}(t),a(e))return e;const r=n(e),y=i(r);if(!t.goDeep||!y.mayDeepCopy)return y.makeShallow(e);const u=t.detectCircular?new s:null;let p=y.makeEmpty(e);return t.detectCircular&&u.setAsCopied(e,p),l(e,{destObject:p,srcType:r,watcher:u,options:t},0),p}},{"./object-library.js":3}],3:[function(e,t,r){const a={},n=e=>{let t=typeof e;return"number"===t||"string"===t||"boolean"===t||"undefined"===t||"bigint"===t||"symbol"===t||null===e},i=e=>{if(n(e)||!(e instanceof Object))return"primitive";const t=e.constructor&&e.constructor.name&&e.constructor.name.toLowerCase();if("string"==typeof t&&t.length&&a[t])return t;let r;for(const t in a)if(!(r=a[t].type)||e instanceof r)return t;return"unknown"},o=(e,t,r)=>Array.prototype.push.call(e,r),s=e=>{const t=[];return Object.setPrototypeOf(t,Object.getPrototypeOf(e)),t},l=e=>{const t=[...e];return Object.setPrototypeOf(t,Object.getPrototypeOf(e)),t},y=(e,t,r)=>{const a=e.length;for(let t=0;t<a;t++){const a=e[t];r({key:t,value:a,type:i(a)})}},u=(e,t,r,a)=>{a?Object.defineProperty(e,t,a):e[t]=r},p=e=>{const t={};return Object.setPrototypeOf(t,Object.getPrototypeOf(e)),t},c=e=>{const t=Object.assign({},e);return Object.setPrototypeOf(t,Object.getPrototypeOf(e)),t},f=(e,t,r)=>{const a=t?Object.getOwnPropertyNames(e):Object.keys(e),n=a.length;for(let o=0;o<n;o++){const n=a[o],s=e[n],l={key:n,value:s,type:i(s)};t&&!e.propertyIsEnumerable(n)&&(l.descriptor=Object.getOwnPropertyDescriptor(e,n)),r(l)}};Object.assign(a,{array:{type:Array,mayDeepCopy:!0,addElement:o,makeEmpty:s,makeShallow:l,iterate:y}}),Object.assign(a,{date:{type:Date,makeShallow:e=>new Date(e.getTime())}}),Object.assign(a,{regexp:{type:RegExp,makeShallow:e=>new RegExp(e)}}),Object.assign(a,{function:{type:Function,makeShallow:e=>e}}),Object.assign(a,{error:{type:Error,makeShallow:e=>{const t=new Error(e.message);return t.stack=e.stack,t}}}),["Int8Array","Uint8Array","Uint8ClampedArray","Int16Array","Uint16Array","Int32Array","Uint32Array","Float32Array","Float32Array","Float64Array","BigInt64Array","BigUint64Array"].forEach(e=>(e=>{let t="undefined"!=typeof global&&global[e]||"undefined"!=typeof window&&window[e]||"undefined"!=typeof WorkerGlobalScope&&WorkerGlobalScope[e];void 0!==t&&(a[e.toLowerCase()]={type:t,makeShallow:e=>t.from(e)})})(e)),"undefined"!=typeof ArrayBuffer&&Object.assign(a,{arraybuffer:{type:ArrayBuffer,makeShallow:e=>e.slice(0)}}),"undefined"!=typeof Map&&Object.assign(a,{map:{type:Map,mayDeepCopy:!0,addElement:(e,t,r)=>e.set(t,r),makeEmpty:()=>new Map,makeShallow:e=>new Map(e),iterate:(e,t,r)=>{e.forEach((e,t)=>{const a={key:t,value:e,type:i(e)};r(a)})}}}),"undefined"!=typeof Set&&Object.assign(a,{set:{type:Set,mayDeepCopy:!0,addElement:(e,t,r)=>e.add(r),makeEmpty:()=>new Set,makeShallow:e=>new Set(e),iterate:(e,t,r)=>{e.forEach(e=>{const t={key:null,value:e,type:i(e)};r(t)})}}}),"undefined"!=typeof WeakSet&&Object.assign(a,{weakset:{type:WeakSet,makeShallow:e=>e}}),"undefined"!=typeof WeakMap&&Object.assign(a,{weakmap:{type:WeakMap,makeShallow:e=>e}}),"undefined"!=typeof Buffer&&Object.assign(a,{buffer:{type:Buffer,makeShallow:e=>Buffer.from(e)}}),Object.assign(a,{object:{type:Object,mayDeepCopy:!0,addElement:u,makeEmpty:p,makeShallow:c,iterate:f}}),Object.assign(a,{unknown:{makeShallow:e=>e},primitive:{makeShallow:e=>e}}),t.exports=[n,i,function(e){return a[e]}]},{}]},{},[1])(1)});