@gogoout/normalizr
Version:
Normalizes and denormalizes JSON according to schema for Redux and Flux applications
2 lines (1 loc) • 7.22 kB
JavaScript
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 a=function(t){return function(e){return i(e)?e.get(t):e[t]}},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 i=r,o=i.idAttribute,u=void 0===o?"id":o,c=i.mergeStrategy,f=void 0===c?function(t,e){return n({},t,e)}:c,s=i.processStrategy,h=void 0===s?function(t){return n({},t)}:s,y=i.fallbackStrategy,l=void 0===y?function(t,e){}:y,m=i.keyNamingStrategy;this._key=t,this._getId="function"==typeof u?u:a(u),this._idAttribute=u,this._mergeStrategy=f,this._processStrategy=h,this._fallbackStrategy=l,this._keyNameAffix="","camelCase"===m?this._keyNameAffix="Id":"snakeCase"===m&&(this._keyNameAffix="_id"),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.getKeyName=function(t,e){return this._keyNameAffix?Array.isArray(e)?""+t+this._keyNameAffix+"s":""+t+this._keyNameAffix:t},r.normalize=function(t,e,n,r,i,o){var a=this,u=this.getId(t,e,n),c=this.key;if(c in o||(o[c]={}),u in o[c]||(o[c][u]=[]),o[c][u].some(function(e){return e===t}))return u;o[c][u].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=a.schema[e],u="function"==typeof n?n(t):n,c=a.getKeyName(e,u);f[c]=r(f[e],f,c,u,i,o),c!==e&&delete f[e]}}),i(this,f,t,e,n),u},r.denormalize=function(t,e){var n=this;return i(t)?o(this.schema,t,e):(Object.keys(this.schema).forEach(function(r){var i=n.schema[r],o=n.getKeyName(r,i);t.hasOwnProperty(o)&&(t[r]=e(t[o],i))}),t)},e(t,[{key:"key",get:function(){return this._key}},{key:"idAttribute",get:function(){return this._idAttribute}}]),t}(),c=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 a=this.inferSchema(t,e,n);if(!a)return t;var u=r(t,e,n,a,i,o);return this.isSingleSchema||null==u?u:{id:u,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}(),f=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}(c),s=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,a){var u=this;return Object.keys(t).reduce(function(e,r,c){var f,s=t[r];return null!=s?n({},e,((f={})[r]=u.normalizeValue(s,t,r,i,o,a),f)):e},{})},i.denormalize=function(t,e){var r=this;return Object.keys(t).reduce(function(i,o){var a,u=t[o];return n({},i,((a={})[o]=r.denormalizeValue(u,e),a))},{})},e}(c),h=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]},y=function(t){return Array.isArray(t)?t:Object.keys(t).map(function(e){return t[e]})},l=function(t,e,n,r,i,o,a){return t=h(t),y(e).map(function(e,u){return i(e,n,r,t,o,a)})},m=function(t,e,n){return t=h(t),e&&e.map?e.map(function(e){return n(e,t)}):e},p=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 a=this;return y(t).map(function(t,u){return a.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}(c),d=function(t,e,r,i,o,a,u){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,a,u);null==f?delete c[n]:c[n]=f}),c},v=function(t,e,r){if(i(e))return o(t,e,r);var a=n({},e);return Object.keys(t).forEach(function(e){null!=a[e]&&(a[e]=r(a[e],t[e]))}),a},g=function t(e,n,r,i,o,a){return"object"==typeof e&&e?"object"!=typeof i||i.normalize&&"function"==typeof i.normalize?i.normalize(e,n,r,t,o,a):(Array.isArray(i)?l:d)(i,e,n,r,t,o,a):e},b={Array:p,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 d.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 v.apply(void 0,[this.schema].concat(e))},t}(),Union:f,Values:s},k=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 a=e.key,u=e.getId(r,i,o);a in t||(t[a]={});var c=t[a][u];t[a][u]=c?e.merge(c,n):n}}(n);return{entities:n,result:g(t,t,null,e,r,{})}},_=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)?m:v)(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]}},A=function(t,e,n){if(void 0!==t)return _(n)(t,e)};export{A as denormalize,k as normalize,b as schema};