normalizr
Version:
Normalizes and denormalizes JSON according to schema for Redux and Flux applications
2 lines (1 loc) • 7.7 kB
JavaScript
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e(t.normalizr={})}(this,function(t){"use strict";function e(t){return!(!t||"function"!=typeof t.hasOwnProperty||!(t.hasOwnProperty("__ownerID")||t._map&&t._map.hasOwnProperty("__ownerID")))}function n(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 r="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},i=function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")},o=function(){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)}}return function(e,n,r){return n&&t(e.prototype,n),r&&t(e,r),e}}(),u=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},c=function(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function, not "+typeof e);t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,enumerable:!1,writable:!0,configurable:!0}}),e&&(Object.setPrototypeOf?Object.setPrototypeOf(t,e):t.__proto__=e)},a=function(t,e){if(!t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!e||"object"!=typeof e&&"function"!=typeof e?t:e},f=function(t){return function(n){return e(n)?n.get(t):n[t]}},s=function(){function t(e){var n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{};if(i(this,t),!e||"string"!=typeof e)throw new Error("Expected a string key for Entity, but found "+e+".");var o=r.idAttribute,c=void 0===o?"id":o,a=r.mergeStrategy,s=void 0===a?function(t,e){return u({},t,e)}:a,h=r.processStrategy,p=void 0===h?function(t){return u({},t)}:h;this._key=e,this._getId="function"==typeof c?c:f(c),this._idAttribute=c,this._mergeStrategy=s,this._processStrategy=p,this.define(n)}return t.prototype.define=function(t){this.schema=Object.keys(t).reduce(function(e,n){var r,i=t[n];return u({},e,((r={})[n]=i,r))},this.schema||{})},t.prototype.getId=function(t,e,n){return this._getId(t,e,n)},t.prototype.merge=function(t,e){return this._mergeStrategy(t,e)},t.prototype.normalize=function(t,e,n,i,o){var u=this,c=this._processStrategy(t,e,n);return Object.keys(this.schema).forEach(function(t){if(c.hasOwnProperty(t)&&"object"===r(c[t])){var e=u.schema[t];c[t]=i(c[t],c,t,e,o)}}),o(this,c,t,e,n),this.getId(t,e,n)},t.prototype.denormalize=function(t,r){var i=this;return e(t)?n(this.schema,t,r):(Object.keys(this.schema).forEach(function(e){if(t.hasOwnProperty(e)){var n=i.schema[e];t[e]=r(t[e],n)}}),t)},o(t,[{key:"key",get:function(){return this._key}},{key:"idAttribute",get:function(){return this._idAttribute}}]),t}(),h=function(){function t(e,n){i(this,t),n&&(this._schemaAttribute="string"==typeof n?function(t){return t[n]}:n),this.define(e)}return t.prototype.define=function(t){this.schema=t},t.prototype.getSchemaAttribute=function(t,e,n){return!this.isSingleSchema&&this._schemaAttribute(t,e,n)},t.prototype.inferSchema=function(t,e,n){if(this.isSingleSchema)return this.schema;var r=this.getSchemaAttribute(t,e,n);return this.schema[r]},t.prototype.normalizeValue=function(t,e,n,r,i){var o=this.inferSchema(t,e,n);if(!o)return t;var u=r(t,e,n,o,i);return this.isSingleSchema||null==u?u:{id:u,schema:this.getSchemaAttribute(t,e,n)}},t.prototype.denormalizeValue=function(t,n){var r=e(t)?t.get("schema"):t.schema;return this.isSingleSchema||r?n((e(t)?t.get("id"):t.id)||t,this.isSingleSchema?this.schema:this.schema[r]):t},o(t,[{key:"isSingleSchema",get:function(){return!this._schemaAttribute}}]),t}(),p=function(t){function e(n,r){if(i(this,e),!r)throw new Error('Expected option "schemaAttribute" not found on UnionSchema.');return a(this,t.call(this,n,r))}return c(e,t),e.prototype.normalize=function(t,e,n,r,i){return this.normalizeValue(t,e,n,r,i)},e.prototype.denormalize=function(t,e){return this.denormalizeValue(t,e)},e}(h),y=function(t){function e(){return i(this,e),a(this,t.apply(this,arguments))}return c(e,t),e.prototype.normalize=function(t,e,n,r,i){var o=this;return Object.keys(t).reduce(function(e,n,c){var a,f=t[n];return null!=f?u({},e,((a={})[n]=o.normalizeValue(f,t,n,r,i),a)):e},{})},e.prototype.denormalize=function(t,e){var n=this;return Object.keys(t).reduce(function(r,i){var o,c=t[i];return u({},r,((o={})[i]=n.denormalizeValue(c,e),o))},{})},e}(h),l=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]},m=function(t){return Array.isArray(t)?t:Object.keys(t).map(function(e){return t[e]})},d=function(t,e,n,r,i,o){return t=l(t),m(e).map(function(e,u){return i(e,n,r,t,o)})},b=function(t,e,n){return t=l(t),e&&e.map?e.map(function(e){return n(e,t)}):e},v=function(t){function e(){return i(this,e),a(this,t.apply(this,arguments))}return c(e,t),e.prototype.normalize=function(t,e,n,r,i){var o=this;return m(t).map(function(t,u){return o.normalizeValue(t,e,n,r,i)}).filter(function(t){return null!=t})},e.prototype.denormalize=function(t,e){var n=this;return t&&t.map?t.map(function(t){return n.denormalizeValue(t,e)}):t},e}(h),g=function(t,e,n,r,i,o){var c=u({},e);return Object.keys(t).forEach(function(n){var r=t[n],u=i(e[n],e,n,r,o);null==u?delete c[n]:c[n]=u}),c},S=function(t,r,i){if(e(r))return n(t,r,i);var o=u({},r);return Object.keys(t).forEach(function(e){o[e]&&(o[e]=i(o[e],t[e]))}),o},z=function(){function t(e){i(this,t),this.define(e)}return t.prototype.define=function(t){this.schema=Object.keys(t).reduce(function(e,n){var r,i=t[n];return u({},e,((r={})[n]=i,r))},this.schema||{})},t.prototype.normalize=function(){for(var t=arguments.length,e=Array(t),n=0;n<t;n++)e[n]=arguments[n];return g.apply(void 0,[this.schema].concat(e))},t.prototype.denormalize=function(){for(var t=arguments.length,e=Array(t),n=0;n<t;n++)e[n]=arguments[n];return S.apply(void 0,[this.schema].concat(e))},t}(),j={Array:v,Entity:s,Object:z,Union:p,Values:y},k=function(t){var n={},i=O(t);return function t(o,c){return"object"!==(void 0===c?"undefined":r(c))||c.denormalize&&"function"==typeof c.denormalize?null==o?o:c instanceof s?function(t,n,i,o,c){var a=o(t,n);if("object"!==(void 0===a?"undefined":r(a))||null===a)return a;if(c[n.key]||(c[n.key]={}),!c[n.key][t]){var f=e(a)?a:u({},a);c[n.key][t]=f,c[n.key][t]=n.denormalize(f,i)}return c[n.key][t]}(o,c,t,i,n):c.denormalize(o,t):(Array.isArray(c)?b:S)(c,o,t)}},O=function(t){var n=e(t);return function(e,i){var o=i.key;return"object"===(void 0===e?"undefined":r(e))?e:n?t.getIn([o,e.toString()]):t[o][e]}};t.schema=j,t.normalize=function(t,e){if(!t||"object"!==(void 0===t?"undefined":r(t)))throw new Error('Unexpected input given to normalize. Expected type to be "object", found "'+(void 0===t?"undefined":r(t))+'".');var n={},i=function(t){return function(e,n,r,i,o){var u=e.key,c=e.getId(r,i,o);u in t||(t[u]={});var a=t[u][c];t[u][c]=a?e.merge(a,n):n}}(n);return{entities:n,result:function t(e,n,i,o,u){return"object"===(void 0===e?"undefined":r(e))&&e?"object"!==(void 0===o?"undefined":r(o))||o.normalize&&"function"==typeof o.normalize?o.normalize(e,n,i,t,u):(Array.isArray(o)?d:g)(o,e,n,i,t,u):e}(t,t,null,e,i)}},t.denormalize=function(t,e,n){if(void 0!==t)return k(n)(t,e)},Object.defineProperty(t,"__esModule",{value:!0})});