Comparing version 1.1.10 to 1.1.11
@@ -0,1 +1,8 @@ | ||
# 1.1.11 | ||
* Introduced `serializr.SKIP`, the can be used to skip certain properties during deserialization when using `custom` serializers. #32 by @nathanstitt | ||
* Fixed #27: Skip non primitive primitive properties instead of throwing for `*` decorator | ||
* Introduced `@serializeAll` decorator, see #27 | ||
# 1.1.10 | ||
@@ -2,0 +9,0 @@ |
{ | ||
"name": "serializr", | ||
"version": "1.1.10", | ||
"version": "1.1.11", | ||
"description": "Serialize and deserialize complex object graphs to JSON", | ||
@@ -5,0 +5,0 @@ "main": "serializr.js", |
@@ -280,3 +280,3 @@ # Serializr | ||
[serializr.js:126-133](https://github.com/mobxjs/serializr/blob/58bf88786bb4ee088b3c267322134ec1f25aefd0/serializr.js#L126-L133 "Source code on GitHub") | ||
[serializr.js:128-135](https://github.com/mobxjs/serializr/blob/eb6d0a682e72f8a75645e3a016c501e26ba5c7c4/serializr.js#L128-L135 "Source code on GitHub") | ||
@@ -325,3 +325,3 @@ JSDOC type defintions for usage w/o typescript. | ||
[serializr.js:126-133](https://github.com/mobxjs/serializr/blob/58bf88786bb4ee088b3c267322134ec1f25aefd0/serializr.js#L126-L133 "Source code on GitHub") | ||
[serializr.js:128-135](https://github.com/mobxjs/serializr/blob/eb6d0a682e72f8a75645e3a016c501e26ba5c7c4/serializr.js#L128-L135 "Source code on GitHub") | ||
@@ -351,3 +351,3 @@ Creates a model schema that (de)serializes from / to plain javascript objects. | ||
[serializr.js:159-177](https://github.com/mobxjs/serializr/blob/58bf88786bb4ee088b3c267322134ec1f25aefd0/serializr.js#L159-L177 "Source code on GitHub") | ||
[serializr.js:161-179](https://github.com/mobxjs/serializr/blob/eb6d0a682e72f8a75645e3a016c501e26ba5c7c4/serializr.js#L161-L179 "Source code on GitHub") | ||
@@ -385,3 +385,3 @@ Creates a model schema that (de)serializes an object created by a constructor function (class). | ||
[serializr.js:208-218](https://github.com/mobxjs/serializr/blob/58bf88786bb4ee088b3c267322134ec1f25aefd0/serializr.js#L208-L218 "Source code on GitHub") | ||
[serializr.js:210-220](https://github.com/mobxjs/serializr/blob/eb6d0a682e72f8a75645e3a016c501e26ba5c7c4/serializr.js#L210-L220 "Source code on GitHub") | ||
@@ -408,5 +408,30 @@ Decorator that defines a new property mapping on the default model schema for the class | ||
## serializeAll | ||
[serializr.js:297-308](https://github.com/mobxjs/serializr/blob/eb6d0a682e72f8a75645e3a016c501e26ba5c7c4/serializr.js#L297-L308 "Source code on GitHub") | ||
The `serializeAll` decorator can be used on a class to signal that all primitive properties should be serialized automatically. | ||
**Parameters** | ||
- `target` | ||
**Examples** | ||
```javascript | ||
\@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}) | ||
``` | ||
## getDefaultModelSchema | ||
[serializr.js:286-295](https://github.com/mobxjs/serializr/blob/58bf88786bb4ee088b3c267322134ec1f25aefd0/serializr.js#L286-L295 "Source code on GitHub") | ||
[serializr.js:316-325](https://github.com/mobxjs/serializr/blob/eb6d0a682e72f8a75645e3a016c501e26ba5c7c4/serializr.js#L316-L325 "Source code on GitHub") | ||
@@ -423,3 +448,3 @@ Returns the standard model schema associated with a class / constructor function | ||
[serializr.js:309-312](https://github.com/mobxjs/serializr/blob/58bf88786bb4ee088b3c267322134ec1f25aefd0/serializr.js#L309-L312 "Source code on GitHub") | ||
[serializr.js:339-342](https://github.com/mobxjs/serializr/blob/eb6d0a682e72f8a75645e3a016c501e26ba5c7c4/serializr.js#L339-L342 "Source code on GitHub") | ||
@@ -442,3 +467,3 @@ Sets the default model schema for class / constructor function. | ||
[serializr.js:364-382](https://github.com/mobxjs/serializr/blob/58bf88786bb4ee088b3c267322134ec1f25aefd0/serializr.js#L364-L382 "Source code on GitHub") | ||
[serializr.js:394-412](https://github.com/mobxjs/serializr/blob/eb6d0a682e72f8a75645e3a016c501e26ba5c7c4/serializr.js#L394-L412 "Source code on GitHub") | ||
@@ -458,3 +483,3 @@ Serializes an object (graph) into json using the provided model schema. | ||
[serializr.js:436-454](https://github.com/mobxjs/serializr/blob/58bf88786bb4ee088b3c267322134ec1f25aefd0/serializr.js#L436-L454 "Source code on GitHub") | ||
[serializr.js:470-488](https://github.com/mobxjs/serializr/blob/eb6d0a682e72f8a75645e3a016c501e26ba5c7c4/serializr.js#L470-L488 "Source code on GitHub") | ||
@@ -478,3 +503,3 @@ Deserializes a json structor into an object graph. | ||
[serializr.js:620-641](https://github.com/mobxjs/serializr/blob/58bf88786bb4ee088b3c267322134ec1f25aefd0/serializr.js#L620-L641 "Source code on GitHub") | ||
[serializr.js:657-678](https://github.com/mobxjs/serializr/blob/eb6d0a682e72f8a75645e3a016c501e26ba5c7c4/serializr.js#L657-L678 "Source code on GitHub") | ||
@@ -495,3 +520,3 @@ Similar to deserialize, but updates an existing object instance. | ||
[serializr.js:663-675](https://github.com/mobxjs/serializr/blob/58bf88786bb4ee088b3c267322134ec1f25aefd0/serializr.js#L663-L675 "Source code on GitHub") | ||
[serializr.js:700-712](https://github.com/mobxjs/serializr/blob/eb6d0a682e72f8a75645e3a016c501e26ba5c7c4/serializr.js#L700-L712 "Source code on GitHub") | ||
@@ -515,3 +540,3 @@ Indicates that this field contains a primitive value (or Date) which should be serialized literally to json. | ||
[serializr.js:714-728](https://github.com/mobxjs/serializr/blob/58bf88786bb4ee088b3c267322134ec1f25aefd0/serializr.js#L714-L728 "Source code on GitHub") | ||
[serializr.js:751-765](https://github.com/mobxjs/serializr/blob/eb6d0a682e72f8a75645e3a016c501e26ba5c7c4/serializr.js#L751-L765 "Source code on GitHub") | ||
@@ -558,3 +583,3 @@ Similar to primitive, but this field will be marked as the identifier for the given Model type. | ||
[serializr.js:739-754](https://github.com/mobxjs/serializr/blob/58bf88786bb4ee088b3c267322134ec1f25aefd0/serializr.js#L739-L754 "Source code on GitHub") | ||
[serializr.js:776-791](https://github.com/mobxjs/serializr/blob/eb6d0a682e72f8a75645e3a016c501e26ba5c7c4/serializr.js#L776-L791 "Source code on GitHub") | ||
@@ -565,3 +590,3 @@ Similar to primitive, serializes instances of Date objects | ||
[serializr.js:773-784](https://github.com/mobxjs/serializr/blob/58bf88786bb4ee088b3c267322134ec1f25aefd0/serializr.js#L773-L784 "Source code on GitHub") | ||
[serializr.js:810-821](https://github.com/mobxjs/serializr/blob/eb6d0a682e72f8a75645e3a016c501e26ba5c7c4/serializr.js#L810-L821 "Source code on GitHub") | ||
@@ -591,3 +616,3 @@ Alias indicates that this model property should be named differently in the generated json. | ||
[serializr.js:803-812](https://github.com/mobxjs/serializr/blob/58bf88786bb4ee088b3c267322134ec1f25aefd0/serializr.js#L803-L812 "Source code on GitHub") | ||
[serializr.js:840-849](https://github.com/mobxjs/serializr/blob/eb6d0a682e72f8a75645e3a016c501e26ba5c7c4/serializr.js#L840-L849 "Source code on GitHub") | ||
@@ -618,3 +643,3 @@ Can be used to create simple custom propSchema. | ||
[serializr.js:843-861](https://github.com/mobxjs/serializr/blob/58bf88786bb4ee088b3c267322134ec1f25aefd0/serializr.js#L843-L861 "Source code on GitHub") | ||
[serializr.js:880-898](https://github.com/mobxjs/serializr/blob/eb6d0a682e72f8a75645e3a016c501e26ba5c7c4/serializr.js#L880-L898 "Source code on GitHub") | ||
@@ -656,3 +681,3 @@ `object` indicates that this property contains an object that needs to be (de)serialized | ||
[serializr.js:920-953](https://github.com/mobxjs/serializr/blob/58bf88786bb4ee088b3c267322134ec1f25aefd0/serializr.js#L920-L953 "Source code on GitHub") | ||
[serializr.js:957-990](https://github.com/mobxjs/serializr/blob/eb6d0a682e72f8a75645e3a016c501e26ba5c7c4/serializr.js#L957-L990 "Source code on GitHub") | ||
@@ -722,3 +747,3 @@ `reference` can be used to (de)serialize references that point to other models. | ||
[serializr.js:990-1011](https://github.com/mobxjs/serializr/blob/58bf88786bb4ee088b3c267322134ec1f25aefd0/serializr.js#L990-L1011 "Source code on GitHub") | ||
[serializr.js:1027-1048](https://github.com/mobxjs/serializr/blob/eb6d0a682e72f8a75645e3a016c501e26ba5c7c4/serializr.js#L1027-L1048 "Source code on GitHub") | ||
@@ -759,3 +784,3 @@ List indicates that this property contains a list of things. | ||
[serializr.js:1025-1074](https://github.com/mobxjs/serializr/blob/58bf88786bb4ee088b3c267322134ec1f25aefd0/serializr.js#L1025-L1074 "Source code on GitHub") | ||
[serializr.js:1062-1111](https://github.com/mobxjs/serializr/blob/eb6d0a682e72f8a75645e3a016c501e26ba5c7c4/serializr.js#L1062-L1111 "Source code on GitHub") | ||
@@ -867,3 +892,4 @@ Similar to list, but map represents a string keyed dynamic collection. | ||
getDefaultModelSchema(Todo).factory = (context, json) => { | ||
getDefaultModelSchema(Todo).factory = (context) => { | ||
const json = context.json; | ||
if (someTodoStoreById[json.id]) | ||
@@ -870,0 +896,0 @@ return someTodoStoreById[json.id] // reuse instance |
@@ -73,1 +73,5 @@ // TODO: put this in the source files, and extract it, to preserve comments | ||
export function custom(serializer: (value: any) => any, deserializer: (jsonValue: any) => any): PropSchema; | ||
export function serializeAll<T extends Function>(clazz: T): T | ||
export const SKIP: {} |
@@ -5,2 +5,4 @@ (function(g) { | ||
function mrFactory() { | ||
// Indicate properties should be skipped | ||
var SKIP = typeof Symbol !== "undefined" ? Symbol("SKIP") : { SKIP: true } | ||
/* | ||
@@ -282,2 +284,30 @@ * Generic utility functions | ||
/** | ||
* 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 | ||
} | ||
/** | ||
* Returns the standard model schema associated with a class / constructor function | ||
@@ -407,3 +437,6 @@ * | ||
return | ||
var jsonValue = propDef.serializer(obj[key]) | ||
var jsonValue = propDef.serializer(obj[key], key, obj) | ||
if (jsonValue === SKIP){ | ||
return | ||
} | ||
res[propDef.jsonname || key] = jsonValue | ||
@@ -417,4 +450,5 @@ }) | ||
var value = obj[key] | ||
invariant(isPrimitive(value), "encountered non primitive value while serializing '*' properties in property '" + key + "': " + value) | ||
target[key] = value | ||
// 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 | ||
} | ||
@@ -498,3 +532,5 @@ } | ||
context.rootContext.createCallback(function (value) { | ||
target[propName] = value | ||
if (value !== SKIP){ | ||
target[propName] = value | ||
} | ||
}), | ||
@@ -517,2 +553,3 @@ context, | ||
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) | ||
@@ -1092,2 +1129,3 @@ obj[key] = value | ||
serialize: serialize, | ||
serializeAll: serializeAll, | ||
deserialize: deserialize, | ||
@@ -1105,3 +1143,4 @@ update: update, | ||
ref: reference, // deprecate | ||
custom: custom | ||
custom: custom, | ||
SKIP: SKIP | ||
} | ||
@@ -1108,0 +1147,0 @@ } |
/** serializr - (c) Michel Weststrate 2016 - MIT Licensed */ | ||
!function(e){"use strict";function r(){function e(e){if(e)throw new Error(e)}function r(e){var r=!1;return function(){return r?void t(!1,"callback was invoked twice"):(r=!0,e.apply(null,arguments))}}function t(e,r){if(!e)throw new Error("[serializr] "+(r||"Illegal State"))}function n(e,r,t){if(0===e.length)return void t(null,[]);var n=e.length,i=[],o=!1,a=function(e,r,a){r?o||(o=!0,t(r)):(i[e]=a,0===--n&&t(null,i))};e.forEach(function(e,t){r(e,a.bind(null,t))})}function i(e){return null===e||"object"!=typeof e&&"function"!=typeof e}function o(e){return{factory:function(){return{}},props:e}}function a(e,r,n){t(e!==Object,"one cannot simply put define a model schema for Object"),t("function"==typeof e,"expected constructor function");var i={targetClass:e,factory:n||function(){return new e},props:r};if(e.prototype.constructor!==Object){var o=l(e.prototype.constructor);o&&o.targetClass!==e&&(i.extends=o)}return f(e,i),i}function s(e,r,n){if(1===arguments.length){var i=e===!0?B:e;return t(d(i),"@serializable expects prop schema"),c.bind(null,i)}return c(E(),e,r,n)}function u(e){var r=e.toString().replace(U,""),t=r.slice(r.indexOf("(")+1,r.indexOf(")")).match($);return null===t&&(t=[]),t}function c(e,r,n,i){t(arguments.length>=2,"too few arguments. Please use @serializable as property decorator");var o;if(void 0===n&&"function"==typeof r&&r.prototype&&void 0!==i&&"number"==typeof i){t(d(e),"Constructor params must use alias(name)"),t(e.jsonname,"Constructor params must use alias(name)");var s=u(r);s.length>=i&&(n=s[i],e.paramNumber=i,i=void 0,r=r.prototype,o=function(e){function t(e){return r.constructor.apply(this,e)}t.prototype=r.constructor.prototype;for(var n=[],i=0;i<r.constructor.length;i++)Object.keys(e.modelSchema.props).forEach(function(r){var t=e.modelSchema.props[r];t.paramNumber===i&&(n[i]=e.json[t.jsonname])});return new t(n)})}t("string"==typeof n,"incorrect usage of @serializable decorator");var c=l(r);return c&&r.constructor.hasOwnProperty("serializeInfo")||(c=a(r.constructor,{},o)),c&&c.targetClass!==r.constructor&&(c=a(r.constructor,{},o)),c.props[n]=e,!i||i.get||i.set||(i.writable=!0),i}function l(e){return e?p(e)?e:p(e.serializeInfo)?e.serializeInfo:e.constructor&&e.constructor.serializeInfo?e.constructor.serializeInfo:void 0:null}function f(e,r){return t(p(r)),e.serializeInfo=r}function p(e){return e&&e.factory&&e.props}function d(e){return e&&e.serializer&&e.deserializer}function h(e){return"object"==typeof e&&!!e.jsonname}function m(e){return"object"==typeof e&&e.identifier===!0}function v(e){for(t(p(e));e;){for(var r in e.props)if("object"==typeof e.props[r]&&e.props[r].identifier===!0)return r;e=e.extends}return null}function g(e,r){for(;e;){if(e===r)return!0;e=e.extends}return!1}function y(e,r){t(1===arguments.length||2===arguments.length,"serialize expects one or 2 arguments");var n=1===arguments.length?e:r,i=1===arguments.length?null:e;if(Array.isArray(n)){if(0===n.length)return[];i||(i=l(n[0]))}else i||(i=l(n));return t(!!i,"Failed to find default schema for "+e),Array.isArray(n)?n.map(function(e){return b(i,e)}):b(i,n)}function b(e,r){t(e&&"object"==typeof e,"Expected schema"),t(r&&"object"==typeof r,"Expected object");var n;return n=e.extends?b(e.extends,r):{},Object.keys(e.props).forEach(function(i){var o=e.props[i];if("*"===i)return t(o===!0,"prop schema '*' can onle be used with 'true'"),void z(e,r,n);if(o===!0&&(o=B),o!==!1){var a=o.serializer(r[i]);n[o.jsonname||i]=a}}),n}function z(e,r,n){for(var o in r)if(r.hasOwnProperty(o)&&!(o in e.props)){var a=r[o];t(i(a),"encountered non primitive value while serializing '*' properties in property '"+o+"': "+a),n[o]=a}}function j(r,i,o,a){if(t(arguments.length>=2,"deserialize expects at least 2 arguments"),r=l(r),t(p(r),"first argument should be model schema"),Array.isArray(i)){var s=[];return n(i,function(e,t){var n=x(null,r,e,t,a);s.push(n)},o||e),s}return x(null,r,i,o,a)}function x(r,n,i,o,a){if(null===i||void 0===i)return void o(null,null);var s=new k(r,n,i,o,a),u=n.factory(s);t(!!u,"No object returned from factory"),s.target=u;var c=s.createCallback(e);return R(s,n,i,u),c(),u}function R(e,r,n,i){r.extends&&R(e,r.extends,n,i),Object.keys(r.props).forEach(function(o){var a=r.props[o];if("*"===o)return t(a===!0,"prop schema '*' can onle be used with 'true'"),void w(r,i,n);if(a===!0&&(a=B),a!==!1){var s=a.jsonname||o;s in n&&a.deserializer(n[s],e.rootContext.createCallback(function(e){i[o]=e}),e,i[o])}})}function C(e,r){for(var t in e.props)if("object"==typeof e.props[t]&&e.props[t].jsonname===r)return!0;return!1}function w(e,r,n){for(var o in n)if(!(o in e.props||C(e,o))){var a=n[o];t(i(a),"encountered non primitive value while deserializing '*' properties in property '"+o+"': "+a),r[o]=a}}function k(r,t,n,i,o){this.parentContext=r,this.isRoot=!r,this.pendingCallbacks=0,this.pendingRefsCount=0,this.onReadyCb=i||e,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=r.rootContext,this.args=r.args)}function S(r,n,i,o,a){var s=2===arguments.length||"function"==typeof arguments[2];s?(n=arguments[0],r=l(n),i=arguments[1],o=arguments[2],a=arguments[3]):r=l(r),t(p(r),"update failed to determine schema"),t("object"==typeof n&&n&&!Array.isArray(n),"update needs an object");var u=new k(null,r,i,o,a);u.target=n;var c=u.createCallback(e);R(u,r,i,n),c()}function E(){return{serializer:function(e){return t(i(e),"this value is not primitive: "+e),e},deserializer:function(e,r){return i(e)?void r(null,e):void r("[serializr] this value is not primitive: "+e)}}}function O(e){return t(!e||"function"==typeof e,"First argument should be ommitted or function"),{identifier:!0,serializer:B.serializer,deserializer:function(r,t,n){B.deserializer(r,function(r,i){A(i,n.target,n),e&&e(i,n.target,n),t(r,i)})}}}function A(e,r,t){t.rootContext.resolve(t.modelSchema,e,t.target)}function I(){return{serializer:function(e){return null===e||void 0===e?e:(t(e instanceof Date,"Expected Date object"),e.getTime())},deserializer:function(e,r){return null===e||void 0===e?void r(null,e):void r(null,new Date(e))}}}function N(e,r){return t(e&&"string"==typeof e,"expected prop name as first argument"),r=r&&r!==!0?r:B,t(d(r),"expected prop schema as second argument"),t(!h(r),"provided prop is already aliased"),{jsonname:e,serializer:r.serializer,deserializer:r.deserializer,identifier:m(r)}}function D(e,r){return t("function"==typeof e,"first argument should be function"),t("function"==typeof r,"second argument should be function"),{serializer:e,deserializer:function(e,t){t(null,r(e))}}}function M(e){return t("object"==typeof e||"function"==typeof e,"No modelschema provided. If you are importing it from another file be aware of circular dependencies."),{serializer:function(r){return e=l(e),t(p(e),"expected modelSchema, got "+e),null===r||void 0===r?r:y(e,r)},deserializer:function(r,n,i){return e=l(e),t(p(e),"expected modelSchema, got "+e),null===r||void 0===r?void n(null,r):void x(i,e,r,n)}}}function P(e,r){function n(){if(o=!0,t("string"!=typeof e||r,"if the reference target is specified by attribute name, a lookup function is required"),t(!r||"function"==typeof r,"second argument should be a lookup function"),"string"==typeof e)i=e;else{var n=l(e);t(p(n),"expected model schema or string as first argument for 'ref', got "+n),r=r||F(n),i=v(n),t(!!i,"provided model schema doesn't define an identifier() property and cannot be used by 'ref'.")}}t(!!e,"No modelschema provided. If you are importing it from another file be aware of circular dependencies.");var i,o=!1;return{serializer:function(e){return o||n(),e?e[i]:null},deserializer:function(e,t,i){o||n(),null===e||void 0===e?t(null,e):r(e,t,i)}}}function F(e){return function(r,t,n){n.rootContext.await(e,r,t)}}function J(e){return e=e||B,t(d(e),"expected prop schema as second argument"),t(!h(e),"provided prop is aliased, please put aliases first"),{serializer:function(r){return t(r&&"length"in r&&"map"in r,"expected array (like) object"),r.map(e.serializer)},deserializer:function(r,t,i){return Array.isArray(r)?void n(r,function(r,t){return e.deserializer(r,t,i)},t):void t("[serializr] expected JSON array")}}}function q(e){return e&&"function"==typeof e.keys&&"function"==typeof e.clear}function T(e){return e=e||B,t(d(e),"expected prop schema as second argument"),t(!h(e),"provided prop is aliased, please put aliases first"),{serializer:function(r){t(r&&"object"==typeof r,"expected object or Map");var n=q(r),i={};if(n)r.forEach(function(r,t){i[t]=e.serializer(r)});else for(var o in r)i[o]=e.serializer(r[o]);return i},deserializer:function(r,t,n,i){if(!r||"object"!=typeof r)return void t("[serializr] expected JSON object");var o=Object.keys(r);J(e).deserializer(o.map(function(e){return r[e]}),function(e,r){if(e)return void t(e);var n,a=q(i);a?(i.clear(),n=i):n={};for(var s=0,u=o.length;s<u;s++)a?n.set(o[s],r[s]):n[o[s]]=r[s];t(null,n)},n)}}}var U=/((\/\/.*$)|(\/\*[\s\S]*?\*\/))/gm,$=/([^\s,]+)/g;k.prototype.createCallback=function(e){return this.pendingCallbacks++,r(function(r,t){r?this.hasError||(this.hasError=!0,this.onReadyCb(r)):this.hasError||(e(t),--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('", "')+'"')):this.onReadyCb(null,this.target)))}.bind(this))},k.prototype.await=function(e,r,n){if(t(this.isRoot),r in this.resolvedRefs){var i=this.resolvedRefs[r].filter(function(r){return g(r.modelSchema,e)})[0];if(i)return void n(null,i.value)}this.pendingRefsCount++,this.pendingRefs[r]||(this.pendingRefs[r]=[]),this.pendingRefs[r].push({modelSchema:e,uuid:r,callback:n})},k.prototype.resolve=function(e,r,n){if(t(this.isRoot),this.resolvedRefs[r]||(this.resolvedRefs[r]=[]),this.resolvedRefs[r].push({modelSchema:e,value:n}),r in this.pendingRefs)for(var i=this.pendingRefs[r].length-1;i>=0;i--){var o=this.pendingRefs[r][i];g(e,o.modelSchema)&&(this.pendingRefs[r].splice(i,1),this.pendingRefsCount--,o.callback(null,n))}};var B=E();return{createModelSchema:a,createSimpleSchema:o,setDefaultModelSchema:f,getDefaultModelSchema:l,serializable:s,serialize:y,deserialize:j,update:S,primitive:E,identifier:O,date:I,alias:N,list:J,map:T,object:M,child:M,reference:P,ref:P,custom:D}}"object"==typeof exports?module.exports=r():"function"==typeof define&&define.amd?define("serializr",[],r):e.serializr=r()}(function(){return this}()); | ||
!function(e){"use strict";function r(){function e(e){if(e)throw new Error(e)}function r(e){var r=!1;return function(){return r?void t(!1,"callback was invoked twice"):(r=!0,e.apply(null,arguments))}}function t(e,r){if(!e)throw new Error("[serializr] "+(r||"Illegal State"))}function n(e,r,t){if(0===e.length)return void t(null,[]);var n=e.length,i=[],o=!1,a=function(e,r,a){r?o||(o=!0,t(r)):(i[e]=a,0===--n&&t(null,i))};e.forEach(function(e,t){r(e,a.bind(null,t))})}function i(e){return null===e||"object"!=typeof e&&"function"!=typeof e}function o(e){return{factory:function(){return{}},props:e}}function a(e,r,n){t(e!==Object,"one cannot simply put define a model schema for Object"),t("function"==typeof e,"expected constructor function");var i={targetClass:e,factory:n||function(){return new e},props:r};if(e.prototype.constructor!==Object){var o=f(e.prototype.constructor);o&&o.targetClass!==e&&(i.extends=o)}return p(e,i),i}function s(e,r,n){if(1===arguments.length){var i=e===!0?G:e;return t(h(i),"@serializable expects prop schema"),c.bind(null,i)}return c(O(),e,r,n)}function u(e){var r=e.toString().replace($,""),t=r.slice(r.indexOf("(")+1,r.indexOf(")")).match(B);return null===t&&(t=[]),t}function c(e,r,n,i){t(arguments.length>=2,"too few arguments. Please use @serializable as property decorator");var o;if(void 0===n&&"function"==typeof r&&r.prototype&&void 0!==i&&"number"==typeof i){t(h(e),"Constructor params must use alias(name)"),t(e.jsonname,"Constructor params must use alias(name)");var s=u(r);s.length>=i&&(n=s[i],e.paramNumber=i,i=void 0,r=r.prototype,o=function(e){function t(e){return r.constructor.apply(this,e)}t.prototype=r.constructor.prototype;for(var n=[],i=0;i<r.constructor.length;i++)Object.keys(e.modelSchema.props).forEach(function(r){var t=e.modelSchema.props[r];t.paramNumber===i&&(n[i]=e.json[t.jsonname])});return new t(n)})}t("string"==typeof n,"incorrect usage of @serializable decorator");var c=f(r);return c&&r.constructor.hasOwnProperty("serializeInfo")||(c=a(r.constructor,{},o)),c&&c.targetClass!==r.constructor&&(c=a(r.constructor,{},o)),c.props[n]=e,!i||i.get||i.set||(i.writable=!0),i}function l(e){t(1===arguments.length&&"function"==typeof e,"@serializeAll can only be used as class decorator");var r=f(e);return r&&e.hasOwnProperty("serializeInfo")||(r=a(e,{}),p(e,r)),f(e).props["*"]=!0,e}function f(e){return e?d(e)?e:d(e.serializeInfo)?e.serializeInfo:e.constructor&&e.constructor.serializeInfo?e.constructor.serializeInfo:void 0:null}function p(e,r){return t(d(r)),e.serializeInfo=r}function d(e){return e&&e.factory&&e.props}function h(e){return e&&e.serializer&&e.deserializer}function m(e){return"object"==typeof e&&!!e.jsonname}function v(e){return"object"==typeof e&&e.identifier===!0}function y(e){for(t(d(e));e;){for(var r in e.props)if("object"==typeof e.props[r]&&e.props[r].identifier===!0)return r;e=e.extends}return null}function g(e,r){for(;e;){if(e===r)return!0;e=e.extends}return!1}function b(e,r){t(1===arguments.length||2===arguments.length,"serialize expects one or 2 arguments");var n=1===arguments.length?e:r,i=1===arguments.length?null:e;if(Array.isArray(n)){if(0===n.length)return[];i||(i=f(n[0]))}else i||(i=f(n));return t(!!i,"Failed to find default schema for "+e),Array.isArray(n)?n.map(function(e){return z(i,e)}):z(i,n)}function z(e,r){t(e&&"object"==typeof e,"Expected schema"),t(r&&"object"==typeof r,"Expected object");var n;return n=e.extends?z(e.extends,r):{},Object.keys(e.props).forEach(function(i){var o=e.props[i];if("*"===i)return t(o===!0,"prop schema '*' can onle be used with 'true'"),void j(e,r,n);if(o===!0&&(o=G),o!==!1){var a=o.serializer(r[i],i,r);a!==U&&(n[o.jsonname||i]=a)}}),n}function j(e,r,t){for(var n in r)if(r.hasOwnProperty(n)&&!(n in e.props)){var o=r[n];i(o)&&(t[n]=o)}}function x(r,i,o,a){if(t(arguments.length>=2,"deserialize expects at least 2 arguments"),r=f(r),t(d(r),"first argument should be model schema"),Array.isArray(i)){var s=[];return n(i,function(e,t){var n=R(null,r,e,t,a);s.push(n)},o||e),s}return R(null,r,i,o,a)}function R(r,n,i,o,a){if(null===i||void 0===i)return void o(null,null);var s=new k(r,n,i,o,a),u=n.factory(s);t(!!u,"No object returned from factory"),s.target=u;var c=s.createCallback(e);return C(s,n,i,u),c(),u}function C(e,r,n,i){r.extends&&C(e,r.extends,n,i),Object.keys(r.props).forEach(function(o){var a=r.props[o];if("*"===o)return t(a===!0,"prop schema '*' can onle be used with 'true'"),void w(r,i,n);if(a===!0&&(a=G),a!==!1){var s=a.jsonname||o;s in n&&a.deserializer(n[s],e.rootContext.createCallback(function(e){e!==U&&(i[o]=e)}),e,i[o])}})}function S(e,r){for(var t in e.props)if("object"==typeof e.props[t]&&e.props[t].jsonname===r)return!0;return!1}function w(e,r,n){for(var o in n)if(!(o in e.props||S(e,o))){var a=n[o];t(i(a),"encountered non primitive value while deserializing '*' properties in property '"+o+"': "+a),r[o]=a}}function k(r,t,n,i,o){this.parentContext=r,this.isRoot=!r,this.pendingCallbacks=0,this.pendingRefsCount=0,this.onReadyCb=i||e,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=r.rootContext,this.args=r.args)}function E(r,n,i,o,a){var s=2===arguments.length||"function"==typeof arguments[2];s?(n=arguments[0],r=f(n),i=arguments[1],o=arguments[2],a=arguments[3]):r=f(r),t(d(r),"update failed to determine schema"),t("object"==typeof n&&n&&!Array.isArray(n),"update needs an object");var u=new k(null,r,i,o,a);u.target=n;var c=u.createCallback(e);C(u,r,i,n),c()}function O(){return{serializer:function(e){return t(i(e),"this value is not primitive: "+e),e},deserializer:function(e,r){return i(e)?void r(null,e):void r("[serializr] this value is not primitive: "+e)}}}function I(e){return t(!e||"function"==typeof e,"First argument should be ommitted or function"),{identifier:!0,serializer:G.serializer,deserializer:function(r,t,n){G.deserializer(r,function(r,i){A(i,n.target,n),e&&e(i,n.target,n),t(r,i)})}}}function A(e,r,t){t.rootContext.resolve(t.modelSchema,e,t.target)}function N(){return{serializer:function(e){return null===e||void 0===e?e:(t(e instanceof Date,"Expected Date object"),e.getTime())},deserializer:function(e,r){return null===e||void 0===e?void r(null,e):void r(null,new Date(e))}}}function P(e,r){return t(e&&"string"==typeof e,"expected prop name as first argument"),r=r&&r!==!0?r:G,t(h(r),"expected prop schema as second argument"),t(!m(r),"provided prop is already aliased"),{jsonname:e,serializer:r.serializer,deserializer:r.deserializer,identifier:v(r)}}function D(e,r){return t("function"==typeof e,"first argument should be function"),t("function"==typeof r,"second argument should be function"),{serializer:e,deserializer:function(e,t){t(null,r(e))}}}function M(e){return t("object"==typeof e||"function"==typeof e,"No modelschema provided. If you are importing it from another file be aware of circular dependencies."),{serializer:function(r){return e=f(e),t(d(e),"expected modelSchema, got "+e),null===r||void 0===r?r:b(e,r)},deserializer:function(r,n,i){return e=f(e),t(d(e),"expected modelSchema, got "+e),null===r||void 0===r?void n(null,r):void R(i,e,r,n)}}}function K(e,r){function n(){if(o=!0,t("string"!=typeof e||r,"if the reference target is specified by attribute name, a lookup function is required"),t(!r||"function"==typeof r,"second argument should be a lookup function"),"string"==typeof e)i=e;else{var n=f(e);t(d(n),"expected model schema or string as first argument for 'ref', got "+n),r=r||F(n),i=y(n),t(!!i,"provided model schema doesn't define an identifier() property and cannot be used by 'ref'.")}}t(!!e,"No modelschema provided. If you are importing it from another file be aware of circular dependencies.");var i,o=!1;return{serializer:function(e){return o||n(),e?e[i]:null},deserializer:function(e,t,i){o||n(),null===e||void 0===e?t(null,e):r(e,t,i)}}}function F(e){return function(r,t,n){n.rootContext.await(e,r,t)}}function J(e){return e=e||G,t(h(e),"expected prop schema as second argument"),t(!m(e),"provided prop is aliased, please put aliases first"),{serializer:function(r){return t(r&&"length"in r&&"map"in r,"expected array (like) object"),r.map(e.serializer)},deserializer:function(r,t,i){return Array.isArray(r)?void n(r,function(r,t){return e.deserializer(r,t,i)},t):void t("[serializr] expected JSON array")}}}function q(e){return e&&"function"==typeof e.keys&&"function"==typeof e.clear}function T(e){return e=e||G,t(h(e),"expected prop schema as second argument"),t(!m(e),"provided prop is aliased, please put aliases first"),{serializer:function(r){t(r&&"object"==typeof r,"expected object or Map");var n=q(r),i={};if(n)r.forEach(function(r,t){i[t]=e.serializer(r)});else for(var o in r)i[o]=e.serializer(r[o]);return i},deserializer:function(r,t,n,i){if(!r||"object"!=typeof r)return void t("[serializr] expected JSON object");var o=Object.keys(r);J(e).deserializer(o.map(function(e){return r[e]}),function(e,r){if(e)return void t(e);var n,a=q(i);a?(i.clear(),n=i):n={};for(var s=0,u=o.length;s<u;s++)a?n.set(o[s],r[s]):n[o[s]]=r[s];t(null,n)},n)}}}var U="undefined"!=typeof Symbol?Symbol("SKIP"):{SKIP:!0},$=/((\/\/.*$)|(\/\*[\s\S]*?\*\/))/gm,B=/([^\s,]+)/g;k.prototype.createCallback=function(e){return this.pendingCallbacks++,r(function(r,t){r?this.hasError||(this.hasError=!0,this.onReadyCb(r)):this.hasError||(e(t),--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('", "')+'"')):this.onReadyCb(null,this.target)))}.bind(this))},k.prototype.await=function(e,r,n){if(t(this.isRoot),r in this.resolvedRefs){var i=this.resolvedRefs[r].filter(function(r){return g(r.modelSchema,e)})[0];if(i)return void n(null,i.value)}this.pendingRefsCount++,this.pendingRefs[r]||(this.pendingRefs[r]=[]),this.pendingRefs[r].push({modelSchema:e,uuid:r,callback:n})},k.prototype.resolve=function(e,r,n){if(t(this.isRoot),this.resolvedRefs[r]||(this.resolvedRefs[r]=[]),this.resolvedRefs[r].push({modelSchema:e,value:n}),r in this.pendingRefs)for(var i=this.pendingRefs[r].length-1;i>=0;i--){var o=this.pendingRefs[r][i];g(e,o.modelSchema)&&(this.pendingRefs[r].splice(i,1),this.pendingRefsCount--,o.callback(null,n))}};var G=O();return{createModelSchema:a,createSimpleSchema:o,setDefaultModelSchema:p,getDefaultModelSchema:f,serializable:s,serialize:b,serializeAll:l,deserialize:x,update:E,primitive:O,identifier:I,date:N,alias:P,list:J,map:T,object:M,child:M,reference:K,ref:K,custom:D,SKIP:U}}"object"==typeof exports?module.exports=r():"function"==typeof define&&define.amd?define("serializr",[],r):e.serializr=r()}(function(){return this}()); | ||
//# sourceMappingURL=serializr.min.js.map |
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
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
113329
1183
1002