Comparing version 1.5.2 to 1.5.3
@@ -0,1 +1,6 @@ | ||
# 1.5.3 | ||
* #105: Support prop schemas for "*" properties by @pyrogenic | ||
* #104: Add the `@optional` decorator to skip properties not present in the serialized object by @pyrogenic | ||
* #99: Allow `serialize`-ing plain objects by specifying constructor by @pyrogenic | ||
# 1.5.2 | ||
@@ -2,0 +7,0 @@ * Fixed potential memory leak, fixes #95 through #100 by @svennergr |
@@ -399,3 +399,3 @@ /** | ||
* | ||
* @param arg1 modelschema to use. Optional | ||
* @param arg1 class or modelschema to use. Optional | ||
* @param arg2 object(s) to serialize | ||
@@ -413,4 +413,8 @@ * @returns {object} serialized representation of the object | ||
schema = getDefaultModelSchema(thing[0]); | ||
else if (typeof schema !== "object") | ||
schema = getDefaultModelSchema(schema); | ||
} else if (!schema) { | ||
schema = getDefaultModelSchema(thing); | ||
} else if (typeof schema !== "object") { | ||
schema = getDefaultModelSchema(schema); | ||
} | ||
@@ -425,4 +429,8 @@ invariant(!!schema, "Failed to find default schema for " + arg1); | ||
function checkStarSchemaInvariant(propDef) { | ||
invariant(propDef === true || propDef.pattern, `prop schema '*' can only be used with 'true' or a prop def with a 'pattern': ${JSON.stringify(propDef)}`); | ||
} | ||
function serializeWithSchema(schema, obj) { | ||
invariant(schema && typeof schema === "object", "Expected schema"); | ||
invariant(schema && typeof schema === "object" && schema.props, "Expected schema"); | ||
invariant(obj && typeof obj === "object", "Expected object"); | ||
@@ -439,4 +447,3 @@ var res; | ||
if (key === "*") { | ||
invariant(propDef === true, "prop schema '*' can only be used with 'true'"); | ||
serializeStarProps(schema, obj, res); | ||
serializeStarProps(schema, propDef, obj, res); | ||
return | ||
@@ -457,38 +464,30 @@ } | ||
function serializeStarProps(schema, obj, target) { | ||
function serializeStarProps(schema, propDef, obj, target) { | ||
checkStarSchemaInvariant(propDef); | ||
for (var key in obj) if (obj.hasOwnProperty(key)) if (!(key in schema.props)) { | ||
var value = obj[key]; | ||
// when serializing only serialize primitive props. Assumes other props (without schema) are local state that doesn't need serialization | ||
if (isPrimitive(value)) | ||
target[key] = value; | ||
if ((propDef === true) || (propDef.pattern && propDef.pattern.test(key))) { | ||
var value = obj[key]; | ||
if (propDef === true) { | ||
if (isPrimitive(value)) { | ||
target[key] = value; | ||
} | ||
} else if (propDef.props) { | ||
var jsonValue = serialize(propDef, value); | ||
if (jsonValue === SKIP){ | ||
return | ||
} | ||
// todo: propDef.jsonname could be a transform function on key | ||
target[key] = jsonValue; | ||
} else { | ||
var jsonValue = propDef.serializer(value, key, obj); | ||
if (jsonValue === SKIP){ | ||
return | ||
} | ||
// todo: propDef.jsonname could be a transform function on key | ||
target[key] = jsonValue; | ||
} | ||
} | ||
} | ||
} | ||
/** | ||
* The `serializeAll` decorator can be used on a class to signal that all primitive properties should be serialized automatically. | ||
* | ||
* @example | ||
* @serializeAll class Store { | ||
* a = 3; | ||
* b; | ||
* } | ||
* | ||
* const store = new Store(); | ||
* store.c = 5; | ||
* store.d = {}; | ||
* t.deepEqual(serialize(store), { a: 3, b: undefined, c: 5 }); | ||
*/ | ||
function serializeAll(target) { | ||
invariant(arguments.length === 1 && typeof target === "function", "@serializeAll can only be used as class decorator"); | ||
var info = getDefaultModelSchema(target); | ||
if (!info || !target.hasOwnProperty("serializeInfo")) { | ||
info = createModelSchema(target, {}); | ||
setDefaultModelSchema(target, info); | ||
} | ||
getDefaultModelSchema(target).props["*"] = true; | ||
return target | ||
} | ||
var rootContextCache = new WeakMap(); | ||
@@ -621,19 +620,2 @@ | ||
/** | ||
* Cancels an asynchronous deserialization or update operation for the specified target object. | ||
* @param instance object that was previously returned from deserialize or update method | ||
*/ | ||
function cancelDeserialize(instance) { | ||
invariant(typeof instance === "object" && instance && !Array.isArray(instance), "cancelDeserialize needs an object"); | ||
var context = getTargetContext(instance); | ||
if (context) { | ||
context.cancelAwaits(); | ||
} | ||
} | ||
/* | ||
* Deserialization | ||
*/ | ||
function schemaHasAlias(schema, name) { | ||
@@ -646,11 +628,34 @@ for (var key in schema.props) | ||
function deserializeStarProps(schema, obj, json) { | ||
function deserializeStarProps(context, schema, propDef, obj, json) { | ||
checkStarSchemaInvariant(propDef); | ||
for (var key in json) if (!(key in schema.props) && !schemaHasAlias(schema, key)) { | ||
var value = json[key]; | ||
// when deserializing we don't want to silently ignore 'unparseable data' to avoid | ||
// confusing bugs | ||
invariant(isPrimitive(value), | ||
"encountered non primitive value while deserializing '*' properties in property '" + | ||
key + "': " + value); | ||
obj[key] = value; | ||
var jsonValue = json[key]; | ||
if (propDef === true) { | ||
// when deserializing we don't want to silently ignore 'unparseable data' to avoid | ||
// confusing bugs | ||
invariant(isPrimitive(jsonValue), | ||
"encountered non primitive value while deserializing '*' properties in property '" + | ||
key + "': " + jsonValue); | ||
obj[key] = jsonValue; | ||
} else if (propDef.pattern.test(key)) { | ||
if (propDef.factory) { | ||
var resultValue = deserializeObjectWithSchema(context, propDef, jsonValue, context.callback || GUARDED_NOOP, {}); | ||
// deserializeObjectWithSchema returns undefined on error | ||
if (resultValue !== undefined) { | ||
obj[key] = resultValue; | ||
} | ||
} else { | ||
function setValue(resultValue) { | ||
if (resultValue !== SKIP) { | ||
obj[key] = resultValue; | ||
} | ||
} | ||
propDef.deserializer(jsonValue, | ||
// for individual props, use root context based callbacks | ||
// this allows props to complete after completing the object itself | ||
// enabling reference resolving and such | ||
context.rootContext.createCallback(setValue), | ||
context); | ||
} | ||
} | ||
} | ||
@@ -766,6 +771,4 @@ } | ||
} | ||
if (propName === "*") { | ||
invariant(propDef === true, "prop schema '*' can only be used with 'true'"); | ||
deserializeStarProps(modelSchema, target, json); | ||
deserializeStarProps(context, modelSchema, propDef, target, json); | ||
return | ||
@@ -806,3 +809,133 @@ } | ||
/** | ||
* `object` indicates that this property contains an object that needs to be (de)serialized | ||
* using its own model schema. | ||
* | ||
* N.B. mind issues with circular dependencies when importing model schema's from other files! The module resolve algorithm might expose classes before `createModelSchema` is executed for the target class. | ||
* | ||
* @example | ||
* class SubTask {} | ||
* class Todo {} | ||
* | ||
* createModelSchema(SubTask, { | ||
* title: true, | ||
* }); | ||
* createModelSchema(Todo, { | ||
* title: true, | ||
* subTask: object(SubTask), | ||
* }); | ||
* | ||
* const todo = deserialize(Todo, { | ||
* title: 'Task', | ||
* subTask: { | ||
* title: 'Sub task', | ||
* }, | ||
* }); | ||
* | ||
* @param {ModelSchema} modelSchema to be used to (de)serialize the object | ||
* @param {AdditionalPropArgs} additionalArgs optional object that contains beforeDeserialize and/or afterDeserialize handlers | ||
* @returns {PropSchema} | ||
*/ | ||
function object(modelSchema, additionalArgs) { | ||
invariant(typeof modelSchema === "object" || typeof modelSchema === "function", "No modelschema provided. If you are importing it from another file be aware of circular dependencies."); | ||
var result = { | ||
serializer: function (item) { | ||
modelSchema = getDefaultModelSchema(modelSchema); | ||
invariant(isModelSchema(modelSchema), "expected modelSchema, got " + modelSchema); | ||
if (item === null || item === undefined) | ||
return item | ||
return serialize(modelSchema, item) | ||
}, | ||
deserializer: function (childJson, done, context) { | ||
modelSchema = getDefaultModelSchema(modelSchema); | ||
invariant(isModelSchema(modelSchema), "expected modelSchema, got " + modelSchema); | ||
if (childJson === null || childJson === undefined) | ||
return void done(null, childJson) | ||
return void deserializeObjectWithSchema(context, modelSchema, childJson, done, additionalArgs) | ||
} | ||
}; | ||
result = processAdditionalPropArgs(result, additionalArgs); | ||
return result | ||
} | ||
/** | ||
* The `serializeAll` decorator can may used on a class to signal that all primitive properties, | ||
* or complex properties with a name matching a `pattern`, should be serialized automatically. | ||
* | ||
* @example | ||
* @serializeAll class Store { | ||
* a = 3; | ||
* b; | ||
* } | ||
* | ||
* const store = new Store(); | ||
* store.c = 5; | ||
* store.d = {}; | ||
* t.deepEqual(serialize(store), { c: 5 }); | ||
* | ||
* @example | ||
* class DataType { | ||
* @serializable | ||
* x; | ||
* @serializable | ||
* y; | ||
* } | ||
* @serializeAll(/^[a-z]$/, DataType) class ComplexStore { | ||
* } | ||
* | ||
* const store = new ComplexStore(); | ||
* store.a = {x: 1, y: 2}; | ||
* store.b = {}; | ||
* store.somethingElse = 5; | ||
* t.deepEqual(serialize(store), { a: {x: 1, y: 2}, b: { x: undefined, y: undefined } }); | ||
*/ | ||
function serializeAll(targetOrPattern, clazzOrSchema) { | ||
let propSchema; | ||
let invokeImmediately = false; | ||
if (arguments.length === 1) { | ||
invariant(typeof targetOrPattern === "function", "@serializeAll can only be used as class decorator"); | ||
propSchema = true; | ||
invokeImmediately = true; | ||
} | ||
else { | ||
invariant(typeof targetOrPattern === "object" && targetOrPattern.test, "@serializeAll pattern doesn't have test"); | ||
if (typeof clazzOrSchema === "function") { | ||
clazzOrSchema = object(clazzOrSchema); | ||
} | ||
invariant(typeof clazzOrSchema === "object" && clazzOrSchema.serializer, "couldn't resolve schema"); | ||
propSchema = Object.assign({}, clazzOrSchema, {pattern: targetOrPattern}); | ||
} | ||
function result(target) { | ||
var info = getDefaultModelSchema(target); | ||
if (!info || !target.hasOwnProperty("serializeInfo")) { | ||
info = createModelSchema(target, {}); | ||
setDefaultModelSchema(target, info); | ||
} | ||
getDefaultModelSchema(target).props["*"] = propSchema; | ||
return target; | ||
} | ||
if (invokeImmediately) { | ||
return result(targetOrPattern); | ||
} | ||
return result; | ||
} | ||
/* | ||
* Deserialization | ||
*/ | ||
/** | ||
* Cancels an asynchronous deserialization or update operation for the specified target object. | ||
* @param instance object that was previously returned from deserialize or update method | ||
*/ | ||
function cancelDeserialize(instance) { | ||
invariant(typeof instance === "object" && instance && !Array.isArray(instance), "cancelDeserialize needs an object"); | ||
var context = getTargetContext(instance); | ||
if (context) { | ||
context.cancelAwaits(); | ||
} | ||
} | ||
/* | ||
* Update | ||
@@ -1042,50 +1175,28 @@ */ | ||
/** | ||
* `object` indicates that this property contains an object that needs to be (de)serialized | ||
* using its own model schema. | ||
* Optional indicates that this model property shouldn't be serialized if it isn't present. | ||
* | ||
* N.B. mind issues with circular dependencies when importing model schema's from other files! The module resolve algorithm might expose classes before `createModelSchema` is executed for the target class. | ||
* | ||
* @example | ||
* class SubTask {} | ||
* class Todo {} | ||
* | ||
* createModelSchema(SubTask, { | ||
* title: true, | ||
* }); | ||
* createModelSchema(Todo, { | ||
* title: true, | ||
* subTask: object(SubTask), | ||
* title: optional(primitive()), | ||
* }); | ||
* | ||
* const todo = deserialize(Todo, { | ||
* title: 'Task', | ||
* subTask: { | ||
* title: 'Sub task', | ||
* }, | ||
* }); | ||
* console.dir(serialize(new Todo())); | ||
* // {} | ||
* | ||
* @param {ModelSchema} modelSchema to be used to (de)serialize the object | ||
* @param {AdditionalPropArgs} additionalArgs optional object that contains beforeDeserialize and/or afterDeserialize handlers | ||
* @param {PropSchema} propSchema propSchema to (de)serialize the contents of this field | ||
* @returns {PropSchema} | ||
*/ | ||
function object(modelSchema, additionalArgs) { | ||
invariant(typeof modelSchema === "object" || typeof modelSchema === "function", "No modelschema provided. If you are importing it from another file be aware of circular dependencies."); | ||
var result = { | ||
serializer: function (item) { | ||
modelSchema = getDefaultModelSchema(modelSchema); | ||
invariant(isModelSchema(modelSchema), "expected modelSchema, got " + modelSchema); | ||
if (item === null || item === undefined) | ||
return item | ||
return serialize(modelSchema, item) | ||
}, | ||
deserializer: function (childJson, done, context) { | ||
modelSchema = getDefaultModelSchema(modelSchema); | ||
invariant(isModelSchema(modelSchema), "expected modelSchema, got " + modelSchema); | ||
if (childJson === null || childJson === undefined) | ||
return void done(null, childJson) | ||
return void deserializeObjectWithSchema(context, modelSchema, childJson, done, additionalArgs) | ||
function optional(name, propSchema) { | ||
propSchema = (!propSchema || propSchema === true) ? _defaultPrimitiveProp : propSchema; | ||
invariant(isPropSchema(propSchema), "expected prop schema as second argument"); | ||
const propSerializer = propSchema.serializer; | ||
invariant(typeof propSerializer === "function", "expected prop schema to have a callable serializer"); | ||
function serializer(...args) { | ||
const result = propSerializer(...args); | ||
if (result === undefined) { | ||
return SKIP | ||
} | ||
}; | ||
result = processAdditionalPropArgs(result, additionalArgs); | ||
return result | ||
return result | ||
} | ||
return Object.assign({}, propSchema, {serializer}) | ||
} | ||
@@ -1448,2 +1559,2 @@ | ||
export { createSimpleSchema, createModelSchema, getDefaultModelSchema, setDefaultModelSchema, serializable, serialize, serializeAll, cancelDeserialize, deserialize, update, primitive, identifier, date, alias, custom, object, object as child, reference, reference as ref, list, map, mapAsArray, raw, SKIP }; | ||
export { createSimpleSchema, createModelSchema, getDefaultModelSchema, setDefaultModelSchema, serializable, serialize, serializeAll, cancelDeserialize, deserialize, update, primitive, identifier, date, alias, custom, object, object as child, optional, reference, reference as ref, list, map, mapAsArray, raw, SKIP }; |
/** serializr - (c) Michel Weststrate 2016 - MIT Licensed */ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define("serializr",["exports"],t):t((e=e||self).serializr={})}(this,function(e){"use strict";var t={j:function(e){try{return JSON.stringify(e)}catch(t){return"[UnexpectedJSONParseError]: "+t.message}}};function n(e,n){if(!e){var r=Array.prototype.slice.call(arguments,2),i=[],o=0,a=n.replace(/%([a-zA-Z%])/g,function(e,n){if("%%"===e)return e;var a=t[n];if("function"==typeof a){var s=r[o++];return i.push(s),a(s)}return e});throw console&&i.length>0&&console.log.apply(console,i),new Error("[serializr] "+(a||"Illegal State"))}}function r(e){if(e)throw new Error(e)}function i(e,t,n){if(0!==e.length){var r=e.filter(function(){return!0}).length,i=[],o=!1;e.forEach(function(e,a){t(e,function(e,t,a){t?o||(o=!0,n(t)):(i[e]=a,0==--r&&n(null,i))}.bind(null,a),a)})}else n(null,[])}function o(e){return null===e||"object"!=typeof e&&"function"!=typeof e}function a(e){return e&&e.factory&&e.props}function s(e){return e&&e.serializer&&e.deserializer}function f(e){return"object"==typeof e&&!!e.jsonname}function u(e){return"object"==typeof e&&!0===e.identifier}function c(e,t){for(;e;){if(e===t)return!0;e=e["extends"]}return!1}function l(e){return e&&"function"==typeof e.keys&&"function"==typeof e.clear}function p(e,t){if(t){n(s(e),"expected a propSchema");["beforeDeserialize","afterDeserialize"].forEach(function(n){"function"==typeof t[n]&&(e[n]=t[n])})}return e}function d(e){return e?a(e)?e:a(e.serializeInfo)?e.serializeInfo:e.constructor&&e.constructor.serializeInfo?e.constructor.serializeInfo:void 0:null}function h(e,t){return n(a(t)),e.serializeInfo=t}function y(e,t,r){n(e!==Object,"one cannot simply put define a model schema for Object"),n("function"==typeof e,"expected constructor function");var i={targetClass:e,factory:r||function(){return new e},props:t};if(e.prototype.constructor!==Object){var o=d(e.prototype.constructor);o&&o.targetClass!==e&&(i["extends"]=o)}return h(e,i),i}function m(e){var t={serializer:function(e){return n(o(e),"this value is not primitive: "+e),e},deserializer:function(e,t){o(e)?t(null,e):t("[serializr] this value is not primitive: "+e)}};return t=p(t,e)}var g="undefined"!=typeof Symbol?Symbol("SKIP"):{SKIP:!0},v=m(),b=/((\/\/.*$)|(\/\*[\s\S]*?\*\/))/gm,z=/([^\s,]+)/g;function j(e,t,r,i){var o,a,f;if(n(arguments.length>=2,"too few arguments. Please use @serializable as property decorator"),r===undefined&&"function"==typeof t&&t.prototype&&i!==undefined&&"number"==typeof i){n(s(e),"Constructor params must use alias(name)"),n(e.jsonname,"Constructor params must use alias(name)");var u=(a=t.toString().replace(b,""),null===(f=a.slice(a.indexOf("(")+1,a.indexOf(")")).match(z))&&(f=[]),f);u.length>=i&&(r=u[i],e.paramNumber=i,i=undefined,t=t.prototype,o=function(e){for(var n=[],r=0;r<t.constructor.length;r++)Object.keys(e.modelSchema.props).forEach(function(t){var i=e.modelSchema.props[t];i.paramNumber===r&&(n[r]=e.json[i.jsonname])});return new(Function.prototype.bind.apply(t.constructor,[null].concat(n)))})}n("string"==typeof r,"incorrect usage of @serializable decorator");var c=d(t);return c&&t.constructor.hasOwnProperty("serializeInfo")||(c=y(t.constructor,{},o)),c&&c.targetClass!==t.constructor&&(c=y(t.constructor,{},o)),c.props[r]=e,!i||i.get||i.set||(i.writable=!0),i}function x(e,t){n(1===arguments.length||2===arguments.length,"serialize expects one or 2 arguments");var r=1===arguments.length?e:t,i=1===arguments.length?null:e;if(Array.isArray(r)){if(0===r.length)return[];i||(i=d(r[0]))}else i||(i=d(r));return n(!!i,"Failed to find default schema for "+e),Array.isArray(r)?r.map(function(e){return R(i,e)}):R(i,r)}function R(e,t){var r;return n(e&&"object"==typeof e,"Expected schema"),n(t&&"object"==typeof t,"Expected object"),r=e["extends"]?R(e["extends"],t):{},Object.keys(e.props).forEach(function(i){var a=e.props[i];if("*"===i)return n(!0===a,"prop schema '*' can only be used with 'true'"),void function(e,t,n){for(var r in t)if(t.hasOwnProperty(r)&&!(r in e.props)){var i=t[r];o(i)&&(n[r]=i)}}(e,t,r);if(!0===a&&(a=v),!1!==a){var s=a.serializer(t[i],i,t);s!==g&&(r[a.jsonname||i]=s)}}),r}var C=new WeakMap;function w(e,t,n,i,o){this.parentContext=e,this.isRoot=!e,this.pendingCallbacks=0,this.pendingRefsCount=0,this.onReadyCb=i||r,this.json=n,this.target=null,this.hasError=!1,this.modelSchema=t,this.isRoot?(this.rootContext=this,this.args=o,this.pendingRefs={},this.resolvedRefs={}):(this.rootContext=e.rootContext,this.args=e.args)}function S(e,t){for(var n in e.props)if("object"==typeof e.props[n]&&e.props[n].jsonname===t)return!0;return!1}function k(e,t,i,o,a){if(null!==i&&i!==undefined&&"object"==typeof i){var s=new w(e,t,i,o,a),f=t.factory(s);n(!!f,"No object returned from factory"),s.setTarget(f);var u=s.createCallback(r);return A(s,t,i,f),u(),f}o(null,null)}function A(e,t,r,i){t["extends"]&&A(e,t["extends"],r,i),Object.keys(t.props).forEach(function(a){var s=t.props[a];if("*"===a)return n(!0===s,"prop schema '*' can only be used with 'true'"),void function(e,t,r){for(var i in r)if(!(i in e.props||S(e,i))){var a=r[i];n(o(a),"encountered non primitive value while deserializing '*' properties in property '"+i+"': "+a),t[i]=a}}(t,i,r);if(!0===s&&(s=v),!1!==s){var f=s.jsonname||a;E(function(t,n){t||n===undefined||function(t,n,o){t.deserializer(n,function a(s){return function(f,u){D(function(n,r){n&&r!==undefined&&"function"==typeof t.afterDeserialize?t.deserializer(r,a(s),e,i[o]):s(n,r)},f,u,n,r,o,e,t)}}(e.rootContext.createCallback(function(e){e!==g&&(i[o]=e)})),e,i[o])}(s,n,a)},r[f],r,f,e,s)}})}function E(e,t,n,r,i,o){o&&"function"==typeof o.beforeDeserialize?o.beforeDeserialize(e,t,n,r,i,o):e(null,t)}function D(e,t,n,r,i,o,a,s){s&&"function"==typeof s.afterDeserialize?s.afterDeserialize(e,t,n,r,i,o,a,s):e(t,n)}function O(e,t){n("object"==typeof e||"function"==typeof e,"No modelschema provided. If you are importing it from another file be aware of circular dependencies.");var r={serializer:function(t){return n(a(e=d(e)),"expected modelSchema, got "+e),null===t||t===undefined?t:x(e,t)},deserializer:function(r,i,o){n(a(e=d(e)),"expected modelSchema, got "+e),null!==r&&r!==undefined?k(o,e,r,i,t):i(null,r)}};return r=p(r,t)}function I(e,t,r){n(!!e,"No modelschema provided. If you are importing it from another file be aware of circular dependencies.");var i,o=!1;function s(){if(o=!0,n("string"!=typeof e||t&&"function"==typeof t,"if the reference target is specified by attribute name, a lookup function is required"),n(!t||"function"==typeof t,"second argument should be a lookup function or additional arguments object"),"string"==typeof e)i=e;else{var r=d(e);n(a(r),"expected model schema or string as first argument for 'ref', got "+r),t=t||function(e){return function(t,n,r){r.rootContext.await(e,t,n)}}(r),i=function(e){for(n(a(e));e;){for(var t in e.props)if("object"==typeof e.props[t]&&!0===e.props[t].identifier)return t;e=e["extends"]}return null}(r),n(!!i,"provided model schema doesn't define an identifier() property and cannot be used by 'ref'.")}}"object"==typeof t&&r===undefined&&(r=t,t=undefined);var f={serializer:function(e){return o||s(),e?e[i]:null},deserializer:function(e,n,r){o||s(),null===e||e===undefined?n(null,e):t(e,n,r)}};return f=p(f,r)}function N(e,t){n(s(e=e||v),"expected prop schema as first argument"),n(!f(e),"provided prop is aliased, please put aliases first");var r={serializer:function(t){return t===undefined?g:(n(t&&"length"in t&&"map"in t,"expected array (like) object"),t.map(e.serializer))},deserializer:function(t,n,r){Array.isArray(t)?i(t,function(n,i,o){function a(t,a){"function"==typeof e.afterDeserialize?D(s,t,a,n,o,r,e):i(t,a)}function s(t,n){t&&n!==undefined&&"function"==typeof e.afterDeserialize?e.deserializer(n,a,r):i(t,n)}E(function(t,n){t?i(t):e.deserializer(n,a,r)},n,t,o,r,e)},n):n("[serializr] expected JSON array")}};return r=p(r,t)}w.prototype.createCallback=function(e){return this.pendingCallbacks++,function(e){var t=!1;return function(){if(!t)return t=!0,e.apply(null,arguments);n(!1,"callback was invoked twice")}}(function(t,n){t?this.hasError||(this.hasError=!0,this.onReadyCb(t),C["delete"](this)):this.hasError||(e(n),--this.pendingCallbacks===this.pendingRefsCount&&(this.pendingRefsCount>0?(this.onReadyCb(new Error('Unresolvable references in json: "'+Object.keys(this.pendingRefs).filter(function(e){return this.pendingRefs[e].length>0},this).join('", "')+'"')),C["delete"](this)):(this.onReadyCb(null,this.target),C["delete"](this))))}.bind(this))},w.prototype.await=function(e,t,r){if(n(this.isRoot),t in this.resolvedRefs){var i=this.resolvedRefs[t].filter(function(t){return c(t.modelSchema,e)})[0];if(i)return void r(null,i.value)}this.pendingRefsCount++,this.pendingRefs[t]||(this.pendingRefs[t]=[]),this.pendingRefs[t].push({modelSchema:e,uuid:t,callback:r})},w.prototype.resolve=function(e,t,r){if(n(this.isRoot),this.resolvedRefs[t]||(this.resolvedRefs[t]=[]),this.resolvedRefs[t].push({modelSchema:e,value:r}),t in this.pendingRefs)for(var i=this.pendingRefs[t].length-1;i>=0;i--){var o=this.pendingRefs[t][i];c(e,o.modelSchema)&&(this.pendingRefs[t].splice(i,1),this.pendingRefsCount--,o.callback(null,r))}},w.prototype.setTarget=function(e){this.isRoot&&this.target&&C["delete"](this.target),this.target=e,C.set(this.target,this)},w.prototype.cancelAwaits=function(){n(this.isRoot);var e=this;Object.keys(this.pendingRefs).forEach(function(t){e.pendingRefs[t].forEach(function(n){e.pendingRefsCount--,n.callback(new Error("Reference resolution canceled for "+t))})}),this.pendingRefs={},this.pendingRefsCount=0},e.createSimpleSchema=function(e){return{factory:function(){return{}},props:e}},e.createModelSchema=y,e.getDefaultModelSchema=d,e.setDefaultModelSchema=h,e.serializable=function(e,t,r){if(1===arguments.length){var i=!0===e?v:e;return n(s(i),"@serializable expects prop schema"),j.bind(null,i)}return j(m(),e,t,r)},e.serialize=x,e.serializeAll=function(e){n(1===arguments.length&&"function"==typeof e,"@serializeAll can only be used as class decorator");var t=d(e);return t&&e.hasOwnProperty("serializeInfo")||h(e,t=y(e,{})),d(e).props["*"]=!0,e},e.cancelDeserialize=function(e){n("object"==typeof e&&e&&!Array.isArray(e),"cancelDeserialize needs an object");var t,r=(t=e,C.get(t));r&&r.cancelAwaits()},e.deserialize=function(e,t,o,s){if(n(arguments.length>=2,"deserialize expects at least 2 arguments"),n(a(e=d(e)),"first argument should be model schema"),Array.isArray(t)){var f=[];return i(t,function(t,n){var r=k(null,e,t,n,s);f.push(r)},o||r),f}return k(null,e,t,o,s)},e.update=function(e,t,i,o,s){2===arguments.length||"function"==typeof arguments[2]?(e=d(t=arguments[0]),i=arguments[1],o=arguments[2],s=arguments[3]):e=d(e),n(a(e),"update failed to determine schema"),n("object"==typeof t&&t&&!Array.isArray(t),"update needs an object");var f=new w(null,e,i,o,s);f.setTarget(t);var u=f.createCallback(r),c=A(f,e,i,t);return u(),c},e.primitive=m,e.identifier=function(e,t){var r,i;"function"==typeof e?(r=e,i=t):i=e,n(!i||"object"==typeof i,"Additional property arguments should be an object, register function should be omitted or a funtion");var o={identifier:!0,serializer:v.serializer,deserializer:function(e,t,n){v.deserializer(e,function(e,i){!function(e,t,n){n.rootContext.resolve(n.modelSchema,e,n.target)}(i,n.target,n),r&&r(i,n.target,n),t(e,i)})}};return o=p(o,i)},e.date=function(e){var t={serializer:function(e){return null===e||e===undefined?e:(n(e instanceof Date,"Expected Date object"),e.getTime())},deserializer:function(e,t){null!==e&&e!==undefined?t(null,new Date(e)):t(null,e)}};return t=p(t,e)},e.alias=function(e,t){return n(e&&"string"==typeof e,"expected prop name as first argument"),n(s(t=t&&!0!==t?t:v),"expected prop schema as second argument"),n(!f(t),"provided prop is already aliased"),{jsonname:e,serializer:t.serializer,deserializer:t.deserializer,identifier:u(t),beforeDeserialize:t.beforeDeserialize,afterDeserialize:t.afterDeserialize}},e.custom=function(e,t,r){n("function"==typeof e,"first argument should be function"),n("function"==typeof t,"second argument should be a function or promise");var i={serializer:e,deserializer:function(e,n,i,o){4===t.length?t(e,i,o,n,r):n(null,t(e,i,o,null,r))}};return i=p(i,r)},e.object=O,e.child=O,e.reference=I,e.ref=I,e.list=N,e.map=function(e,t){n(s(e=e||v),"expected prop schema as first argument"),n(!f(e),"provided prop is aliased, please put aliases first");var r={serializer:function(t){n(t&&"object"==typeof t,"expected object or Map");var r=l(t),i={};if(r)t.forEach(function(t,n){i[n]=e.serializer(t)});else for(var o in t)i[o]=e.serializer(t[o]);return i},deserializer:function(n,r,i,o){if(n&&"object"==typeof n){var a=Object.keys(n);N(e,t).deserializer(a.map(function(e){return n[e]}),function(e,t){if(e)r(e);else{var n,i=l(o);i?(o.clear(),n=o):n={};for(var s=0,f=a.length;s<f;s++)i?n.set(a[s],t[s]):n[a[s]]=t[s];r(null,n)}},i)}else r("[serializr] expected JSON object")}};return r=p(r,t)},e.mapAsArray=function(e,t,r){n(s(e=e||v),"expected prop schema as first argument"),n(!!t,"expected key property name as second argument");var i={serializer:function(t){n(t&&"object"==typeof t,"expected object or Map");var r=l(t),i=[];if(r)t.forEach(function(t){i.push(e.serializer(t))});else for(var o in t)i.push(e.serializer(t[o]));return i},deserializer:function(n,i,o,a){N(e,r).deserializer(n,function(e,r){if(e)i(e);else{var o,s=l(a);s?(a.clear(),o=a):o={};for(var f=0,u=n.length;f<u;f++)s?o.set(r[f][t],r[f]):o[r[f][t].toString()]=r[f];i(null,o)}},o)}};return i=p(i,r)},e.raw=function(e){var t={serializer:function(e){return e},deserializer:function(e,t){t(null,e)}};return t=p(t,e)},e.SKIP=g,Object.defineProperty(e,"__esModule",{value:!0})}); | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define("serializr",["exports"],t):t((e=e||self).serializr={})}(this,function(e){"use strict";var t={j:function(e){try{return JSON.stringify(e)}catch(t){return"[UnexpectedJSONParseError]: "+t.message}}};function n(e,n){if(!e){var r=Array.prototype.slice.call(arguments,2),i=[],o=0,a=n.replace(/%([a-zA-Z%])/g,function(e,n){if("%%"===e)return e;var a=t[n];if("function"==typeof a){var s=r[o++];return i.push(s),a(s)}return e});throw console&&i.length>0&&console.log.apply(console,i),new Error("[serializr] "+(a||"Illegal State"))}}function r(e){if(e)throw new Error(e)}function i(e,t,n){if(0!==e.length){var r=e.filter(function(){return!0}).length,i=[],o=!1;e.forEach(function(e,a){t(e,function(e,t,a){t?o||(o=!0,n(t)):(i[e]=a,0==--r&&n(null,i))}.bind(null,a),a)})}else n(null,[])}function o(e){return null===e||"object"!=typeof e&&"function"!=typeof e}function a(e){return e&&e.factory&&e.props}function s(e){return e&&e.serializer&&e.deserializer}function f(e){return"object"==typeof e&&!!e.jsonname}function c(e){return"object"==typeof e&&!0===e.identifier}function u(e,t){for(;e;){if(e===t)return!0;e=e["extends"]}return!1}function l(e){return e&&"function"==typeof e.keys&&"function"==typeof e.clear}function p(e,t){if(t){n(s(e),"expected a propSchema");["beforeDeserialize","afterDeserialize"].forEach(function(n){"function"==typeof t[n]&&(e[n]=t[n])})}return e}function d(e){return e?a(e)?e:a(e.serializeInfo)?e.serializeInfo:e.constructor&&e.constructor.serializeInfo?e.constructor.serializeInfo:void 0:null}function h(e,t){return n(a(t)),e.serializeInfo=t}function y(e,t,r){n(e!==Object,"one cannot simply put define a model schema for Object"),n("function"==typeof e,"expected constructor function");var i={targetClass:e,factory:r||function(){return new e},props:t};if(e.prototype.constructor!==Object){var o=d(e.prototype.constructor);o&&o.targetClass!==e&&(i["extends"]=o)}return h(e,i),i}function m(e){var t={serializer:function(e){return n(o(e),"this value is not primitive: "+e),e},deserializer:function(e,t){o(e)?t(null,e):t("[serializr] this value is not primitive: "+e)}};return t=p(t,e)}var g="undefined"!=typeof Symbol?Symbol("SKIP"):{SKIP:!0},b=m(),v=/((\/\/.*$)|(\/\*[\s\S]*?\*\/))/gm,z=/([^\s,]+)/g;function j(e,t,r,i){var o,a,f;if(n(arguments.length>=2,"too few arguments. Please use @serializable as property decorator"),r===undefined&&"function"==typeof t&&t.prototype&&i!==undefined&&"number"==typeof i){n(s(e),"Constructor params must use alias(name)"),n(e.jsonname,"Constructor params must use alias(name)");var c=(a=t.toString().replace(v,""),null===(f=a.slice(a.indexOf("(")+1,a.indexOf(")")).match(z))&&(f=[]),f);c.length>=i&&(r=c[i],e.paramNumber=i,i=undefined,t=t.prototype,o=function(e){for(var n=[],r=0;r<t.constructor.length;r++)Object.keys(e.modelSchema.props).forEach(function(t){var i=e.modelSchema.props[t];i.paramNumber===r&&(n[r]=e.json[i.jsonname])});return new(Function.prototype.bind.apply(t.constructor,[null].concat(n)))})}n("string"==typeof r,"incorrect usage of @serializable decorator");var u=d(t);return u&&t.constructor.hasOwnProperty("serializeInfo")||(u=y(t.constructor,{},o)),u&&u.targetClass!==t.constructor&&(u=y(t.constructor,{},o)),u.props[r]=e,!i||i.get||i.set||(i.writable=!0),i}function x(e,t){n(1===arguments.length||2===arguments.length,"serialize expects one or 2 arguments");var r=1===arguments.length?e:t,i=1===arguments.length?null:e;if(Array.isArray(r)){if(0===r.length)return[];i?"object"!=typeof i&&(i=d(i)):i=d(r[0])}else i?"object"!=typeof i&&(i=d(i)):i=d(r);return n(!!i,"Failed to find default schema for "+e),Array.isArray(r)?r.map(function(e){return C(i,e)}):C(i,r)}function R(e){n(!0===e||e.pattern,`prop schema '*' can only be used with 'true' or a prop def with a 'pattern': ${JSON.stringify(e)}`)}function C(e,t){var r;return n(e&&"object"==typeof e&&e.props,"Expected schema"),n(t&&"object"==typeof t,"Expected object"),r=e["extends"]?C(e["extends"],t):{},Object.keys(e.props).forEach(function(n){var i=e.props[n];if("*"!==n){if(!0===i&&(i=b),!1!==i){var a=i.serializer(t[n],n,t);a!==g&&(r[i.jsonname||n]=a)}}else!function(e,t,n,r){for(var i in R(t),n)if(n.hasOwnProperty(i)&&!(i in e.props)&&(!0===t||t.pattern&&t.pattern.test(i))){var a=n[i];if(!0===t)o(a)&&(r[i]=a);else if(t.props){var s=x(t,a);if(s===g)return;r[i]=s}else{var s=t.serializer(a,i,n);if(s===g)return;r[i]=s}}}(e,i,t,r)}),r}var S=new WeakMap;function w(e,t,n,i,o){this.parentContext=e,this.isRoot=!e,this.pendingCallbacks=0,this.pendingRefsCount=0,this.onReadyCb=i||r,this.json=n,this.target=null,this.hasError=!1,this.modelSchema=t,this.isRoot?(this.rootContext=this,this.args=o,this.pendingRefs={},this.resolvedRefs={}):(this.rootContext=e.rootContext,this.args=e.args)}function k(e,t){for(var n in e.props)if("object"==typeof e.props[n]&&e.props[n].jsonname===t)return!0;return!1}function A(e,t,i,o,a){if(null!==i&&i!==undefined&&"object"==typeof i){var s=new w(e,t,i,o,a),f=t.factory(s);n(!!f,"No object returned from factory"),s.setTarget(f);var c=s.createCallback(r);return O(s,t,i,f),c(),f}o(null,null)}function O(e,t,i,a){t["extends"]&&O(e,t["extends"],i,a),Object.keys(t.props).forEach(function(s){var f=t.props[s];if("*"!==s){if(!0===f&&(f=b),!1!==f){var c=f.jsonname||s;E(function(t,n){t||n===undefined||function(t,n,r){t.deserializer(n,function o(s){return function(f,c){D(function(n,i){n&&i!==undefined&&"function"==typeof t.afterDeserialize?t.deserializer(i,o(s),e,a[r]):s(n,i)},f,c,n,i,r,e,t)}}(e.rootContext.createCallback(function(e){e!==g&&(a[r]=e)})),e,a[r])}(f,n,s)},i[c],i,c,e,f)}}else!function(e,t,i,a,s){for(var f in R(i),s)if(!(f in t.props||k(t,f))){var c=s[f];if(!0===i)n(o(c),"encountered non primitive value while deserializing '*' properties in property '"+f+"': "+c),a[f]=c;else if(i.pattern.test(f))if(i.factory){var u=A(e,i,c,e.callback||r,{});u!==undefined&&(a[f]=u)}else{function l(e){e!==g&&(a[f]=e)}i.deserializer(c,e.rootContext.createCallback(l),e)}}}(e,t,f,a,i)})}function E(e,t,n,r,i,o){o&&"function"==typeof o.beforeDeserialize?o.beforeDeserialize(e,t,n,r,i,o):e(null,t)}function D(e,t,n,r,i,o,a,s){s&&"function"==typeof s.afterDeserialize?s.afterDeserialize(e,t,n,r,i,o,a,s):e(t,n)}function I(e,t){n("object"==typeof e||"function"==typeof e,"No modelschema provided. If you are importing it from another file be aware of circular dependencies.");var r={serializer:function(t){return n(a(e=d(e)),"expected modelSchema, got "+e),null===t||t===undefined?t:x(e,t)},deserializer:function(r,i,o){n(a(e=d(e)),"expected modelSchema, got "+e),null!==r&&r!==undefined?A(o,e,r,i,t):i(null,r)}};return r=p(r,t)}function N(e,t,r){n(!!e,"No modelschema provided. If you are importing it from another file be aware of circular dependencies.");var i,o=!1;function s(){if(o=!0,n("string"!=typeof e||t&&"function"==typeof t,"if the reference target is specified by attribute name, a lookup function is required"),n(!t||"function"==typeof t,"second argument should be a lookup function or additional arguments object"),"string"==typeof e)i=e;else{var r=d(e);n(a(r),"expected model schema or string as first argument for 'ref', got "+r),t=t||function(e){return function(t,n,r){r.rootContext.await(e,t,n)}}(r),i=function(e){for(n(a(e));e;){for(var t in e.props)if("object"==typeof e.props[t]&&!0===e.props[t].identifier)return t;e=e["extends"]}return null}(r),n(!!i,"provided model schema doesn't define an identifier() property and cannot be used by 'ref'.")}}"object"==typeof t&&r===undefined&&(r=t,t=undefined);var f={serializer:function(e){return o||s(),e?e[i]:null},deserializer:function(e,n,r){o||s(),null===e||e===undefined?n(null,e):t(e,n,r)}};return f=p(f,r)}function P(e,t){n(s(e=e||b),"expected prop schema as first argument"),n(!f(e),"provided prop is aliased, please put aliases first");var r={serializer:function(t){return t===undefined?g:(n(t&&"length"in t&&"map"in t,"expected array (like) object"),t.map(e.serializer))},deserializer:function(t,n,r){Array.isArray(t)?i(t,function(n,i,o){function a(t,a){"function"==typeof e.afterDeserialize?D(s,t,a,n,o,r,e):i(t,a)}function s(t,n){t&&n!==undefined&&"function"==typeof e.afterDeserialize?e.deserializer(n,a,r):i(t,n)}E(function(t,n){t?i(t):e.deserializer(n,a,r)},n,t,o,r,e)},n):n("[serializr] expected JSON array")}};return r=p(r,t)}w.prototype.createCallback=function(e){return this.pendingCallbacks++,function(e){var t=!1;return function(){if(!t)return t=!0,e.apply(null,arguments);n(!1,"callback was invoked twice")}}(function(t,n){t?this.hasError||(this.hasError=!0,this.onReadyCb(t),S["delete"](this)):this.hasError||(e(n),--this.pendingCallbacks===this.pendingRefsCount&&(this.pendingRefsCount>0?(this.onReadyCb(new Error('Unresolvable references in json: "'+Object.keys(this.pendingRefs).filter(function(e){return this.pendingRefs[e].length>0},this).join('", "')+'"')),S["delete"](this)):(this.onReadyCb(null,this.target),S["delete"](this))))}.bind(this))},w.prototype.await=function(e,t,r){if(n(this.isRoot),t in this.resolvedRefs){var i=this.resolvedRefs[t].filter(function(t){return u(t.modelSchema,e)})[0];if(i)return void r(null,i.value)}this.pendingRefsCount++,this.pendingRefs[t]||(this.pendingRefs[t]=[]),this.pendingRefs[t].push({modelSchema:e,uuid:t,callback:r})},w.prototype.resolve=function(e,t,r){if(n(this.isRoot),this.resolvedRefs[t]||(this.resolvedRefs[t]=[]),this.resolvedRefs[t].push({modelSchema:e,value:r}),t in this.pendingRefs)for(var i=this.pendingRefs[t].length-1;i>=0;i--){var o=this.pendingRefs[t][i];u(e,o.modelSchema)&&(this.pendingRefs[t].splice(i,1),this.pendingRefsCount--,o.callback(null,r))}},w.prototype.setTarget=function(e){this.isRoot&&this.target&&S["delete"](this.target),this.target=e,S.set(this.target,this)},w.prototype.cancelAwaits=function(){n(this.isRoot);var e=this;Object.keys(this.pendingRefs).forEach(function(t){e.pendingRefs[t].forEach(function(n){e.pendingRefsCount--,n.callback(new Error("Reference resolution canceled for "+t))})}),this.pendingRefs={},this.pendingRefsCount=0},e.createSimpleSchema=function(e){return{factory:function(){return{}},props:e}},e.createModelSchema=y,e.getDefaultModelSchema=d,e.setDefaultModelSchema=h,e.serializable=function(e,t,r){if(1===arguments.length){var i=!0===e?b:e;return n(s(i),"@serializable expects prop schema"),j.bind(null,i)}return j(m(),e,t,r)},e.serialize=x,e.serializeAll=function(e,t){let r,i=!1;function o(e){var t=d(e);return t&&e.hasOwnProperty("serializeInfo")||h(e,t=y(e,{})),d(e).props["*"]=r,e}return 1===arguments.length?(n("function"==typeof e,"@serializeAll can only be used as class decorator"),r=!0,i=!0):(n("object"==typeof e&&e.test,"@serializeAll pattern doesn't have test"),"function"==typeof t&&(t=I(t)),n("object"==typeof t&&t.serializer,"couldn't resolve schema"),r=Object.assign({},t,{pattern:e})),i?o(e):o},e.cancelDeserialize=function(e){n("object"==typeof e&&e&&!Array.isArray(e),"cancelDeserialize needs an object");var t,r=(t=e,S.get(t));r&&r.cancelAwaits()},e.deserialize=function(e,t,o,s){if(n(arguments.length>=2,"deserialize expects at least 2 arguments"),n(a(e=d(e)),"first argument should be model schema"),Array.isArray(t)){var f=[];return i(t,function(t,n){var r=A(null,e,t,n,s);f.push(r)},o||r),f}return A(null,e,t,o,s)},e.update=function(e,t,i,o,s){2===arguments.length||"function"==typeof arguments[2]?(e=d(t=arguments[0]),i=arguments[1],o=arguments[2],s=arguments[3]):e=d(e),n(a(e),"update failed to determine schema"),n("object"==typeof t&&t&&!Array.isArray(t),"update needs an object");var f=new w(null,e,i,o,s);f.setTarget(t);var c=f.createCallback(r),u=O(f,e,i,t);return c(),u},e.primitive=m,e.identifier=function(e,t){var r,i;"function"==typeof e?(r=e,i=t):i=e,n(!i||"object"==typeof i,"Additional property arguments should be an object, register function should be omitted or a funtion");var o={identifier:!0,serializer:b.serializer,deserializer:function(e,t,n){b.deserializer(e,function(e,i){!function(e,t,n){n.rootContext.resolve(n.modelSchema,e,n.target)}(i,n.target,n),r&&r(i,n.target,n),t(e,i)})}};return o=p(o,i)},e.date=function(e){var t={serializer:function(e){return null===e||e===undefined?e:(n(e instanceof Date,"Expected Date object"),e.getTime())},deserializer:function(e,t){null!==e&&e!==undefined?t(null,new Date(e)):t(null,e)}};return t=p(t,e)},e.alias=function(e,t){return n(e&&"string"==typeof e,"expected prop name as first argument"),n(s(t=t&&!0!==t?t:b),"expected prop schema as second argument"),n(!f(t),"provided prop is already aliased"),{jsonname:e,serializer:t.serializer,deserializer:t.deserializer,identifier:c(t),beforeDeserialize:t.beforeDeserialize,afterDeserialize:t.afterDeserialize}},e.custom=function(e,t,r){n("function"==typeof e,"first argument should be function"),n("function"==typeof t,"second argument should be a function or promise");var i={serializer:e,deserializer:function(e,n,i,o){4===t.length?t(e,i,o,n,r):n(null,t(e,i,o,null,r))}};return i=p(i,r)},e.object=I,e.child=I,e.optional=function(e,t){n(s(t=t&&!0!==t?t:b),"expected prop schema as second argument");const r=t.serializer;return n("function"==typeof r,"expected prop schema to have a callable serializer"),Object.assign({},t,{serializer:function(...e){const t=r(...e);return t===undefined?g:t}})},e.reference=N,e.ref=N,e.list=P,e.map=function(e,t){n(s(e=e||b),"expected prop schema as first argument"),n(!f(e),"provided prop is aliased, please put aliases first");var r={serializer:function(t){n(t&&"object"==typeof t,"expected object or Map");var r=l(t),i={};if(r)t.forEach(function(t,n){i[n]=e.serializer(t)});else for(var o in t)i[o]=e.serializer(t[o]);return i},deserializer:function(n,r,i,o){if(n&&"object"==typeof n){var a=Object.keys(n);P(e,t).deserializer(a.map(function(e){return n[e]}),function(e,t){if(e)r(e);else{var n,i=l(o);i?(o.clear(),n=o):n={};for(var s=0,f=a.length;s<f;s++)i?n.set(a[s],t[s]):n[a[s]]=t[s];r(null,n)}},i)}else r("[serializr] expected JSON object")}};return r=p(r,t)},e.mapAsArray=function(e,t,r){n(s(e=e||b),"expected prop schema as first argument"),n(!!t,"expected key property name as second argument");var i={serializer:function(t){n(t&&"object"==typeof t,"expected object or Map");var r=l(t),i=[];if(r)t.forEach(function(t){i.push(e.serializer(t))});else for(var o in t)i.push(e.serializer(t[o]));return i},deserializer:function(n,i,o,a){P(e,r).deserializer(n,function(e,r){if(e)i(e);else{var o,s=l(a);s?(a.clear(),o=a):o={};for(var f=0,c=n.length;f<c;f++)s?o.set(r[f][t],r[f]):o[r[f][t].toString()]=r[f];i(null,o)}},o)}};return i=p(i,r)},e.raw=function(e){var t={serializer:function(e){return e},deserializer:function(e,t){t(null,e)}};return t=p(t,e)},e.SKIP=g,Object.defineProperty(e,"__esModule",{value:!0})}); | ||
//# sourceMappingURL=serializr.min.js.map |
{ | ||
"name": "serializr", | ||
"version": "1.5.2", | ||
"version": "1.5.3", | ||
"description": "Serialize and deserialize complex object graphs to JSON", | ||
@@ -5,0 +5,0 @@ "main": "lib/serializr.js", |
116
README.md
@@ -373,2 +373,3 @@ # Serializr | ||
- [Parameters](#parameters-7) | ||
- [Examples](#examples-3) | ||
- [cancelDeserialize](#canceldeserialize) | ||
@@ -382,6 +383,6 @@ - [Parameters](#parameters-8) | ||
- [Parameters](#parameters-11) | ||
- [Examples](#examples-3) | ||
- [Examples](#examples-4) | ||
- [identifier](#identifier) | ||
- [Parameters](#parameters-12) | ||
- [Examples](#examples-4) | ||
- [Examples](#examples-5) | ||
- [date](#date) | ||
@@ -391,28 +392,31 @@ - [Parameters](#parameters-13) | ||
- [Parameters](#parameters-14) | ||
- [Examples](#examples-5) | ||
- [Examples](#examples-6) | ||
- [custom](#custom) | ||
- [Parameters](#parameters-15) | ||
- [Examples](#examples-6) | ||
- [Examples](#examples-7) | ||
- [object](#object) | ||
- [Parameters](#parameters-16) | ||
- [Examples](#examples-7) | ||
- [Examples](#examples-8) | ||
- [optional](#optional) | ||
- [Parameters](#parameters-17) | ||
- [Examples](#examples-9) | ||
- [reference](#reference) | ||
- [Parameters](#parameters-17) | ||
- [Examples](#examples-8) | ||
- [Parameters](#parameters-18) | ||
- [Examples](#examples-10) | ||
- [list](#list) | ||
- [Parameters](#parameters-18) | ||
- [Examples](#examples-9) | ||
- [Parameters](#parameters-19) | ||
- [Examples](#examples-11) | ||
- [map](#map) | ||
- [Parameters](#parameters-19) | ||
- [Parameters](#parameters-20) | ||
- [mapAsArray](#mapasarray) | ||
- [Parameters](#parameters-20) | ||
- [Parameters](#parameters-21) | ||
- [raw](#raw) | ||
- [Parameters](#parameters-21) | ||
- [Examples](#examples-10) | ||
- [Parameters](#parameters-22) | ||
- [Examples](#examples-12) | ||
- [SKIP](#skip) | ||
- [Examples](#examples-11) | ||
- [Examples](#examples-13) | ||
## ModelSchema | ||
[src/serializr.js:52-52](https://git@github.com/:mobxjs/serializr/blob/132a653e346444c53e17679fcef07f389da1d939/src/serializr.js#L5-L51 "Source code on GitHub") | ||
[src/serializr.js:52-52](https://git@github.com/:mobxjs/serializr/blob/6d21f2cf33d728b8991d2f70409323137837894b/src/serializr.js#L5-L51 "Source code on GitHub") | ||
@@ -463,3 +467,3 @@ JSDOC type defintions for usage w/o typescript. | ||
[src/api/createSimpleSchema.js:17-24](https://git@github.com/:mobxjs/serializr/blob/132a653e346444c53e17679fcef07f389da1d939/src/api/createSimpleSchema.js#L17-L24 "Source code on GitHub") | ||
[src/api/createSimpleSchema.js:17-24](https://git@github.com/:mobxjs/serializr/blob/6d21f2cf33d728b8991d2f70409323137837894b/src/api/createSimpleSchema.js#L17-L24 "Source code on GitHub") | ||
@@ -489,3 +493,3 @@ Creates a model schema that (de)serializes from / to plain javascript objects. | ||
[src/api/createModelSchema.js:29-47](https://git@github.com/:mobxjs/serializr/blob/132a653e346444c53e17679fcef07f389da1d939/src/api/createModelSchema.js#L29-L47 "Source code on GitHub") | ||
[src/api/createModelSchema.js:29-47](https://git@github.com/:mobxjs/serializr/blob/6d21f2cf33d728b8991d2f70409323137837894b/src/api/createModelSchema.js#L29-L47 "Source code on GitHub") | ||
@@ -523,3 +527,3 @@ Creates a model schema that (de)serializes an object created by a constructor function (class). | ||
[src/api/getDefaultModelSchema.js:9-18](https://git@github.com/:mobxjs/serializr/blob/132a653e346444c53e17679fcef07f389da1d939/src/api/getDefaultModelSchema.js#L9-L18 "Source code on GitHub") | ||
[src/api/getDefaultModelSchema.js:9-18](https://git@github.com/:mobxjs/serializr/blob/6d21f2cf33d728b8991d2f70409323137837894b/src/api/getDefaultModelSchema.js#L9-L18 "Source code on GitHub") | ||
@@ -536,3 +540,3 @@ Returns the standard model schema associated with a class / constructor function | ||
[src/api/setDefaultModelSchema.js:15-18](https://git@github.com/:mobxjs/serializr/blob/132a653e346444c53e17679fcef07f389da1d939/src/api/setDefaultModelSchema.js#L15-L18 "Source code on GitHub") | ||
[src/api/setDefaultModelSchema.js:15-18](https://git@github.com/:mobxjs/serializr/blob/6d21f2cf33d728b8991d2f70409323137837894b/src/api/setDefaultModelSchema.js#L15-L18 "Source code on GitHub") | ||
@@ -555,3 +559,3 @@ Sets the default model schema for class / constructor function. | ||
[src/api/serializable.js:93-103](https://git@github.com/:mobxjs/serializr/blob/132a653e346444c53e17679fcef07f389da1d939/src/api/serializable.js#L93-L103 "Source code on GitHub") | ||
[src/api/serializable.js:93-103](https://git@github.com/:mobxjs/serializr/blob/6d21f2cf33d728b8991d2f70409323137837894b/src/api/serializable.js#L93-L103 "Source code on GitHub") | ||
@@ -580,3 +584,3 @@ Decorator that defines a new property mapping on the default model schema for the class | ||
[src/core/serialize.js:16-34](https://git@github.com/:mobxjs/serializr/blob/132a653e346444c53e17679fcef07f389da1d939/src/core/serialize.js#L16-L34 "Source code on GitHub") | ||
[src/core/serialize.js:14-36](https://git@github.com/:mobxjs/serializr/blob/6d21f2cf33d728b8991d2f70409323137837894b/src/core/serialize.js#L14-L36 "Source code on GitHub") | ||
@@ -589,3 +593,3 @@ Serializes an object (graph) into json using the provided model schema. | ||
- `arg1` modelschema to use. Optional | ||
- `arg1` class or modelschema to use. Optional | ||
- `arg2` object(s) to serialize | ||
@@ -597,13 +601,21 @@ | ||
[src/core/serialize.js:89-100](https://git@github.com/:mobxjs/serializr/blob/132a653e346444c53e17679fcef07f389da1d939/src/core/serialize.js#L89-L100 "Source code on GitHub") | ||
[src/core/serializeAll.js:38-67](https://git@github.com/:mobxjs/serializr/blob/6d21f2cf33d728b8991d2f70409323137837894b/src/core/serializeAll.js#L38-L67 "Source code on GitHub") | ||
The `serializeAll` decorator can be used on a class to signal that all primitive properties should be serialized automatically. | ||
The `serializeAll` decorator can may used on a class to signal that all primitive properties, | ||
or complex properties with a name matching a `pattern`, should be serialized automatically. | ||
### Parameters | ||
- `target` | ||
- `targetOrPattern` | ||
- `clazzOrSchema` | ||
### Examples | ||
```javascript | ||
class DataType { | ||
``` | ||
## cancelDeserialize | ||
[src/core/cancelDeserialize.js:12-18](https://git@github.com/:mobxjs/serializr/blob/132a653e346444c53e17679fcef07f389da1d939/src/core/cancelDeserialize.js#L12-L18 "Source code on GitHub") | ||
[src/core/cancelDeserialize.js:12-18](https://git@github.com/:mobxjs/serializr/blob/6d21f2cf33d728b8991d2f70409323137837894b/src/core/cancelDeserialize.js#L12-L18 "Source code on GitHub") | ||
@@ -618,3 +630,3 @@ Cancels an asynchronous deserialization or update operation for the specified target object. | ||
[src/core/deserialize.js:45-63](https://git@github.com/:mobxjs/serializr/blob/132a653e346444c53e17679fcef07f389da1d939/src/core/deserialize.js#L45-L63 "Source code on GitHub") | ||
[src/core/deserialize.js:69-87](https://git@github.com/:mobxjs/serializr/blob/6d21f2cf33d728b8991d2f70409323137837894b/src/core/deserialize.js#L69-L87 "Source code on GitHub") | ||
@@ -642,3 +654,3 @@ Deserializes a json structure into an object graph. | ||
[src/core/update.js:22-44](https://git@github.com/:mobxjs/serializr/blob/132a653e346444c53e17679fcef07f389da1d939/src/core/update.js#L22-L44 "Source code on GitHub") | ||
[src/core/update.js:22-44](https://git@github.com/:mobxjs/serializr/blob/6d21f2cf33d728b8991d2f70409323137837894b/src/core/update.js#L22-L44 "Source code on GitHub") | ||
@@ -661,3 +673,3 @@ Similar to deserialize, but updates an existing object instance. | ||
[src/types/primitive.js:18-32](https://git@github.com/:mobxjs/serializr/blob/132a653e346444c53e17679fcef07f389da1d939/src/types/primitive.js#L18-L32 "Source code on GitHub") | ||
[src/types/primitive.js:18-32](https://git@github.com/:mobxjs/serializr/blob/6d21f2cf33d728b8991d2f70409323137837894b/src/types/primitive.js#L18-L32 "Source code on GitHub") | ||
@@ -685,3 +697,3 @@ Indicates that this field contains a primitive value (or Date) which should be serialized literally to json. | ||
[src/types/identifier.js:43-66](https://git@github.com/:mobxjs/serializr/blob/132a653e346444c53e17679fcef07f389da1d939/src/types/identifier.js#L43-L66 "Source code on GitHub") | ||
[src/types/identifier.js:43-66](https://git@github.com/:mobxjs/serializr/blob/6d21f2cf33d728b8991d2f70409323137837894b/src/types/identifier.js#L43-L66 "Source code on GitHub") | ||
@@ -727,3 +739,3 @@ Similar to primitive, but this field will be marked as the identifier for the given Model type. | ||
[src/types/date.js:9-26](https://git@github.com/:mobxjs/serializr/blob/132a653e346444c53e17679fcef07f389da1d939/src/types/date.js#L9-L26 "Source code on GitHub") | ||
[src/types/date.js:9-26](https://git@github.com/:mobxjs/serializr/blob/6d21f2cf33d728b8991d2f70409323137837894b/src/types/date.js#L9-L26 "Source code on GitHub") | ||
@@ -740,3 +752,3 @@ Similar to primitive, serializes instances of Date objects | ||
[src/types/alias.js:20-33](https://git@github.com/:mobxjs/serializr/blob/132a653e346444c53e17679fcef07f389da1d939/src/types/alias.js#L20-L33 "Source code on GitHub") | ||
[src/types/alias.js:20-33](https://git@github.com/:mobxjs/serializr/blob/6d21f2cf33d728b8991d2f70409323137837894b/src/types/alias.js#L20-L33 "Source code on GitHub") | ||
@@ -766,3 +778,3 @@ Alias indicates that this model property should be named differently in the generated json. | ||
[src/types/custom.js:60-75](https://git@github.com/:mobxjs/serializr/blob/132a653e346444c53e17679fcef07f389da1d939/src/types/custom.js#L60-L75 "Source code on GitHub") | ||
[src/types/custom.js:60-75](https://git@github.com/:mobxjs/serializr/blob/6d21f2cf33d728b8991d2f70409323137837894b/src/types/custom.js#L60-L75 "Source code on GitHub") | ||
@@ -832,3 +844,3 @@ Can be used to create simple custom propSchema. Multiple things can be done inside of a custom propSchema, like deserializing and serializing other (polymorphic) objects, skipping the serialization of something or checking the context of the obj being (de)serialized. | ||
[src/types/object.js:35-55](https://git@github.com/:mobxjs/serializr/blob/132a653e346444c53e17679fcef07f389da1d939/src/types/object.js#L35-L55 "Source code on GitHub") | ||
[src/types/object.js:35-55](https://git@github.com/:mobxjs/serializr/blob/6d21f2cf33d728b8991d2f70409323137837894b/src/types/object.js#L35-L55 "Source code on GitHub") | ||
@@ -869,5 +881,29 @@ `object` indicates that this property contains an object that needs to be (de)serialized | ||
## optional | ||
[src/types/optional.js:18-31](https://git@github.com/:mobxjs/serializr/blob/6d21f2cf33d728b8991d2f70409323137837894b/src/types/optional.js#L18-L31 "Source code on GitHub") | ||
Optional indicates that this model property shouldn't be serialized if it isn't present. | ||
### Parameters | ||
- `name` | ||
- `propSchema` **PropSchema** propSchema to (de)serialize the contents of this field | ||
### Examples | ||
```javascript | ||
createModelSchema(Todo, { | ||
title: optional(primitive()), | ||
}); | ||
console.dir(serialize(new Todo())); | ||
// {} | ||
``` | ||
Returns **PropSchema** | ||
## reference | ||
[src/types/reference.js:66-105](https://git@github.com/:mobxjs/serializr/blob/132a653e346444c53e17679fcef07f389da1d939/src/types/reference.js#L66-L105 "Source code on GitHub") | ||
[src/types/reference.js:66-105](https://git@github.com/:mobxjs/serializr/blob/6d21f2cf33d728b8991d2f70409323137837894b/src/types/reference.js#L66-L105 "Source code on GitHub") | ||
@@ -937,3 +973,3 @@ `reference` can be used to (de)serialize references that point to other models. | ||
[src/types/list.js:43-105](https://git@github.com/:mobxjs/serializr/blob/132a653e346444c53e17679fcef07f389da1d939/src/types/list.js#L43-L105 "Source code on GitHub") | ||
[src/types/list.js:43-105](https://git@github.com/:mobxjs/serializr/blob/6d21f2cf33d728b8991d2f70409323137837894b/src/types/list.js#L43-L105 "Source code on GitHub") | ||
@@ -977,3 +1013,3 @@ List indicates that this property contains a list of things. | ||
[src/types/map.js:14-65](https://git@github.com/:mobxjs/serializr/blob/132a653e346444c53e17679fcef07f389da1d939/src/types/map.js#L14-L65 "Source code on GitHub") | ||
[src/types/map.js:14-65](https://git@github.com/:mobxjs/serializr/blob/6d21f2cf33d728b8991d2f70409323137837894b/src/types/map.js#L14-L65 "Source code on GitHub") | ||
@@ -993,3 +1029,3 @@ Similar to list, but map represents a string keyed dynamic collection. | ||
[src/types/mapAsArray.js:19-66](https://git@github.com/:mobxjs/serializr/blob/132a653e346444c53e17679fcef07f389da1d939/src/types/mapAsArray.js#L19-L66 "Source code on GitHub") | ||
[src/types/mapAsArray.js:19-66](https://git@github.com/:mobxjs/serializr/blob/6d21f2cf33d728b8991d2f70409323137837894b/src/types/mapAsArray.js#L19-L66 "Source code on GitHub") | ||
@@ -1014,3 +1050,3 @@ Similar to map, mapAsArray can be used to serialize a map-like collection where the key is | ||
[src/types/raw.js:18-29](https://git@github.com/:mobxjs/serializr/blob/132a653e346444c53e17679fcef07f389da1d939/src/types/raw.js#L18-L29 "Source code on GitHub") | ||
[src/types/raw.js:18-29](https://git@github.com/:mobxjs/serializr/blob/6d21f2cf33d728b8991d2f70409323137837894b/src/types/raw.js#L18-L29 "Source code on GitHub") | ||
@@ -1039,3 +1075,3 @@ Indicates that this field is only need to putted in the serialized json or | ||
[src/constants.js:20-20](https://git@github.com/:mobxjs/serializr/blob/132a653e346444c53e17679fcef07f389da1d939/src/constants.js#L20-L20 "Source code on GitHub") | ||
[src/constants.js:20-20](https://git@github.com/:mobxjs/serializr/blob/6d21f2cf33d728b8991d2f70409323137837894b/src/constants.js#L20-L20 "Source code on GitHub") | ||
@@ -1042,0 +1078,0 @@ In the event that a property needs to be deserialized, but not serialized, you can use the SKIP symbol to omit the property. This has to be used with the custom serializer. |
@@ -73,2 +73,3 @@ // TODO: put this in the source files, and extract it, to preserve comments | ||
export function alias(jsonName: string, propSchema?: PropSchema | boolean): PropSchema; | ||
export function optional(propSchema?: PropSchema | boolean): PropSchema; | ||
@@ -97,3 +98,4 @@ export function child(modelschema: ClazzOrModelSchema<any>, additionalArgs?: AdditionalPropArgs): PropSchema; | ||
export function serializeAll<T extends Function>(clazz: T): T | ||
export function serializeAll<T>(clazz: Clazz<T>): Clazz<T>; | ||
export function serializeAll(pattern: RegExp, propSchema: PropSchema | true | Function): (clazz: Clazz<any>) => Clazz<any>; | ||
@@ -100,0 +102,0 @@ export function raw(): any; |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
274576
3058
1316
1