UNPKG

normalizr

Version:

Normalizes and denormalizes JSON according to schema for Redux and Flux applications

2 lines (1 loc) 6.94 kB
function t(t,e){for(var n=0;n<e.length;n++){var r=e[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(t,r.key,r)}}function e(e,n,r){return n&&t(e.prototype,n),r&&t(e,r),e}function n(){return(n=Object.assign||function(t){for(var e=1;e<arguments.length;e++){var n=arguments[e];for(var r in n)Object.prototype.hasOwnProperty.call(n,r)&&(t[r]=n[r])}return t}).apply(this,arguments)}function r(t,e){t.prototype=Object.create(e.prototype),t.prototype.constructor=t,t.__proto__=e}function i(t){return!(!t||"function"!=typeof t.hasOwnProperty||!(t.hasOwnProperty("__ownerID")||t._map&&t._map.hasOwnProperty("__ownerID")))}function o(t,e,n){return Object.keys(t).reduce((function(e,r){var i=""+r;return e.has(i)?e.set(i,n(e.get(i),t[i])):e}),e)}var u=function(){function t(t,e,r){if(void 0===e&&(e={}),void 0===r&&(r={}),!t||"string"!=typeof t)throw new Error("Expected a string key for Entity, but found "+t+".");var o=r,u=o.idAttribute,a=void 0===u?"id":u,c=o.mergeStrategy,f=void 0===c?function(t,e){return n({},t,e)}:c,s=o.processStrategy,h=void 0===s?function(t){return n({},t)}:s,l=o.fallbackStrategy,y=void 0===l?function(t,e){}:l;this._key=t,this._getId="function"==typeof a?a:function(t){return function(e){return i(e)?e.get(t):e[t]}}(a),this._idAttribute=a,this._mergeStrategy=f,this._processStrategy=h,this._fallbackStrategy=y,this.define(e)}var r=t.prototype;return r.define=function(t){this.schema=Object.keys(t).reduce((function(e,r){var i,o=t[r];return n({},e,((i={})[r]=o,i))}),this.schema||{})},r.getId=function(t,e,n){return this._getId(t,e,n)},r.merge=function(t,e){return this._mergeStrategy(t,e)},r.fallback=function(t,e){return this._fallbackStrategy(t,e)},r.normalize=function(t,e,n,r,i,o){var u=this,a=this.getId(t,e,n),c=this.key;if(c in o||(o[c]={}),a in o[c]||(o[c][a]=[]),o[c][a].some((function(e){return e===t})))return a;o[c][a].push(t);var f=this._processStrategy(t,e,n);return Object.keys(this.schema).forEach((function(e){if(f.hasOwnProperty(e)&&"object"==typeof f[e]){var n=u.schema[e],a="function"==typeof n?n(t):n;f[e]=r(f[e],f,e,a,i,o)}})),i(this,f,t,e,n),a},r.denormalize=function(t,e){var n=this;return i(t)?o(this.schema,t,e):(Object.keys(this.schema).forEach((function(r){if(t.hasOwnProperty(r)){var i=n.schema[r];t[r]=e(t[r],i)}})),t)},e(t,[{key:"key",get:function(){return this._key}},{key:"idAttribute",get:function(){return this._idAttribute}}]),t}(),a=function(){function t(t,e){e&&(this._schemaAttribute="string"==typeof e?function(t){return t[e]}:e),this.define(t)}var n=t.prototype;return n.define=function(t){this.schema=t},n.getSchemaAttribute=function(t,e,n){return!this.isSingleSchema&&this._schemaAttribute(t,e,n)},n.inferSchema=function(t,e,n){if(this.isSingleSchema)return this.schema;var r=this.getSchemaAttribute(t,e,n);return this.schema[r]},n.normalizeValue=function(t,e,n,r,i,o){var u=this.inferSchema(t,e,n);if(!u)return t;var a=r(t,e,n,u,i,o);return this.isSingleSchema||null==a?a:{id:a,schema:this.getSchemaAttribute(t,e,n)}},n.denormalizeValue=function(t,e){var n=i(t)?t.get("schema"):t.schema;return this.isSingleSchema||n?e((this.isSingleSchema?void 0:i(t)?t.get("id"):t.id)||t,this.isSingleSchema?this.schema:this.schema[n]):t},e(t,[{key:"isSingleSchema",get:function(){return!this._schemaAttribute}}]),t}(),c=function(t){function e(e,n){if(!n)throw new Error('Expected option "schemaAttribute" not found on UnionSchema.');return t.call(this,e,n)||this}r(e,t);var n=e.prototype;return n.normalize=function(t,e,n,r,i,o){return this.normalizeValue(t,e,n,r,i,o)},n.denormalize=function(t,e){return this.denormalizeValue(t,e)},e}(a),f=function(t){function e(){return t.apply(this,arguments)||this}r(e,t);var i=e.prototype;return i.normalize=function(t,e,r,i,o,u){var a=this;return Object.keys(t).reduce((function(e,r,c){var f,s=t[r];return null!=s?n({},e,((f={})[r]=a.normalizeValue(s,t,r,i,o,u),f)):e}),{})},i.denormalize=function(t,e){var r=this;return Object.keys(t).reduce((function(i,o){var u,a=t[o];return n({},i,((u={})[o]=r.denormalizeValue(a,e),u))}),{})},e}(a),s=function(t){if(Array.isArray(t)&&t.length>1)throw new Error("Expected schema definition to be a single schema, but found "+t.length+".");return t[0]},h=function(t){return Array.isArray(t)?t:Object.keys(t).map((function(e){return t[e]}))},l=function(t,e,n,r,i,o,u){return t=s(t),h(e).map((function(e,a){return i(e,n,r,t,o,u)}))},y=function(t,e,n){return t=s(t),e&&e.map?e.map((function(e){return n(e,t)})):e},m=function(t){function e(){return t.apply(this,arguments)||this}r(e,t);var n=e.prototype;return n.normalize=function(t,e,n,r,i,o){var u=this;return h(t).map((function(t,a){return u.normalizeValue(t,e,n,r,i,o)})).filter((function(t){return null!=t}))},n.denormalize=function(t,e){var n=this;return t&&t.map?t.map((function(t){return n.denormalizeValue(t,e)})):t},e}(a),p=function(t,e,r,i,o,u,a){var c=n({},e);return Object.keys(t).forEach((function(n){var r=t[n],i="function"==typeof r?r(e):r,f=o(e[n],e,n,i,u,a);null==f?delete c[n]:c[n]=f})),c},d=function(t,e,r){if(i(e))return o(t,e,r);var u=n({},e);return Object.keys(t).forEach((function(e){null!=u[e]&&(u[e]=r(u[e],t[e]))})),u},v=function t(e,n,r,i,o,u){return"object"==typeof e&&e?"object"!=typeof i||i.normalize&&"function"==typeof i.normalize?i.normalize(e,n,r,t,o,u):(Array.isArray(i)?l:p)(i,e,n,r,t,o,u):e},g={Array:m,Entity:u,Object:function(){function t(t){this.define(t)}var e=t.prototype;return e.define=function(t){this.schema=Object.keys(t).reduce((function(e,r){var i,o=t[r];return n({},e,((i={})[r]=o,i))}),this.schema||{})},e.normalize=function(){for(var t=arguments.length,e=new Array(t),n=0;n<t;n++)e[n]=arguments[n];return p.apply(void 0,[this.schema].concat(e))},e.denormalize=function(){for(var t=arguments.length,e=new Array(t),n=0;n<t;n++)e[n]=arguments[n];return d.apply(void 0,[this.schema].concat(e))},t}(),Union:c,Values:f},b=function(t,e){if(!t||"object"!=typeof t)throw new Error('Unexpected input given to normalize. Expected type to be "object", found "'+(null===t?"null":typeof t)+'".');var n={},r=function(t){return function(e,n,r,i,o){var u=e.key,a=e.getId(r,i,o);u in t||(t[u]={});var c=t[u][a];t[u][a]=c?e.merge(c,n):n}}(n);return{entities:n,result:v(t,t,null,e,r,{})}},k=function(t){var e={},r=S(t);return function t(o,a){return"object"!=typeof a||a.denormalize&&"function"==typeof a.denormalize?null==o?o:a instanceof u?function(t,e,r,o,a){var c=o(t,e);if(void 0===c&&e instanceof u&&(c=e.fallback(t,e)),"object"!=typeof c||null===c)return c;if(a[e.key]||(a[e.key]={}),!a[e.key][t]){var f=i(c)?c:n({},c);a[e.key][t]=f,a[e.key][t]=e.denormalize(f,r)}return a[e.key][t]}(o,a,t,r,e):a.denormalize(o,t):(Array.isArray(a)?y:d)(a,o,t)}},S=function(t){var e=i(t);return function(n,r){var i=r.key;return"object"==typeof n?n:e?t.getIn([i,n.toString()]):t[i]&&t[i][n]}},z=function(t,e,n){if(void 0!==t)return k(n)(t,e)};export{z as denormalize,b as normalize,g as schema};