universal-serialize
Advanced tools
Comparing version 1.0.5 to 1.0.6
@@ -53,2 +53,8 @@ import { TYPE } from './constants'; | ||
} | ||
} | ||
export function serializeType(type, val) { | ||
return { | ||
__type__: type, | ||
__val__: val | ||
}; | ||
} |
@@ -25,18 +25,25 @@ var _DESERIALIZER; | ||
var type = isSerializedType(val) ? val.__type__ : determineType(val); | ||
var type; | ||
var value; | ||
if (isSerializedType(val)) { | ||
type = val.__type__; | ||
value = val.__val__; | ||
} else { | ||
type = determineType(val); | ||
value = val; | ||
} | ||
if (!type) { | ||
return val; | ||
return value; | ||
} // $FlowFixMe | ||
if (deserializers[type]) { | ||
// $FlowFixMe | ||
return deserializers[type](val.__val__, key); | ||
} else if (DESERIALIZER[type]) { | ||
// $FlowFixMe | ||
return DESERIALIZER[type](val, key); | ||
} else { | ||
return val; | ||
var deserializer = deserializers[type] || DESERIALIZER[type]; | ||
if (!deserializer) { | ||
return value; | ||
} | ||
return deserializer(value, key); | ||
} | ||
@@ -43,0 +50,0 @@ |
@@ -5,2 +5,3 @@ export * from './serialize'; | ||
export * from './constants'; | ||
export * from './common'; | ||
export * from './types'; |
@@ -44,8 +44,2 @@ var _SERIALIZER; | ||
return result; | ||
} | ||
export function serializeType(type, val) { | ||
return { | ||
__type__: type, | ||
__val__: val | ||
}; | ||
} |
@@ -0,11 +1,8 @@ | ||
import { serializeType } from '../common'; | ||
import { TYPE } from '../constants'; | ||
export function serializeDate(val) { | ||
return { | ||
__type__: TYPE.DATE, | ||
__date__: val.toJSON() | ||
}; | ||
return serializeType(TYPE.DATE, val.toJSON()); | ||
} | ||
export function deserializeDate(_ref) { | ||
var __date__ = _ref.__date__; | ||
return new Date(__date__); | ||
export function deserializeDate(val) { | ||
return new Date(val); | ||
} |
@@ -0,20 +1,23 @@ | ||
import { serializeType } from '../common'; | ||
import { TYPE } from '../constants'; | ||
export function serializeError(val) { | ||
return { | ||
__type__: TYPE.ERROR, | ||
__message__: val.message, | ||
__stack__: val.stack, | ||
// $FlowFixMe | ||
__code__: val.code | ||
}; | ||
// $FlowFixMe | ||
export function serializeError(_ref) { | ||
var message = _ref.message, | ||
stack = _ref.stack, | ||
code = _ref.code; | ||
return serializeType(TYPE.ERROR, { | ||
message: message, | ||
stack: stack, | ||
code: code | ||
}); | ||
} | ||
export function deserializeError(_ref) { | ||
var __message__ = _ref.__message__, | ||
__code__ = _ref.__code__, | ||
__stack__ = _ref.__stack__; | ||
var error = new Error(__message__); // $FlowFixMe | ||
export function deserializeError(_ref2) { | ||
var message = _ref2.message, | ||
stack = _ref2.stack, | ||
code = _ref2.code; | ||
var error = new Error(message); // $FlowFixMe | ||
error.code = __code__; | ||
error.stack = __stack__ + "\n\n" + error.stack; | ||
error.code = code; | ||
error.stack = stack + "\n\n" + error.stack; | ||
return error; | ||
} |
@@ -0,12 +1,9 @@ | ||
import { serializeType } from '../common'; | ||
import { TYPE } from '../constants'; | ||
export function serializeRegex(val) { | ||
return { | ||
__type__: TYPE.REGEX, | ||
__source__: val.source | ||
}; | ||
return serializeType(TYPE.REGEX, val.source); | ||
} | ||
export function deserializeRegex(_ref) { | ||
var __source__ = _ref.__source__; | ||
export function deserializeRegex(val) { | ||
// eslint-disable-next-line security/detect-non-literal-regexp | ||
return new RegExp(__source__); | ||
return new RegExp(val); | ||
} |
@@ -1,2 +0,2 @@ | ||
!function(e,n){"object"==typeof exports&&"object"==typeof module?module.exports=n():"function"==typeof define&&define.amd?define("universal-serialize",[],n):"object"==typeof exports?exports["universal-serialize"]=n():e["universal-serialize"]=n()}("undefined"!=typeof self?self:this,function(){return function(r){var t={};function i(e){if(t[e])return t[e].exports;var n=t[e]={i:e,l:!1,exports:{}};return r[e].call(n.exports,n,n.exports,i),n.l=!0,n.exports}return i.m=r,i.c=t,i.d=function(e,n,r){i.o(e,n)||Object.defineProperty(e,n,{enumerable:!0,get:r})},i.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},i.t=function(n,e){if(1&e&&(n=i(n)),8&e)return n;if(4&e&&"object"==typeof n&&n&&n.__esModule)return n;var r=Object.create(null);if(i.r(r),Object.defineProperty(r,"default",{enumerable:!0,value:n}),2&e&&"string"!=typeof n)for(var t in n)i.d(r,t,function(e){return n[e]}.bind(null,t));return r},i.n=function(e){var n=e&&e.__esModule?function(){return e.default}:function(){return e};return i.d(n,"a",n),n},i.o=function(e,n){return Object.prototype.hasOwnProperty.call(e,n)},i.p="",i(i.s=0)}([function(e,n,r){"use strict";r.r(n);var t,o={FUNCTION:"function",ERROR:"error",PROMISE:"promise",REGEX:"regex",DATE:"date",ARRAY:"array",OBJECT:"object",STRING:"string",NUMBER:"number",BOOLEAN:"boolean",NULL:"null",UNDEFINED:"undefined"};function u(e){return"object"==typeof e&&null!==e&&"string"==typeof e.__type__}function c(e){return void 0===e?o.UNDEFINED:null===e?o.NULL:Array.isArray(e)?o.ARRAY:"function"==typeof e?o.FUNCTION:"object"==typeof e?e instanceof Error?o.ERROR:"function"==typeof e.then?o.PROMISE:"[object RegExp]"===Object.prototype.toString.call(e)?o.REGEX:"[object Date]"===Object.prototype.toString.call(e)?o.DATE:o.OBJECT:"string"==typeof e?o.STRING:"number"==typeof e?o.NUMBER:"boolean"==typeof e?o.BOOLEAN:void 0}function i(e){return e}function f(e){return e}function a(e){return e}function l(e){return e}function s(e){return{__type__:o.DATE,__date__:e.toJSON()}}function d(e){var n=e.__date__;return new Date(n)}function _(e){return{__type__:o.ERROR,__message__:e.message,__stack__:e.stack,__code__:e.code}}function E(e){var n=e.__message__,r=e.__code__,t=e.__stack__,i=new Error(n);return i.code=r,i.stack=t+"\n\n"+i.stack,i}function p(){}function R(){throw new Error("Function serialization is not implemented; nothing to deserialize")}function y(e){return e}function N(e){return e}function O(e){return e}function z(e){return e}function b(){}function v(){throw new Error("Promise serialization is not implemented; nothing to deserialize")}function g(e){return{__type__:o.REGEX,__source__:e.source}}function m(e){var n=e.__source__;return new RegExp(n)}function A(e){return e}function T(e){return e}function S(e){return e}function j(e){return e}var D,I=((t={})[o.FUNCTION]=p,t[o.ERROR]=_,t[o.PROMISE]=b,t[o.REGEX]=g,t[o.DATE]=s,t[o.ARRAY]=i,t[o.OBJECT]=O,t[o.STRING]=A,t[o.NUMBER]=y,t[o.BOOLEAN]=a,t[o.NULL]=S,t),U={};function x(e,i){void 0===i&&(i=U);var n=JSON.stringify(e,function(e){var n=this[e];if(u(this))return n;var r=c(n);if(!r)return n;var t=i[r]||I[r];return t?t(n,e):n});return void 0===n?o.UNDEFINED:n}function B(e,n){return{__type__:e,__val__:n}}var P=((D={})[o.FUNCTION]=R,D[o.ERROR]=E,D[o.PROMISE]=v,D[o.REGEX]=m,D[o.DATE]=d,D[o.ARRAY]=f,D[o.OBJECT]=z,D[o.STRING]=T,D[o.NUMBER]=N,D[o.BOOLEAN]=l,D[o.NULL]=j,D),L={};function M(e,t){if(void 0===t&&(t=L),e!==o.UNDEFINED)return JSON.parse(e,function(e,n){if(u(this))return n;var r=u(n)?n.__type__:c(n);return r?t[r]?t[r](n.__val__,e):P[r]?P[r](n,e):n:n})}r.d(n,"serialize",function(){return x}),r.d(n,"serializeType",function(){return B}),r.d(n,"deserialize",function(){return M}),r.d(n,"serializeArray",function(){return i}),r.d(n,"deserializeArray",function(){return f}),r.d(n,"serializeBoolean",function(){return a}),r.d(n,"deserializeBoolean",function(){return l}),r.d(n,"serializeDate",function(){return s}),r.d(n,"deserializeDate",function(){return d}),r.d(n,"serializeError",function(){return _}),r.d(n,"deserializeError",function(){return E}),r.d(n,"serializeFunction",function(){return p}),r.d(n,"deserializeFunction",function(){return R}),r.d(n,"serializeNumber",function(){return y}),r.d(n,"deserializeNumber",function(){return N}),r.d(n,"serializeObject",function(){return O}),r.d(n,"deserializeObject",function(){return z}),r.d(n,"serializePromise",function(){return b}),r.d(n,"deserializePromise",function(){return v}),r.d(n,"serializeRegex",function(){return g}),r.d(n,"deserializeRegex",function(){return m}),r.d(n,"serializeString",function(){return A}),r.d(n,"deserializeString",function(){return T}),r.d(n,"serializeNull",function(){return S}),r.d(n,"deserializeNull",function(){return j}),r.d(n,"TYPE",function(){return o})}])}); | ||
!function(e,n){"object"==typeof exports&&"object"==typeof module?module.exports=n():"function"==typeof define&&define.amd?define("universal-serialize",[],n):"object"==typeof exports?exports["universal-serialize"]=n():e["universal-serialize"]=n()}("undefined"!=typeof self?self:this,function(){return function(r){var t={};function i(e){if(t[e])return t[e].exports;var n=t[e]={i:e,l:!1,exports:{}};return r[e].call(n.exports,n,n.exports,i),n.l=!0,n.exports}return i.m=r,i.c=t,i.d=function(e,n,r){i.o(e,n)||Object.defineProperty(e,n,{enumerable:!0,get:r})},i.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},i.t=function(n,e){if(1&e&&(n=i(n)),8&e)return n;if(4&e&&"object"==typeof n&&n&&n.__esModule)return n;var r=Object.create(null);if(i.r(r),Object.defineProperty(r,"default",{enumerable:!0,value:n}),2&e&&"string"!=typeof n)for(var t in n)i.d(r,t,function(e){return n[e]}.bind(null,t));return r},i.n=function(e){var n=e&&e.__esModule?function(){return e.default}:function(){return e};return i.d(n,"a",n),n},i.o=function(e,n){return Object.prototype.hasOwnProperty.call(e,n)},i.p="",i(i.s=0)}([function(e,n,r){"use strict";r.r(n);var t,u={FUNCTION:"function",ERROR:"error",PROMISE:"promise",REGEX:"regex",DATE:"date",ARRAY:"array",OBJECT:"object",STRING:"string",NUMBER:"number",BOOLEAN:"boolean",NULL:"null",UNDEFINED:"undefined"};function f(e){return"object"==typeof e&&null!==e&&"string"==typeof e.__type__}function c(e){return void 0===e?u.UNDEFINED:null===e?u.NULL:Array.isArray(e)?u.ARRAY:"function"==typeof e?u.FUNCTION:"object"==typeof e?e instanceof Error?u.ERROR:"function"==typeof e.then?u.PROMISE:"[object RegExp]"===Object.prototype.toString.call(e)?u.REGEX:"[object Date]"===Object.prototype.toString.call(e)?u.DATE:u.OBJECT:"string"==typeof e?u.STRING:"number"==typeof e?u.NUMBER:"boolean"==typeof e?u.BOOLEAN:void 0}function i(e,n){return{__type__:e,__val__:n}}function o(e){return e}function a(e){return e}function l(e){return e}function s(e){return e}function d(e){return i(u.DATE,e.toJSON())}function E(e){return new Date(e)}function p(e){var n=e.message,r=e.stack,t=e.code;return i(u.ERROR,{message:n,stack:r,code:t})}function R(e){var n=e.message,r=e.stack,t=e.code,i=new Error(n);return i.code=t,i.stack=r+"\n\n"+i.stack,i}function y(){}function N(){throw new Error("Function serialization is not implemented; nothing to deserialize")}function O(e){return e}function z(e){return e}function b(e){return e}function v(e){return e}function _(){}function m(){throw new Error("Promise serialization is not implemented; nothing to deserialize")}function g(e){return i(u.REGEX,e.source)}function T(e){return new RegExp(e)}function A(e){return e}function S(e){return e}function j(e){return e}function D(e){return e}var I,U=((t={})[u.FUNCTION]=y,t[u.ERROR]=p,t[u.PROMISE]=_,t[u.REGEX]=g,t[u.DATE]=d,t[u.ARRAY]=o,t[u.OBJECT]=b,t[u.STRING]=A,t[u.NUMBER]=O,t[u.BOOLEAN]=l,t[u.NULL]=j,t),x={};function B(e,i){void 0===i&&(i=x);var n=JSON.stringify(e,function(e){var n=this[e];if(f(this))return n;var r=c(n);if(!r)return n;var t=i[r]||U[r];return t?t(n,e):n});return void 0===n?u.UNDEFINED:n}var P=((I={})[u.FUNCTION]=N,I[u.ERROR]=R,I[u.PROMISE]=m,I[u.REGEX]=T,I[u.DATE]=E,I[u.ARRAY]=a,I[u.OBJECT]=v,I[u.STRING]=S,I[u.NUMBER]=z,I[u.BOOLEAN]=s,I[u.NULL]=D,I),L={};function M(e,o){if(void 0===o&&(o=L),e!==u.UNDEFINED)return JSON.parse(e,function(e,n){if(f(this))return n;var r,t;if(t=f(n)?(r=n.__type__,n.__val__):(r=c(n),n),!r)return t;var i=o[r]||P[r];return i?i(t,e):t})}r.d(n,"serialize",function(){return B}),r.d(n,"deserialize",function(){return M}),r.d(n,"serializeArray",function(){return o}),r.d(n,"deserializeArray",function(){return a}),r.d(n,"serializeBoolean",function(){return l}),r.d(n,"deserializeBoolean",function(){return s}),r.d(n,"serializeDate",function(){return d}),r.d(n,"deserializeDate",function(){return E}),r.d(n,"serializeError",function(){return p}),r.d(n,"deserializeError",function(){return R}),r.d(n,"serializeFunction",function(){return y}),r.d(n,"deserializeFunction",function(){return N}),r.d(n,"serializeNumber",function(){return O}),r.d(n,"deserializeNumber",function(){return z}),r.d(n,"serializeObject",function(){return b}),r.d(n,"deserializeObject",function(){return v}),r.d(n,"serializePromise",function(){return _}),r.d(n,"deserializePromise",function(){return m}),r.d(n,"serializeRegex",function(){return g}),r.d(n,"deserializeRegex",function(){return T}),r.d(n,"serializeString",function(){return A}),r.d(n,"deserializeString",function(){return S}),r.d(n,"serializeNull",function(){return j}),r.d(n,"deserializeNull",function(){return D}),r.d(n,"TYPE",function(){return u}),r.d(n,"isSerializedType",function(){return f}),r.d(n,"determineType",function(){return c}),r.d(n,"serializeType",function(){return i})}])}); | ||
//# sourceMappingURL=universalSerialize.js.map |
@@ -1,2 +0,2 @@ | ||
!function(e,n){"object"==typeof exports&&"object"==typeof module?module.exports=n():"function"==typeof define&&define.amd?define("universal-serialize",[],n):"object"==typeof exports?exports["universal-serialize"]=n():e["universal-serialize"]=n()}("undefined"!=typeof self?self:this,function(){return function(r){var t={};function i(e){if(t[e])return t[e].exports;var n=t[e]={i:e,l:!1,exports:{}};return r[e].call(n.exports,n,n.exports,i),n.l=!0,n.exports}return i.m=r,i.c=t,i.d=function(e,n,r){i.o(e,n)||Object.defineProperty(e,n,{enumerable:!0,get:r})},i.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},i.t=function(n,e){if(1&e&&(n=i(n)),8&e)return n;if(4&e&&"object"==typeof n&&n&&n.__esModule)return n;var r=Object.create(null);if(i.r(r),Object.defineProperty(r,"default",{enumerable:!0,value:n}),2&e&&"string"!=typeof n)for(var t in n)i.d(r,t,function(e){return n[e]}.bind(null,t));return r},i.n=function(e){var n=e&&e.__esModule?function(){return e.default}:function(){return e};return i.d(n,"a",n),n},i.o=function(e,n){return Object.prototype.hasOwnProperty.call(e,n)},i.p="",i(i.s=0)}([function(e,n,r){"use strict";r.r(n);var t,o={FUNCTION:"function",ERROR:"error",PROMISE:"promise",REGEX:"regex",DATE:"date",ARRAY:"array",OBJECT:"object",STRING:"string",NUMBER:"number",BOOLEAN:"boolean",NULL:"null",UNDEFINED:"undefined"};function u(e){return"object"==typeof e&&null!==e&&"string"==typeof e.__type__}function c(e){return void 0===e?o.UNDEFINED:null===e?o.NULL:Array.isArray(e)?o.ARRAY:"function"==typeof e?o.FUNCTION:"object"==typeof e?e instanceof Error?o.ERROR:"function"==typeof e.then?o.PROMISE:"[object RegExp]"===Object.prototype.toString.call(e)?o.REGEX:"[object Date]"===Object.prototype.toString.call(e)?o.DATE:o.OBJECT:"string"==typeof e?o.STRING:"number"==typeof e?o.NUMBER:"boolean"==typeof e?o.BOOLEAN:void 0}function i(e){return e}function f(e){return e}function a(e){return e}function l(e){return e}function s(e){return{__type__:o.DATE,__date__:e.toJSON()}}function d(e){var n=e.__date__;return new Date(n)}function _(e){return{__type__:o.ERROR,__message__:e.message,__stack__:e.stack,__code__:e.code}}function E(e){var n=e.__message__,r=e.__code__,t=e.__stack__,i=new Error(n);return i.code=r,i.stack=t+"\n\n"+i.stack,i}function p(){}function R(){throw new Error("Function serialization is not implemented; nothing to deserialize")}function y(e){return e}function N(e){return e}function O(e){return e}function z(e){return e}function b(){}function v(){throw new Error("Promise serialization is not implemented; nothing to deserialize")}function g(e){return{__type__:o.REGEX,__source__:e.source}}function m(e){var n=e.__source__;return new RegExp(n)}function A(e){return e}function T(e){return e}function S(e){return e}function j(e){return e}var D,I=((t={})[o.FUNCTION]=p,t[o.ERROR]=_,t[o.PROMISE]=b,t[o.REGEX]=g,t[o.DATE]=s,t[o.ARRAY]=i,t[o.OBJECT]=O,t[o.STRING]=A,t[o.NUMBER]=y,t[o.BOOLEAN]=a,t[o.NULL]=S,t),U={};function x(e,i){void 0===i&&(i=U);var n=JSON.stringify(e,function(e){var n=this[e];if(u(this))return n;var r=c(n);if(!r)return n;var t=i[r]||I[r];return t?t(n,e):n});return void 0===n?o.UNDEFINED:n}function B(e,n){return{__type__:e,__val__:n}}var P=((D={})[o.FUNCTION]=R,D[o.ERROR]=E,D[o.PROMISE]=v,D[o.REGEX]=m,D[o.DATE]=d,D[o.ARRAY]=f,D[o.OBJECT]=z,D[o.STRING]=T,D[o.NUMBER]=N,D[o.BOOLEAN]=l,D[o.NULL]=j,D),L={};function M(e,t){if(void 0===t&&(t=L),e!==o.UNDEFINED)return JSON.parse(e,function(e,n){if(u(this))return n;var r=u(n)?n.__type__:c(n);return r?t[r]?t[r](n.__val__,e):P[r]?P[r](n,e):n:n})}r.d(n,"serialize",function(){return x}),r.d(n,"serializeType",function(){return B}),r.d(n,"deserialize",function(){return M}),r.d(n,"serializeArray",function(){return i}),r.d(n,"deserializeArray",function(){return f}),r.d(n,"serializeBoolean",function(){return a}),r.d(n,"deserializeBoolean",function(){return l}),r.d(n,"serializeDate",function(){return s}),r.d(n,"deserializeDate",function(){return d}),r.d(n,"serializeError",function(){return _}),r.d(n,"deserializeError",function(){return E}),r.d(n,"serializeFunction",function(){return p}),r.d(n,"deserializeFunction",function(){return R}),r.d(n,"serializeNumber",function(){return y}),r.d(n,"deserializeNumber",function(){return N}),r.d(n,"serializeObject",function(){return O}),r.d(n,"deserializeObject",function(){return z}),r.d(n,"serializePromise",function(){return b}),r.d(n,"deserializePromise",function(){return v}),r.d(n,"serializeRegex",function(){return g}),r.d(n,"deserializeRegex",function(){return m}),r.d(n,"serializeString",function(){return A}),r.d(n,"deserializeString",function(){return T}),r.d(n,"serializeNull",function(){return S}),r.d(n,"deserializeNull",function(){return j}),r.d(n,"TYPE",function(){return o})}])}); | ||
!function(e,n){"object"==typeof exports&&"object"==typeof module?module.exports=n():"function"==typeof define&&define.amd?define("universal-serialize",[],n):"object"==typeof exports?exports["universal-serialize"]=n():e["universal-serialize"]=n()}("undefined"!=typeof self?self:this,function(){return function(r){var t={};function i(e){if(t[e])return t[e].exports;var n=t[e]={i:e,l:!1,exports:{}};return r[e].call(n.exports,n,n.exports,i),n.l=!0,n.exports}return i.m=r,i.c=t,i.d=function(e,n,r){i.o(e,n)||Object.defineProperty(e,n,{enumerable:!0,get:r})},i.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},i.t=function(n,e){if(1&e&&(n=i(n)),8&e)return n;if(4&e&&"object"==typeof n&&n&&n.__esModule)return n;var r=Object.create(null);if(i.r(r),Object.defineProperty(r,"default",{enumerable:!0,value:n}),2&e&&"string"!=typeof n)for(var t in n)i.d(r,t,function(e){return n[e]}.bind(null,t));return r},i.n=function(e){var n=e&&e.__esModule?function(){return e.default}:function(){return e};return i.d(n,"a",n),n},i.o=function(e,n){return Object.prototype.hasOwnProperty.call(e,n)},i.p="",i(i.s=0)}([function(e,n,r){"use strict";r.r(n);var t,u={FUNCTION:"function",ERROR:"error",PROMISE:"promise",REGEX:"regex",DATE:"date",ARRAY:"array",OBJECT:"object",STRING:"string",NUMBER:"number",BOOLEAN:"boolean",NULL:"null",UNDEFINED:"undefined"};function f(e){return"object"==typeof e&&null!==e&&"string"==typeof e.__type__}function c(e){return void 0===e?u.UNDEFINED:null===e?u.NULL:Array.isArray(e)?u.ARRAY:"function"==typeof e?u.FUNCTION:"object"==typeof e?e instanceof Error?u.ERROR:"function"==typeof e.then?u.PROMISE:"[object RegExp]"===Object.prototype.toString.call(e)?u.REGEX:"[object Date]"===Object.prototype.toString.call(e)?u.DATE:u.OBJECT:"string"==typeof e?u.STRING:"number"==typeof e?u.NUMBER:"boolean"==typeof e?u.BOOLEAN:void 0}function i(e,n){return{__type__:e,__val__:n}}function o(e){return e}function a(e){return e}function l(e){return e}function s(e){return e}function d(e){return i(u.DATE,e.toJSON())}function E(e){return new Date(e)}function p(e){var n=e.message,r=e.stack,t=e.code;return i(u.ERROR,{message:n,stack:r,code:t})}function R(e){var n=e.message,r=e.stack,t=e.code,i=new Error(n);return i.code=t,i.stack=r+"\n\n"+i.stack,i}function y(){}function N(){throw new Error("Function serialization is not implemented; nothing to deserialize")}function O(e){return e}function z(e){return e}function b(e){return e}function v(e){return e}function _(){}function m(){throw new Error("Promise serialization is not implemented; nothing to deserialize")}function g(e){return i(u.REGEX,e.source)}function T(e){return new RegExp(e)}function A(e){return e}function S(e){return e}function j(e){return e}function D(e){return e}var I,U=((t={})[u.FUNCTION]=y,t[u.ERROR]=p,t[u.PROMISE]=_,t[u.REGEX]=g,t[u.DATE]=d,t[u.ARRAY]=o,t[u.OBJECT]=b,t[u.STRING]=A,t[u.NUMBER]=O,t[u.BOOLEAN]=l,t[u.NULL]=j,t),x={};function B(e,i){void 0===i&&(i=x);var n=JSON.stringify(e,function(e){var n=this[e];if(f(this))return n;var r=c(n);if(!r)return n;var t=i[r]||U[r];return t?t(n,e):n});return void 0===n?u.UNDEFINED:n}var P=((I={})[u.FUNCTION]=N,I[u.ERROR]=R,I[u.PROMISE]=m,I[u.REGEX]=T,I[u.DATE]=E,I[u.ARRAY]=a,I[u.OBJECT]=v,I[u.STRING]=S,I[u.NUMBER]=z,I[u.BOOLEAN]=s,I[u.NULL]=D,I),L={};function M(e,o){if(void 0===o&&(o=L),e!==u.UNDEFINED)return JSON.parse(e,function(e,n){if(f(this))return n;var r,t;if(t=f(n)?(r=n.__type__,n.__val__):(r=c(n),n),!r)return t;var i=o[r]||P[r];return i?i(t,e):t})}r.d(n,"serialize",function(){return B}),r.d(n,"deserialize",function(){return M}),r.d(n,"serializeArray",function(){return o}),r.d(n,"deserializeArray",function(){return a}),r.d(n,"serializeBoolean",function(){return l}),r.d(n,"deserializeBoolean",function(){return s}),r.d(n,"serializeDate",function(){return d}),r.d(n,"deserializeDate",function(){return E}),r.d(n,"serializeError",function(){return p}),r.d(n,"deserializeError",function(){return R}),r.d(n,"serializeFunction",function(){return y}),r.d(n,"deserializeFunction",function(){return N}),r.d(n,"serializeNumber",function(){return O}),r.d(n,"deserializeNumber",function(){return z}),r.d(n,"serializeObject",function(){return b}),r.d(n,"deserializeObject",function(){return v}),r.d(n,"serializePromise",function(){return _}),r.d(n,"deserializePromise",function(){return m}),r.d(n,"serializeRegex",function(){return g}),r.d(n,"deserializeRegex",function(){return T}),r.d(n,"serializeString",function(){return A}),r.d(n,"deserializeString",function(){return S}),r.d(n,"serializeNull",function(){return j}),r.d(n,"deserializeNull",function(){return D}),r.d(n,"TYPE",function(){return u}),r.d(n,"isSerializedType",function(){return f}),r.d(n,"determineType",function(){return c}),r.d(n,"serializeType",function(){return i})}])}); | ||
//# sourceMappingURL=universalSerialize.min.js.map |
{ | ||
"name": "universal-serialize", | ||
"version": "1.0.5", | ||
"version": "1.0.6", | ||
"description": "Javascript module template.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
/* @flow */ | ||
import { TYPE } from './constants'; | ||
import type { CustomSerializedType } from './types'; | ||
@@ -59,1 +60,8 @@ export function isSerializedType(item : mixed) : boolean { | ||
} | ||
export function serializeType<T : string, V : mixed>(type : T, val : V) : CustomSerializedType<T, V> { | ||
return { | ||
__type__: type, | ||
__val__: val | ||
}; | ||
} |
/* @flow */ | ||
import type { Thenable, NativeSerializedType, CustomSerializedType } from './types'; | ||
import type { Thenable } from './types'; | ||
import { TYPE } from './constants'; | ||
@@ -8,6 +8,6 @@ import { determineType, isSerializedType } from './common'; | ||
deserializeFunction, | ||
deserializeError, | ||
deserializeError, type SerializedError, | ||
deserializePromise, | ||
deserializeRegex, | ||
deserializeDate, | ||
deserializeRegex, type SerializedRegex, | ||
deserializeDate, type SerializedDate, | ||
deserializeArray, | ||
@@ -21,17 +21,18 @@ deserializeObject, | ||
type Deserializer<O : mixed> = (NativeSerializedType | CustomSerializedType | mixed, string) => O; | ||
type Deserializer<V : mixed, S : mixed> = (serializedValue : S, key : string) => V; | ||
type PrimitiveDeserializer<V, S = V> = (serializedValue : S, key : string) => V; | ||
type Deserializers = { | ||
function? : Deserializer<Function>, | ||
error? : Deserializer<Error>, | ||
promise? : Deserializer<Thenable>, | ||
regex? : Deserializer<RegExp>, | ||
date? : Deserializer<Date>, | ||
array? : Deserializer<$ReadOnlyArray<mixed>>, | ||
object? : Deserializer<Object>, | ||
string? : Deserializer<string>, | ||
number? : Deserializer<number>, | ||
boolean? : Deserializer<boolean>, | ||
null? : Deserializer<null>, | ||
[string] : Deserializer<mixed> | ||
function? : Deserializer<Function, *>, | ||
error? : Deserializer<Error, SerializedError>, | ||
promise? : Deserializer<Thenable, *>, | ||
regex? : Deserializer<RegExp, SerializedRegex>, | ||
date? : Deserializer<Date, SerializedDate>, | ||
array? : PrimitiveDeserializer<$ReadOnlyArray<mixed>>, | ||
object? : PrimitiveDeserializer<Object>, | ||
string? : PrimitiveDeserializer<string>, | ||
number? : PrimitiveDeserializer<number>, | ||
boolean? : PrimitiveDeserializer<boolean>, | ||
null? : PrimitiveDeserializer<null>, | ||
[string] : Deserializer<mixed, *> | ||
}; | ||
@@ -68,20 +69,25 @@ | ||
const type = isSerializedType(val) | ||
? val.__type__ | ||
: determineType(val); | ||
let type; | ||
let value; | ||
if (isSerializedType(val)) { | ||
type = val.__type__; | ||
value = val.__val__; | ||
} else { | ||
type = determineType(val); | ||
value = val; | ||
} | ||
if (!type) { | ||
return val; | ||
return value; | ||
} | ||
// $FlowFixMe | ||
if (deserializers[type]) { | ||
// $FlowFixMe | ||
return deserializers[type](val.__val__, key); | ||
} else if (DESERIALIZER[type]) { | ||
// $FlowFixMe | ||
return DESERIALIZER[type](val, key); | ||
} else { | ||
return val; | ||
const deserializer = deserializers[type] || DESERIALIZER[type]; | ||
if (!deserializer) { | ||
return value; | ||
} | ||
return deserializer(value, key); | ||
} | ||
@@ -88,0 +94,0 @@ |
@@ -7,2 +7,3 @@ /* @flow */ | ||
export * from './constants'; | ||
export * from './common'; | ||
export * from './types'; |
@@ -8,6 +8,6 @@ /* @flow */ | ||
serializeFunction, | ||
serializeError, | ||
serializeError, type SerializedError, | ||
serializePromise, | ||
serializeRegex, | ||
serializeDate, | ||
serializeRegex, type SerializedRegex, | ||
serializeDate, type SerializedDate, | ||
serializeArray, | ||
@@ -21,16 +21,20 @@ serializeObject, | ||
type Serializer<I : mixed> = (I, string) => NativeSerializedType | CustomSerializedType | mixed | void; | ||
type NativeSerializer<V : mixed, S : mixed, T : $Values<typeof TYPE>> = (value : V, key : string) => NativeSerializedType<T, S>; | ||
type CustomSerializer<V : mixed, S : mixed, T : string> = (value : V, key : string) => CustomSerializedType<T, S>; | ||
type PrimitiveSerializer<V : mixed, S : mixed> = (value : V, key : string) => S; | ||
type CustomOrPrimitiveSerializer<V : mixed, T : string> = CustomSerializer<V, *, T> | PrimitiveSerializer<V, *>; | ||
type NativeOrCustomOrPrimitiveSerializer<V : mixed, S : mixed, T : string> = NativeSerializer<V, S, T> | CustomOrPrimitiveSerializer<V, T>; | ||
type Serializers = {| | ||
function? : Serializer<Function>, | ||
error? : Serializer<Error>, | ||
promise? : Serializer<Thenable>, | ||
regex? : Serializer<RegExp>, | ||
date? : Serializer<Date>, | ||
array? : Serializer<$ReadOnlyArray<mixed>>, | ||
object? : Serializer<Object>, | ||
string? : Serializer<string>, | ||
number? : Serializer<number>, | ||
boolean? : Serializer<boolean>, | ||
null? : Serializer<null> | ||
function? : CustomOrPrimitiveSerializer<Function, typeof TYPE.FUNCTION>, | ||
error? : NativeOrCustomOrPrimitiveSerializer<Error, SerializedError, typeof TYPE.ERROR>, | ||
promise? : CustomOrPrimitiveSerializer<Thenable, typeof TYPE.PROMISE>, | ||
regex? : NativeOrCustomOrPrimitiveSerializer<RegExp, SerializedRegex, typeof TYPE.REGEX>, | ||
date? : NativeOrCustomOrPrimitiveSerializer<Date, SerializedDate, typeof TYPE.DATE>, | ||
array? : CustomOrPrimitiveSerializer<$ReadOnlyArray<mixed>, typeof TYPE.ARRAY>, | ||
object? : CustomOrPrimitiveSerializer<Object, typeof TYPE.OBJECT>, | ||
string? : CustomOrPrimitiveSerializer<string, typeof TYPE.STRING>, | ||
number? : CustomOrPrimitiveSerializer<number, typeof TYPE.NUMBER>, | ||
boolean? : CustomOrPrimitiveSerializer<boolean, typeof TYPE.BOOLEAN>, | ||
null? : CustomOrPrimitiveSerializer<null, typeof TYPE.NULL> | ||
|}; | ||
@@ -88,8 +92,1 @@ | ||
} | ||
export function serializeType<T : string, V : mixed>(type : T, val : V) : CustomSerializedType { | ||
return { | ||
__type__: type, | ||
__val__: val | ||
}; | ||
} |
/* @flow */ | ||
import { serializeType } from '../common'; | ||
import { TYPE } from '../constants'; | ||
import type { NativeSerializedType } from '../types'; | ||
export type SerializedDate = {| | ||
__type__ : typeof TYPE.DATE, | ||
__date__ : string | ||
|}; | ||
export type SerializedDate = string; | ||
export function serializeDate(val : Date) : SerializedDate { | ||
return { | ||
__type__: TYPE.DATE, | ||
__date__: val.toJSON() | ||
}; | ||
export function serializeDate(val : Date) : NativeSerializedType<typeof TYPE.DATE, SerializedDate> { | ||
return serializeType(TYPE.DATE, val.toJSON()); | ||
} | ||
export function deserializeDate({ __date__ } : SerializedDate) : Date { | ||
return new Date(__date__); | ||
export function deserializeDate(val : string) : Date { | ||
return new Date(val); | ||
} |
/* @flow */ | ||
import { serializeType } from '../common'; | ||
import { TYPE } from '../constants'; | ||
import type { NativeSerializedType } from '../types'; | ||
export type SerializedError = {| | ||
__type__ : typeof TYPE.ERROR, | ||
__message__ : string, | ||
__stack__ : string, | ||
__code__ : string | number | void | ||
message : string, | ||
stack : string, | ||
code : string | number | void | ||
|}; | ||
export function serializeError(val : Error) : SerializedError { | ||
return { | ||
__type__: TYPE.ERROR, | ||
__message__: val.message, | ||
__stack__: val.stack, | ||
// $FlowFixMe | ||
__code__: val.code | ||
}; | ||
// $FlowFixMe | ||
export function serializeError({ message, stack, code } : Error) : NativeSerializedType<typeof TYPE.ERROR, SerializedError> { | ||
return serializeType(TYPE.ERROR, { message, stack, code }); | ||
} | ||
export function deserializeError({ __message__, __code__, __stack__ } : SerializedError) : Error { | ||
const error = new Error(__message__); | ||
export function deserializeError({ message, stack, code } : SerializedError) : Error { | ||
const error = new Error(message); | ||
// $FlowFixMe | ||
error.code = __code__; | ||
error.stack = `${ __stack__ }\n\n${ error.stack }`; | ||
error.code = code; | ||
error.stack = `${ stack }\n\n${ error.stack }`; | ||
return error; | ||
} |
/* @flow */ | ||
import { serializeType } from '../common'; | ||
import { TYPE } from '../constants'; | ||
import type { NativeSerializedType } from '../types'; | ||
export type SerializedRegex = {| | ||
__type__ : typeof TYPE.REGEX, | ||
__source__ : string | ||
|}; | ||
export type SerializedRegex = string; | ||
export function serializeRegex(val : RegExp) : SerializedRegex { | ||
return { | ||
__type__: TYPE.REGEX, | ||
__source__: val.source | ||
}; | ||
export function serializeRegex(val : RegExp) : NativeSerializedType<typeof TYPE.REGEX, SerializedRegex> { | ||
return serializeType(TYPE.REGEX, val.source); | ||
} | ||
export function deserializeRegex({ __source__ } : SerializedRegex) : RegExp { | ||
export function deserializeRegex(val : string) : RegExp { | ||
// eslint-disable-next-line security/detect-non-literal-regexp | ||
return new RegExp(__source__); | ||
return new RegExp(val); | ||
} |
@@ -7,18 +7,15 @@ /* @flow */ | ||
export type Thenable = { | ||
then : () => Thenable, | ||
catch : () => Thenable | ||
then : (onSuccess? : (val? : mixed) => mixed, onError? : (err? : mixed) => mixed) => Thenable, | ||
catch : (onError? : (err? : mixed) => mixed) => Thenable | ||
}; | ||
// eslint-disable-next-line flowtype/require-exact-type | ||
export type SerializedType = { | ||
__type__ : string | ||
export type NativeSerializedType<T : $Values<typeof TYPE>, V : mixed> = { | ||
__type__ : T, | ||
__val__ : V | ||
}; | ||
export type NativeSerializedType = SerializedType & { | ||
__type__ : $Values<typeof TYPE> | ||
}; | ||
export type CustomSerializedType = SerializedType & {| | ||
__type__ : string, | ||
__val__ : mixed | ||
export type CustomSerializedType<T : string, V : mixed> = {| | ||
__type__ : T, | ||
__val__ : V | ||
|}; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
95299
626