UNPKG

@macfja/serializer

Version:

Transform any object, class, array, primitive to a serialized string and vice-versa

2 lines (1 loc) 5.63 kB
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var r=function(e,t){return r=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(r,e){r.__proto__=e}||function(r,e){for(var t in e)Object.prototype.hasOwnProperty.call(e,t)&&(r[t]=e[t])},r(e,t)};var e=function(){return e=Object.assign||function(r){for(var e,t=1,n=arguments.length;t<n;t++)for(var o in e=arguments[t])Object.prototype.hasOwnProperty.call(e,o)&&(r[o]=e[o]);return r},e.apply(this,arguments)};function t(r,e,t){if(t||2===arguments.length)for(var n,o=0,i=e.length;o<i;o++)!n&&o in e||(n||(n=Array.prototype.slice.call(e,0,o)),n[o]=e[o]);return r.concat(n||Array.prototype.slice.call(e))}var n=function(e){function t(){return null!==e&&e.apply(this,arguments)||this}return function(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Class extends value "+String(t)+" is not a constructor or null");function n(){this.constructor=e}r(e,t),e.prototype=null===t?Object.create(t):(n.prototype=t.prototype,new n)}(t,e),t}(Error),o={},i=new Set,a={};function u(r,t){if("string"==typeof r||"number"==typeof r&&!isNaN(r)&&Math.abs(r)!==1/0||null==r||"boolean"==typeof r)return r;var n=t.indexOf(r);if(-1!==n)return"#$@__instance__"+n;t.push(r);var o=t.length-1;if(r.constructor===Array){var c=r.map((function(r){return u(r,t)}));return c.unshift("#$@__reference__"+o),c}if(r.constructor===Object){var f=Object.fromEntries(Object.entries(r).map((function(r){return[r[0],u(r[1],t)]})));return f["#$@__reference__"]=o,f}i.add(r.constructor);var s={};s=Object.defineProperty(s,"#$@__constructor__",{value:r.constructor.name,enumerable:!0}),s=Object.defineProperty(s,"#$@__reference__",{value:o,enumerable:!0});var l=function(r,e){if(!Object.keys(a).includes(r.constructor.name))return;return a[r.constructor.name].toPlain(r,(function(r){return u(r,e)}))}(r,t);return void 0!==l?e(e({},l),s):(Object.getOwnPropertyNames(r).forEach((function(e){Object.defineProperty(s,e,{value:u(r[e],t),enumerable:!0})})),s)}function c(r,e,t){var o;if("string"==typeof r&&0===r.indexOf("#$@__instance__"))return t[s=parseInt(r.slice("#$@__instance__".length))];if("string"==typeof r||"number"==typeof r||null==r||"boolean"==typeof r)return r;if(r.constructor===Array){if(0===r.length)return[];var i=r.shift();if("string"!=typeof i||"#$@__reference__"!==i.substring(0,"#$@__reference__".length)){if(0===Object.keys(t).length)return r.unshift(i),r;throw new n}var u=[];t[s=i.slice("#$@__reference__".length)]=u;for(var f=0;f<r.length;f++)u[f]=c(r[f],e,t);return u}if(r.constructor===Object){var s;if(null===(s=null!==(o=r["#$@__reference__"])&&void 0!==o?o:null)){if(0===Object.keys(t).length)return r;throw new n}if(delete r["#$@__reference__"],!Object.keys(r).includes("#$@__constructor__")){u={};for(var l in t[s]=u,r)u[l]=c(r[l],e,t);return u}var p=r["#$@__constructor__"];delete r["#$@__constructor__"];var y=function(r,e,t,n){if(!Object.keys(a).includes(e))return;return a[e].fromPlain(r,(function(r){return c(r,t,n)}))}(r,p,e,t);if(void 0!==y)return y;if(!Object.keys(e).includes(p))throw new Error("The class "+p+" is not allowed");var _={};for(var l in t[s]=_,r)_[l]=c(r[l],e,t);return Object.setPrototypeOf(_,e[p].prototype),_}}function f(){i.clear()}function s(r){o[r.name]=r}function l(r,e,t){a[r]={toPlain:e,fromPlain:t}}!function(r,e){r("Date",(r=>({time:r.getTime()})),(function(r){let e=new Date;return e.setTime(r.time),e})),r("BigInt",(r=>({number:r.toString()})),(r=>BigInt(r.number))),r("String",(r=>({text:r.toString()})),(r=>new String(r.text))),r("RegExp",(r=>({source:r.source,flags:r.flags})),(r=>new RegExp(r.source,r.flags))),r("Number",(function(r){let e={nan:!1,infinity:!1,positive:!0,number:null};return Math.abs(r)===1/0?(e.infinity=!0,e.positive=Math.abs(r)===r,e):isNaN(r)?(e.nan=!0,e):(e.number=r.valueOf(),e)}),(function(r){return r.nan?NaN:r.infinity?1/0*(r.positive?1:-1):new Number(r.number)})),r("Map",((r,e)=>({data:Array.from(r.entries()).map((r=>e(r)))})),(function(r,e){const t=new Map;return r.data.map((r=>e(r))).forEach((([r,e])=>t.set(r,e))),t})),r("Set",((r,e)=>({data:Array.from(r.values()).map((r=>e(r)))})),(function(r,e){const t=new Set;return r.data.map((r=>e(r))).forEach((r=>t.add(r))),t})),r("ArrayBuffer",((r,e)=>({data:e(new Uint8Array(r))})),((r,e)=>e(r.data).buffer)),r("DataView",((r,e)=>({buffer:e(r.buffer),offset:r.byteOffset,length:r.byteLength})),((r,e)=>new DataView(e(r.buffer),r.offset,r.length))),["Error","EvalError","RangeError","AggregateError","ReferenceError","SyntaxError","TypeError","URIError","Int8Array","Uint8Array","Uint8ClampedArray","Int16Array","Uint16Array","Int32Array","Uint32Array","Float32Array","Float64Array","BigInt64Array","BigUint64Array"].forEach((r=>{const t="undefined"!=typeof globalThis&&globalThis||"undefined"!=typeof global&&global;t&&void 0!==t[r]&&e(t[r])}))}(l,s),exports.addClassHandler=l,exports.addGlobalAllowedClass=s,exports.deserialize=function(r,e){if(void 0===e&&(e=void 0),"string"!=typeof r)return r;if(void 0===e&&(e={}),"undefined"!==r){e=Object.fromEntries(t(t([],Object.entries(e),!0),Object.entries(o),!0));try{return c(JSON.parse(r),e,{})}catch(e){return r}}},exports.getCollectedClasses=function(r){void 0===r&&(r=!1);var e=Array.from(i);return r&&f(),e},exports.resetCollectedClasses=f,exports.serialize=function(r){return void 0===r?"undefined":JSON.stringify(u(r,[]))},exports.setGlobalAllowedClasses=function(r,e){void 0===e&&(e=!1);var n={};r.constructor===Array&&(n=Object.fromEntries(r.map((function(r){return[r.name,r]})))),"object"==typeof r&&(n=r),o=e?Object.fromEntries(t(t([],Object.entries(o),!0),Object.entries(n),!0)):n};