New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

serializr

Package Overview
Dependencies
Maintainers
2
Versions
38
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

serializr - npm Package Compare versions

Comparing version 1.2.0 to 1.3.0

4

CHANGELOG.md

@@ -0,1 +1,5 @@

# 1.2.1
* Introduced `customAsync`, to support asynchronous custom deserialization
# 1.1.12

@@ -2,0 +6,0 @@

@@ -780,9 +780,12 @@ /**

* The `deserializer` function has the signature:
* `(value, context) => void`
* The `deserializer` function has the following signature for synchronous processing
* `(value, context, oldValue) => void`
* For asynchronous processing the function expects the following signature
* `(value, context, oldValue, callback) => void`
* When deserializing the object `{b: 2}` the `deserializer` function will be called with `deserializer(2, contextObj)` ([contextObj reference](https://github.com/mobxjs/serializr#deserialization-context)).
*
* @example
* var schema = _.createSimpleSchema({
* var schemaDefault = _.createSimpleSchema({
* a: _.custom(

@@ -797,5 +800,25 @@ * function(v) {

* });
* t.deepEqual(_.serialize(s, { a: 4 }), { a: 6 });
* t.deepEqual(_.deserialize(s, { a: 6 }), { a: 4 });
* t.deepEqual(_.serialize(schemaDefault, { a: 4 }), { a: 6 });
* t.deepEqual(_.deserialize(schemaDefault, { a: 6 }), { a: 4 });
*
* var schemaWithAsyncProps = _.createSimpleSchema({
* a: _.customAsync(
* function(v) {
* return v + 2;
* },
* function(v, context, oldValue, callback) {
* somePromise(v, context, oldValue).then((result) => {
* callback(null, result - 2)
* }.catch((err) => {
* callback(err)
* }
* }
* ),
* });
* t.deepEqual(_.serialize(schemaWithAsyncProps, { a: 4 }), { a: 6 });
* _.deserialize(schemaWithAsyncProps, { a: 6 }, (err, res) => {
* t.deepEqual(res.a, 4)
* };
*
* @param {function} serializer function that takes a model value and turns it into a json value

@@ -807,7 +830,11 @@ * @param {function} deserializer function that takes a json value and turns it into a model value. It also takes context argument, which can allow you to deserialize based on the context of other parameters.

invariant(typeof serializer === "function", "first argument should be function");
invariant(typeof deserializer === "function", "second argument should be function");
invariant((typeof deserializer === "function"), "second argument should be a function or promise");
return {
serializer: serializer,
deserializer: function (jsonValue, done, context, oldValue) {
done(null, deserializer(jsonValue, context, oldValue));
if (deserializer.length === 4) {
deserializer(jsonValue, context, oldValue, done);
} else {
done(null, deserializer(jsonValue, context, oldValue));
}
}

@@ -814,0 +841,0 @@ }

@@ -787,9 +787,12 @@ /** serializr - (c) Michel Weststrate 2016 - MIT Licensed */

* The `deserializer` function has the signature:
* `(value, context) => void`
* The `deserializer` function has the following signature for synchronous processing
* `(value, context, oldValue) => void`
* For asynchronous processing the function expects the following signature
* `(value, context, oldValue, callback) => void`
* When deserializing the object `{b: 2}` the `deserializer` function will be called with `deserializer(2, contextObj)` ([contextObj reference](https://github.com/mobxjs/serializr#deserialization-context)).
*
* @example
* var schema = _.createSimpleSchema({
* var schemaDefault = _.createSimpleSchema({
* a: _.custom(

@@ -804,5 +807,25 @@ * function(v) {

* });
* t.deepEqual(_.serialize(s, { a: 4 }), { a: 6 });
* t.deepEqual(_.deserialize(s, { a: 6 }), { a: 4 });
* t.deepEqual(_.serialize(schemaDefault, { a: 4 }), { a: 6 });
* t.deepEqual(_.deserialize(schemaDefault, { a: 6 }), { a: 4 });
*
* var schemaWithAsyncProps = _.createSimpleSchema({
* a: _.customAsync(
* function(v) {
* return v + 2;
* },
* function(v, context, oldValue, callback) {
* somePromise(v, context, oldValue).then((result) => {
* callback(null, result - 2)
* }.catch((err) => {
* callback(err)
* }
* }
* ),
* });
* t.deepEqual(_.serialize(schemaWithAsyncProps, { a: 4 }), { a: 6 });
* _.deserialize(schemaWithAsyncProps, { a: 6 }, (err, res) => {
* t.deepEqual(res.a, 4)
* };
*
* @param {function} serializer function that takes a model value and turns it into a json value

@@ -814,7 +837,11 @@ * @param {function} deserializer function that takes a json value and turns it into a model value. It also takes context argument, which can allow you to deserialize based on the context of other parameters.

invariant(typeof serializer === "function", "first argument should be function");
invariant(typeof deserializer === "function", "second argument should be function");
invariant((typeof deserializer === "function"), "second argument should be a function or promise");
return {
serializer: serializer,
deserializer: function (jsonValue, done, context, oldValue) {
done(null, deserializer(jsonValue, context, oldValue));
if (deserializer.length === 4) {
deserializer(jsonValue, context, oldValue, done);
} else {
done(null, deserializer(jsonValue, context, oldValue));
}
}

@@ -821,0 +848,0 @@ }

2

lib/serializr.min.js
/** serializr - (c) Michel Weststrate 2016 - MIT Licensed */
!function(e,r){"object"==typeof exports&&"undefined"!=typeof module?r(exports):"function"==typeof define&&define.amd?define("serializr",["exports"],r):r(e.serializr={})}(this,function(e){"use strict";function r(e){if(e)throw new Error(e)}function n(e,r){if(!e)throw new Error("[serializr] "+(r||"Illegal State"))}function t(e,r,n){if(0!==e.length){var t=e.length,i=[],o=!1;e.forEach(function(e,a){r(e,function(e,r,a){r?o||(o=!0,n(r)):(i[e]=a,0==--t&&n(null,i))}.bind(null,a))})}else n(null,[])}function i(e){return null===e||"object"!=typeof e&&"function"!=typeof e}function o(e){return e&&e.factory&&e.props}function a(e){return e&&e.serializer&&e.deserializer}function s(e){return"object"==typeof e&&!!e.jsonname}function u(e,r){for(;e;){if(e===r)return!0;e=e["extends"]}return!1}function l(e){return e&&"function"==typeof e.keys&&"function"==typeof e.clear}function c(e){return e?o(e)?e:o(e.serializeInfo)?e.serializeInfo:e.constructor&&e.constructor.serializeInfo?e.constructor.serializeInfo:void 0:null}function f(e,r){return n(o(r)),e.serializeInfo=r}function p(e,r,t){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:t||function(){return new e},props:r};if(e.prototype.constructor!==Object){var o=c(e.prototype.constructor);o&&o.targetClass!==e&&(i["extends"]=o)}return f(e,i),i}function d(){return{serializer:function(e){return n(i(e),"this value is not primitive: "+e),e},deserializer:function(e,r){i(e)?r(null,e):r("[serializr] this value is not primitive: "+e)}}}var h="undefined"!=typeof Symbol?Symbol("SKIP"):{SKIP:!0},m=d(),y=/((\/\/.*$)|(\/\*[\s\S]*?\*\/))/gm,g=/([^\s,]+)/g;function v(e,r,t,i){var o,s,u;if(n(arguments.length>=2,"too few arguments. Please use @serializable as property decorator"),t===undefined&&"function"==typeof r&&r.prototype&&i!==undefined&&"number"==typeof i){n(a(e),"Constructor params must use alias(name)"),n(e.jsonname,"Constructor params must use alias(name)");var l=(s=r.toString().replace(y,""),null===(u=s.slice(s.indexOf("(")+1,s.indexOf(")")).match(g))&&(u=[]),u);l.length>=i&&(t=l[i],e.paramNumber=i,i=undefined,r=r.prototype,o=function(e){for(var n=[],t=0;t<r.constructor.length;t++)Object.keys(e.modelSchema.props).forEach(function(r){var i=e.modelSchema.props[r];i.paramNumber===t&&(n[t]=e.json[i.jsonname])});return new(Function.prototype.bind.apply(r.constructor,[null].concat(n)))})}n("string"==typeof t,"incorrect usage of @serializable decorator");var f=c(r);return f&&r.constructor.hasOwnProperty("serializeInfo")||(f=p(r.constructor,{},o)),f&&f.targetClass!==r.constructor&&(f=p(r.constructor,{},o)),f.props[t]=e,!i||i.get||i.set||(i.writable=!0),i}function b(e,r){n(1===arguments.length||2===arguments.length,"serialize expects one or 2 arguments");var t=1===arguments.length?e:r,i=1===arguments.length?null:e;if(Array.isArray(t)){if(0===t.length)return[];i||(i=c(t[0]))}else i||(i=c(t));return n(!!i,"Failed to find default schema for "+e),Array.isArray(t)?t.map(function(e){return z(i,e)}):z(i,t)}function z(e,r){var t;return n(e&&"object"==typeof e,"Expected schema"),n(r&&"object"==typeof r,"Expected object"),t=e["extends"]?z(e["extends"],r):{},Object.keys(e.props).forEach(function(o){var a=e.props[o];if("*"===o)return n(!0===a,"prop schema '*' can onle be used with 'true'"),void function(e,r,n){for(var t in r)if(r.hasOwnProperty(t)&&!(t in e.props)){var o=r[t];i(o)&&(n[t]=o)}}(e,r,t);if(!0===a&&(a=m),!1!==a){var s=a.serializer(r[o],o,r);s!==h&&(t[a.jsonname||o]=s)}}),t}function j(e,n,t,i,o){this.parentContext=e,this.isRoot=!e,this.pendingCallbacks=0,this.pendingRefsCount=0,this.onReadyCb=i||r,this.json=t,this.target=null,this.hasError=!1,this.modelSchema=n,this.isRoot?(this.rootContext=this,this.args=o,this.pendingRefs={},this.resolvedRefs={}):(this.rootContext=e.rootContext,this.args=e.args)}function x(e,r){for(var n in e.props)if("object"==typeof e.props[n]&&e.props[n].jsonname===r)return!0;return!1}function R(e,t,i,o,a){if(null!==i&&i!==undefined){var s=new j(e,t,i,o,a),u=t.factory(s);n(!!u,"No object returned from factory"),s.target=u;var l=s.createCallback(r);return C(s,t,i,u),l(),u}o(null,null)}function C(e,r,t,o){r["extends"]&&C(e,r["extends"],t,o),Object.keys(r.props).forEach(function(a){var s=r.props[a];if("*"===a)return n(!0===s,"prop schema '*' can onle be used with 'true'"),void function(e,r,t){for(var o in t)if(!(o in e.props||x(e,o))){var a=t[o];n(i(a),"encountered non primitive value while deserializing '*' properties in property '"+o+"': "+a),r[o]=a}}(r,o,t);if(!0===s&&(s=m),!1!==s){var u=s.jsonname||a;u in t&&s.deserializer(t[u],e.rootContext.createCallback(function(e){e!==h&&(o[a]=e)}),e,o[a])}})}function S(e){return n("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 n(o(e=c(e)),"expected modelSchema, got "+e),null===r||r===undefined?r:b(e,r)},deserializer:function(r,t,i){n(o(e=c(e)),"expected modelSchema, got "+e),null!==r&&r!==undefined?R(i,e,r,t):t(null,r)}}}function w(e,r){n(!!e,"No modelschema provided. If you are importing it from another file be aware of circular dependencies.");var t,i=!1;function a(){if(i=!0,n("string"!=typeof e||r,"if the reference target is specified by attribute name, a lookup function is required"),n(!r||"function"==typeof r,"second argument should be a lookup function"),"string"==typeof e)t=e;else{var a=c(e);n(o(a),"expected model schema or string as first argument for 'ref', got "+a),r=r||(s=a,function(e,r,n){n.rootContext.await(s,e,r)}),n(!!(t=function(e){for(n(o(e));e;){for(var r in e.props)if("object"==typeof e.props[r]&&!0===e.props[r].identifier)return r;e=e["extends"]}return null}(a)),"provided model schema doesn't define an identifier() property and cannot be used by 'ref'.")}var s}return{serializer:function(e){return i||a(),e?e[t]:null},deserializer:function(e,n,t){i||a(),null===e||e===undefined?n(null,e):r(e,n,t)}}}function k(e){return n(a(e=e||m),"expected prop schema as first argument"),n(!s(e),"provided prop is aliased, please put aliases first"),{serializer:function(r){return n(r&&"length"in r&&"map"in r,"expected array (like) object"),r.map(e.serializer)},deserializer:function(r,n,i){Array.isArray(r)?t(r,function(r,n){return e.deserializer(r,n,i)},n):n("[serializr] expected JSON array")}}}j.prototype.createCallback=function(e){return this.pendingCallbacks++,r=function(r,n){r?this.hasError||(this.hasError=!0,this.onReadyCb(r)):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('", "')+'"')):this.onReadyCb(null,this.target)))}.bind(this),t=!1,function(){if(!t)return t=!0,r.apply(null,arguments);n(!1,"callback was invoked twice")};var r,t},j.prototype.await=function(e,r,t){if(n(this.isRoot),r in this.resolvedRefs){var i=this.resolvedRefs[r].filter(function(r){return u(r.modelSchema,e)})[0];if(i)return void t(null,i.value)}this.pendingRefsCount++,this.pendingRefs[r]||(this.pendingRefs[r]=[]),this.pendingRefs[r].push({modelSchema:e,uuid:r,callback:t})},j.prototype.resolve=function(e,r,t){if(n(this.isRoot),this.resolvedRefs[r]||(this.resolvedRefs[r]=[]),this.resolvedRefs[r].push({modelSchema:e,value:t}),r in this.pendingRefs)for(var i=this.pendingRefs[r].length-1;i>=0;i--){var o=this.pendingRefs[r][i];u(e,o.modelSchema)&&(this.pendingRefs[r].splice(i,1),this.pendingRefsCount--,o.callback(null,t))}},e.createSimpleSchema=function(e){return{factory:function(){return{}},props:e}},e.createModelSchema=p,e.getDefaultModelSchema=c,e.setDefaultModelSchema=f,e.serializable=function(e,r,t){if(1===arguments.length){var i=!0===e?m:e;return n(a(i),"@serializable expects prop schema"),v.bind(null,i)}return v(d(),e,r,t)},e.serialize=b,e.serializeAll=function(e){n(1===arguments.length&&"function"==typeof e,"@serializeAll can only be used as class decorator");var r=c(e);return r&&e.hasOwnProperty("serializeInfo")||f(e,r=p(e,{})),c(e).props["*"]=!0,e},e.deserialize=function(e,i,a,s){if(n(arguments.length>=2,"deserialize expects at least 2 arguments"),n(o(e=c(e)),"first argument should be model schema"),Array.isArray(i)){var u=[];return t(i,function(r,n){var t=R(null,e,r,n,s);u.push(t)},a||r),u}return R(null,e,i,a,s)},e.update=function(e,t,i,a,s){2===arguments.length||"function"==typeof arguments[2]?(e=c(t=arguments[0]),i=arguments[1],a=arguments[2],s=arguments[3]):e=c(e),n(o(e),"update failed to determine schema"),n("object"==typeof t&&t&&!Array.isArray(t),"update needs an object");var u=new j(null,e,i,a,s);u.target=t;var l=u.createCallback(r);C(u,e,i,t),l()},e.primitive=d,e.identifier=function(e){return n(!e||"function"==typeof e,"First argument should be omitted or function"),{identifier:!0,serializer:m.serializer,deserializer:function(r,n,t){m.deserializer(r,function(r,i){var o,a;o=i,t.target,(a=t).rootContext.resolve(a.modelSchema,o,a.target),e&&e(i,t.target,t),n(r,i)})}}},e.date=function(){return{serializer:function(e){return null===e||e===undefined?e:(n(e instanceof Date,"Expected Date object"),e.getTime())},deserializer:function(e,r){null!==e&&e!==undefined?r(null,new Date(e)):r(null,e)}}},e.alias=function(e,r){return n(e&&"string"==typeof e,"expected prop name as first argument"),n(a(r=r&&!0!==r?r:m),"expected prop schema as second argument"),n(!s(r),"provided prop is already aliased"),{jsonname:e,serializer:r.serializer,deserializer:r.deserializer,identifier:(t=r,"object"==typeof t&&!0===t.identifier)};var t},e.custom=function(e,r){return n("function"==typeof e,"first argument should be function"),n("function"==typeof r,"second argument should be function"),{serializer:e,deserializer:function(e,n,t,i){n(null,r(e,t,i))}}},e.object=S,e.reference=w,e.list=k,e.map=function(e){return n(a(e=e||m),"expected prop schema as first argument"),n(!s(e),"provided prop is aliased, please put aliases first"),{serializer:function(r){n(r&&"object"==typeof r,"expected object or Map");var t=l(r),i={};if(t)r.forEach(function(r,n){i[n]=e.serializer(r)});else for(var o in r)i[o]=e.serializer(r[o]);return i},deserializer:function(r,n,t,i){if(r&&"object"==typeof r){var o=Object.keys(r);k(e).deserializer(o.map(function(e){return r[e]}),function(e,r){if(e)n(e);else{var t,a=l(i);a?(i.clear(),t=i):t={};for(var s=0,u=o.length;s<u;s++)a?t.set(o[s],r[s]):t[o[s]]=r[s];n(null,t)}},t)}else n("[serializr] expected JSON object")}}},e.mapAsArray=function(e,r){return n(a(e=e||m),"expected prop schema as first argument"),n(!!r,"expected key property name as second argument"),{serializer:function(r){var n=[];return r.forEach(function(r,t){n.push(e.serializer(r))}),n},deserializer:function(n,t,i,o){k(e).deserializer(n,function(e,i){if(e)t(e);else{var a,s=l(o);s?(o.clear(),a=o):a={};for(var u=0,c=n.length;u<c;u++)s?a.set(i[u][r],i[u]):a[i[u][r].toString()]=i[u];t(null,a)}},i)}}},e.raw=function(){return{serializer:function(e){return e},deserializer:function(e,r){r(null,e)}}},e.SKIP=h,e.child=S,e.ref=w,Object.defineProperty(e,"__esModule",{value:!0})});
!function(e,r){"object"==typeof exports&&"undefined"!=typeof module?r(exports):"function"==typeof define&&define.amd?define("serializr",["exports"],r):r(e.serializr={})}(this,function(e){"use strict";function r(e){if(e)throw new Error(e)}function n(e,r){if(!e)throw new Error("[serializr] "+(r||"Illegal State"))}function t(e,r,n){if(0!==e.length){var t=e.length,i=[],o=!1;e.forEach(function(e,a){r(e,function(e,r,a){r?o||(o=!0,n(r)):(i[e]=a,0==--t&&n(null,i))}.bind(null,a))})}else n(null,[])}function i(e){return null===e||"object"!=typeof e&&"function"!=typeof e}function o(e){return e&&e.factory&&e.props}function a(e){return e&&e.serializer&&e.deserializer}function s(e){return"object"==typeof e&&!!e.jsonname}function u(e,r){for(;e;){if(e===r)return!0;e=e["extends"]}return!1}function l(e){return e&&"function"==typeof e.keys&&"function"==typeof e.clear}function c(e){return e?o(e)?e:o(e.serializeInfo)?e.serializeInfo:e.constructor&&e.constructor.serializeInfo?e.constructor.serializeInfo:void 0:null}function f(e,r){return n(o(r)),e.serializeInfo=r}function p(e,r,t){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:t||function(){return new e},props:r};if(e.prototype.constructor!==Object){var o=c(e.prototype.constructor);o&&o.targetClass!==e&&(i["extends"]=o)}return f(e,i),i}function d(){return{serializer:function(e){return n(i(e),"this value is not primitive: "+e),e},deserializer:function(e,r){i(e)?r(null,e):r("[serializr] this value is not primitive: "+e)}}}var h="undefined"!=typeof Symbol?Symbol("SKIP"):{SKIP:!0},m=d(),y=/((\/\/.*$)|(\/\*[\s\S]*?\*\/))/gm,g=/([^\s,]+)/g;function v(e,r,t,i){var o,s,u;if(n(arguments.length>=2,"too few arguments. Please use @serializable as property decorator"),t===undefined&&"function"==typeof r&&r.prototype&&i!==undefined&&"number"==typeof i){n(a(e),"Constructor params must use alias(name)"),n(e.jsonname,"Constructor params must use alias(name)");var l=(s=r.toString().replace(y,""),null===(u=s.slice(s.indexOf("(")+1,s.indexOf(")")).match(g))&&(u=[]),u);l.length>=i&&(t=l[i],e.paramNumber=i,i=undefined,r=r.prototype,o=function(e){for(var n=[],t=0;t<r.constructor.length;t++)Object.keys(e.modelSchema.props).forEach(function(r){var i=e.modelSchema.props[r];i.paramNumber===t&&(n[t]=e.json[i.jsonname])});return new(Function.prototype.bind.apply(r.constructor,[null].concat(n)))})}n("string"==typeof t,"incorrect usage of @serializable decorator");var f=c(r);return f&&r.constructor.hasOwnProperty("serializeInfo")||(f=p(r.constructor,{},o)),f&&f.targetClass!==r.constructor&&(f=p(r.constructor,{},o)),f.props[t]=e,!i||i.get||i.set||(i.writable=!0),i}function b(e,r){n(1===arguments.length||2===arguments.length,"serialize expects one or 2 arguments");var t=1===arguments.length?e:r,i=1===arguments.length?null:e;if(Array.isArray(t)){if(0===t.length)return[];i||(i=c(t[0]))}else i||(i=c(t));return n(!!i,"Failed to find default schema for "+e),Array.isArray(t)?t.map(function(e){return z(i,e)}):z(i,t)}function z(e,r){var t;return n(e&&"object"==typeof e,"Expected schema"),n(r&&"object"==typeof r,"Expected object"),t=e["extends"]?z(e["extends"],r):{},Object.keys(e.props).forEach(function(o){var a=e.props[o];if("*"===o)return n(!0===a,"prop schema '*' can onle be used with 'true'"),void function(e,r,n){for(var t in r)if(r.hasOwnProperty(t)&&!(t in e.props)){var o=r[t];i(o)&&(n[t]=o)}}(e,r,t);if(!0===a&&(a=m),!1!==a){var s=a.serializer(r[o],o,r);s!==h&&(t[a.jsonname||o]=s)}}),t}function j(e,n,t,i,o){this.parentContext=e,this.isRoot=!e,this.pendingCallbacks=0,this.pendingRefsCount=0,this.onReadyCb=i||r,this.json=t,this.target=null,this.hasError=!1,this.modelSchema=n,this.isRoot?(this.rootContext=this,this.args=o,this.pendingRefs={},this.resolvedRefs={}):(this.rootContext=e.rootContext,this.args=e.args)}function x(e,r){for(var n in e.props)if("object"==typeof e.props[n]&&e.props[n].jsonname===r)return!0;return!1}function R(e,t,i,o,a){if(null!==i&&i!==undefined){var s=new j(e,t,i,o,a),u=t.factory(s);n(!!u,"No object returned from factory"),s.target=u;var l=s.createCallback(r);return C(s,t,i,u),l(),u}o(null,null)}function C(e,r,t,o){r["extends"]&&C(e,r["extends"],t,o),Object.keys(r.props).forEach(function(a){var s=r.props[a];if("*"===a)return n(!0===s,"prop schema '*' can onle be used with 'true'"),void function(e,r,t){for(var o in t)if(!(o in e.props||x(e,o))){var a=t[o];n(i(a),"encountered non primitive value while deserializing '*' properties in property '"+o+"': "+a),r[o]=a}}(r,o,t);if(!0===s&&(s=m),!1!==s){var u=s.jsonname||a;u in t&&s.deserializer(t[u],e.rootContext.createCallback(function(e){e!==h&&(o[a]=e)}),e,o[a])}})}function S(e){return n("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 n(o(e=c(e)),"expected modelSchema, got "+e),null===r||r===undefined?r:b(e,r)},deserializer:function(r,t,i){n(o(e=c(e)),"expected modelSchema, got "+e),null!==r&&r!==undefined?R(i,e,r,t):t(null,r)}}}function w(e,r){n(!!e,"No modelschema provided. If you are importing it from another file be aware of circular dependencies.");var t,i=!1;function a(){if(i=!0,n("string"!=typeof e||r,"if the reference target is specified by attribute name, a lookup function is required"),n(!r||"function"==typeof r,"second argument should be a lookup function"),"string"==typeof e)t=e;else{var a=c(e);n(o(a),"expected model schema or string as first argument for 'ref', got "+a),r=r||(s=a,function(e,r,n){n.rootContext.await(s,e,r)}),n(!!(t=function(e){for(n(o(e));e;){for(var r in e.props)if("object"==typeof e.props[r]&&!0===e.props[r].identifier)return r;e=e["extends"]}return null}(a)),"provided model schema doesn't define an identifier() property and cannot be used by 'ref'.")}var s}return{serializer:function(e){return i||a(),e?e[t]:null},deserializer:function(e,n,t){i||a(),null===e||e===undefined?n(null,e):r(e,n,t)}}}function k(e){return n(a(e=e||m),"expected prop schema as first argument"),n(!s(e),"provided prop is aliased, please put aliases first"),{serializer:function(r){return n(r&&"length"in r&&"map"in r,"expected array (like) object"),r.map(e.serializer)},deserializer:function(r,n,i){Array.isArray(r)?t(r,function(r,n){return e.deserializer(r,n,i)},n):n("[serializr] expected JSON array")}}}j.prototype.createCallback=function(e){return this.pendingCallbacks++,r=function(r,n){r?this.hasError||(this.hasError=!0,this.onReadyCb(r)):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('", "')+'"')):this.onReadyCb(null,this.target)))}.bind(this),t=!1,function(){if(!t)return t=!0,r.apply(null,arguments);n(!1,"callback was invoked twice")};var r,t},j.prototype.await=function(e,r,t){if(n(this.isRoot),r in this.resolvedRefs){var i=this.resolvedRefs[r].filter(function(r){return u(r.modelSchema,e)})[0];if(i)return void t(null,i.value)}this.pendingRefsCount++,this.pendingRefs[r]||(this.pendingRefs[r]=[]),this.pendingRefs[r].push({modelSchema:e,uuid:r,callback:t})},j.prototype.resolve=function(e,r,t){if(n(this.isRoot),this.resolvedRefs[r]||(this.resolvedRefs[r]=[]),this.resolvedRefs[r].push({modelSchema:e,value:t}),r in this.pendingRefs)for(var i=this.pendingRefs[r].length-1;i>=0;i--){var o=this.pendingRefs[r][i];u(e,o.modelSchema)&&(this.pendingRefs[r].splice(i,1),this.pendingRefsCount--,o.callback(null,t))}},e.createSimpleSchema=function(e){return{factory:function(){return{}},props:e}},e.createModelSchema=p,e.getDefaultModelSchema=c,e.setDefaultModelSchema=f,e.serializable=function(e,r,t){if(1===arguments.length){var i=!0===e?m:e;return n(a(i),"@serializable expects prop schema"),v.bind(null,i)}return v(d(),e,r,t)},e.serialize=b,e.serializeAll=function(e){n(1===arguments.length&&"function"==typeof e,"@serializeAll can only be used as class decorator");var r=c(e);return r&&e.hasOwnProperty("serializeInfo")||f(e,r=p(e,{})),c(e).props["*"]=!0,e},e.deserialize=function(e,i,a,s){if(n(arguments.length>=2,"deserialize expects at least 2 arguments"),n(o(e=c(e)),"first argument should be model schema"),Array.isArray(i)){var u=[];return t(i,function(r,n){var t=R(null,e,r,n,s);u.push(t)},a||r),u}return R(null,e,i,a,s)},e.update=function(e,t,i,a,s){2===arguments.length||"function"==typeof arguments[2]?(e=c(t=arguments[0]),i=arguments[1],a=arguments[2],s=arguments[3]):e=c(e),n(o(e),"update failed to determine schema"),n("object"==typeof t&&t&&!Array.isArray(t),"update needs an object");var u=new j(null,e,i,a,s);u.target=t;var l=u.createCallback(r);C(u,e,i,t),l()},e.primitive=d,e.identifier=function(e){return n(!e||"function"==typeof e,"First argument should be omitted or function"),{identifier:!0,serializer:m.serializer,deserializer:function(r,n,t){m.deserializer(r,function(r,i){var o,a;o=i,t.target,(a=t).rootContext.resolve(a.modelSchema,o,a.target),e&&e(i,t.target,t),n(r,i)})}}},e.date=function(){return{serializer:function(e){return null===e||e===undefined?e:(n(e instanceof Date,"Expected Date object"),e.getTime())},deserializer:function(e,r){null!==e&&e!==undefined?r(null,new Date(e)):r(null,e)}}},e.alias=function(e,r){return n(e&&"string"==typeof e,"expected prop name as first argument"),n(a(r=r&&!0!==r?r:m),"expected prop schema as second argument"),n(!s(r),"provided prop is already aliased"),{jsonname:e,serializer:r.serializer,deserializer:r.deserializer,identifier:(t=r,"object"==typeof t&&!0===t.identifier)};var t},e.custom=function(e,r){return n("function"==typeof e,"first argument should be function"),n("function"==typeof r,"second argument should be a function or promise"),{serializer:e,deserializer:function(e,n,t,i){4===r.length?r(e,t,i,n):n(null,r(e,t,i))}}},e.object=S,e.reference=w,e.list=k,e.map=function(e){return n(a(e=e||m),"expected prop schema as first argument"),n(!s(e),"provided prop is aliased, please put aliases first"),{serializer:function(r){n(r&&"object"==typeof r,"expected object or Map");var t=l(r),i={};if(t)r.forEach(function(r,n){i[n]=e.serializer(r)});else for(var o in r)i[o]=e.serializer(r[o]);return i},deserializer:function(r,n,t,i){if(r&&"object"==typeof r){var o=Object.keys(r);k(e).deserializer(o.map(function(e){return r[e]}),function(e,r){if(e)n(e);else{var t,a=l(i);a?(i.clear(),t=i):t={};for(var s=0,u=o.length;s<u;s++)a?t.set(o[s],r[s]):t[o[s]]=r[s];n(null,t)}},t)}else n("[serializr] expected JSON object")}}},e.mapAsArray=function(e,r){return n(a(e=e||m),"expected prop schema as first argument"),n(!!r,"expected key property name as second argument"),{serializer:function(r){var n=[];return r.forEach(function(r,t){n.push(e.serializer(r))}),n},deserializer:function(n,t,i,o){k(e).deserializer(n,function(e,i){if(e)t(e);else{var a,s=l(o);s?(o.clear(),a=o):a={};for(var u=0,c=n.length;u<c;u++)s?a.set(i[u][r],i[u]):a[i[u][r].toString()]=i[u];t(null,a)}},i)}}},e.raw=function(){return{serializer:function(e){return e},deserializer:function(e,r){r(null,e)}}},e.SKIP=h,e.child=S,e.ref=w,Object.defineProperty(e,"__esModule",{value:!0})});
//# sourceMappingURL=serializr.min.js.map
{
"name": "serializr",
"version": "1.2.0",
"version": "1.3.0",
"description": "Serialize and deserialize complex object graphs to JSON",

@@ -5,0 +5,0 @@ "main": "lib/serializr.js",

@@ -78,3 +78,3 @@ # Serializr

// This is a lookup function
// identifier is the identifier being resolved
// uuid is the identifier being resolved
// callback is a node style callback function to be invoked with the found object (as second arg) or an error (first arg)

@@ -299,3 +299,3 @@ // context is an object detailing the execution context of the serializer now

[src/serializr.js:52-52](https://github.com/mobxjs/serializr/blob/8f0e459a1354a7f253a5c1c746269bfa71044093/src/serializr.js#L52-L52 "Source code on GitHub")
[src/serializr.js:52-52](https://github.com/mobxjs/serializr/blob/b34674d96cb101c30739a497287d3c24ede31010/src/serializr.js#L52-L52 "Source code on GitHub")

@@ -346,3 +346,3 @@ JSDOC type defintions for usage w/o typescript.

[src/api/createSimpleSchema.js:17-24](https://github.com/mobxjs/serializr/blob/8f0e459a1354a7f253a5c1c746269bfa71044093/src/api/createSimpleSchema.js#L17-L24 "Source code on GitHub")
[src/api/createSimpleSchema.js:17-24](https://github.com/mobxjs/serializr/blob/b34674d96cb101c30739a497287d3c24ede31010/src/api/createSimpleSchema.js#L17-L24 "Source code on GitHub")

@@ -372,3 +372,3 @@ Creates a model schema that (de)serializes from / to plain javascript objects.

[src/api/createModelSchema.js:29-47](https://github.com/mobxjs/serializr/blob/8f0e459a1354a7f253a5c1c746269bfa71044093/src/api/createModelSchema.js#L29-L47 "Source code on GitHub")
[src/api/createModelSchema.js:29-47](https://github.com/mobxjs/serializr/blob/b34674d96cb101c30739a497287d3c24ede31010/src/api/createModelSchema.js#L29-L47 "Source code on GitHub")

@@ -406,3 +406,3 @@ Creates a model schema that (de)serializes an object created by a constructor function (class).

[src/api/getDefaultModelSchema.js:9-18](https://github.com/mobxjs/serializr/blob/8f0e459a1354a7f253a5c1c746269bfa71044093/src/api/getDefaultModelSchema.js#L9-L18 "Source code on GitHub")
[src/api/getDefaultModelSchema.js:9-18](https://github.com/mobxjs/serializr/blob/b34674d96cb101c30739a497287d3c24ede31010/src/api/getDefaultModelSchema.js#L9-L18 "Source code on GitHub")

@@ -419,3 +419,3 @@ Returns the standard model schema associated with a class / constructor function

[src/api/setDefaultModelSchema.js:15-18](https://github.com/mobxjs/serializr/blob/8f0e459a1354a7f253a5c1c746269bfa71044093/src/api/setDefaultModelSchema.js#L15-L18 "Source code on GitHub")
[src/api/setDefaultModelSchema.js:15-18](https://github.com/mobxjs/serializr/blob/b34674d96cb101c30739a497287d3c24ede31010/src/api/setDefaultModelSchema.js#L15-L18 "Source code on GitHub")

@@ -438,3 +438,3 @@ Sets the default model schema for class / constructor function.

[src/api/serializable.js:93-103](https://github.com/mobxjs/serializr/blob/8f0e459a1354a7f253a5c1c746269bfa71044093/src/api/serializable.js#L93-L103 "Source code on GitHub")
[src/api/serializable.js:93-103](https://github.com/mobxjs/serializr/blob/b34674d96cb101c30739a497287d3c24ede31010/src/api/serializable.js#L93-L103 "Source code on GitHub")

@@ -463,3 +463,3 @@ Decorator that defines a new property mapping on the default model schema for the class

[src/core/serialize.js:16-34](https://github.com/mobxjs/serializr/blob/8f0e459a1354a7f253a5c1c746269bfa71044093/src/core/serialize.js#L16-L34 "Source code on GitHub")
[src/core/serialize.js:16-34](https://github.com/mobxjs/serializr/blob/b34674d96cb101c30739a497287d3c24ede31010/src/core/serialize.js#L16-L34 "Source code on GitHub")

@@ -479,3 +479,3 @@ Serializes an object (graph) into json using the provided model schema.

[src/core/serialize.js:89-100](https://github.com/mobxjs/serializr/blob/8f0e459a1354a7f253a5c1c746269bfa71044093/src/core/serialize.js#L89-L100 "Source code on GitHub")
[src/core/serialize.js:89-100](https://github.com/mobxjs/serializr/blob/b34674d96cb101c30739a497287d3c24ede31010/src/core/serialize.js#L89-L100 "Source code on GitHub")

@@ -490,3 +490,3 @@ The `serializeAll` decorator can be used on a class to signal that all primitive properties should be serialized automatically.

[src/core/deserialize.js:38-56](https://github.com/mobxjs/serializr/blob/8f0e459a1354a7f253a5c1c746269bfa71044093/src/core/deserialize.js#L38-L56 "Source code on GitHub")
[src/core/deserialize.js:38-56](https://github.com/mobxjs/serializr/blob/b34674d96cb101c30739a497287d3c24ede31010/src/core/deserialize.js#L38-L56 "Source code on GitHub")

@@ -510,3 +510,3 @@ Deserializes a json structor into an object graph.

[src/core/update.js:21-42](https://github.com/mobxjs/serializr/blob/8f0e459a1354a7f253a5c1c746269bfa71044093/src/core/update.js#L21-L42 "Source code on GitHub")
[src/core/update.js:21-42](https://github.com/mobxjs/serializr/blob/b34674d96cb101c30739a497287d3c24ede31010/src/core/update.js#L21-L42 "Source code on GitHub")

@@ -527,3 +527,3 @@ Similar to deserialize, but updates an existing object instance.

[src/types/primitive.js:17-29](https://github.com/mobxjs/serializr/blob/8f0e459a1354a7f253a5c1c746269bfa71044093/src/types/primitive.js#L17-L29 "Source code on GitHub")
[src/types/primitive.js:17-29](https://github.com/mobxjs/serializr/blob/b34674d96cb101c30739a497287d3c24ede31010/src/types/primitive.js#L17-L29 "Source code on GitHub")

@@ -547,3 +547,3 @@ Indicates that this field contains a primitive value (or Date) which should be serialized literally to json.

[src/types/identifier.js:42-56](https://github.com/mobxjs/serializr/blob/8f0e459a1354a7f253a5c1c746269bfa71044093/src/types/identifier.js#L42-L56 "Source code on GitHub")
[src/types/identifier.js:42-56](https://github.com/mobxjs/serializr/blob/b34674d96cb101c30739a497287d3c24ede31010/src/types/identifier.js#L42-L56 "Source code on GitHub")

@@ -588,3 +588,3 @@ Similar to primitive, but this field will be marked as the identifier for the given Model type.

[src/types/date.js:8-23](https://github.com/mobxjs/serializr/blob/8f0e459a1354a7f253a5c1c746269bfa71044093/src/types/date.js#L8-L23 "Source code on GitHub")
[src/types/date.js:8-23](https://github.com/mobxjs/serializr/blob/b34674d96cb101c30739a497287d3c24ede31010/src/types/date.js#L8-L23 "Source code on GitHub")

@@ -595,3 +595,3 @@ Similar to primitive, serializes instances of Date objects

[src/types/alias.js:20-31](https://github.com/mobxjs/serializr/blob/8f0e459a1354a7f253a5c1c746269bfa71044093/src/types/alias.js#L20-L31 "Source code on GitHub")
[src/types/alias.js:20-31](https://github.com/mobxjs/serializr/blob/b34674d96cb101c30739a497287d3c24ede31010/src/types/alias.js#L20-L31 "Source code on GitHub")

@@ -621,3 +621,3 @@ Alias indicates that this model property should be named differently in the generated json.

[src/types/custom.js:36-45](https://github.com/mobxjs/serializr/blob/8f0e459a1354a7f253a5c1c746269bfa71044093/src/types/custom.js#L36-L45 "Source code on GitHub")
[src/types/custom.js:59-72](https://github.com/mobxjs/serializr/blob/b34674d96cb101c30739a497287d3c24ede31010/src/types/custom.js#L59-L72 "Source code on GitHub")

@@ -633,5 +633,8 @@ 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.

The `deserializer` function has the signature:
`(value, context) => void`
The `deserializer` function has the following signature for synchronous processing
`(value, context, oldValue) => void`
For asynchronous processing the function expects the following signature
`(value, context, oldValue, callback) => void`
When deserializing the object `{b: 2}` the `deserializer` function will be called with `deserializer(2, contextObj)` ([contextObj reference](https://github.com/mobxjs/serializr#deserialization-context)).

@@ -647,3 +650,3 @@

```javascript
var schema = _.createSimpleSchema({
var schemaDefault = _.createSimpleSchema({
a: _.custom(

@@ -658,4 +661,23 @@ function(v) {

});
t.deepEqual(_.serialize(s, { a: 4 }), { a: 6 });
t.deepEqual(_.deserialize(s, { a: 6 }), { a: 4 });
t.deepEqual(_.serialize(schemaDefault, { a: 4 }), { a: 6 });
t.deepEqual(_.deserialize(schemaDefault, { a: 6 }), { a: 4 });
var schemaWithAsyncProps = _.createSimpleSchema({
a: _.customAsync(
function(v) {
return v + 2;
},
function(v, context, oldValue, callback) {
somePromise(v, context, oldValue).then((result) => {
callback(null, result - 2)
}.catch((err) => {
callback(err)
}
}
),
});
t.deepEqual(_.serialize(schemaWithAsyncProps, { a: 4 }), { a: 6 });
_.deserialize(schemaWithAsyncProps, { a: 6 }, (err, res) => {
t.deepEqual(res.a, 4)
};
```

@@ -667,3 +689,3 @@

[src/types/object.js:34-52](https://github.com/mobxjs/serializr/blob/8f0e459a1354a7f253a5c1c746269bfa71044093/src/types/object.js#L34-L52 "Source code on GitHub")
[src/types/object.js:34-52](https://github.com/mobxjs/serializr/blob/b34674d96cb101c30739a497287d3c24ede31010/src/types/object.js#L34-L52 "Source code on GitHub")

@@ -705,3 +727,3 @@ `object` indicates that this property contains an object that needs to be (de)serialized

[src/types/reference.js:65-98](https://github.com/mobxjs/serializr/blob/8f0e459a1354a7f253a5c1c746269bfa71044093/src/types/reference.js#L65-L98 "Source code on GitHub")
[src/types/reference.js:65-98](https://github.com/mobxjs/serializr/blob/b34674d96cb101c30739a497287d3c24ede31010/src/types/reference.js#L65-L98 "Source code on GitHub")

@@ -770,3 +792,3 @@ `reference` can be used to (de)serialize references that point to other models.

[src/types/list.js:33-54](https://github.com/mobxjs/serializr/blob/8f0e459a1354a7f253a5c1c746269bfa71044093/src/types/list.js#L33-L54 "Source code on GitHub")
[src/types/list.js:33-54](https://github.com/mobxjs/serializr/blob/b34674d96cb101c30739a497287d3c24ede31010/src/types/list.js#L33-L54 "Source code on GitHub")

@@ -809,3 +831,3 @@ List indicates that this property contains a list of things.

[src/types/map.js:13-62](https://github.com/mobxjs/serializr/blob/8f0e459a1354a7f253a5c1c746269bfa71044093/src/types/map.js#L13-L62 "Source code on GitHub")
[src/types/map.js:13-62](https://github.com/mobxjs/serializr/blob/b34674d96cb101c30739a497287d3c24ede31010/src/types/map.js#L13-L62 "Source code on GitHub")

@@ -822,3 +844,3 @@ Similar to list, but map represents a string keyed dynamic collection.

[src/types/mapAsArray.js:15-52](https://github.com/mobxjs/serializr/blob/8f0e459a1354a7f253a5c1c746269bfa71044093/src/types/mapAsArray.js#L15-L52 "Source code on GitHub")
[src/types/mapAsArray.js:15-52](https://github.com/mobxjs/serializr/blob/b34674d96cb101c30739a497287d3c24ede31010/src/types/mapAsArray.js#L15-L52 "Source code on GitHub")

@@ -838,3 +860,3 @@ Similar to map, mapAsArray can be used to serialize a map-like collection where the key is contained in the 'value object'.

[src/types/raw.js:15-24](https://github.com/mobxjs/serializr/blob/8f0e459a1354a7f253a5c1c746269bfa71044093/src/types/raw.js#L15-L24 "Source code on GitHub")
[src/types/raw.js:15-24](https://github.com/mobxjs/serializr/blob/b34674d96cb101c30739a497287d3c24ede31010/src/types/raw.js#L15-L24 "Source code on GitHub")

@@ -859,3 +881,3 @@ Indicates that this field is only need to putted in the serialized json or

[src/constants.js:20-20](https://github.com/mobxjs/serializr/blob/8f0e459a1354a7f253a5c1c746269bfa71044093/src/constants.js#L20-L20 "Source code on GitHub")
[src/constants.js:20-20](https://github.com/mobxjs/serializr/blob/b34674d96cb101c30739a497287d3c24ede31010/src/constants.js#L20-L20 "Source code on GitHub")

@@ -862,0 +884,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.

@@ -74,6 +74,9 @@ // 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 custom(serializer: (value: any) => any, deserializer: (jsonValue: any, context?: any, oldValue?: any) => any): PropSchema;
export function custom(serializer: (value: any) => any, deserializer: (jsonValue: any, context: any, oldValue: any, callback: (err: any, result: any) => void) => any): PropSchema;
export function serializeAll<T extends Function>(clazz: T): T
export const SKIP: {}
export function raw(): any;
export const SKIP: {}

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc