UNPKG

normalizr

Version:

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

2 lines (1 loc) 8.58 kB
"use strict";function isImmutable(e){return!(!e||"function"!=typeof e.hasOwnProperty||!(e.hasOwnProperty("__ownerID")||e._map&&e._map.hasOwnProperty("__ownerID")))}function denormalizeImmutable(e,t,n){return Object.keys(e).reduce(function(t,r){var i=""+r;return t.has(i)?t.set(i,n(t.get(i),e[i])):t},t)}Object.defineProperty(exports,"__esModule",{value:!0});var _typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},classCallCheck=function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")},createClass=function(){function e(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}return function(t,n,r){return n&&e(t.prototype,n),r&&e(t,r),t}}(),_extends=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var r in n)Object.prototype.hasOwnProperty.call(n,r)&&(e[r]=n[r])}return e},inherits=function(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function, not "+typeof t);e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,enumerable:!1,writable:!0,configurable:!0}}),t&&(Object.setPrototypeOf?Object.setPrototypeOf(e,t):e.__proto__=t)},possibleConstructorReturn=function(e,t){if(!e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!t||"object"!=typeof t&&"function"!=typeof t?e:t},getDefaultGetId=function(e){return function(t){return isImmutable(t)?t.get(e):t[e]}},EntitySchema=function(){function e(t){var n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{};if(classCallCheck(this,e),!t||"string"!=typeof t)throw new Error("Expected a string key for Entity, but found "+t+".");var i=r.idAttribute,o=void 0===i?"id":i,a=r.mergeStrategy,u=void 0===a?function(e,t){return _extends({},e,t)}:a,c=r.processStrategy,s=void 0===c?function(e){return _extends({},e)}:c;this._key=t,this._getId="function"==typeof o?o:getDefaultGetId(o),this._idAttribute=o,this._mergeStrategy=u,this._processStrategy=s,this.define(n)}return e.prototype.define=function(e){this.schema=Object.keys(e).reduce(function(t,n){var r,i=e[n];return _extends({},t,((r={})[n]=i,r))},this.schema||{})},e.prototype.getId=function(e,t,n){return this._getId(e,t,n)},e.prototype.merge=function(e,t){return this._mergeStrategy(e,t)},e.prototype.normalize=function(e,t,n,r,i){var o=this,a=this._processStrategy(e,t,n);return Object.keys(this.schema).forEach(function(e){if(a.hasOwnProperty(e)&&"object"===_typeof(a[e])){var t=o.schema[e];a[e]=r(a[e],a,e,t,i)}}),i(this,a,e,t,n),this.getId(e,t,n)},e.prototype.denormalize=function(e,t){var n=this;return isImmutable(e)?denormalizeImmutable(this.schema,e,t):(Object.keys(this.schema).forEach(function(r){if(e.hasOwnProperty(r)){var i=n.schema[r];e[r]=t(e[r],i)}}),e)},createClass(e,[{key:"key",get:function(){return this._key}},{key:"idAttribute",get:function(){return this._idAttribute}}]),e}(),PolymorphicSchema=function(){function e(t,n){classCallCheck(this,e),n&&(this._schemaAttribute="string"==typeof n?function(e){return e[n]}:n),this.define(t)}return e.prototype.define=function(e){this.schema=e},e.prototype.getSchemaAttribute=function(e,t,n){return!this.isSingleSchema&&this._schemaAttribute(e,t,n)},e.prototype.inferSchema=function(e,t,n){if(this.isSingleSchema)return this.schema;var r=this.getSchemaAttribute(e,t,n);return this.schema[r]},e.prototype.normalizeValue=function(e,t,n,r,i){var o=this.inferSchema(e,t,n);if(!o)return e;var a=r(e,t,n,o,i);return this.isSingleSchema||null==a?a:{id:a,schema:this.getSchemaAttribute(e,t,n)}},e.prototype.denormalizeValue=function(e,t){var n=isImmutable(e)?e.get("schema"):e.schema;return this.isSingleSchema||n?t((isImmutable(e)?e.get("id"):e.id)||e,this.isSingleSchema?this.schema:this.schema[n]):e},createClass(e,[{key:"isSingleSchema",get:function(){return!this._schemaAttribute}}]),e}(),UnionSchema=function(e){function t(n,r){if(classCallCheck(this,t),!r)throw new Error('Expected option "schemaAttribute" not found on UnionSchema.');return possibleConstructorReturn(this,e.call(this,n,r))}return inherits(t,e),t.prototype.normalize=function(e,t,n,r,i){return this.normalizeValue(e,t,n,r,i)},t.prototype.denormalize=function(e,t){return this.denormalizeValue(e,t)},t}(PolymorphicSchema),ValuesSchema=function(e){function t(){return classCallCheck(this,t),possibleConstructorReturn(this,e.apply(this,arguments))}return inherits(t,e),t.prototype.normalize=function(e,t,n,r,i){var o=this;return Object.keys(e).reduce(function(t,n,a){var u,c=e[n];return null!=c?_extends({},t,((u={})[n]=o.normalizeValue(c,e,n,r,i),u)):t},{})},t.prototype.denormalize=function(e,t){var n=this;return Object.keys(e).reduce(function(r,i){var o,a=e[i];return _extends({},r,((o={})[i]=n.denormalizeValue(a,t),o))},{})},t}(PolymorphicSchema),validateSchema=function(e){if(Array.isArray(e)&&e.length>1)throw new Error("Expected schema definition to be a single schema, but found "+e.length+".");return e[0]},getValues=function(e){return Array.isArray(e)?e:Object.keys(e).map(function(t){return e[t]})},normalize=function(e,t,n,r,i,o){return e=validateSchema(e),getValues(t).map(function(t,a){return i(t,n,r,e,o)})},denormalize=function(e,t,n){return e=validateSchema(e),t&&t.map?t.map(function(t){return n(t,e)}):t},ArraySchema=function(e){function t(){return classCallCheck(this,t),possibleConstructorReturn(this,e.apply(this,arguments))}return inherits(t,e),t.prototype.normalize=function(e,t,n,r,i){var o=this;return getValues(e).map(function(e,a){return o.normalizeValue(e,t,n,r,i)}).filter(function(e){return null!=e})},t.prototype.denormalize=function(e,t){var n=this;return e&&e.map?e.map(function(e){return n.denormalizeValue(e,t)}):e},t}(PolymorphicSchema),_normalize=function(e,t,n,r,i,o){var a=_extends({},t);return Object.keys(e).forEach(function(n){var r=e[n],u=i(t[n],t,n,r,o);null==u?delete a[n]:a[n]=u}),a},_denormalize=function(e,t,n){if(isImmutable(t))return denormalizeImmutable(e,t,n);var r=_extends({},t);return Object.keys(e).forEach(function(t){r[t]&&(r[t]=n(r[t],e[t]))}),r},ObjectSchema=function(){function e(t){classCallCheck(this,e),this.define(t)}return e.prototype.define=function(e){this.schema=Object.keys(e).reduce(function(t,n){var r,i=e[n];return _extends({},t,((r={})[n]=i,r))},this.schema||{})},e.prototype.normalize=function(){for(var e=arguments.length,t=Array(e),n=0;n<e;n++)t[n]=arguments[n];return _normalize.apply(void 0,[this.schema].concat(t))},e.prototype.denormalize=function(){for(var e=arguments.length,t=Array(e),n=0;n<e;n++)t[n]=arguments[n];return _denormalize.apply(void 0,[this.schema].concat(t))},e}(),visit=function e(t,n,r,i,o){return"object"===(void 0===t?"undefined":_typeof(t))&&t?"object"!==(void 0===i?"undefined":_typeof(i))||i.normalize&&"function"==typeof i.normalize?i.normalize(t,n,r,e,o):(Array.isArray(i)?normalize:_normalize)(i,t,n,r,e,o):t},addEntities=function(e){return function(t,n,r,i,o){var a=t.key,u=t.getId(r,i,o);a in e||(e[a]={});var c=e[a][u];e[a][u]=c?t.merge(c,n):n}},schema={Array:ArraySchema,Entity:EntitySchema,Object:ObjectSchema,Union:UnionSchema,Values:ValuesSchema},normalize$1=function(e,t){if(!e||"object"!==(void 0===e?"undefined":_typeof(e)))throw new Error('Unexpected input given to normalize. Expected type to be "object", found "'+(void 0===e?"undefined":_typeof(e))+'".');var n={},r=addEntities(n);return{entities:n,result:visit(e,e,null,t,r)}},unvisitEntity=function(e,t,n,r,i){var o=r(e,t);if("object"!==(void 0===o?"undefined":_typeof(o))||null===o)return o;if(i[t.key]||(i[t.key]={}),!i[t.key][e]){var a=isImmutable(o)?o:_extends({},o);i[t.key][e]=a,i[t.key][e]=t.denormalize(a,n)}return i[t.key][e]},getUnvisit=function(e){var t={},n=getEntities(e);return function e(r,i){return"object"!==(void 0===i?"undefined":_typeof(i))||i.denormalize&&"function"==typeof i.denormalize?null==r?r:i instanceof EntitySchema?unvisitEntity(r,i,e,n,t):i.denormalize(r,e):(Array.isArray(i)?denormalize:_denormalize)(i,r,e)}},getEntities=function(e){var t=isImmutable(e);return function(n,r){var i=r.key;return"object"===(void 0===n?"undefined":_typeof(n))?n:t?e.getIn([i,n.toString()]):e[i][n]}},denormalize$1=function(e,t,n){if(void 0!==e)return getUnvisit(n)(e,t)};exports.schema=schema,exports.normalize=normalize$1,exports.denormalize=denormalize$1;