Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

normalizr

Package Overview
Dependencies
Maintainers
1
Versions
37
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

normalizr - npm Package Compare versions

Comparing version 3.2.1 to 3.2.2

2

CHANGELOG.md

@@ -6,3 +6,3 @@ # v3.2.0

* **Fixed** Gracefully handle missing data in `denormalize` (gh-232)
* ~~**Fixed** Prevent infinite recursion in `denormalize` (gh-220)~~ Reverted in v3.2.1
* **Fixed** Prevent infinite recursion in `denormalize` (gh-220)

@@ -9,0 +9,0 @@ # v3.1.0

@@ -32,3 +32,3 @@ 'use strict';

*/
function denormalizeImmutable(schema, input, unvisit, getDenormalizedEntity) {
function denormalizeImmutable(schema, input, unvisit) {
return Object.keys(schema).reduce(function (object, key) {

@@ -40,3 +40,3 @@ // Immutable maps cast keys to strings on write so we need to ensure

if (object.has(stringKey)) {
return object.set(stringKey, unvisit(object.get(stringKey), schema[stringKey], getDenormalizedEntity));
return object.set(stringKey, unvisit(object.get(stringKey), schema[stringKey]));
} else {

@@ -276,22 +276,16 @@ return object;

key: 'denormalize',
value: function denormalize(entityOrId, unvisit, getDenormalizedEntity) {
value: function denormalize(entity, unvisit) {
var _this2 = this;
var entity = getDenormalizedEntity(this, entityOrId);
if ((typeof entity === 'undefined' ? 'undefined' : _typeof(entity)) !== 'object' || entity === null) {
return entity;
}
if (isImmutable(entity)) {
return denormalizeImmutable(this.schema, entity, unvisit, getDenormalizedEntity);
return denormalizeImmutable(this.schema, entity, unvisit);
}
var processedEntity = _extends({}, entity);
Object.keys(this.schema).forEach(function (key) {
if (processedEntity.hasOwnProperty(key)) {
if (entity.hasOwnProperty(key)) {
var schema = _this2.schema[key];
processedEntity[key] = unvisit(processedEntity[key], schema, getDenormalizedEntity);
entity[key] = unvisit(entity[key], schema);
}
});
return processedEntity;
return entity;
}

@@ -356,3 +350,3 @@ }, {

key: 'denormalizeValue',
value: function denormalizeValue(value, unvisit, getDenormalizedEntity) {
value: function denormalizeValue(value, unvisit) {
if (!this.isSingleSchema && !value.schema) {

@@ -362,3 +356,3 @@ return value;

var schema = this.isSingleSchema ? this.schema : this.schema[value.schema];
return unvisit(value.id || value, schema, getDenormalizedEntity);
return unvisit(value.id || value, schema);
}

@@ -393,4 +387,4 @@ }, {

key: 'denormalize',
value: function denormalize(input, unvisit, getDenormalizedEntity) {
return this.denormalizeValue(input, unvisit, getDenormalizedEntity);
value: function denormalize(input, unvisit) {
return this.denormalizeValue(input, unvisit);
}

@@ -421,3 +415,3 @@ }]);

key: 'denormalize',
value: function denormalize(input, unvisit, getDenormalizedEntity) {
value: function denormalize(input, unvisit) {
var _this3 = this;

@@ -427,3 +421,3 @@

var entityOrId = input[key];
return _extends({}, output, defineProperty({}, key, _this3.denormalizeValue(entityOrId, unvisit, getDenormalizedEntity)));
return _extends({}, output, defineProperty({}, key, _this3.denormalizeValue(entityOrId, unvisit)));
}, {});

@@ -462,6 +456,6 @@ }

var denormalize$2 = function denormalize$2(schema, input, unvisit, getDenormalizedEntity) {
var denormalize$2 = function denormalize$2(schema, input, unvisit) {
schema = validateSchema(schema);
return Array.isArray(input) ? input.map(function (entityOrId) {
return unvisit(entityOrId, schema, getDenormalizedEntity);
return input && input.map ? input.map(function (entityOrId) {
return unvisit(entityOrId, schema);
}) : input;

@@ -493,7 +487,7 @@ };

key: 'denormalize',
value: function denormalize$2(input, unvisit, getDenormalizedEntity) {
value: function denormalize$2(input, unvisit) {
var _this3 = this;
return Array.isArray(input) ? input.map(function (value) {
return _this3.denormalizeValue(value, unvisit, getDenormalizedEntity);
return input && input.map ? input.map(function (value) {
return _this3.denormalizeValue(value, unvisit);
}) : input;

@@ -519,5 +513,5 @@ }

var _denormalize = function _denormalize(schema, input, unvisit, getDenormalizedEntity) {
var _denormalize = function _denormalize(schema, input, unvisit) {
if (isImmutable(input)) {
return denormalizeImmutable(schema, input, unvisit, getDenormalizedEntity);
return denormalizeImmutable(schema, input, unvisit);
}

@@ -528,3 +522,3 @@

if (object[key]) {
object[key] = unvisit(object[key], schema[key], getDenormalizedEntity);
object[key] = unvisit(object[key], schema[key]);
}

@@ -622,28 +616,60 @@ });

var unvisit = function unvisit(input, schema, getDenormalizedEntity) {
if ((typeof schema === 'undefined' ? 'undefined' : _typeof(schema)) === 'object' && (!schema.denormalize || typeof schema.denormalize !== 'function')) {
var method = Array.isArray(schema) ? denormalize$2 : _denormalize;
return method(schema, input, unvisit, getDenormalizedEntity);
var unvisitEntity = function unvisitEntity(input, schema, unvisit, getEntity, cache) {
var entity = getEntity(input, schema);
if ((typeof entity === 'undefined' ? 'undefined' : _typeof(entity)) !== 'object' || entity === null) {
return entity;
}
if (input === undefined || input === null) {
return input;
var id = schema.getId(entity);
if (!cache[schema.key]) {
cache[schema.key] = {};
}
return schema.denormalize(input, unvisit, getDenormalizedEntity);
};
if (!cache[schema.key][id]) {
// Ensure we don't mutate it non-immutable objects
var entityCopy = isImmutable(entity) ? entity : _extends({}, entity);
var getEntity = function getEntity(entityOrId, schemaKey, entities, isImmutable$$1) {
if ((typeof entityOrId === 'undefined' ? 'undefined' : _typeof(entityOrId)) === 'object') {
return entityOrId;
// Need to set this first so that if it is referenced further within the
// denormalization the reference will already exist.
cache[schema.key][id] = entityCopy;
cache[schema.key][id] = schema.denormalize(entityCopy, unvisit);
}
return isImmutable$$1 ? entities.getIn([schemaKey, entityOrId.toString()]) : entities[schemaKey][entityOrId];
return cache[schema.key][id];
};
var getEntities = function getEntities(entities, isImmutable$$1) {
return function (schema, entityOrId) {
var getUnvisit = function getUnvisit(entities) {
var cache = {};
var getEntity = getEntities(entities);
return function unvisit(input, schema) {
if ((typeof schema === 'undefined' ? 'undefined' : _typeof(schema)) === 'object' && (!schema.denormalize || typeof schema.denormalize !== 'function')) {
var method = Array.isArray(schema) ? denormalize$2 : _denormalize;
return method(schema, input, unvisit);
}
if (input === undefined || input === null) {
return input;
}
if (schema instanceof EntitySchema) {
return unvisitEntity(input, schema, unvisit, getEntity, cache);
}
return schema.denormalize(input, unvisit);
};
};
var getEntities = function getEntities(entities) {
var isImmutable$$1 = isImmutable(entities);
return function (entityOrId, schema) {
var schemaKey = schema.key;
return getEntity(entityOrId, schemaKey, entities, isImmutable$$1);
if ((typeof entityOrId === 'undefined' ? 'undefined' : _typeof(entityOrId)) === 'object') {
return entityOrId;
}
return isImmutable$$1 ? entities.getIn([schemaKey, entityOrId.toString()]) : entities[schemaKey][entityOrId];
};

@@ -657,5 +683,3 @@ };

var isImmutable$$1 = isImmutable(entities);
var getDenormalizedEntity = getEntities(entities, isImmutable$$1);
return unvisit(input, schema, getDenormalizedEntity);
return getUnvisit(entities)(input, schema);
};

@@ -662,0 +686,0 @@

@@ -1,1 +0,1 @@

"use strict";function isImmutable(e){return!(!e||!(e.hasOwnProperty("__ownerID")||e._map&&e._map.hasOwnProperty("__ownerID")))}function denormalizeImmutable(e,t,n,r){return Object.keys(e).reduce(function(t,i){var o=""+i;return t.has(o)?t.set(o,n(t.get(o),e[o],r)):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}}(),defineProperty=function(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e},_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},get$1=function e(t,n,r){null===t&&(t=Function.prototype);var i=Object.getOwnPropertyDescriptor(t,n);if(void 0===i){var o=Object.getPrototypeOf(t);return null===o?void 0:e(o,n,r)}if("value"in i)return i.value;var a=i.get;if(void 0!==a)return a.call(r)},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},set=function e(t,n,r,i){var o=Object.getOwnPropertyDescriptor(t,n);if(void 0===o){var a=Object.getPrototypeOf(t);null!==a&&e(a,n,r,i)}else if("value"in o&&o.writable)o.value=r;else{var u=o.set;void 0!==u&&u.call(i,r)}return r},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 createClass(e,[{key:"define",value:function(e){this.schema=Object.keys(e).reduce(function(t,n){var r=e[n];return _extends({},t,defineProperty({},n,r))},this.schema||{})}},{key:"getId",value:function(e,t,n){return this._getId(e,t,n)}},{key:"merge",value:function(e,t){return this._mergeStrategy(e,t)}},{key:"normalize",value: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)}},{key:"denormalize",value:function(e,t,n){var r=this,i=n(this,e);if("object"!==("undefined"==typeof i?"undefined":_typeof(i))||null===i)return i;if(isImmutable(i))return denormalizeImmutable(this.schema,i,t,n);var o=_extends({},i);return Object.keys(this.schema).forEach(function(e){if(o.hasOwnProperty(e)){var i=r.schema[e];o[e]=t(o[e],i,n)}}),o}},{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 createClass(e,[{key:"define",value:function(e){this.schema=e}},{key:"getSchemaAttribute",value:function(e,t,n){return!this.isSingleSchema&&this._schemaAttribute(e,t,n)}},{key:"inferSchema",value:function(e,t,n){if(this.isSingleSchema)return this.schema;var r=this.getSchemaAttribute(e,t,n);return this.schema[r]}},{key:"normalizeValue",value: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||void 0===a||null===a?a:{id:a,schema:this.getSchemaAttribute(e,t,n)}}},{key:"denormalizeValue",value:function(e,t,n){if(!this.isSingleSchema&&!e.schema)return e;var r=this.isSingleSchema?this.schema:this.schema[e.schema];return t(e.id||e,r,n)}},{key:"isSingleSchema",get:function(){return!this._schemaAttribute}}]),e}(),UnionSchema=function(e){function t(e,n){if(classCallCheck(this,t),!n)throw new Error('Expected option "schemaAttribute" not found on UnionSchema.');return possibleConstructorReturn(this,(t.__proto__||Object.getPrototypeOf(t)).call(this,e,n))}return inherits(t,e),createClass(t,[{key:"normalize",value:function(e,t,n,r,i){return this.normalizeValue(e,t,n,r,i)}},{key:"denormalize",value:function(e,t,n){return this.denormalizeValue(e,t,n)}}]),t}(PolymorphicSchema),ValuesSchema=function(e){function t(){return classCallCheck(this,t),possibleConstructorReturn(this,(t.__proto__||Object.getPrototypeOf(t)).apply(this,arguments))}return inherits(t,e),createClass(t,[{key:"normalize",value:function(e,t,n,r,i){var o=this;return Object.keys(e).reduce(function(t,n,a){var u=e[n];return void 0!==u&&null!==u?_extends({},t,defineProperty({},n,o.normalizeValue(u,e,n,r,i))):t},{})}},{key:"denormalize",value:function(e,t,n){var r=this;return Object.keys(e).reduce(function(i,o){var a=e[o];return _extends({},i,defineProperty({},o,r.denormalizeValue(a,t,n)))},{})}}]),t}(PolymorphicSchema),validateSchema=function(e){var t=Array.isArray(e);if(t&&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$2=function(e,t,n,r,i,o){e=validateSchema(e);var a=getValues(t);return a.map(function(t,a){return i(t,n,r,e,o)})},denormalize$2=function(e,t,n,r){return e=validateSchema(e),Array.isArray(t)?t.map(function(t){return n(t,e,r)}):t},ArraySchema=function(e){function t(){return classCallCheck(this,t),possibleConstructorReturn(this,(t.__proto__||Object.getPrototypeOf(t)).apply(this,arguments))}return inherits(t,e),createClass(t,[{key:"normalize",value:function(e,t,n,r,i){var o=this,a=getValues(e);return a.map(function(e,a){return o.normalizeValue(e,t,n,r,i)}).filter(function(e){return void 0!==e&&null!==e})}},{key:"denormalize",value:function(e,t,n){var r=this;return Array.isArray(e)?e.map(function(e){return r.denormalizeValue(e,t,n)}):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);void 0===u||null===u?delete a[n]:a[n]=u}),a},_denormalize=function(e,t,n,r){if(isImmutable(t))return denormalizeImmutable(e,t,n,r);var i=_extends({},t);return Object.keys(e).forEach(function(t){i[t]&&(i[t]=n(i[t],e[t],r))}),i},ObjectSchema=function(){function e(t){classCallCheck(this,e),this.define(t)}return createClass(e,[{key:"define",value:function(e){this.schema=Object.keys(e).reduce(function(t,n){var r=e[n];return _extends({},t,defineProperty({},n,r))},this.schema||{})}},{key:"normalize",value: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))}},{key:"denormalize",value: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){if("object"!==("undefined"==typeof t?"undefined":_typeof(t))||!t)return t;if("object"===("undefined"==typeof i?"undefined":_typeof(i))&&(!i.normalize||"function"!=typeof i.normalize)){var a=Array.isArray(i)?normalize$2:_normalize;return a(i,t,n,r,e,o)}return i.normalize(t,n,r,e,o)},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];c?e[a][u]=t.merge(c,n):e[a][u]=n}},schema={Array:ArraySchema,Entity:EntitySchema,Object:ObjectSchema,Union:UnionSchema,Values:ValuesSchema},normalize$1=function(e,t){if(!e||"object"!==("undefined"==typeof e?"undefined":_typeof(e)))throw new Error('Unexpected input given to normalize. Expected type to be "object", found "'+("undefined"==typeof e?"undefined":_typeof(e))+'".');var n={},r=addEntities(n),i=visit(e,e,null,t,r);return{entities:n,result:i}},unvisit=function e(t,n,r){if("object"===("undefined"==typeof n?"undefined":_typeof(n))&&(!n.denormalize||"function"!=typeof n.denormalize)){var i=Array.isArray(n)?denormalize$2:_denormalize;return i(n,t,e,r)}return void 0===t||null===t?t:n.denormalize(t,e,r)},getEntity=function(e,t,n,r){return"object"===("undefined"==typeof e?"undefined":_typeof(e))?e:r?n.getIn([t,e.toString()]):n[t][e]},getEntities=function(e,t){return function(n,r){var i=n.key;return getEntity(r,i,e,t)}},denormalize$1=function(e,t,n){if(!e)return e;var r=isImmutable(n),i=getEntities(n,r);return unvisit(e,t,i)};exports.schema=schema,exports.normalize=normalize$1,exports.denormalize=denormalize$1;
"use strict";function isImmutable(e){return!(!e||!(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}}(),defineProperty=function(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e},_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},get$1=function e(t,n,r){null===t&&(t=Function.prototype);var i=Object.getOwnPropertyDescriptor(t,n);if(void 0===i){var o=Object.getPrototypeOf(t);return null===o?void 0:e(o,n,r)}if("value"in i)return i.value;var a=i.get;if(void 0!==a)return a.call(r)},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},set=function e(t,n,r,i){var o=Object.getOwnPropertyDescriptor(t,n);if(void 0===o){var a=Object.getPrototypeOf(t);null!==a&&e(a,n,r,i)}else if("value"in o&&o.writable)o.value=r;else{var u=o.set;void 0!==u&&u.call(i,r)}return r},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 createClass(e,[{key:"define",value:function(e){this.schema=Object.keys(e).reduce(function(t,n){var r=e[n];return _extends({},t,defineProperty({},n,r))},this.schema||{})}},{key:"getId",value:function(e,t,n){return this._getId(e,t,n)}},{key:"merge",value:function(e,t){return this._mergeStrategy(e,t)}},{key:"normalize",value: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)}},{key:"denormalize",value: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)}},{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 createClass(e,[{key:"define",value:function(e){this.schema=e}},{key:"getSchemaAttribute",value:function(e,t,n){return!this.isSingleSchema&&this._schemaAttribute(e,t,n)}},{key:"inferSchema",value:function(e,t,n){if(this.isSingleSchema)return this.schema;var r=this.getSchemaAttribute(e,t,n);return this.schema[r]}},{key:"normalizeValue",value: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||void 0===a||null===a?a:{id:a,schema:this.getSchemaAttribute(e,t,n)}}},{key:"denormalizeValue",value:function(e,t){if(!this.isSingleSchema&&!e.schema)return e;var n=this.isSingleSchema?this.schema:this.schema[e.schema];return t(e.id||e,n)}},{key:"isSingleSchema",get:function(){return!this._schemaAttribute}}]),e}(),UnionSchema=function(e){function t(e,n){if(classCallCheck(this,t),!n)throw new Error('Expected option "schemaAttribute" not found on UnionSchema.');return possibleConstructorReturn(this,(t.__proto__||Object.getPrototypeOf(t)).call(this,e,n))}return inherits(t,e),createClass(t,[{key:"normalize",value:function(e,t,n,r,i){return this.normalizeValue(e,t,n,r,i)}},{key:"denormalize",value:function(e,t){return this.denormalizeValue(e,t)}}]),t}(PolymorphicSchema),ValuesSchema=function(e){function t(){return classCallCheck(this,t),possibleConstructorReturn(this,(t.__proto__||Object.getPrototypeOf(t)).apply(this,arguments))}return inherits(t,e),createClass(t,[{key:"normalize",value:function(e,t,n,r,i){var o=this;return Object.keys(e).reduce(function(t,n,a){var u=e[n];return void 0!==u&&null!==u?_extends({},t,defineProperty({},n,o.normalizeValue(u,e,n,r,i))):t},{})}},{key:"denormalize",value:function(e,t){var n=this;return Object.keys(e).reduce(function(r,i){var o=e[i];return _extends({},r,defineProperty({},i,n.denormalizeValue(o,t)))},{})}}]),t}(PolymorphicSchema),validateSchema=function(e){var t=Array.isArray(e);if(t&&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$2=function(e,t,n,r,i,o){e=validateSchema(e);var a=getValues(t);return a.map(function(t,a){return i(t,n,r,e,o)})},denormalize$2=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,(t.__proto__||Object.getPrototypeOf(t)).apply(this,arguments))}return inherits(t,e),createClass(t,[{key:"normalize",value:function(e,t,n,r,i){var o=this,a=getValues(e);return a.map(function(e,a){return o.normalizeValue(e,t,n,r,i)}).filter(function(e){return void 0!==e&&null!==e})}},{key:"denormalize",value: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);void 0===u||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 createClass(e,[{key:"define",value:function(e){this.schema=Object.keys(e).reduce(function(t,n){var r=e[n];return _extends({},t,defineProperty({},n,r))},this.schema||{})}},{key:"normalize",value: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))}},{key:"denormalize",value: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){if("object"!==("undefined"==typeof t?"undefined":_typeof(t))||!t)return t;if("object"===("undefined"==typeof i?"undefined":_typeof(i))&&(!i.normalize||"function"!=typeof i.normalize)){var a=Array.isArray(i)?normalize$2:_normalize;return a(i,t,n,r,e,o)}return i.normalize(t,n,r,e,o)},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];c?e[a][u]=t.merge(c,n):e[a][u]=n}},schema={Array:ArraySchema,Entity:EntitySchema,Object:ObjectSchema,Union:UnionSchema,Values:ValuesSchema},normalize$1=function(e,t){if(!e||"object"!==("undefined"==typeof e?"undefined":_typeof(e)))throw new Error('Unexpected input given to normalize. Expected type to be "object", found "'+("undefined"==typeof e?"undefined":_typeof(e))+'".');var n={},r=addEntities(n),i=visit(e,e,null,t,r);return{entities:n,result:i}},unvisitEntity=function(e,t,n,r,i){var o=r(e,t);if("object"!==("undefined"==typeof o?"undefined":_typeof(o))||null===o)return o;var a=t.getId(o);if(i[t.key]||(i[t.key]={}),!i[t.key][a]){var u=isImmutable(o)?o:_extends({},o);i[t.key][a]=u,i[t.key][a]=t.denormalize(u,n)}return i[t.key][a]},getUnvisit=function(e){var t={},n=getEntities(e);return function e(r,i){if("object"===("undefined"==typeof i?"undefined":_typeof(i))&&(!i.denormalize||"function"!=typeof i.denormalize)){var o=Array.isArray(i)?denormalize$2:_denormalize;return o(i,r,e)}return void 0===r||null===r?r:i instanceof EntitySchema?unvisitEntity(r,i,e,n,t):i.denormalize(r,e)}},getEntities=function(e){var t=isImmutable(e);return function(n,r){var i=r.key;return"object"===("undefined"==typeof n?"undefined":_typeof(n))?n:t?e.getIn([i,n.toString()]):e[i][n]}},denormalize$1=function(e,t,n){return e?getUnvisit(n)(e,t):e};exports.schema=schema,exports.normalize=normalize$1,exports.denormalize=denormalize$1;

@@ -8,2 +8,4 @@ 'use strict';

var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };

@@ -89,28 +91,60 @@

var unvisit = function unvisit(input, schema, getDenormalizedEntity) {
if ((typeof schema === 'undefined' ? 'undefined' : _typeof(schema)) === 'object' && (!schema.denormalize || typeof schema.denormalize !== 'function')) {
var method = Array.isArray(schema) ? ArrayUtils.denormalize : ObjectUtils.denormalize;
return method(schema, input, unvisit, getDenormalizedEntity);
var unvisitEntity = function unvisitEntity(input, schema, unvisit, getEntity, cache) {
var entity = getEntity(input, schema);
if ((typeof entity === 'undefined' ? 'undefined' : _typeof(entity)) !== 'object' || entity === null) {
return entity;
}
if (input === undefined || input === null) {
return input;
var id = schema.getId(entity);
if (!cache[schema.key]) {
cache[schema.key] = {};
}
return schema.denormalize(input, unvisit, getDenormalizedEntity);
};
if (!cache[schema.key][id]) {
// Ensure we don't mutate it non-immutable objects
var entityCopy = ImmutableUtils.isImmutable(entity) ? entity : _extends({}, entity);
var getEntity = function getEntity(entityOrId, schemaKey, entities, isImmutable) {
if ((typeof entityOrId === 'undefined' ? 'undefined' : _typeof(entityOrId)) === 'object') {
return entityOrId;
// Need to set this first so that if it is referenced further within the
// denormalization the reference will already exist.
cache[schema.key][id] = entityCopy;
cache[schema.key][id] = schema.denormalize(entityCopy, unvisit);
}
return isImmutable ? entities.getIn([schemaKey, entityOrId.toString()]) : entities[schemaKey][entityOrId];
return cache[schema.key][id];
};
var getEntities = function getEntities(entities, isImmutable) {
return function (schema, entityOrId) {
var getUnvisit = function getUnvisit(entities) {
var cache = {};
var getEntity = getEntities(entities);
return function unvisit(input, schema) {
if ((typeof schema === 'undefined' ? 'undefined' : _typeof(schema)) === 'object' && (!schema.denormalize || typeof schema.denormalize !== 'function')) {
var method = Array.isArray(schema) ? ArrayUtils.denormalize : ObjectUtils.denormalize;
return method(schema, input, unvisit);
}
if (input === undefined || input === null) {
return input;
}
if (schema instanceof _Entity2.default) {
return unvisitEntity(input, schema, unvisit, getEntity, cache);
}
return schema.denormalize(input, unvisit);
};
};
var getEntities = function getEntities(entities) {
var isImmutable = ImmutableUtils.isImmutable(entities);
return function (entityOrId, schema) {
var schemaKey = schema.key;
return getEntity(entityOrId, schemaKey, entities, isImmutable);
if ((typeof entityOrId === 'undefined' ? 'undefined' : _typeof(entityOrId)) === 'object') {
return entityOrId;
}
return isImmutable ? entities.getIn([schemaKey, entityOrId.toString()]) : entities[schemaKey][entityOrId];
};

@@ -124,5 +158,3 @@ };

var isImmutable = ImmutableUtils.isImmutable(entities);
var getDenormalizedEntity = getEntities(entities, isImmutable);
return unvisit(input, schema, getDenormalizedEntity);
return getUnvisit(entities)(input, schema);
};

@@ -49,6 +49,6 @@ 'use strict';

var denormalize = exports.denormalize = function denormalize(schema, input, unvisit, getDenormalizedEntity) {
var denormalize = exports.denormalize = function denormalize(schema, input, unvisit) {
schema = validateSchema(schema);
return Array.isArray(input) ? input.map(function (entityOrId) {
return unvisit(entityOrId, schema, getDenormalizedEntity);
return input && input.map ? input.map(function (entityOrId) {
return unvisit(entityOrId, schema);
}) : input;

@@ -81,7 +81,7 @@ };

key: 'denormalize',
value: function denormalize(input, unvisit, getDenormalizedEntity) {
value: function denormalize(input, unvisit) {
var _this3 = this;
return Array.isArray(input) ? input.map(function (value) {
return _this3.denormalizeValue(value, unvisit, getDenormalizedEntity);
return input && input.map ? input.map(function (value) {
return _this3.denormalizeValue(value, unvisit);
}) : input;

@@ -88,0 +88,0 @@ }

@@ -96,22 +96,16 @@ 'use strict';

key: 'denormalize',
value: function denormalize(entityOrId, unvisit, getDenormalizedEntity) {
value: function denormalize(entity, unvisit) {
var _this2 = this;
var entity = getDenormalizedEntity(this, entityOrId);
if ((typeof entity === 'undefined' ? 'undefined' : _typeof(entity)) !== 'object' || entity === null) {
return entity;
}
if (ImmutableUtils.isImmutable(entity)) {
return ImmutableUtils.denormalizeImmutable(this.schema, entity, unvisit, getDenormalizedEntity);
return ImmutableUtils.denormalizeImmutable(this.schema, entity, unvisit);
}
var processedEntity = _extends({}, entity);
Object.keys(this.schema).forEach(function (key) {
if (processedEntity.hasOwnProperty(key)) {
if (entity.hasOwnProperty(key)) {
var schema = _this2.schema[key];
processedEntity[key] = unvisit(processedEntity[key], schema, getDenormalizedEntity);
entity[key] = unvisit(entity[key], schema);
}
});
return processedEntity;
return entity;
}

@@ -118,0 +112,0 @@ }, {

@@ -35,3 +35,3 @@ 'use strict';

*/
function denormalizeImmutable(schema, input, unvisit, getDenormalizedEntity) {
function denormalizeImmutable(schema, input, unvisit) {
return Object.keys(schema).reduce(function (object, key) {

@@ -43,3 +43,3 @@ // Immutable maps cast keys to strings on write so we need to ensure

if (object.has(stringKey)) {
return object.set(stringKey, unvisit(object.get(stringKey), schema[stringKey], getDenormalizedEntity));
return object.set(stringKey, unvisit(object.get(stringKey), schema[stringKey]));
} else {

@@ -46,0 +46,0 @@ return object;

@@ -37,5 +37,5 @@ 'use strict';

exports.normalize = _normalize;
var _denormalize = function _denormalize(schema, input, unvisit, getDenormalizedEntity) {
var _denormalize = function _denormalize(schema, input, unvisit) {
if (ImmutableUtils.isImmutable(input)) {
return ImmutableUtils.denormalizeImmutable(schema, input, unvisit, getDenormalizedEntity);
return ImmutableUtils.denormalizeImmutable(schema, input, unvisit);
}

@@ -46,3 +46,3 @@

if (object[key]) {
object[key] = unvisit(object[key], schema[key], getDenormalizedEntity);
object[key] = unvisit(object[key], schema[key]);
}

@@ -49,0 +49,0 @@ });

@@ -55,3 +55,3 @@ 'use strict';

key: 'denormalizeValue',
value: function denormalizeValue(value, unvisit, getDenormalizedEntity) {
value: function denormalizeValue(value, unvisit) {
if (!this.isSingleSchema && !value.schema) {

@@ -61,3 +61,3 @@ return value;

var schema = this.isSingleSchema ? this.schema : this.schema[value.schema];
return unvisit(value.id || value, schema, getDenormalizedEntity);
return unvisit(value.id || value, schema);
}

@@ -64,0 +64,0 @@ }, {

@@ -40,4 +40,4 @@ 'use strict';

key: 'denormalize',
value: function denormalize(input, unvisit, getDenormalizedEntity) {
return this.denormalizeValue(input, unvisit, getDenormalizedEntity);
value: function denormalize(input, unvisit) {
return this.denormalizeValue(input, unvisit);
}

@@ -44,0 +44,0 @@ }]);

@@ -46,3 +46,3 @@ 'use strict';

key: 'denormalize',
value: function denormalize(input, unvisit, getDenormalizedEntity) {
value: function denormalize(input, unvisit) {
var _this3 = this;

@@ -52,3 +52,3 @@

var entityOrId = input[key];
return _extends({}, output, _defineProperty({}, key, _this3.denormalizeValue(entityOrId, unvisit, getDenormalizedEntity)));
return _extends({}, output, _defineProperty({}, key, _this3.denormalizeValue(entityOrId, unvisit)));
}, {});

@@ -55,0 +55,0 @@ }

{
"name": "normalizr",
"version": "3.2.1",
"version": "3.2.2",
"description": "Normalizes and denormalizes JSON according to schema for Redux and Flux applications",

@@ -5,0 +5,0 @@ "bugs": {

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