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

serializr

Package Overview
Dependencies
Maintainers
1
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 0.0.3 to 1.0.0

11

package.json
{
"name": "serializr",
"version": "0.0.3",
"version": "1.0.0",
"description": "Serialize and deserialize complex object graphs to JSON",
"main": "serializr.js",
"typings": "serializr.d.ts",
"scripts": {
"test": "node test/index",
"test": "tsc -p test/typescript && babel test/babel/babel.js -o test/babel/babel-compiled.js && node test/index",
"lint": "eslint serializr.js",

@@ -36,2 +37,4 @@ "prepublish": "npm run small-build && npm run build-docs",

"devDependencies": {
"babel-cli": "^6.11.4",
"babel-plugin-transform-decorators-legacy": "^1.3.4",
"coveralls": "^2.11.9",

@@ -41,5 +44,7 @@ "documentation": "^4.0.0-beta9",

"istanbul": "^0.4.4",
"mobx": "^2.4.1",
"tape": "^4.5.1",
"typescript": "^1.8.10",
"uglify-js": "^2.6.4"
}
}
}

@@ -28,2 +28,3 @@ # Serializr

- Serializr is not an ORM or data management library. It doesn't manage object instances, provided api's like fetch, search etc. If you are building such a thing though, serializr might definitely take care of the serialization part for you :-).
- Serializr is not a MobX specific (de)serialization mechanism, it is generic and should fit work with any type of model objects

@@ -96,3 +97,3 @@ # Installation

@serializable(primitive())
@serializable
displayName = "John Doe";

@@ -102,3 +103,3 @@ }

class Message {
@serializable(primitive())
@serializable
message = "Test";

@@ -172,2 +173,3 @@

- `identifier()`: Serialize a field as primitive value, use it as identifier when serializing references (see `ref`)
- `date()`: Serializes dates (as epoch number)
- `alias(name, propSchema)`: Serializes a field under a different name

@@ -210,3 +212,3 @@ - `list(propSchema)`: Serializes an array based collection

[serializr.js:80-87](https://github.com/mobxjs/serializr/blob/38537734cc2efe159b95cba91b090fbfa97679e2/serializr.js#L80-L87 "Source code on GitHub")
[serializr.js:83-90](https://github.com/mobxjs/serializr/blob/43860a7738285370caf8f7a99b9ffe32afe538a0/serializr.js#L83-L90 "Source code on GitHub")

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

[serializr.js:113-122](https://github.com/mobxjs/serializr/blob/38537734cc2efe159b95cba91b090fbfa97679e2/serializr.js#L113-L122 "Source code on GitHub")
[serializr.js:116-127](https://github.com/mobxjs/serializr/blob/43860a7738285370caf8f7a99b9ffe32afe538a0/serializr.js#L116-L127 "Source code on GitHub")

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

[serializr.js:148-158](https://github.com/mobxjs/serializr/blob/38537734cc2efe159b95cba91b090fbfa97679e2/serializr.js#L148-L158 "Source code on GitHub")
[serializr.js:155-165](https://github.com/mobxjs/serializr/blob/43860a7738285370caf8f7a99b9ffe32afe538a0/serializr.js#L155-L165 "Source code on GitHub")

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

[serializr.js:186-195](https://github.com/mobxjs/serializr/blob/38537734cc2efe159b95cba91b090fbfa97679e2/serializr.js#L186-L195 "Source code on GitHub")
[serializr.js:186-195](https://github.com/mobxjs/serializr/blob/43860a7738285370caf8f7a99b9ffe32afe538a0/serializr.js#L186-L195 "Source code on GitHub")

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

[serializr.js:208-211](https://github.com/mobxjs/serializr/blob/38537734cc2efe159b95cba91b090fbfa97679e2/serializr.js#L208-L211 "Source code on GitHub")
[serializr.js:208-211](https://github.com/mobxjs/serializr/blob/43860a7738285370caf8f7a99b9ffe32afe538a0/serializr.js#L208-L211 "Source code on GitHub")

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

[serializr.js:251-269](https://github.com/mobxjs/serializr/blob/38537734cc2efe159b95cba91b090fbfa97679e2/serializr.js#L251-L269 "Source code on GitHub")
[serializr.js:251-269](https://github.com/mobxjs/serializr/blob/43860a7738285370caf8f7a99b9ffe32afe538a0/serializr.js#L251-L269 "Source code on GitHub")

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

[serializr.js:306-325](https://github.com/mobxjs/serializr/blob/38537734cc2efe159b95cba91b090fbfa97679e2/serializr.js#L306-L325 "Source code on GitHub")
[serializr.js:306-324](https://github.com/mobxjs/serializr/blob/43860a7738285370caf8f7a99b9ffe32afe538a0/serializr.js#L306-L324 "Source code on GitHub")

@@ -359,3 +361,3 @@ Deserializes an json structor into an object graph.

[serializr.js:402-421](https://github.com/mobxjs/serializr/blob/38537734cc2efe159b95cba91b090fbfa97679e2/serializr.js#L402-L421 "Source code on GitHub")
[serializr.js:401-420](https://github.com/mobxjs/serializr/blob/43860a7738285370caf8f7a99b9ffe32afe538a0/serializr.js#L401-L420 "Source code on GitHub")

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

[serializr.js:443-457](https://github.com/mobxjs/serializr/blob/38537734cc2efe159b95cba91b090fbfa97679e2/serializr.js#L443-L457 "Source code on GitHub")
[serializr.js:442-454](https://github.com/mobxjs/serializr/blob/43860a7738285370caf8f7a99b9ffe32afe538a0/serializr.js#L442-L454 "Source code on GitHub")

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

[serializr.js:465-469](https://github.com/mobxjs/serializr/blob/38537734cc2efe159b95cba91b090fbfa97679e2/serializr.js#L465-L469 "Source code on GitHub")
[serializr.js:462-466](https://github.com/mobxjs/serializr/blob/43860a7738285370caf8f7a99b9ffe32afe538a0/serializr.js#L462-L466 "Source code on GitHub")

@@ -402,5 +404,11 @@ Similar to primitive, but this field will be marked as the identifier for the given Model type.

## date
[serializr.js:473-488](https://github.com/mobxjs/serializr/blob/43860a7738285370caf8f7a99b9ffe32afe538a0/serializr.js#L473-L488 "Source code on GitHub")
Similar to primitive, serializes instances of Date objects
## alias
[serializr.js:487-498](https://github.com/mobxjs/serializr/blob/38537734cc2efe159b95cba91b090fbfa97679e2/serializr.js#L487-L498 "Source code on GitHub")
[serializr.js:507-518](https://github.com/mobxjs/serializr/blob/43860a7738285370caf8f7a99b9ffe32afe538a0/serializr.js#L507-L518 "Source code on GitHub")

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

[serializr.js:526-541](https://github.com/mobxjs/serializr/blob/38537734cc2efe159b95cba91b090fbfa97679e2/serializr.js#L526-L541 "Source code on GitHub")
[serializr.js:546-561](https://github.com/mobxjs/serializr/blob/43860a7738285370caf8f7a99b9ffe32afe538a0/serializr.js#L546-L561 "Source code on GitHub")

@@ -466,3 +474,3 @@ Child indicates that this property contains an object that needs to be (de)serialized

[serializr.js:593-612](https://github.com/mobxjs/serializr/blob/38537734cc2efe159b95cba91b090fbfa97679e2/serializr.js#L593-L612 "Source code on GitHub")
[serializr.js:613-636](https://github.com/mobxjs/serializr/blob/43860a7738285370caf8f7a99b9ffe32afe538a0/serializr.js#L613-L636 "Source code on GitHub")

@@ -528,3 +536,3 @@ Ref can be used to (de)serialize references that points to other models.

[serializr.js:638-659](https://github.com/mobxjs/serializr/blob/38537734cc2efe159b95cba91b090fbfa97679e2/serializr.js#L638-L659 "Source code on GitHub")
[serializr.js:662-683](https://github.com/mobxjs/serializr/blob/43860a7738285370caf8f7a99b9ffe32afe538a0/serializr.js#L662-L683 "Source code on GitHub")

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

[serializr.js:673-722](https://github.com/mobxjs/serializr/blob/38537734cc2efe159b95cba91b090fbfa97679e2/serializr.js#L673-L722 "Source code on GitHub")
[serializr.js:697-746](https://github.com/mobxjs/serializr/blob/43860a7738285370caf8f7a99b9ffe32afe538a0/serializr.js#L697-L746 "Source code on GitHub")

@@ -706,10 +714,93 @@ Similar to list, but map represents a string keyed dynamic collection.

## 7. Putting it together: MobX store with plain objects, classes and internal references
```javascript
// Box.js:
import {observable, computed} from 'mobx';
import {randomUuid} from '../utils';
import {serializable, identifier} from 'serializr';
export default class Box {
@serializable(identifier()) id;
@observable @serializable name = 'Box' + this.id;
@serializable @observable x = 0;
@serializable @observable y = 0;
@computed get width() {
return this.name.length * 15;
}
}
// Store.js:
import {observable, transaction} from 'mobx';
import {createSimpleSchema, ref, identifier, child, list, serialize, deserialize, update} from 'serializr';
import Box from './box';
// The store that holds our domain: boxes and arrows
const store = observable({
boxes: [],
arrows: [],
selection: null
});
// Model of an arrow
const arrowModel = createSimpleSchema({
id: identifier(),
// context.target is the current arrow being deserialized
// context.parentContext.target is the owner of the arrow; the store
from: ref(
Box,
(id, callback, context) => findBox(context.parentContext.target, id, callback)
),
to: ref(
Box,
(id, callback, context) => findBox(context.parentContext.target, id, callback)
)
})
// Model of the store itself
const storeModel = createSimpleSchema({
boxes: list(child(Box)),
arrows: list(child(arrowModel)),
// context.target is the current store being deserialized
selection: ref(Box, (id, callback, context) => findBox(context.target, id, callback))
})
function findBox(store, id, callback) {
const boxes = store.boxes.filter(box => box.id === id);
if (boxes.length !== 1)
callback("Invalid box ref: " + id);
else
callback(null, boxes[0]);
}
// example data
store.boxes.push(
new Box('Rotterdam', 100, 100),
new Box('Vienna', 650, 300)
);
store.arrows.push({
id: randomUuid(),
from: store.boxes[0],
to: store.boxes[1]
});
// (de) serialize functions
function serializeState(store) {
return serialize(storeModel, store);
}
function deserializeState = (store, json) {
transaction(() => {
update(storeModel, store, json);
})
}
```
* * *
# TODO
# Future ideas
- [ ] Typings
- [ ] Test with MobX / Typescript
- [ ] Blog
- [ ] If MobX, use createTransformer, transaction (future)
- [ ] If MobX, optimize by leveraging createTransformer and transactions
- [ ] Support async serialization (future)
- [ ] Support ImmutableJS out of the box

@@ -8,3 +8,6 @@ (function() {

*/
function NOOP() {}
function GUARDED_NOOP(err) {
if (err) // unguarded error...
throw new Error(err)
}

@@ -117,3 +120,3 @@ function once(fn) {

invariant(typeof clazz === "function", "expected constructor function")
setDefaultModelSchema(clazz, {
var model = {
factory: factory || function() {

@@ -123,3 +126,5 @@ return new clazz()

props: props
})
}
setDefaultModelSchema(clazz, model)
return model
}

@@ -148,3 +153,5 @@

*
* @param
* @param arg1
* @param arg2
* @param arg3
* @returns {PropertyDescriptor}

@@ -155,3 +162,3 @@ */

// decorated with propSchema
var propSchema = arg1
var propSchema = arg1 === true ? _defaultPrimitiveProp : arg1
invariant(isPropSchema(propSchema), "@serializable expects prop schema")

@@ -169,15 +176,8 @@ return serializableDecorator.bind(null, propSchema)

var info = getDefaultModelSchema(target)
if (!info) {
var constructor = target.constructor
info = setDefaultModelSchema(
constructor,
{
factory: function() {
return new constructor()
},
props: {}
}
)
}
if (!info)
info = createModelSchema(target.constructor, {})
info.props[propName] = propSchema
// MWE: why won't babel work without?
if (descriptor && !descriptor.get && !descriptor.set)
descriptor.writable = true
return descriptor

@@ -316,3 +316,2 @@ }

invariant(isModelSchema(schema), "first argument should be model schema")
callback = callback || NOOP
if (Array.isArray(json)) {

@@ -327,3 +326,3 @@ var items = []

},
callback
callback || GUARDED_NOOP
)

@@ -343,3 +342,3 @@ return items

context.target = target
var lock = context.createCallback(NOOP)
var lock = context.createCallback(GUARDED_NOOP)
deserializePropsWithSchema(context, schema, json, target)

@@ -373,3 +372,3 @@ lock()

this.parentContext = parentContext
this.onReadyCb = onReadyCb || NOOP
this.onReadyCb = onReadyCb || GUARDED_NOOP
this.json = json

@@ -428,3 +427,3 @@ this.target = null

context.target = target
var lock = context.createCallback(NOOP)
var lock = context.createCallback(GUARDED_NOOP)
deserializePropsWithSchema(context, modelSchema, json, target)

@@ -457,5 +456,3 @@ lock()

serializer: function (value) {
if (value instanceof Date)
return 0 + value
invariant((typeof value !== "object" || value === null) && typeof value !== "function", "this value is not primitive: " + value)
invariant(value === null || (typeof value !== "object" && typeof value !== "function"), "this value is not primitive: " + value)
return value

@@ -484,2 +481,25 @@ },

/**
* Similar to primitive, serializes instances of Date objects
*
* @returns
*/
function date() {
// TODO: add format option?
return {
serializer: function(value) {
if (value === null || value === undefined)
return value
invariant(value instanceof Date, "Expected Date object")
return value.getTime()
},
deserializer: function (jsonValue, done) {
if (jsonValue === null || jsonValue === undefined)
return void done(null, jsonValue)
return void done(null, new Date(jsonValue))
}
}
}
/**
* Alias indicates that this model property should be named differently in the generated json.

@@ -622,3 +642,7 @@ * Alias should be the outermost propschema.

deserializer: function(identifierValue, done, context) {
lookupFn(identifierValue, done, context)
// TODO: refs should always be deserialized at the end of the root context!
if (identifierValue === null || identifierValue === undefined)
done(null, identifierValue)
else
lookupFn(identifierValue, done, context)
}

@@ -752,2 +776,3 @@ }

identifier: identifier,
date: date,
alias: alias,

@@ -754,0 +779,0 @@ list: list,

/** serializr - (c) Michel Weststrate 2016 - MIT Licensed */
!function(){"use strict";function e(){function e(){}function r(e){var r=!1;return function(){return r?void n(!1,"callback was invoked twice"):(r=!0,e.apply(null,arguments))}}function n(e,r){if(!e)throw new Error("[serializr] "+(r||"Illegal State"))}function t(e,r,n){if(0===e.length)return void n(null,[]);var t=e.length,i=[],o=!1,a=function(e,r,a){r?o||(o=!0,n(r)):(i[e]=a,0===--t&&n(null,i))};e.forEach(function(e,n){r(e,a.bind(null,n))})}function i(e){for(var r=1;r<arguments.length;r++){var n=arguments[r];if(n&&"object"==typeof n)for(var t in n)n.hasOwnProperty(t)&&(e[t]=n[t])}return e}function o(e){return{factory:function(){return{}},props:e}}function a(e,r,t){n(e!==Object,"one cannot simply put define a model schema for Object"),n("function"==typeof e,"expected constructor function"),l(e,{factory:t||function(){return new e},props:r})}function s(e,r,t){if(1===arguments.length){var i=e;return n(p(i),"@serializable expects prop schema"),u.bind(null,i)}return u(k(),e,r,t)}function u(e,r,t,i){n(arguments.length>=2,"too few arguments. Please use @serializable as property decorator"),n("string"==typeof t,"incorrect usage of @serializable decorator");var o=c(r);if(!o){var a=r.constructor;o=l(a,{factory:function(){return new a},props:{}})}return o.props[t]=e,i}function c(e){return e?f(e)?e:f(e.serializeInfo)?e.serializeInfo:e.constructor&&e.constructor.serializeInfo?e.constructor.serializeInfo:void 0:null}function l(e,r){return n(f(r)),e.serializeInfo=r}function f(e){return e&&e.factory&&e.props}function p(e){return e&&e.serializer&&e.deserializer}function d(e){return!!e.jsonname}function h(e){return e.identifier===!0}function v(e){n(f(e));for(var r in e.props)if(e.props[r].identifier===!0)return r;return null}function y(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 m(i,e)}):m(i,t)}function m(e,r){n(e&&"object"==typeof e,"Expected schema"),n(r&&"object"==typeof r,"Expected object");var t;return t=e.extends?m(e.extends,r):{},Object.keys(e.props).forEach(function(n){var i=e.props[n];i===!0&&(i=M);var o=i.serializer(r[n]);t[i.jsonname||n]=o}),t}function b(r,i,o,a){if(n(arguments.length>=2,"deserialize expects at least 2 arguments"),r=c(r),n(f(r),"first argument should be model schema"),o=o||e,Array.isArray(i)){var s=[];return t(i,function(e,n){var t=g(null,r,e,n,a);s.push(t)},o),s}return g(null,r,i,o,a)}function g(r,t,i,o,a){if(null===i||void 0===i)return void o(null,null);var s=new j(r,i,o,a),u=t.factory(s);n(!!u,"No object returned from factory"),s.target=u;var c=s.createCallback(e);return z(s,t,i,u),c(),u}function z(e,r,n,t){r.extends&&z(e,r.extends,n,t),Object.keys(r.props).forEach(function(i){var o=r.props[i];o===!0&&(o=M);var a=o.jsonname||i;a in n&&o.deserializer(n[a],e.createCallback(function(e){t[i]=e}),e,t[i])})}function j(r,n,t,i){this.parentContext=r,this.onReadyCb=t||e,this.json=n,this.target=null,this.pendingCallbacks=0,this.hasError=!1,this.args=r?r.args:i}function x(r,t,i,o,a){var s=2===arguments.length||"function"==typeof arguments[2];s&&(t=arguments[0],r=c(t),i=arguments[1],o=arguments[2],a=arguments[2]),n(f(r),"update failed to determine schema"),n("object"==typeof t&&t&&!Array.isArray(t),"update needs an object");var u=new j(null,i,o,a);u.target=t;var l=u.createCallback(e);z(u,r,i,t),l()}function k(){return{serializer:function(e){return e instanceof Date?0+e:(n(("object"!=typeof e||null===e)&&"function"!=typeof e,"this value is not primitive: "+e),e)},deserializer:function(e,r){return"object"==typeof e&&null!==e||"function"==typeof e?void r("[serializr] this value is not primitive: "+e):void r(null,e)}}}function C(){return i({identifier:!0},M)}function E(e,r){return n(e&&"string"==typeof e,"expected prop name as first argument"),r=r||M,n(p(r),"expected prop schema as second argument"),n(!d(r),"provided prop is already aliased"),{jsonname:e,serializer:r.serializer,deserializer:r.deserializer,identifier:h(r)}}function w(e){return e=c(e),n(f(e),"expected modelSchema, got "+e),{serializer:function(r){return null===r||void 0===r?r:y(e,r)},deserializer:function(r,n,t){return null===r||void 0===r?void n(null,r):void g(t,e,r,n)}}}function A(e,r){n("function"==typeof r,"second argument should be a lookup function");var t;if("string"==typeof e)t=e;else{var i=c(e);n(f(i),"expected model schema or string as first argument for 'ref', got "+i),t=v(i),n(!!t,"provided model schema doesn't define an identifier() property and cannot be used by 'ref'.")}return{serializer:function(e){return e?e[t]:null},deserializer:function(e,n,t){r(e,n,t)}}}function S(e){return e=e||M,n(p(e),"expected prop schema as second argument"),n(!d(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){return Array.isArray(r)?void t(r,function(r,n){return e.deserializer(r,n,i)},n):void n("[serializr] expected JSON array")}}}function O(e){return e&&"function"==typeof e.keys&&"function"==typeof e.clear}function I(e){return e=e||M,n(p(e),"expected prop schema as second argument"),n(!d(e),"provided prop is aliased, please put aliases first"),{serializer:function(r){n(r&&"object"==typeof r,"expected object or Map");var t=O(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)return void n("[serializr] expected JSON object");var o=Object.keys(r);S(e).deserializer(o.map(function(e){return r[e]}),function(e,r){if(e)return void n(e);var t,a=O(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)}}}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),0===--this.pendingCallbacks&&this.onReadyCb(null,this.target))}.bind(this))};var M=k();return{createModelSchema:a,createSimpleSchema:o,setDefaultModelSchema:c,getDefaultModelSchema:c,serializable:s,serialize:y,deserialize:b,update:x,primitive:k,identifier:C,alias:E,list:S,map:I,child:w,ref:A}}"object"==typeof exports?module.exports=e():"function"==typeof define&&define.amd?define("serializer",[],e):this.serializer=e()}();
!function(){"use strict";function e(){function e(e){if(e)throw new Error(e)}function r(e){var r=!1;return function(){return r?void n(!1,"callback was invoked twice"):(r=!0,e.apply(null,arguments))}}function n(e,r){if(!e)throw new Error("[serializr] "+(r||"Illegal State"))}function t(e,r,n){if(0===e.length)return void n(null,[]);var t=e.length,i=[],o=!1,a=function(e,r,a){r?o||(o=!0,n(r)):(i[e]=a,0===--t&&n(null,i))};e.forEach(function(e,n){r(e,a.bind(null,n))})}function i(e){for(var r=1;r<arguments.length;r++){var n=arguments[r];if(n&&"object"==typeof n)for(var t in n)n.hasOwnProperty(t)&&(e[t]=n[t])}return e}function o(e){return{factory:function(){return{}},props:e}}function a(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={factory:t||function(){return new e},props:r};return c(e,i),i}function u(e,r,t){if(1===arguments.length){var i=e===!0?M:e;return n(d(i),"@serializable expects prop schema"),l.bind(null,i)}return l(k(),e,r,t)}function l(e,r,t,i){n(arguments.length>=2,"too few arguments. Please use @serializable as property decorator"),n("string"==typeof t,"incorrect usage of @serializable decorator");var o=s(r);return o||(o=a(r.constructor,{})),o.props[t]=e,!i||i.get||i.set||(i.writable=!0),i}function s(e){return e?f(e)?e:f(e.serializeInfo)?e.serializeInfo:e.constructor&&e.constructor.serializeInfo?e.constructor.serializeInfo:void 0:null}function c(e,r){return n(f(r)),e.serializeInfo=r}function f(e){return e&&e.factory&&e.props}function d(e){return e&&e.serializer&&e.deserializer}function p(e){return!!e.jsonname}function h(e){return e.identifier===!0}function v(e){n(f(e));for(var r in e.props)if(e.props[r].identifier===!0)return r;return null}function y(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=s(t[0]))}else i||(i=s(t));return n(!!i,"Failed to find default schema for "+e),Array.isArray(t)?t.map(function(e){return m(i,e)}):m(i,t)}function m(e,r){n(e&&"object"==typeof e,"Expected schema"),n(r&&"object"==typeof r,"Expected object");var t;return t=e.extends?m(e.extends,r):{},Object.keys(e.props).forEach(function(n){var i=e.props[n];i===!0&&(i=M);var o=i.serializer(r[n]);t[i.jsonname||n]=o}),t}function b(r,i,o,a){if(n(arguments.length>=2,"deserialize expects at least 2 arguments"),r=s(r),n(f(r),"first argument should be model schema"),Array.isArray(i)){var u=[];return t(i,function(e,n){var t=g(null,r,e,n,a);u.push(t)},o||e),u}return g(null,r,i,o,a)}function g(r,t,i,o,a){if(null===i||void 0===i)return void o(null,null);var u=new j(r,i,o,a),l=t.factory(u);n(!!l,"No object returned from factory"),u.target=l;var s=u.createCallback(e);return z(u,t,i,l),s(),l}function z(e,r,n,t){r.extends&&z(e,r.extends,n,t),Object.keys(r.props).forEach(function(i){var o=r.props[i];o===!0&&(o=M);var a=o.jsonname||i;a in n&&o.deserializer(n[a],e.createCallback(function(e){t[i]=e}),e,t[i])})}function j(r,n,t,i){this.parentContext=r,this.onReadyCb=t||e,this.json=n,this.target=null,this.pendingCallbacks=0,this.hasError=!1,this.args=r?r.args:i}function x(r,t,i,o,a){var u=2===arguments.length||"function"==typeof arguments[2];u&&(t=arguments[0],r=s(t),i=arguments[1],o=arguments[2],a=arguments[2]),n(f(r),"update failed to determine schema"),n("object"==typeof t&&t&&!Array.isArray(t),"update needs an object");var l=new j(null,i,o,a);l.target=t;var c=l.createCallback(e);z(l,r,i,t),c()}function k(){return{serializer:function(e){return n(null===e||"object"!=typeof e&&"function"!=typeof e,"this value is not primitive: "+e),e},deserializer:function(e,r){return"object"==typeof e&&null!==e||"function"==typeof e?void r("[serializr] this value is not primitive: "+e):void r(null,e)}}}function w(){return i({identifier:!0},M)}function E(){return{serializer:function(e){return null===e||void 0===e?e:(n(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 C(e,r){return n(e&&"string"==typeof e,"expected prop name as first argument"),r=r||M,n(d(r),"expected prop schema as second argument"),n(!p(r),"provided prop is already aliased"),{jsonname:e,serializer:r.serializer,deserializer:r.deserializer,identifier:h(r)}}function A(e){return e=s(e),n(f(e),"expected modelSchema, got "+e),{serializer:function(r){return null===r||void 0===r?r:y(e,r)},deserializer:function(r,n,t){return null===r||void 0===r?void n(null,r):void g(t,e,r,n)}}}function S(e,r){n("function"==typeof r,"second argument should be a lookup function");var t;if("string"==typeof e)t=e;else{var i=s(e);n(f(i),"expected model schema or string as first argument for 'ref', got "+i),t=v(i),n(!!t,"provided model schema doesn't define an identifier() property and cannot be used by 'ref'.")}return{serializer:function(e){return e?e[t]:null},deserializer:function(e,n,t){null===e||void 0===e?n(null,e):r(e,n,t)}}}function O(e){return e=e||M,n(d(e),"expected prop schema as second argument"),n(!p(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){return Array.isArray(r)?void t(r,function(r,n){return e.deserializer(r,n,i)},n):void n("[serializr] expected JSON array")}}}function I(e){return e&&"function"==typeof e.keys&&"function"==typeof e.clear}function D(e){return e=e||M,n(d(e),"expected prop schema as second argument"),n(!p(e),"provided prop is aliased, please put aliases first"),{serializer:function(r){n(r&&"object"==typeof r,"expected object or Map");var t=I(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)return void n("[serializr] expected JSON object");var o=Object.keys(r);O(e).deserializer(o.map(function(e){return r[e]}),function(e,r){if(e)return void n(e);var t,a=I(i);a?(i.clear(),t=i):t={};for(var u=0,l=o.length;u<l;u++)a?t.set(o[u],r[u]):t[o[u]]=r[u];n(null,t)},t)}}}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),0===--this.pendingCallbacks&&this.onReadyCb(null,this.target))}.bind(this))};var M=k();return{createModelSchema:a,createSimpleSchema:o,setDefaultModelSchema:s,getDefaultModelSchema:s,serializable:u,serialize:y,deserialize:b,update:x,primitive:k,identifier:w,date:E,alias:C,list:O,map:D,child:A,ref:S}}"object"==typeof exports?module.exports=e():"function"==typeof define&&define.amd?define("serializer",[],e):this.serializer=e()}();
//# sourceMappingURL=serializr.min.js.map

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