@rest-hooks/normalizr
Advanced tools
Comparing version 4.3.3 to 5.0.0
@@ -6,2 +6,26 @@ # Change Log | ||
## [5.0.0](https://github.com/coinbase/rest-hooks/compare/@rest-hooks/normalizr@4.3.3...@rest-hooks/normalizr@5.0.0) (2020-01-29) | ||
### ⚠ 💥 BREAKING CHANGES | ||
* Remove denormalizeAndTracked | ||
### 💅 Enhancement | ||
* Only export one denormalize ([3f77da2](https://github.com/coinbase/rest-hooks/commit/3f77da206c1e4bc4065b98c8bdcfcfa3693586b7)) | ||
* Use minified build for es modules ([a1a760c](https://github.com/coinbase/rest-hooks/commit/a1a760c74f5f389951ec1c827403e02bf5fb2442)) | ||
### 🐛 Bug Fix | ||
* **schemas:** don't use schema to attribute mapping on singular array schemas ([a39d3e5](https://github.com/coinbase/rest-hooks/commit/a39d3e5402adfaef4651e0e8efeea0f1587186d5)) | ||
### 🏠 Internal | ||
* Upgrade jest to 25 ([#253](https://github.com/coinbase/rest-hooks/issues/253)) ([745a253](https://github.com/coinbase/rest-hooks/commit/745a2532cb1ec3471f1fcb9c90fd87ffb55d6d1a)) | ||
### [4.3.3](https://github.com/coinbase/rest-hooks/compare/@rest-hooks/normalizr@4.3.2...@rest-hooks/normalizr@4.3.3) (2020-01-27) | ||
@@ -8,0 +32,0 @@ |
@@ -52,2 +52,8 @@ define(['exports'], function (exports) { 'use strict'; | ||
function _inheritsLoose(subClass, superClass) { | ||
subClass.prototype = Object.create(superClass.prototype); | ||
subClass.prototype.constructor = subClass; | ||
subClass.__proto__ = superClass; | ||
} | ||
function _defineProperties(target, props) { | ||
@@ -69,143 +75,2 @@ for (var i = 0; i < props.length; i++) { | ||
var getDefaultGetId = function getDefaultGetId(idAttribute) { | ||
return function (input) { | ||
return isImmutable(input) ? input.get(idAttribute) : input[idAttribute]; | ||
}; | ||
}; | ||
var EntitySchema = | ||
/*#__PURE__*/ | ||
function () { | ||
function EntitySchema(key, definition, options) { | ||
if (definition === void 0) { | ||
definition = {}; | ||
} | ||
if (options === void 0) { | ||
options = {}; | ||
} | ||
if (!key || typeof key !== 'string') { | ||
throw new Error("Expected a string key for Entity, but found " + key + "."); | ||
} | ||
var _options = options, | ||
_options$idAttribute = _options.idAttribute, | ||
idAttribute = _options$idAttribute === void 0 ? 'id' : _options$idAttribute, | ||
_options$mergeStrateg = _options.mergeStrategy, | ||
mergeStrategy = _options$mergeStrateg === void 0 ? function (entityA, entityB) { | ||
return Object.assign({}, entityA, {}, entityB); | ||
} : _options$mergeStrateg, | ||
_options$processStrat = _options.processStrategy, | ||
processStrategy = _options$processStrat === void 0 ? function (input) { | ||
return Object.assign({}, input); | ||
} : _options$processStrat; | ||
this._key = key; | ||
this._getId = typeof idAttribute === 'function' ? idAttribute : getDefaultGetId(idAttribute); | ||
this._idAttribute = idAttribute; | ||
this._mergeStrategy = mergeStrategy; | ||
this._processStrategy = processStrategy; | ||
this.define(definition); | ||
} | ||
var _proto = EntitySchema.prototype; | ||
_proto.define = function define(definition) { | ||
this.schema = Object.keys(definition).reduce(function (entitySchema, key) { | ||
var _Object$assign; | ||
var schema = definition[key]; | ||
return Object.assign({}, entitySchema, (_Object$assign = {}, _Object$assign[key] = schema, _Object$assign)); | ||
}, this.schema || {}); | ||
}; | ||
_proto.getId = function getId(input, parent, key) { | ||
return this._getId(input, parent, key); | ||
}; | ||
_proto.merge = function merge(entityA, entityB) { | ||
return this._mergeStrategy(entityA, entityB); | ||
}; | ||
_proto.normalize = function normalize(input, parent, key, visit, addEntity, visitedEntities) { | ||
var _this = this; | ||
var id = this.getId(input, parent, key); | ||
var entityType = this.key; | ||
if (!(entityType in visitedEntities)) { | ||
visitedEntities[entityType] = {}; | ||
} | ||
if (!(id in visitedEntities[entityType])) { | ||
visitedEntities[entityType][id] = []; | ||
} | ||
if (visitedEntities[entityType][id].some(function (entity) { | ||
return entity === input; | ||
})) { | ||
return id; | ||
} | ||
visitedEntities[entityType][id].push(input); | ||
var processedEntity = this._processStrategy(input, parent, key); | ||
Object.keys(this.schema).forEach(function (key) { | ||
if (Object.hasOwnProperty.call(processedEntity, key) && typeof processedEntity[key] === 'object') { | ||
var schema = _this.schema[key]; | ||
processedEntity[key] = visit(processedEntity[key], processedEntity, key, schema, addEntity, visitedEntities); | ||
} | ||
}); | ||
addEntity(this, processedEntity, input, parent, key); | ||
return id; | ||
}; | ||
_proto.denormalize = function denormalize(entity, unvisit) { | ||
var _this2 = this; | ||
if (isImmutable(entity)) { | ||
return denormalizeImmutable(this.schema, entity, unvisit); | ||
} | ||
var found = true; | ||
Object.keys(this.schema).forEach(function (key) { | ||
var schema = _this2.schema[key]; | ||
var _unvisit = unvisit(entity[key], schema), | ||
value = _unvisit[0], | ||
foundItem = _unvisit[1]; | ||
if (!foundItem) { | ||
found = false; | ||
} | ||
if (Object.hasOwnProperty.call(entity, key)) { | ||
entity[key] = value; | ||
} | ||
}); | ||
return [entity, found]; | ||
}; | ||
_createClass(EntitySchema, [{ | ||
key: "key", | ||
get: function get() { | ||
return this._key; | ||
} | ||
}, { | ||
key: "idAttribute", | ||
get: function get() { | ||
return this._idAttribute; | ||
} | ||
}]); | ||
return EntitySchema; | ||
}(); | ||
function _inheritsLoose(subClass, superClass) { | ||
subClass.prototype = Object.create(superClass.prototype); | ||
subClass.prototype.constructor = subClass; | ||
subClass.__proto__ = superClass; | ||
} | ||
var PolymorphicSchema = | ||
@@ -264,3 +129,3 @@ /*#__PURE__*/ | ||
var id = isImmutable(value) ? value.get('id') : value.id; | ||
var id = this.isSingleSchema ? undefined : isImmutable(value) ? value.get('id') : value.id; | ||
var schema = this.isSingleSchema ? this.schema : this.schema[schemaKey]; | ||
@@ -280,74 +145,2 @@ return unvisit(id || value, schema); | ||
var UnionSchema = | ||
/*#__PURE__*/ | ||
function (_PolymorphicSchema) { | ||
_inheritsLoose(UnionSchema, _PolymorphicSchema); | ||
function UnionSchema(definition, schemaAttribute) { | ||
if (!schemaAttribute) { | ||
throw new Error('Expected option "schemaAttribute" not found on UnionSchema.'); | ||
} | ||
return _PolymorphicSchema.call(this, definition, schemaAttribute) || this; | ||
} | ||
var _proto = UnionSchema.prototype; | ||
_proto.normalize = function normalize(input, parent, key, visit, addEntity, visitedEntities) { | ||
return this.normalizeValue(input, parent, key, visit, addEntity, visitedEntities); | ||
}; | ||
_proto.denormalize = function denormalize(input, unvisit) { | ||
return this.denormalizeValue(input, unvisit); | ||
}; | ||
return UnionSchema; | ||
}(PolymorphicSchema); | ||
var ValuesSchema = | ||
/*#__PURE__*/ | ||
function (_PolymorphicSchema) { | ||
_inheritsLoose(ValuesSchema, _PolymorphicSchema); | ||
function ValuesSchema() { | ||
return _PolymorphicSchema.apply(this, arguments) || this; | ||
} | ||
var _proto = ValuesSchema.prototype; | ||
_proto.normalize = function normalize(input, parent, key, visit, addEntity, visitedEntities) { | ||
var _this = this; | ||
return Object.keys(input).reduce(function (output, key, index) { | ||
var _Object$assign; | ||
var value = input[key]; | ||
return value !== undefined && value !== null ? Object.assign({}, output, (_Object$assign = {}, _Object$assign[key] = _this.normalizeValue(value, input, key, visit, addEntity, visitedEntities), _Object$assign)) : output; | ||
}, {}); | ||
}; | ||
_proto.denormalize = function denormalize(input, unvisit) { | ||
var _this2 = this; | ||
var found = true; | ||
return [Object.keys(input).reduce(function (output, key) { | ||
var _Object$assign2; | ||
var entityOrId = input[key]; | ||
var _this2$denormalizeVal = _this2.denormalizeValue(entityOrId, unvisit), | ||
value = _this2$denormalizeVal[0], | ||
foundItem = _this2$denormalizeVal[1]; | ||
if (!foundItem) { | ||
found = false; | ||
} | ||
return Object.assign({}, output, (_Object$assign2 = {}, _Object$assign2[key] = value, _Object$assign2)); | ||
}, {}), found]; | ||
}; | ||
return ValuesSchema; | ||
}(PolymorphicSchema); | ||
var validateSchema = function validateSchema(definition) { | ||
@@ -521,2 +314,304 @@ var isArray = Array.isArray(definition); | ||
var unvisitEntity = function unvisitEntity(id, schema, unvisit, getEntity, cache) { | ||
var entity = getEntity(id, schema); | ||
if (typeof entity !== 'object' || entity === null) { | ||
return [entity, false]; | ||
} | ||
if (!cache[schema.key]) { | ||
cache[schema.key] = {}; | ||
} | ||
var found = true; | ||
if (!cache[schema.key][id]) { | ||
// Ensure we don't mutate it non-immutable objects | ||
var entityCopy = isImmutable(entity) ? entity : Object.assign({}, entity); // 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; | ||
var _schema$denormalize = schema.denormalize(entityCopy, unvisit); | ||
cache[schema.key][id] = _schema$denormalize[0]; | ||
found = _schema$denormalize[1]; | ||
} | ||
return [cache[schema.key][id], found]; | ||
}; | ||
var getUnvisit = function getUnvisit(entities) { | ||
var cache = {}; | ||
var getEntity = getEntities(entities); | ||
return [function unvisit(input, schema) { | ||
if (!schema) return [input, true]; | ||
if (typeof schema === 'object' && (!schema.denormalize || typeof schema.denormalize !== 'function')) { | ||
var method = Array.isArray(schema) ? denormalize : _denormalize; | ||
return method(schema, input, unvisit); | ||
} // null is considered intentional, thus always 'found' as true | ||
if (input === null) { | ||
return [input, true]; | ||
} | ||
if (typeof schema.getId === 'function' && typeof schema.normalize === 'function') { | ||
// unvisitEntity just can't handle undefined | ||
if (input === undefined) { | ||
return [input, false]; | ||
} | ||
return unvisitEntity(input, schema, unvisit, getEntity, cache); | ||
} | ||
if (typeof schema.denormalize === 'function') { | ||
return schema.denormalize(input, unvisit); | ||
} | ||
return [input, true]; | ||
}, cache]; | ||
}; | ||
var getEntities = function getEntities(entities) { | ||
var isImmutable$1 = isImmutable(entities); | ||
return function (entityOrId, schema) { | ||
var schemaKey = schema.key; | ||
if (typeof entityOrId === 'object') { | ||
return entityOrId; | ||
} | ||
if (isImmutable$1) { | ||
return entities.getIn([schemaKey, entityOrId.toString()]); | ||
} | ||
return entities[schemaKey] && entities[schemaKey][entityOrId]; | ||
}; | ||
}; | ||
var denormalize$1 = function denormalize(input, schema, entities) { | ||
/* istanbul ignore next */ | ||
// eslint-disable-next-line no-undef | ||
if (process.env.NODE_ENV !== 'production' && schema === undefined) throw new Error('shema needed'); | ||
if (typeof input !== 'undefined') { | ||
var _getUnvisit = getUnvisit(entities), | ||
unvisit = _getUnvisit[0], | ||
cache = _getUnvisit[1]; | ||
return [].concat(unvisit(input, schema), [cache]); | ||
} | ||
return [undefined, false, {}]; | ||
}; | ||
var getDefaultGetId = function getDefaultGetId(idAttribute) { | ||
return function (input) { | ||
return isImmutable(input) ? input.get(idAttribute) : input[idAttribute]; | ||
}; | ||
}; | ||
var EntitySchema = | ||
/*#__PURE__*/ | ||
function () { | ||
function EntitySchema(key, definition, options) { | ||
if (definition === void 0) { | ||
definition = {}; | ||
} | ||
if (options === void 0) { | ||
options = {}; | ||
} | ||
if (!key || typeof key !== 'string') { | ||
throw new Error("Expected a string key for Entity, but found " + key + "."); | ||
} | ||
var _options = options, | ||
_options$idAttribute = _options.idAttribute, | ||
idAttribute = _options$idAttribute === void 0 ? 'id' : _options$idAttribute, | ||
_options$mergeStrateg = _options.mergeStrategy, | ||
mergeStrategy = _options$mergeStrateg === void 0 ? function (entityA, entityB) { | ||
return Object.assign({}, entityA, {}, entityB); | ||
} : _options$mergeStrateg, | ||
_options$processStrat = _options.processStrategy, | ||
processStrategy = _options$processStrat === void 0 ? function (input) { | ||
return Object.assign({}, input); | ||
} : _options$processStrat; | ||
this._key = key; | ||
this._getId = typeof idAttribute === 'function' ? idAttribute : getDefaultGetId(idAttribute); | ||
this._idAttribute = idAttribute; | ||
this._mergeStrategy = mergeStrategy; | ||
this._processStrategy = processStrategy; | ||
this.define(definition); | ||
} | ||
var _proto = EntitySchema.prototype; | ||
_proto.define = function define(definition) { | ||
this.schema = Object.keys(definition).reduce(function (entitySchema, key) { | ||
var _Object$assign; | ||
var schema = definition[key]; | ||
return Object.assign({}, entitySchema, (_Object$assign = {}, _Object$assign[key] = schema, _Object$assign)); | ||
}, this.schema || {}); | ||
}; | ||
_proto.getId = function getId(input, parent, key) { | ||
return this._getId(input, parent, key); | ||
}; | ||
_proto.merge = function merge(entityA, entityB) { | ||
return this._mergeStrategy(entityA, entityB); | ||
}; | ||
_proto.normalize = function normalize(input, parent, key, visit, addEntity, visitedEntities) { | ||
var _this = this; | ||
var id = this.getId(input, parent, key); | ||
var entityType = this.key; | ||
if (!(entityType in visitedEntities)) { | ||
visitedEntities[entityType] = {}; | ||
} | ||
if (!(id in visitedEntities[entityType])) { | ||
visitedEntities[entityType][id] = []; | ||
} | ||
if (visitedEntities[entityType][id].some(function (entity) { | ||
return entity === input; | ||
})) { | ||
return id; | ||
} | ||
visitedEntities[entityType][id].push(input); | ||
var processedEntity = this._processStrategy(input, parent, key); | ||
Object.keys(this.schema).forEach(function (key) { | ||
if (Object.hasOwnProperty.call(processedEntity, key) && typeof processedEntity[key] === 'object') { | ||
var schema = _this.schema[key]; | ||
processedEntity[key] = visit(processedEntity[key], processedEntity, key, schema, addEntity, visitedEntities); | ||
} | ||
}); | ||
addEntity(this, processedEntity, input, parent, key); | ||
return id; | ||
}; | ||
_proto.denormalize = function denormalize(entity, unvisit) { | ||
var _this2 = this; | ||
if (isImmutable(entity)) { | ||
return denormalizeImmutable(this.schema, entity, unvisit); | ||
} | ||
var found = true; | ||
Object.keys(this.schema).forEach(function (key) { | ||
var schema = _this2.schema[key]; | ||
var _unvisit = unvisit(entity[key], schema), | ||
value = _unvisit[0], | ||
foundItem = _unvisit[1]; | ||
if (!foundItem) { | ||
found = false; | ||
} | ||
if (Object.hasOwnProperty.call(entity, key)) { | ||
entity[key] = value; | ||
} | ||
}); | ||
return [entity, found]; | ||
}; | ||
_createClass(EntitySchema, [{ | ||
key: "key", | ||
get: function get() { | ||
return this._key; | ||
} | ||
}, { | ||
key: "idAttribute", | ||
get: function get() { | ||
return this._idAttribute; | ||
} | ||
}]); | ||
return EntitySchema; | ||
}(); | ||
var UnionSchema = | ||
/*#__PURE__*/ | ||
function (_PolymorphicSchema) { | ||
_inheritsLoose(UnionSchema, _PolymorphicSchema); | ||
function UnionSchema(definition, schemaAttribute) { | ||
if (!schemaAttribute) { | ||
throw new Error('Expected option "schemaAttribute" not found on UnionSchema.'); | ||
} | ||
return _PolymorphicSchema.call(this, definition, schemaAttribute) || this; | ||
} | ||
var _proto = UnionSchema.prototype; | ||
_proto.normalize = function normalize(input, parent, key, visit, addEntity, visitedEntities) { | ||
return this.normalizeValue(input, parent, key, visit, addEntity, visitedEntities); | ||
}; | ||
_proto.denormalize = function denormalize(input, unvisit) { | ||
return this.denormalizeValue(input, unvisit); | ||
}; | ||
return UnionSchema; | ||
}(PolymorphicSchema); | ||
var ValuesSchema = | ||
/*#__PURE__*/ | ||
function (_PolymorphicSchema) { | ||
_inheritsLoose(ValuesSchema, _PolymorphicSchema); | ||
function ValuesSchema() { | ||
return _PolymorphicSchema.apply(this, arguments) || this; | ||
} | ||
var _proto = ValuesSchema.prototype; | ||
_proto.normalize = function normalize(input, parent, key, visit, addEntity, visitedEntities) { | ||
var _this = this; | ||
return Object.keys(input).reduce(function (output, key, index) { | ||
var _Object$assign; | ||
var value = input[key]; | ||
return value !== undefined && value !== null ? Object.assign({}, output, (_Object$assign = {}, _Object$assign[key] = _this.normalizeValue(value, input, key, visit, addEntity, visitedEntities), _Object$assign)) : output; | ||
}, {}); | ||
}; | ||
_proto.denormalize = function denormalize(input, unvisit) { | ||
var _this2 = this; | ||
var found = true; | ||
return [Object.keys(input).reduce(function (output, key) { | ||
var _Object$assign2; | ||
var entityOrId = input[key]; | ||
var _this2$denormalizeVal = _this2.denormalizeValue(entityOrId, unvisit), | ||
value = _this2$denormalizeVal[0], | ||
foundItem = _this2$denormalizeVal[1]; | ||
if (!foundItem) { | ||
found = false; | ||
} | ||
return Object.assign({}, output, (_Object$assign2 = {}, _Object$assign2[key] = value, _Object$assign2)); | ||
}, {}), found]; | ||
}; | ||
return ValuesSchema; | ||
}(PolymorphicSchema); | ||
var visit = function visit(value, parent, key, schema, addEntity, visitedEntities) { | ||
@@ -628,102 +723,3 @@ if (typeof value !== 'object' || !value || !schema) { | ||
var unvisitEntity = function unvisitEntity(id, schema, unvisit, getEntity, cache) { | ||
var entity = getEntity(id, schema); | ||
if (typeof entity !== 'object' || entity === null) { | ||
return [entity, false]; | ||
} | ||
if (!cache[schema.key]) { | ||
cache[schema.key] = {}; | ||
} | ||
var found = true; | ||
if (!cache[schema.key][id]) { | ||
// Ensure we don't mutate it non-immutable objects | ||
var entityCopy = isImmutable(entity) ? entity : Object.assign({}, entity); // 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; | ||
var _schema$denormalize = schema.denormalize(entityCopy, unvisit); | ||
cache[schema.key][id] = _schema$denormalize[0]; | ||
found = _schema$denormalize[1]; | ||
} | ||
return [cache[schema.key][id], found]; | ||
}; | ||
var getUnvisit = function getUnvisit(entities) { | ||
var cache = {}; | ||
var getEntity = getEntities(entities); | ||
return [function unvisit(input, schema) { | ||
if (!schema) return [input, true]; | ||
if (typeof schema === 'object' && (!schema.denormalize || typeof schema.denormalize !== 'function')) { | ||
var method = Array.isArray(schema) ? denormalize : _denormalize; | ||
return method(schema, input, unvisit); | ||
} // null is considered intentional, thus always 'found' as true | ||
if (input === null) { | ||
return [input, true]; | ||
} | ||
if (typeof schema.getId === 'function' && typeof schema.normalize === 'function') { | ||
// unvisitEntity just can't handle undefined | ||
if (input === undefined) { | ||
return [input, false]; | ||
} | ||
return unvisitEntity(input, schema, unvisit, getEntity, cache); | ||
} | ||
if (typeof schema.denormalize === 'function') { | ||
return schema.denormalize(input, unvisit); | ||
} | ||
return [input, true]; | ||
}, cache]; | ||
}; | ||
var getEntities = function getEntities(entities) { | ||
var isImmutable$1 = isImmutable(entities); | ||
return function (entityOrId, schema) { | ||
var schemaKey = schema.key; | ||
if (typeof entityOrId === 'object') { | ||
return entityOrId; | ||
} | ||
if (isImmutable$1) { | ||
return entities.getIn([schemaKey, entityOrId.toString()]); | ||
} | ||
return entities[schemaKey] && entities[schemaKey][entityOrId]; | ||
}; | ||
}; | ||
var denormalizeAndTracked = function denormalizeAndTracked(input, schema, entities) { | ||
/* istanbul ignore next */ | ||
// eslint-disable-next-line no-undef | ||
if (process.env.NODE_ENV !== 'production' && schema === undefined) throw new Error('shema needed'); | ||
if (typeof input !== 'undefined') { | ||
var _getUnvisit = getUnvisit(entities), | ||
unvisit = _getUnvisit[0], | ||
cache = _getUnvisit[1]; | ||
return [].concat(unvisit(input, schema), [cache]); | ||
} | ||
return [undefined, false, {}]; | ||
}; | ||
var denormalize$1 = function denormalize(input, schema, entities) { | ||
return denormalizeAndTracked(input, schema, entities).slice(0, 2); | ||
}; | ||
exports.denormalize = denormalize$1; | ||
exports.denormalizeAndTracked = denormalizeAndTracked; | ||
exports.normalize = normalize$1; | ||
@@ -730,0 +726,0 @@ exports.schema = schema; |
@@ -1,1 +0,1 @@ | ||
define(["exports"],(function(e){"use strict";function t(e){return!(!e||"function"!=typeof e.hasOwnProperty||!(Object.hasOwnProperty.call(e,"__ownerID")||e._map&&Object.hasOwnProperty.call(e._map,"__ownerID")))}function r(e,t,r){let n=!0;return[Object.keys(e).reduce((t,i)=>{const s=""+i,[o,c]=r(t.get(s),e[s]);return c||(n=!1),t.has(s)?t.set(s,o):t},t),n]}class n{constructor(e,t){t&&(this._schemaAttribute="string"==typeof t?e=>e[t]:t),this.define(e)}get isSingleSchema(){return!this._schemaAttribute}define(e){this.schema=e}getSchemaAttribute(e,t,r){return!this.isSingleSchema&&this._schemaAttribute(e,t,r)}inferSchema(e,t,r){if(this.isSingleSchema)return this.schema;const n=this.getSchemaAttribute(e,t,r);return this.schema[n]}normalizeValue(e,t,r,n,i,s){const o=this.inferSchema(e,t,r);if(!o)return e;const c=n(e,t,r,o,i,s);return this.isSingleSchema||null==c?c:{id:c,schema:this.getSchemaAttribute(e,t,r)}}denormalizeValue(e,r){const n=t(e)?e.get("schema"):e.schema;return this.isSingleSchema||n?r((t(e)?e.get("id"):e.id)||e,this.isSingleSchema?this.schema:this.schema[n]):[e,!0]}}const i=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]},s=e=>Array.isArray(e)?e:Object.keys(e).map(t=>e[t]),o=(e,t,r,n,o,c,a)=>(e=i(e),s(t).map((t,i)=>o(t,r,n,e,c,a))),c=(e,t,r)=>{e=i(e);let n=!0;return void 0===t&&e&&([,n]=r(void 0,e)),[t&&t.map?t.map(t=>r(t,e)).filter(([,e])=>e).map(([e])=>e):t,n]};const a=(e,t,r,n,i,s,o)=>{const c=Object.assign({},t);return Object.keys(e).forEach(r=>{const n=e[r],a=i(t[r],t,r,n,s,o);null==a?delete c[r]:c[r]=a}),c},u=(e,n,i)=>{if(t(n))return r(e,n,i);const s=Object.assign({},n);let o=!0;return Object.keys(e).forEach(t=>{const[r,n]=i(s[t],e[t]);void 0!==s[t]&&(s[t]=r),n||(o=!1)}),[s,o]};const h=(e,t,r,n,i,s)=>{if("object"!=typeof e||!e||!n)return e;if("object"==typeof n&&(!n.normalize||"function"!=typeof n.normalize)){return(Array.isArray(n)?o:a)(n,e,t,r,h,i,s)}return n.normalize(e,t,r,h,i,s)},l={Array:class extends n{normalize(e,t,r,n,i,o){return s(e).map((e,s)=>this.normalizeValue(e,t,r,n,i,o)).filter(e=>null!=e)}denormalize(e,t){let r=!0;return void 0===e&&this.schema&&([,r]=t(void 0,this.schema)),[e&&e.map?e.map(e=>this.denormalizeValue(e,t)).filter(([,e])=>e).map(([e])=>e):e,r]}},Entity:class{constructor(e,r={},n={}){if(!e||"string"!=typeof e)throw new Error("Expected a string key for Entity, but found "+e+".");const{idAttribute:i="id",mergeStrategy:s=((e,t)=>Object.assign({},e,{},t)),processStrategy:o=(e=>Object.assign({},e))}=n;this._key=e,this._getId="function"==typeof i?i:(e=>r=>t(r)?r.get(e):r[e])(i),this._idAttribute=i,this._mergeStrategy=s,this._processStrategy=o,this.define(r)}get key(){return this._key}get idAttribute(){return this._idAttribute}define(e){this.schema=Object.keys(e).reduce((t,r)=>{const n=e[r];return Object.assign({},t,{[r]:n})},this.schema||{})}getId(e,t,r){return this._getId(e,t,r)}merge(e,t){return this._mergeStrategy(e,t)}normalize(e,t,r,n,i,s){const o=this.getId(e,t,r),c=this.key;if(c in s||(s[c]={}),o in s[c]||(s[c][o]=[]),s[c][o].some(t=>t===e))return o;s[c][o].push(e);const a=this._processStrategy(e,t,r);return Object.keys(this.schema).forEach(e=>{if(Object.hasOwnProperty.call(a,e)&&"object"==typeof a[e]){const t=this.schema[e];a[e]=n(a[e],a,e,t,i,s)}}),i(this,a,e,t,r),o}denormalize(e,n){if(t(e))return r(this.schema,e,n);let i=!0;return Object.keys(this.schema).forEach(t=>{const r=this.schema[t],[s,o]=n(e[t],r);o||(i=!1),Object.hasOwnProperty.call(e,t)&&(e[t]=s)}),[e,i]}},Object:class{constructor(e){this.define(e)}define(e){this.schema=Object.keys(e).reduce((t,r)=>{const n=e[r];return Object.assign({},t,{[r]:n})},this.schema||{})}normalize(...e){return a(this.schema,...e)}denormalize(...e){return u(this.schema,...e)}},Union:class extends n{constructor(e,t){if(!t)throw new Error('Expected option "schemaAttribute" not found on UnionSchema.');super(e,t)}normalize(e,t,r,n,i,s){return this.normalizeValue(e,t,r,n,i,s)}denormalize(e,t){return this.denormalizeValue(e,t)}},Values:class extends n{normalize(e,t,r,n,i,s){return Object.keys(e).reduce((t,r,o)=>{const c=e[r];return null!=c?Object.assign({},t,{[r]:this.normalizeValue(c,e,r,n,i,s)}):t},{})}denormalize(e,t){let r=!0;return[Object.keys(e).reduce((n,i)=>{const s=e[i],[o,c]=this.denormalizeValue(s,t);return c||(r=!1),Object.assign({},n,{[i]:o})},{}),r]}}};const m=e=>{const r={},n=d(e);return[function e(i,s){if(!s)return[i,!0];if("object"==typeof s&&(!s.denormalize||"function"!=typeof s.denormalize)){return(Array.isArray(s)?c:u)(s,i,e)}return null===i?[i,!0]:"function"==typeof s.getId&&"function"==typeof s.normalize?void 0===i?[i,!1]:((e,r,n,i,s)=>{const o=i(e,r);if("object"!=typeof o||null===o)return[o,!1];s[r.key]||(s[r.key]={});let c=!0;if(!s[r.key][e]){const i=t(o)?o:Object.assign({},o);s[r.key][e]=i,[s[r.key][e],c]=r.denormalize(i,n)}return[s[r.key][e],c]})(i,s,e,n,r):"function"==typeof s.denormalize?s.denormalize(i,e):[i,!0]},r]},d=e=>{const r=t(e);return(t,n)=>{const i=n.key;return"object"==typeof t?t:r?e.getIn([i,t.toString()]):e[i]&&e[i][t]}},f=(e,t,r)=>{if("production"!==process.env.NODE_ENV&&void 0===t)throw new Error("shema needed");if(void 0!==e){const[n,i]=m(r);return[...n(e,t),i]}return[void 0,!1,{}]};e.denormalize=(e,t,r)=>f(e,t,r).slice(0,2),e.denormalizeAndTracked=f,e.normalize=(e,t)=>{const r=function(e){return["object","function"].includes(typeof e)?"object":typeof e}(t);if(null===e||typeof e!==r)throw new Error('Unexpected input given to normalize. Expected type to be "'+r+'", found "'+(null===e?"null":typeof e)+'".');const n={},i={},s=((e,t)=>(r,n,i,s,o)=>{const c=r.key,a=r.getId(i,s,o);c in e||(e[c]={});const u=e[c][a];if(e[c][a]=u?r.merge(u,n):n,Array.isArray(r.indexes)){const n=e[c][a];c in t||(t[c]={});for(const e of r.indexes){e in t[c]||(t[c][e]={});const r=t[c][e];u&&delete r[u[e]],e in n?r[n[e]]=a:"production"!==process.env.NODE_ENV&&console.warn("Index not found in entity. Indexes must be top-level members of your entity.\nIndex: "+e+"\nEntity: "+JSON.stringify(n,void 0,2))}}})(n,i);return{entities:n,indexes:i,result:h(e,e,void 0,t,s,{})}},e.schema=l,Object.defineProperty(e,"__esModule",{value:!0})})); | ||
define(["exports"],(function(e){"use strict";function t(e){return!(!e||"function"!=typeof e.hasOwnProperty||!(Object.hasOwnProperty.call(e,"__ownerID")||e._map&&Object.hasOwnProperty.call(e._map,"__ownerID")))}function n(e,t,n){let r=!0;return[Object.keys(e).reduce((t,i)=>{const s=""+i,[o,c]=n(t.get(s),e[s]);return c||(r=!1),t.has(s)?t.set(s,o):t},t),r]}class r{constructor(e,t){t&&(this._schemaAttribute="string"==typeof t?e=>e[t]:t),this.define(e)}get isSingleSchema(){return!this._schemaAttribute}define(e){this.schema=e}getSchemaAttribute(e,t,n){return!this.isSingleSchema&&this._schemaAttribute(e,t,n)}inferSchema(e,t,n){if(this.isSingleSchema)return this.schema;const r=this.getSchemaAttribute(e,t,n);return this.schema[r]}normalizeValue(e,t,n,r,i,s){const o=this.inferSchema(e,t,n);if(!o)return e;const c=r(e,t,n,o,i,s);return this.isSingleSchema||null==c?c:{id:c,schema:this.getSchemaAttribute(e,t,n)}}denormalizeValue(e,n){const r=t(e)?e.get("schema"):e.schema;return this.isSingleSchema||r?n((this.isSingleSchema?void 0:t(e)?e.get("id"):e.id)||e,this.isSingleSchema?this.schema:this.schema[r]):[e,!0]}}const i=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]},s=e=>Array.isArray(e)?e:Object.keys(e).map(t=>e[t]),o=(e,t,n,r,o,c,a)=>(e=i(e),s(t).map((t,i)=>o(t,n,r,e,c,a))),c=(e,t,n)=>{e=i(e);let r=!0;return void 0===t&&e&&([,r]=n(void 0,e)),[t&&t.map?t.map(t=>n(t,e)).filter(([,e])=>e).map(([e])=>e):t,r]};const a=(e,t,n,r,i,s,o)=>{const c=Object.assign({},t);return Object.keys(e).forEach(n=>{const r=e[n],a=i(t[n],t,n,r,s,o);null==a?delete c[n]:c[n]=a}),c},u=(e,r,i)=>{if(t(r))return n(e,r,i);const s=Object.assign({},r);let o=!0;return Object.keys(e).forEach(t=>{const[n,r]=i(s[t],e[t]);void 0!==s[t]&&(s[t]=n),r||(o=!1)}),[s,o]};const h=e=>{const n={},r=l(e);return[function e(i,s){if(!s)return[i,!0];if("object"==typeof s&&(!s.denormalize||"function"!=typeof s.denormalize)){return(Array.isArray(s)?c:u)(s,i,e)}return null===i?[i,!0]:"function"==typeof s.getId&&"function"==typeof s.normalize?void 0===i?[i,!1]:((e,n,r,i,s)=>{const o=i(e,n);if("object"!=typeof o||null===o)return[o,!1];s[n.key]||(s[n.key]={});let c=!0;if(!s[n.key][e]){const i=t(o)?o:Object.assign({},o);s[n.key][e]=i,[s[n.key][e],c]=n.denormalize(i,r)}return[s[n.key][e],c]})(i,s,e,r,n):"function"==typeof s.denormalize?s.denormalize(i,e):[i,!0]},n]},l=e=>{const n=t(e);return(t,r)=>{const i=r.key;return"object"==typeof t?t:n?e.getIn([i,t.toString()]):e[i]&&e[i][t]}};const m=(e,t,n,r,i,s)=>{if("object"!=typeof e||!e||!r)return e;if("object"==typeof r&&(!r.normalize||"function"!=typeof r.normalize)){return(Array.isArray(r)?o:a)(r,e,t,n,m,i,s)}return r.normalize(e,t,n,m,i,s)},d={Array:class extends r{normalize(e,t,n,r,i,o){return s(e).map((e,s)=>this.normalizeValue(e,t,n,r,i,o)).filter(e=>null!=e)}denormalize(e,t){let n=!0;return void 0===e&&this.schema&&([,n]=t(void 0,this.schema)),[e&&e.map?e.map(e=>this.denormalizeValue(e,t)).filter(([,e])=>e).map(([e])=>e):e,n]}},Entity:class{constructor(e,n={},r={}){if(!e||"string"!=typeof e)throw new Error("Expected a string key for Entity, but found "+e+".");const{idAttribute:i="id",mergeStrategy:s=((e,t)=>Object.assign({},e,{},t)),processStrategy:o=(e=>Object.assign({},e))}=r;this._key=e,this._getId="function"==typeof i?i:(e=>n=>t(n)?n.get(e):n[e])(i),this._idAttribute=i,this._mergeStrategy=s,this._processStrategy=o,this.define(n)}get key(){return this._key}get idAttribute(){return this._idAttribute}define(e){this.schema=Object.keys(e).reduce((t,n)=>{const r=e[n];return Object.assign({},t,{[n]:r})},this.schema||{})}getId(e,t,n){return this._getId(e,t,n)}merge(e,t){return this._mergeStrategy(e,t)}normalize(e,t,n,r,i,s){const o=this.getId(e,t,n),c=this.key;if(c in s||(s[c]={}),o in s[c]||(s[c][o]=[]),s[c][o].some(t=>t===e))return o;s[c][o].push(e);const a=this._processStrategy(e,t,n);return Object.keys(this.schema).forEach(e=>{if(Object.hasOwnProperty.call(a,e)&&"object"==typeof a[e]){const t=this.schema[e];a[e]=r(a[e],a,e,t,i,s)}}),i(this,a,e,t,n),o}denormalize(e,r){if(t(e))return n(this.schema,e,r);let i=!0;return Object.keys(this.schema).forEach(t=>{const n=this.schema[t],[s,o]=r(e[t],n);o||(i=!1),Object.hasOwnProperty.call(e,t)&&(e[t]=s)}),[e,i]}},Object:class{constructor(e){this.define(e)}define(e){this.schema=Object.keys(e).reduce((t,n)=>{const r=e[n];return Object.assign({},t,{[n]:r})},this.schema||{})}normalize(...e){return a(this.schema,...e)}denormalize(...e){return u(this.schema,...e)}},Union:class extends r{constructor(e,t){if(!t)throw new Error('Expected option "schemaAttribute" not found on UnionSchema.');super(e,t)}normalize(e,t,n,r,i,s){return this.normalizeValue(e,t,n,r,i,s)}denormalize(e,t){return this.denormalizeValue(e,t)}},Values:class extends r{normalize(e,t,n,r,i,s){return Object.keys(e).reduce((t,n,o)=>{const c=e[n];return null!=c?Object.assign({},t,{[n]:this.normalizeValue(c,e,n,r,i,s)}):t},{})}denormalize(e,t){let n=!0;return[Object.keys(e).reduce((r,i)=>{const s=e[i],[o,c]=this.denormalizeValue(s,t);return c||(n=!1),Object.assign({},r,{[i]:o})},{}),n]}}};e.denormalize=(e,t,n)=>{if("production"!==process.env.NODE_ENV&&void 0===t)throw new Error("shema needed");if(void 0!==e){const[r,i]=h(n);return[...r(e,t),i]}return[void 0,!1,{}]},e.normalize=(e,t)=>{const n=function(e){return["object","function"].includes(typeof e)?"object":typeof e}(t);if(null===e||typeof e!==n)throw new Error('Unexpected input given to normalize. Expected type to be "'+n+'", found "'+(null===e?"null":typeof e)+'".');const r={},i={},s=((e,t)=>(n,r,i,s,o)=>{const c=n.key,a=n.getId(i,s,o);c in e||(e[c]={});const u=e[c][a];if(e[c][a]=u?n.merge(u,r):r,Array.isArray(n.indexes)){const r=e[c][a];c in t||(t[c]={});for(const e of n.indexes){e in t[c]||(t[c][e]={});const n=t[c][e];u&&delete n[u[e]],e in r?n[r[e]]=a:"production"!==process.env.NODE_ENV&&console.warn("Index not found in entity. Indexes must be top-level members of your entity.\nIndex: "+e+"\nEntity: "+JSON.stringify(r,void 0,2))}}})(r,i);return{entities:r,indexes:i,result:m(e,e,void 0,t,s,{})}},e.schema=d,Object.defineProperty(e,"__esModule",{value:!0})})); |
@@ -53,2 +53,8 @@ var rest_hooks_normalizr = (function (exports) { | ||
function _inheritsLoose(subClass, superClass) { | ||
subClass.prototype = Object.create(superClass.prototype); | ||
subClass.prototype.constructor = subClass; | ||
subClass.__proto__ = superClass; | ||
} | ||
function _defineProperties(target, props) { | ||
@@ -70,143 +76,2 @@ for (var i = 0; i < props.length; i++) { | ||
var getDefaultGetId = function getDefaultGetId(idAttribute) { | ||
return function (input) { | ||
return isImmutable(input) ? input.get(idAttribute) : input[idAttribute]; | ||
}; | ||
}; | ||
var EntitySchema = | ||
/*#__PURE__*/ | ||
function () { | ||
function EntitySchema(key, definition, options) { | ||
if (definition === void 0) { | ||
definition = {}; | ||
} | ||
if (options === void 0) { | ||
options = {}; | ||
} | ||
if (!key || typeof key !== 'string') { | ||
throw new Error("Expected a string key for Entity, but found " + key + "."); | ||
} | ||
var _options = options, | ||
_options$idAttribute = _options.idAttribute, | ||
idAttribute = _options$idAttribute === void 0 ? 'id' : _options$idAttribute, | ||
_options$mergeStrateg = _options.mergeStrategy, | ||
mergeStrategy = _options$mergeStrateg === void 0 ? function (entityA, entityB) { | ||
return Object.assign({}, entityA, {}, entityB); | ||
} : _options$mergeStrateg, | ||
_options$processStrat = _options.processStrategy, | ||
processStrategy = _options$processStrat === void 0 ? function (input) { | ||
return Object.assign({}, input); | ||
} : _options$processStrat; | ||
this._key = key; | ||
this._getId = typeof idAttribute === 'function' ? idAttribute : getDefaultGetId(idAttribute); | ||
this._idAttribute = idAttribute; | ||
this._mergeStrategy = mergeStrategy; | ||
this._processStrategy = processStrategy; | ||
this.define(definition); | ||
} | ||
var _proto = EntitySchema.prototype; | ||
_proto.define = function define(definition) { | ||
this.schema = Object.keys(definition).reduce(function (entitySchema, key) { | ||
var _Object$assign; | ||
var schema = definition[key]; | ||
return Object.assign({}, entitySchema, (_Object$assign = {}, _Object$assign[key] = schema, _Object$assign)); | ||
}, this.schema || {}); | ||
}; | ||
_proto.getId = function getId(input, parent, key) { | ||
return this._getId(input, parent, key); | ||
}; | ||
_proto.merge = function merge(entityA, entityB) { | ||
return this._mergeStrategy(entityA, entityB); | ||
}; | ||
_proto.normalize = function normalize(input, parent, key, visit, addEntity, visitedEntities) { | ||
var _this = this; | ||
var id = this.getId(input, parent, key); | ||
var entityType = this.key; | ||
if (!(entityType in visitedEntities)) { | ||
visitedEntities[entityType] = {}; | ||
} | ||
if (!(id in visitedEntities[entityType])) { | ||
visitedEntities[entityType][id] = []; | ||
} | ||
if (visitedEntities[entityType][id].some(function (entity) { | ||
return entity === input; | ||
})) { | ||
return id; | ||
} | ||
visitedEntities[entityType][id].push(input); | ||
var processedEntity = this._processStrategy(input, parent, key); | ||
Object.keys(this.schema).forEach(function (key) { | ||
if (Object.hasOwnProperty.call(processedEntity, key) && typeof processedEntity[key] === 'object') { | ||
var schema = _this.schema[key]; | ||
processedEntity[key] = visit(processedEntity[key], processedEntity, key, schema, addEntity, visitedEntities); | ||
} | ||
}); | ||
addEntity(this, processedEntity, input, parent, key); | ||
return id; | ||
}; | ||
_proto.denormalize = function denormalize(entity, unvisit) { | ||
var _this2 = this; | ||
if (isImmutable(entity)) { | ||
return denormalizeImmutable(this.schema, entity, unvisit); | ||
} | ||
var found = true; | ||
Object.keys(this.schema).forEach(function (key) { | ||
var schema = _this2.schema[key]; | ||
var _unvisit = unvisit(entity[key], schema), | ||
value = _unvisit[0], | ||
foundItem = _unvisit[1]; | ||
if (!foundItem) { | ||
found = false; | ||
} | ||
if (Object.hasOwnProperty.call(entity, key)) { | ||
entity[key] = value; | ||
} | ||
}); | ||
return [entity, found]; | ||
}; | ||
_createClass(EntitySchema, [{ | ||
key: "key", | ||
get: function get() { | ||
return this._key; | ||
} | ||
}, { | ||
key: "idAttribute", | ||
get: function get() { | ||
return this._idAttribute; | ||
} | ||
}]); | ||
return EntitySchema; | ||
}(); | ||
function _inheritsLoose(subClass, superClass) { | ||
subClass.prototype = Object.create(superClass.prototype); | ||
subClass.prototype.constructor = subClass; | ||
subClass.__proto__ = superClass; | ||
} | ||
var PolymorphicSchema = | ||
@@ -265,3 +130,3 @@ /*#__PURE__*/ | ||
var id = isImmutable(value) ? value.get('id') : value.id; | ||
var id = this.isSingleSchema ? undefined : isImmutable(value) ? value.get('id') : value.id; | ||
var schema = this.isSingleSchema ? this.schema : this.schema[schemaKey]; | ||
@@ -281,74 +146,2 @@ return unvisit(id || value, schema); | ||
var UnionSchema = | ||
/*#__PURE__*/ | ||
function (_PolymorphicSchema) { | ||
_inheritsLoose(UnionSchema, _PolymorphicSchema); | ||
function UnionSchema(definition, schemaAttribute) { | ||
if (!schemaAttribute) { | ||
throw new Error('Expected option "schemaAttribute" not found on UnionSchema.'); | ||
} | ||
return _PolymorphicSchema.call(this, definition, schemaAttribute) || this; | ||
} | ||
var _proto = UnionSchema.prototype; | ||
_proto.normalize = function normalize(input, parent, key, visit, addEntity, visitedEntities) { | ||
return this.normalizeValue(input, parent, key, visit, addEntity, visitedEntities); | ||
}; | ||
_proto.denormalize = function denormalize(input, unvisit) { | ||
return this.denormalizeValue(input, unvisit); | ||
}; | ||
return UnionSchema; | ||
}(PolymorphicSchema); | ||
var ValuesSchema = | ||
/*#__PURE__*/ | ||
function (_PolymorphicSchema) { | ||
_inheritsLoose(ValuesSchema, _PolymorphicSchema); | ||
function ValuesSchema() { | ||
return _PolymorphicSchema.apply(this, arguments) || this; | ||
} | ||
var _proto = ValuesSchema.prototype; | ||
_proto.normalize = function normalize(input, parent, key, visit, addEntity, visitedEntities) { | ||
var _this = this; | ||
return Object.keys(input).reduce(function (output, key, index) { | ||
var _Object$assign; | ||
var value = input[key]; | ||
return value !== undefined && value !== null ? Object.assign({}, output, (_Object$assign = {}, _Object$assign[key] = _this.normalizeValue(value, input, key, visit, addEntity, visitedEntities), _Object$assign)) : output; | ||
}, {}); | ||
}; | ||
_proto.denormalize = function denormalize(input, unvisit) { | ||
var _this2 = this; | ||
var found = true; | ||
return [Object.keys(input).reduce(function (output, key) { | ||
var _Object$assign2; | ||
var entityOrId = input[key]; | ||
var _this2$denormalizeVal = _this2.denormalizeValue(entityOrId, unvisit), | ||
value = _this2$denormalizeVal[0], | ||
foundItem = _this2$denormalizeVal[1]; | ||
if (!foundItem) { | ||
found = false; | ||
} | ||
return Object.assign({}, output, (_Object$assign2 = {}, _Object$assign2[key] = value, _Object$assign2)); | ||
}, {}), found]; | ||
}; | ||
return ValuesSchema; | ||
}(PolymorphicSchema); | ||
var validateSchema = function validateSchema(definition) { | ||
@@ -522,2 +315,304 @@ var isArray = Array.isArray(definition); | ||
var unvisitEntity = function unvisitEntity(id, schema, unvisit, getEntity, cache) { | ||
var entity = getEntity(id, schema); | ||
if (typeof entity !== 'object' || entity === null) { | ||
return [entity, false]; | ||
} | ||
if (!cache[schema.key]) { | ||
cache[schema.key] = {}; | ||
} | ||
var found = true; | ||
if (!cache[schema.key][id]) { | ||
// Ensure we don't mutate it non-immutable objects | ||
var entityCopy = isImmutable(entity) ? entity : Object.assign({}, entity); // 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; | ||
var _schema$denormalize = schema.denormalize(entityCopy, unvisit); | ||
cache[schema.key][id] = _schema$denormalize[0]; | ||
found = _schema$denormalize[1]; | ||
} | ||
return [cache[schema.key][id], found]; | ||
}; | ||
var getUnvisit = function getUnvisit(entities) { | ||
var cache = {}; | ||
var getEntity = getEntities(entities); | ||
return [function unvisit(input, schema) { | ||
if (!schema) return [input, true]; | ||
if (typeof schema === 'object' && (!schema.denormalize || typeof schema.denormalize !== 'function')) { | ||
var method = Array.isArray(schema) ? denormalize : _denormalize; | ||
return method(schema, input, unvisit); | ||
} // null is considered intentional, thus always 'found' as true | ||
if (input === null) { | ||
return [input, true]; | ||
} | ||
if (typeof schema.getId === 'function' && typeof schema.normalize === 'function') { | ||
// unvisitEntity just can't handle undefined | ||
if (input === undefined) { | ||
return [input, false]; | ||
} | ||
return unvisitEntity(input, schema, unvisit, getEntity, cache); | ||
} | ||
if (typeof schema.denormalize === 'function') { | ||
return schema.denormalize(input, unvisit); | ||
} | ||
return [input, true]; | ||
}, cache]; | ||
}; | ||
var getEntities = function getEntities(entities) { | ||
var isImmutable$1 = isImmutable(entities); | ||
return function (entityOrId, schema) { | ||
var schemaKey = schema.key; | ||
if (typeof entityOrId === 'object') { | ||
return entityOrId; | ||
} | ||
if (isImmutable$1) { | ||
return entities.getIn([schemaKey, entityOrId.toString()]); | ||
} | ||
return entities[schemaKey] && entities[schemaKey][entityOrId]; | ||
}; | ||
}; | ||
var denormalize$1 = function denormalize(input, schema, entities) { | ||
/* istanbul ignore next */ | ||
// eslint-disable-next-line no-undef | ||
if (process.env.NODE_ENV !== 'production' && schema === undefined) throw new Error('shema needed'); | ||
if (typeof input !== 'undefined') { | ||
var _getUnvisit = getUnvisit(entities), | ||
unvisit = _getUnvisit[0], | ||
cache = _getUnvisit[1]; | ||
return [].concat(unvisit(input, schema), [cache]); | ||
} | ||
return [undefined, false, {}]; | ||
}; | ||
var getDefaultGetId = function getDefaultGetId(idAttribute) { | ||
return function (input) { | ||
return isImmutable(input) ? input.get(idAttribute) : input[idAttribute]; | ||
}; | ||
}; | ||
var EntitySchema = | ||
/*#__PURE__*/ | ||
function () { | ||
function EntitySchema(key, definition, options) { | ||
if (definition === void 0) { | ||
definition = {}; | ||
} | ||
if (options === void 0) { | ||
options = {}; | ||
} | ||
if (!key || typeof key !== 'string') { | ||
throw new Error("Expected a string key for Entity, but found " + key + "."); | ||
} | ||
var _options = options, | ||
_options$idAttribute = _options.idAttribute, | ||
idAttribute = _options$idAttribute === void 0 ? 'id' : _options$idAttribute, | ||
_options$mergeStrateg = _options.mergeStrategy, | ||
mergeStrategy = _options$mergeStrateg === void 0 ? function (entityA, entityB) { | ||
return Object.assign({}, entityA, {}, entityB); | ||
} : _options$mergeStrateg, | ||
_options$processStrat = _options.processStrategy, | ||
processStrategy = _options$processStrat === void 0 ? function (input) { | ||
return Object.assign({}, input); | ||
} : _options$processStrat; | ||
this._key = key; | ||
this._getId = typeof idAttribute === 'function' ? idAttribute : getDefaultGetId(idAttribute); | ||
this._idAttribute = idAttribute; | ||
this._mergeStrategy = mergeStrategy; | ||
this._processStrategy = processStrategy; | ||
this.define(definition); | ||
} | ||
var _proto = EntitySchema.prototype; | ||
_proto.define = function define(definition) { | ||
this.schema = Object.keys(definition).reduce(function (entitySchema, key) { | ||
var _Object$assign; | ||
var schema = definition[key]; | ||
return Object.assign({}, entitySchema, (_Object$assign = {}, _Object$assign[key] = schema, _Object$assign)); | ||
}, this.schema || {}); | ||
}; | ||
_proto.getId = function getId(input, parent, key) { | ||
return this._getId(input, parent, key); | ||
}; | ||
_proto.merge = function merge(entityA, entityB) { | ||
return this._mergeStrategy(entityA, entityB); | ||
}; | ||
_proto.normalize = function normalize(input, parent, key, visit, addEntity, visitedEntities) { | ||
var _this = this; | ||
var id = this.getId(input, parent, key); | ||
var entityType = this.key; | ||
if (!(entityType in visitedEntities)) { | ||
visitedEntities[entityType] = {}; | ||
} | ||
if (!(id in visitedEntities[entityType])) { | ||
visitedEntities[entityType][id] = []; | ||
} | ||
if (visitedEntities[entityType][id].some(function (entity) { | ||
return entity === input; | ||
})) { | ||
return id; | ||
} | ||
visitedEntities[entityType][id].push(input); | ||
var processedEntity = this._processStrategy(input, parent, key); | ||
Object.keys(this.schema).forEach(function (key) { | ||
if (Object.hasOwnProperty.call(processedEntity, key) && typeof processedEntity[key] === 'object') { | ||
var schema = _this.schema[key]; | ||
processedEntity[key] = visit(processedEntity[key], processedEntity, key, schema, addEntity, visitedEntities); | ||
} | ||
}); | ||
addEntity(this, processedEntity, input, parent, key); | ||
return id; | ||
}; | ||
_proto.denormalize = function denormalize(entity, unvisit) { | ||
var _this2 = this; | ||
if (isImmutable(entity)) { | ||
return denormalizeImmutable(this.schema, entity, unvisit); | ||
} | ||
var found = true; | ||
Object.keys(this.schema).forEach(function (key) { | ||
var schema = _this2.schema[key]; | ||
var _unvisit = unvisit(entity[key], schema), | ||
value = _unvisit[0], | ||
foundItem = _unvisit[1]; | ||
if (!foundItem) { | ||
found = false; | ||
} | ||
if (Object.hasOwnProperty.call(entity, key)) { | ||
entity[key] = value; | ||
} | ||
}); | ||
return [entity, found]; | ||
}; | ||
_createClass(EntitySchema, [{ | ||
key: "key", | ||
get: function get() { | ||
return this._key; | ||
} | ||
}, { | ||
key: "idAttribute", | ||
get: function get() { | ||
return this._idAttribute; | ||
} | ||
}]); | ||
return EntitySchema; | ||
}(); | ||
var UnionSchema = | ||
/*#__PURE__*/ | ||
function (_PolymorphicSchema) { | ||
_inheritsLoose(UnionSchema, _PolymorphicSchema); | ||
function UnionSchema(definition, schemaAttribute) { | ||
if (!schemaAttribute) { | ||
throw new Error('Expected option "schemaAttribute" not found on UnionSchema.'); | ||
} | ||
return _PolymorphicSchema.call(this, definition, schemaAttribute) || this; | ||
} | ||
var _proto = UnionSchema.prototype; | ||
_proto.normalize = function normalize(input, parent, key, visit, addEntity, visitedEntities) { | ||
return this.normalizeValue(input, parent, key, visit, addEntity, visitedEntities); | ||
}; | ||
_proto.denormalize = function denormalize(input, unvisit) { | ||
return this.denormalizeValue(input, unvisit); | ||
}; | ||
return UnionSchema; | ||
}(PolymorphicSchema); | ||
var ValuesSchema = | ||
/*#__PURE__*/ | ||
function (_PolymorphicSchema) { | ||
_inheritsLoose(ValuesSchema, _PolymorphicSchema); | ||
function ValuesSchema() { | ||
return _PolymorphicSchema.apply(this, arguments) || this; | ||
} | ||
var _proto = ValuesSchema.prototype; | ||
_proto.normalize = function normalize(input, parent, key, visit, addEntity, visitedEntities) { | ||
var _this = this; | ||
return Object.keys(input).reduce(function (output, key, index) { | ||
var _Object$assign; | ||
var value = input[key]; | ||
return value !== undefined && value !== null ? Object.assign({}, output, (_Object$assign = {}, _Object$assign[key] = _this.normalizeValue(value, input, key, visit, addEntity, visitedEntities), _Object$assign)) : output; | ||
}, {}); | ||
}; | ||
_proto.denormalize = function denormalize(input, unvisit) { | ||
var _this2 = this; | ||
var found = true; | ||
return [Object.keys(input).reduce(function (output, key) { | ||
var _Object$assign2; | ||
var entityOrId = input[key]; | ||
var _this2$denormalizeVal = _this2.denormalizeValue(entityOrId, unvisit), | ||
value = _this2$denormalizeVal[0], | ||
foundItem = _this2$denormalizeVal[1]; | ||
if (!foundItem) { | ||
found = false; | ||
} | ||
return Object.assign({}, output, (_Object$assign2 = {}, _Object$assign2[key] = value, _Object$assign2)); | ||
}, {}), found]; | ||
}; | ||
return ValuesSchema; | ||
}(PolymorphicSchema); | ||
var visit = function visit(value, parent, key, schema, addEntity, visitedEntities) { | ||
@@ -629,102 +724,3 @@ if (typeof value !== 'object' || !value || !schema) { | ||
var unvisitEntity = function unvisitEntity(id, schema, unvisit, getEntity, cache) { | ||
var entity = getEntity(id, schema); | ||
if (typeof entity !== 'object' || entity === null) { | ||
return [entity, false]; | ||
} | ||
if (!cache[schema.key]) { | ||
cache[schema.key] = {}; | ||
} | ||
var found = true; | ||
if (!cache[schema.key][id]) { | ||
// Ensure we don't mutate it non-immutable objects | ||
var entityCopy = isImmutable(entity) ? entity : Object.assign({}, entity); // 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; | ||
var _schema$denormalize = schema.denormalize(entityCopy, unvisit); | ||
cache[schema.key][id] = _schema$denormalize[0]; | ||
found = _schema$denormalize[1]; | ||
} | ||
return [cache[schema.key][id], found]; | ||
}; | ||
var getUnvisit = function getUnvisit(entities) { | ||
var cache = {}; | ||
var getEntity = getEntities(entities); | ||
return [function unvisit(input, schema) { | ||
if (!schema) return [input, true]; | ||
if (typeof schema === 'object' && (!schema.denormalize || typeof schema.denormalize !== 'function')) { | ||
var method = Array.isArray(schema) ? denormalize : _denormalize; | ||
return method(schema, input, unvisit); | ||
} // null is considered intentional, thus always 'found' as true | ||
if (input === null) { | ||
return [input, true]; | ||
} | ||
if (typeof schema.getId === 'function' && typeof schema.normalize === 'function') { | ||
// unvisitEntity just can't handle undefined | ||
if (input === undefined) { | ||
return [input, false]; | ||
} | ||
return unvisitEntity(input, schema, unvisit, getEntity, cache); | ||
} | ||
if (typeof schema.denormalize === 'function') { | ||
return schema.denormalize(input, unvisit); | ||
} | ||
return [input, true]; | ||
}, cache]; | ||
}; | ||
var getEntities = function getEntities(entities) { | ||
var isImmutable$1 = isImmutable(entities); | ||
return function (entityOrId, schema) { | ||
var schemaKey = schema.key; | ||
if (typeof entityOrId === 'object') { | ||
return entityOrId; | ||
} | ||
if (isImmutable$1) { | ||
return entities.getIn([schemaKey, entityOrId.toString()]); | ||
} | ||
return entities[schemaKey] && entities[schemaKey][entityOrId]; | ||
}; | ||
}; | ||
var denormalizeAndTracked = function denormalizeAndTracked(input, schema, entities) { | ||
/* istanbul ignore next */ | ||
// eslint-disable-next-line no-undef | ||
if (process.env.NODE_ENV !== 'production' && schema === undefined) throw new Error('shema needed'); | ||
if (typeof input !== 'undefined') { | ||
var _getUnvisit = getUnvisit(entities), | ||
unvisit = _getUnvisit[0], | ||
cache = _getUnvisit[1]; | ||
return [].concat(unvisit(input, schema), [cache]); | ||
} | ||
return [undefined, false, {}]; | ||
}; | ||
var denormalize$1 = function denormalize(input, schema, entities) { | ||
return denormalizeAndTracked(input, schema, entities).slice(0, 2); | ||
}; | ||
exports.denormalize = denormalize$1; | ||
exports.denormalizeAndTracked = denormalizeAndTracked; | ||
exports.normalize = normalize$1; | ||
@@ -731,0 +727,0 @@ exports.schema = schema; |
@@ -1,1 +0,1 @@ | ||
var rest_hooks_normalizr=function(e){"use strict";function t(e){return!(!e||"function"!=typeof e.hasOwnProperty||!(Object.hasOwnProperty.call(e,"__ownerID")||e._map&&Object.hasOwnProperty.call(e._map,"__ownerID")))}function r(e,t,r){let n=!0;return[Object.keys(e).reduce((t,i)=>{const s=""+i,[o,c]=r(t.get(s),e[s]);return c||(n=!1),t.has(s)?t.set(s,o):t},t),n]}class n{constructor(e,t){t&&(this._schemaAttribute="string"==typeof t?e=>e[t]:t),this.define(e)}get isSingleSchema(){return!this._schemaAttribute}define(e){this.schema=e}getSchemaAttribute(e,t,r){return!this.isSingleSchema&&this._schemaAttribute(e,t,r)}inferSchema(e,t,r){if(this.isSingleSchema)return this.schema;const n=this.getSchemaAttribute(e,t,r);return this.schema[n]}normalizeValue(e,t,r,n,i,s){const o=this.inferSchema(e,t,r);if(!o)return e;const c=n(e,t,r,o,i,s);return this.isSingleSchema||null==c?c:{id:c,schema:this.getSchemaAttribute(e,t,r)}}denormalizeValue(e,r){const n=t(e)?e.get("schema"):e.schema;return this.isSingleSchema||n?r((t(e)?e.get("id"):e.id)||e,this.isSingleSchema?this.schema:this.schema[n]):[e,!0]}}const i=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]},s=e=>Array.isArray(e)?e:Object.keys(e).map(t=>e[t]),o=(e,t,r,n,o,c,a)=>(e=i(e),s(t).map((t,i)=>o(t,r,n,e,c,a))),c=(e,t,r)=>{e=i(e);let n=!0;return void 0===t&&e&&([,n]=r(void 0,e)),[t&&t.map?t.map(t=>r(t,e)).filter(([,e])=>e).map(([e])=>e):t,n]};const a=(e,t,r,n,i,s,o)=>{const c=Object.assign({},t);return Object.keys(e).forEach(r=>{const n=e[r],a=i(t[r],t,r,n,s,o);null==a?delete c[r]:c[r]=a}),c},u=(e,n,i)=>{if(t(n))return r(e,n,i);const s=Object.assign({},n);let o=!0;return Object.keys(e).forEach(t=>{const[r,n]=i(s[t],e[t]);void 0!==s[t]&&(s[t]=r),n||(o=!1)}),[s,o]};const h=(e,t,r,n,i,s)=>{if("object"!=typeof e||!e||!n)return e;if("object"==typeof n&&(!n.normalize||"function"!=typeof n.normalize)){return(Array.isArray(n)?o:a)(n,e,t,r,h,i,s)}return n.normalize(e,t,r,h,i,s)},l={Array:class extends n{normalize(e,t,r,n,i,o){return s(e).map((e,s)=>this.normalizeValue(e,t,r,n,i,o)).filter(e=>null!=e)}denormalize(e,t){let r=!0;return void 0===e&&this.schema&&([,r]=t(void 0,this.schema)),[e&&e.map?e.map(e=>this.denormalizeValue(e,t)).filter(([,e])=>e).map(([e])=>e):e,r]}},Entity:class{constructor(e,r={},n={}){if(!e||"string"!=typeof e)throw new Error("Expected a string key for Entity, but found "+e+".");const{idAttribute:i="id",mergeStrategy:s=((e,t)=>Object.assign({},e,{},t)),processStrategy:o=(e=>Object.assign({},e))}=n;this._key=e,this._getId="function"==typeof i?i:(e=>r=>t(r)?r.get(e):r[e])(i),this._idAttribute=i,this._mergeStrategy=s,this._processStrategy=o,this.define(r)}get key(){return this._key}get idAttribute(){return this._idAttribute}define(e){this.schema=Object.keys(e).reduce((t,r)=>{const n=e[r];return Object.assign({},t,{[r]:n})},this.schema||{})}getId(e,t,r){return this._getId(e,t,r)}merge(e,t){return this._mergeStrategy(e,t)}normalize(e,t,r,n,i,s){const o=this.getId(e,t,r),c=this.key;if(c in s||(s[c]={}),o in s[c]||(s[c][o]=[]),s[c][o].some(t=>t===e))return o;s[c][o].push(e);const a=this._processStrategy(e,t,r);return Object.keys(this.schema).forEach(e=>{if(Object.hasOwnProperty.call(a,e)&&"object"==typeof a[e]){const t=this.schema[e];a[e]=n(a[e],a,e,t,i,s)}}),i(this,a,e,t,r),o}denormalize(e,n){if(t(e))return r(this.schema,e,n);let i=!0;return Object.keys(this.schema).forEach(t=>{const r=this.schema[t],[s,o]=n(e[t],r);o||(i=!1),Object.hasOwnProperty.call(e,t)&&(e[t]=s)}),[e,i]}},Object:class{constructor(e){this.define(e)}define(e){this.schema=Object.keys(e).reduce((t,r)=>{const n=e[r];return Object.assign({},t,{[r]:n})},this.schema||{})}normalize(...e){return a(this.schema,...e)}denormalize(...e){return u(this.schema,...e)}},Union:class extends n{constructor(e,t){if(!t)throw new Error('Expected option "schemaAttribute" not found on UnionSchema.');super(e,t)}normalize(e,t,r,n,i,s){return this.normalizeValue(e,t,r,n,i,s)}denormalize(e,t){return this.denormalizeValue(e,t)}},Values:class extends n{normalize(e,t,r,n,i,s){return Object.keys(e).reduce((t,r,o)=>{const c=e[r];return null!=c?Object.assign({},t,{[r]:this.normalizeValue(c,e,r,n,i,s)}):t},{})}denormalize(e,t){let r=!0;return[Object.keys(e).reduce((n,i)=>{const s=e[i],[o,c]=this.denormalizeValue(s,t);return c||(r=!1),Object.assign({},n,{[i]:o})},{}),r]}}};const m=e=>{const r={},n=d(e);return[function e(i,s){if(!s)return[i,!0];if("object"==typeof s&&(!s.denormalize||"function"!=typeof s.denormalize)){return(Array.isArray(s)?c:u)(s,i,e)}return null===i?[i,!0]:"function"==typeof s.getId&&"function"==typeof s.normalize?void 0===i?[i,!1]:((e,r,n,i,s)=>{const o=i(e,r);if("object"!=typeof o||null===o)return[o,!1];s[r.key]||(s[r.key]={});let c=!0;if(!s[r.key][e]){const i=t(o)?o:Object.assign({},o);s[r.key][e]=i,[s[r.key][e],c]=r.denormalize(i,n)}return[s[r.key][e],c]})(i,s,e,n,r):"function"==typeof s.denormalize?s.denormalize(i,e):[i,!0]},r]},d=e=>{const r=t(e);return(t,n)=>{const i=n.key;return"object"==typeof t?t:r?e.getIn([i,t.toString()]):e[i]&&e[i][t]}},f=(e,t,r)=>{if("production"!==process.env.NODE_ENV&&void 0===t)throw new Error("shema needed");if(void 0!==e){const[n,i]=m(r);return[...n(e,t),i]}return[void 0,!1,{}]};return e.denormalize=(e,t,r)=>f(e,t,r).slice(0,2),e.denormalizeAndTracked=f,e.normalize=(e,t)=>{const r=function(e){return["object","function"].includes(typeof e)?"object":typeof e}(t);if(null===e||typeof e!==r)throw new Error('Unexpected input given to normalize. Expected type to be "'+r+'", found "'+(null===e?"null":typeof e)+'".');const n={},i={},s=((e,t)=>(r,n,i,s,o)=>{const c=r.key,a=r.getId(i,s,o);c in e||(e[c]={});const u=e[c][a];if(e[c][a]=u?r.merge(u,n):n,Array.isArray(r.indexes)){const n=e[c][a];c in t||(t[c]={});for(const e of r.indexes){e in t[c]||(t[c][e]={});const r=t[c][e];u&&delete r[u[e]],e in n?r[n[e]]=a:"production"!==process.env.NODE_ENV&&console.warn("Index not found in entity. Indexes must be top-level members of your entity.\nIndex: "+e+"\nEntity: "+JSON.stringify(n,void 0,2))}}})(n,i);return{entities:n,indexes:i,result:h(e,e,void 0,t,s,{})}},e.schema=l,e}({}); | ||
var rest_hooks_normalizr=function(e){"use strict";function t(e){return!(!e||"function"!=typeof e.hasOwnProperty||!(Object.hasOwnProperty.call(e,"__ownerID")||e._map&&Object.hasOwnProperty.call(e._map,"__ownerID")))}function r(e,t,r){let n=!0;return[Object.keys(e).reduce((t,i)=>{const s=""+i,[o,c]=r(t.get(s),e[s]);return c||(n=!1),t.has(s)?t.set(s,o):t},t),n]}class n{constructor(e,t){t&&(this._schemaAttribute="string"==typeof t?e=>e[t]:t),this.define(e)}get isSingleSchema(){return!this._schemaAttribute}define(e){this.schema=e}getSchemaAttribute(e,t,r){return!this.isSingleSchema&&this._schemaAttribute(e,t,r)}inferSchema(e,t,r){if(this.isSingleSchema)return this.schema;const n=this.getSchemaAttribute(e,t,r);return this.schema[n]}normalizeValue(e,t,r,n,i,s){const o=this.inferSchema(e,t,r);if(!o)return e;const c=n(e,t,r,o,i,s);return this.isSingleSchema||null==c?c:{id:c,schema:this.getSchemaAttribute(e,t,r)}}denormalizeValue(e,r){const n=t(e)?e.get("schema"):e.schema;return this.isSingleSchema||n?r((this.isSingleSchema?void 0:t(e)?e.get("id"):e.id)||e,this.isSingleSchema?this.schema:this.schema[n]):[e,!0]}}const i=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]},s=e=>Array.isArray(e)?e:Object.keys(e).map(t=>e[t]),o=(e,t,r,n,o,c,a)=>(e=i(e),s(t).map((t,i)=>o(t,r,n,e,c,a))),c=(e,t,r)=>{e=i(e);let n=!0;return void 0===t&&e&&([,n]=r(void 0,e)),[t&&t.map?t.map(t=>r(t,e)).filter(([,e])=>e).map(([e])=>e):t,n]};const a=(e,t,r,n,i,s,o)=>{const c=Object.assign({},t);return Object.keys(e).forEach(r=>{const n=e[r],a=i(t[r],t,r,n,s,o);null==a?delete c[r]:c[r]=a}),c},u=(e,n,i)=>{if(t(n))return r(e,n,i);const s=Object.assign({},n);let o=!0;return Object.keys(e).forEach(t=>{const[r,n]=i(s[t],e[t]);void 0!==s[t]&&(s[t]=r),n||(o=!1)}),[s,o]};const h=e=>{const r={},n=l(e);return[function e(i,s){if(!s)return[i,!0];if("object"==typeof s&&(!s.denormalize||"function"!=typeof s.denormalize)){return(Array.isArray(s)?c:u)(s,i,e)}return null===i?[i,!0]:"function"==typeof s.getId&&"function"==typeof s.normalize?void 0===i?[i,!1]:((e,r,n,i,s)=>{const o=i(e,r);if("object"!=typeof o||null===o)return[o,!1];s[r.key]||(s[r.key]={});let c=!0;if(!s[r.key][e]){const i=t(o)?o:Object.assign({},o);s[r.key][e]=i,[s[r.key][e],c]=r.denormalize(i,n)}return[s[r.key][e],c]})(i,s,e,n,r):"function"==typeof s.denormalize?s.denormalize(i,e):[i,!0]},r]},l=e=>{const r=t(e);return(t,n)=>{const i=n.key;return"object"==typeof t?t:r?e.getIn([i,t.toString()]):e[i]&&e[i][t]}};const m=(e,t,r,n,i,s)=>{if("object"!=typeof e||!e||!n)return e;if("object"==typeof n&&(!n.normalize||"function"!=typeof n.normalize)){return(Array.isArray(n)?o:a)(n,e,t,r,m,i,s)}return n.normalize(e,t,r,m,i,s)},d={Array:class extends n{normalize(e,t,r,n,i,o){return s(e).map((e,s)=>this.normalizeValue(e,t,r,n,i,o)).filter(e=>null!=e)}denormalize(e,t){let r=!0;return void 0===e&&this.schema&&([,r]=t(void 0,this.schema)),[e&&e.map?e.map(e=>this.denormalizeValue(e,t)).filter(([,e])=>e).map(([e])=>e):e,r]}},Entity:class{constructor(e,r={},n={}){if(!e||"string"!=typeof e)throw new Error("Expected a string key for Entity, but found "+e+".");const{idAttribute:i="id",mergeStrategy:s=((e,t)=>Object.assign({},e,{},t)),processStrategy:o=(e=>Object.assign({},e))}=n;this._key=e,this._getId="function"==typeof i?i:(e=>r=>t(r)?r.get(e):r[e])(i),this._idAttribute=i,this._mergeStrategy=s,this._processStrategy=o,this.define(r)}get key(){return this._key}get idAttribute(){return this._idAttribute}define(e){this.schema=Object.keys(e).reduce((t,r)=>{const n=e[r];return Object.assign({},t,{[r]:n})},this.schema||{})}getId(e,t,r){return this._getId(e,t,r)}merge(e,t){return this._mergeStrategy(e,t)}normalize(e,t,r,n,i,s){const o=this.getId(e,t,r),c=this.key;if(c in s||(s[c]={}),o in s[c]||(s[c][o]=[]),s[c][o].some(t=>t===e))return o;s[c][o].push(e);const a=this._processStrategy(e,t,r);return Object.keys(this.schema).forEach(e=>{if(Object.hasOwnProperty.call(a,e)&&"object"==typeof a[e]){const t=this.schema[e];a[e]=n(a[e],a,e,t,i,s)}}),i(this,a,e,t,r),o}denormalize(e,n){if(t(e))return r(this.schema,e,n);let i=!0;return Object.keys(this.schema).forEach(t=>{const r=this.schema[t],[s,o]=n(e[t],r);o||(i=!1),Object.hasOwnProperty.call(e,t)&&(e[t]=s)}),[e,i]}},Object:class{constructor(e){this.define(e)}define(e){this.schema=Object.keys(e).reduce((t,r)=>{const n=e[r];return Object.assign({},t,{[r]:n})},this.schema||{})}normalize(...e){return a(this.schema,...e)}denormalize(...e){return u(this.schema,...e)}},Union:class extends n{constructor(e,t){if(!t)throw new Error('Expected option "schemaAttribute" not found on UnionSchema.');super(e,t)}normalize(e,t,r,n,i,s){return this.normalizeValue(e,t,r,n,i,s)}denormalize(e,t){return this.denormalizeValue(e,t)}},Values:class extends n{normalize(e,t,r,n,i,s){return Object.keys(e).reduce((t,r,o)=>{const c=e[r];return null!=c?Object.assign({},t,{[r]:this.normalizeValue(c,e,r,n,i,s)}):t},{})}denormalize(e,t){let r=!0;return[Object.keys(e).reduce((n,i)=>{const s=e[i],[o,c]=this.denormalizeValue(s,t);return c||(r=!1),Object.assign({},n,{[i]:o})},{}),r]}}};return e.denormalize=(e,t,r)=>{if("production"!==process.env.NODE_ENV&&void 0===t)throw new Error("shema needed");if(void 0!==e){const[n,i]=h(r);return[...n(e,t),i]}return[void 0,!1,{}]},e.normalize=(e,t)=>{const r=function(e){return["object","function"].includes(typeof e)?"object":typeof e}(t);if(null===e||typeof e!==r)throw new Error('Unexpected input given to normalize. Expected type to be "'+r+'", found "'+(null===e?"null":typeof e)+'".');const n={},i={},s=((e,t)=>(r,n,i,s,o)=>{const c=r.key,a=r.getId(i,s,o);c in e||(e[c]={});const u=e[c][a];if(e[c][a]=u?r.merge(u,n):n,Array.isArray(r.indexes)){const n=e[c][a];c in t||(t[c]={});for(const e of r.indexes){e in t[c]||(t[c][e]={});const r=t[c][e];u&&delete r[u[e]],e in n?r[n[e]]=a:"production"!==process.env.NODE_ENV&&console.warn("Index not found in entity. Indexes must be top-level members of your entity.\nIndex: "+e+"\nEntity: "+JSON.stringify(n,void 0,2))}}})(n,i);return{entities:n,indexes:i,result:m(e,e,void 0,t,s,{})}},e.schema=d,e}({}); |
@@ -0,3 +1,3 @@ | ||
import _inheritsLoose from '@babel/runtime/helpers/esm/inheritsLoose'; | ||
import _createClass from '@babel/runtime/helpers/esm/createClass'; | ||
import _inheritsLoose from '@babel/runtime/helpers/esm/inheritsLoose'; | ||
@@ -53,137 +53,2 @@ /** | ||
var getDefaultGetId = function getDefaultGetId(idAttribute) { | ||
return function (input) { | ||
return isImmutable(input) ? input.get(idAttribute) : input[idAttribute]; | ||
}; | ||
}; | ||
var EntitySchema = | ||
/*#__PURE__*/ | ||
function () { | ||
function EntitySchema(key, definition, options) { | ||
if (definition === void 0) { | ||
definition = {}; | ||
} | ||
if (options === void 0) { | ||
options = {}; | ||
} | ||
if (!key || typeof key !== 'string') { | ||
throw new Error("Expected a string key for Entity, but found " + key + "."); | ||
} | ||
var _options = options, | ||
_options$idAttribute = _options.idAttribute, | ||
idAttribute = _options$idAttribute === void 0 ? 'id' : _options$idAttribute, | ||
_options$mergeStrateg = _options.mergeStrategy, | ||
mergeStrategy = _options$mergeStrateg === void 0 ? function (entityA, entityB) { | ||
return Object.assign({}, entityA, {}, entityB); | ||
} : _options$mergeStrateg, | ||
_options$processStrat = _options.processStrategy, | ||
processStrategy = _options$processStrat === void 0 ? function (input) { | ||
return Object.assign({}, input); | ||
} : _options$processStrat; | ||
this._key = key; | ||
this._getId = typeof idAttribute === 'function' ? idAttribute : getDefaultGetId(idAttribute); | ||
this._idAttribute = idAttribute; | ||
this._mergeStrategy = mergeStrategy; | ||
this._processStrategy = processStrategy; | ||
this.define(definition); | ||
} | ||
var _proto = EntitySchema.prototype; | ||
_proto.define = function define(definition) { | ||
this.schema = Object.keys(definition).reduce(function (entitySchema, key) { | ||
var _Object$assign; | ||
var schema = definition[key]; | ||
return Object.assign({}, entitySchema, (_Object$assign = {}, _Object$assign[key] = schema, _Object$assign)); | ||
}, this.schema || {}); | ||
}; | ||
_proto.getId = function getId(input, parent, key) { | ||
return this._getId(input, parent, key); | ||
}; | ||
_proto.merge = function merge(entityA, entityB) { | ||
return this._mergeStrategy(entityA, entityB); | ||
}; | ||
_proto.normalize = function normalize(input, parent, key, visit, addEntity, visitedEntities) { | ||
var _this = this; | ||
var id = this.getId(input, parent, key); | ||
var entityType = this.key; | ||
if (!(entityType in visitedEntities)) { | ||
visitedEntities[entityType] = {}; | ||
} | ||
if (!(id in visitedEntities[entityType])) { | ||
visitedEntities[entityType][id] = []; | ||
} | ||
if (visitedEntities[entityType][id].some(function (entity) { | ||
return entity === input; | ||
})) { | ||
return id; | ||
} | ||
visitedEntities[entityType][id].push(input); | ||
var processedEntity = this._processStrategy(input, parent, key); | ||
Object.keys(this.schema).forEach(function (key) { | ||
if (Object.hasOwnProperty.call(processedEntity, key) && typeof processedEntity[key] === 'object') { | ||
var schema = _this.schema[key]; | ||
processedEntity[key] = visit(processedEntity[key], processedEntity, key, schema, addEntity, visitedEntities); | ||
} | ||
}); | ||
addEntity(this, processedEntity, input, parent, key); | ||
return id; | ||
}; | ||
_proto.denormalize = function denormalize(entity, unvisit) { | ||
var _this2 = this; | ||
if (isImmutable(entity)) { | ||
return denormalizeImmutable(this.schema, entity, unvisit); | ||
} | ||
var found = true; | ||
Object.keys(this.schema).forEach(function (key) { | ||
var schema = _this2.schema[key]; | ||
var _unvisit = unvisit(entity[key], schema), | ||
value = _unvisit[0], | ||
foundItem = _unvisit[1]; | ||
if (!foundItem) { | ||
found = false; | ||
} | ||
if (Object.hasOwnProperty.call(entity, key)) { | ||
entity[key] = value; | ||
} | ||
}); | ||
return [entity, found]; | ||
}; | ||
_createClass(EntitySchema, [{ | ||
key: "key", | ||
get: function get() { | ||
return this._key; | ||
} | ||
}, { | ||
key: "idAttribute", | ||
get: function get() { | ||
return this._idAttribute; | ||
} | ||
}]); | ||
return EntitySchema; | ||
}(); | ||
var PolymorphicSchema = | ||
@@ -242,3 +107,3 @@ /*#__PURE__*/ | ||
var id = isImmutable(value) ? value.get('id') : value.id; | ||
var id = this.isSingleSchema ? undefined : isImmutable(value) ? value.get('id') : value.id; | ||
var schema = this.isSingleSchema ? this.schema : this.schema[schemaKey]; | ||
@@ -258,74 +123,2 @@ return unvisit(id || value, schema); | ||
var UnionSchema = | ||
/*#__PURE__*/ | ||
function (_PolymorphicSchema) { | ||
_inheritsLoose(UnionSchema, _PolymorphicSchema); | ||
function UnionSchema(definition, schemaAttribute) { | ||
if (!schemaAttribute) { | ||
throw new Error('Expected option "schemaAttribute" not found on UnionSchema.'); | ||
} | ||
return _PolymorphicSchema.call(this, definition, schemaAttribute) || this; | ||
} | ||
var _proto = UnionSchema.prototype; | ||
_proto.normalize = function normalize(input, parent, key, visit, addEntity, visitedEntities) { | ||
return this.normalizeValue(input, parent, key, visit, addEntity, visitedEntities); | ||
}; | ||
_proto.denormalize = function denormalize(input, unvisit) { | ||
return this.denormalizeValue(input, unvisit); | ||
}; | ||
return UnionSchema; | ||
}(PolymorphicSchema); | ||
var ValuesSchema = | ||
/*#__PURE__*/ | ||
function (_PolymorphicSchema) { | ||
_inheritsLoose(ValuesSchema, _PolymorphicSchema); | ||
function ValuesSchema() { | ||
return _PolymorphicSchema.apply(this, arguments) || this; | ||
} | ||
var _proto = ValuesSchema.prototype; | ||
_proto.normalize = function normalize(input, parent, key, visit, addEntity, visitedEntities) { | ||
var _this = this; | ||
return Object.keys(input).reduce(function (output, key, index) { | ||
var _Object$assign; | ||
var value = input[key]; | ||
return value !== undefined && value !== null ? Object.assign({}, output, (_Object$assign = {}, _Object$assign[key] = _this.normalizeValue(value, input, key, visit, addEntity, visitedEntities), _Object$assign)) : output; | ||
}, {}); | ||
}; | ||
_proto.denormalize = function denormalize(input, unvisit) { | ||
var _this2 = this; | ||
var found = true; | ||
return [Object.keys(input).reduce(function (output, key) { | ||
var _Object$assign2; | ||
var entityOrId = input[key]; | ||
var _this2$denormalizeVal = _this2.denormalizeValue(entityOrId, unvisit), | ||
value = _this2$denormalizeVal[0], | ||
foundItem = _this2$denormalizeVal[1]; | ||
if (!foundItem) { | ||
found = false; | ||
} | ||
return Object.assign({}, output, (_Object$assign2 = {}, _Object$assign2[key] = value, _Object$assign2)); | ||
}, {}), found]; | ||
}; | ||
return ValuesSchema; | ||
}(PolymorphicSchema); | ||
var validateSchema = function validateSchema(definition) { | ||
@@ -499,2 +292,304 @@ var isArray = Array.isArray(definition); | ||
var unvisitEntity = function unvisitEntity(id, schema, unvisit, getEntity, cache) { | ||
var entity = getEntity(id, schema); | ||
if (typeof entity !== 'object' || entity === null) { | ||
return [entity, false]; | ||
} | ||
if (!cache[schema.key]) { | ||
cache[schema.key] = {}; | ||
} | ||
var found = true; | ||
if (!cache[schema.key][id]) { | ||
// Ensure we don't mutate it non-immutable objects | ||
var entityCopy = isImmutable(entity) ? entity : Object.assign({}, entity); // 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; | ||
var _schema$denormalize = schema.denormalize(entityCopy, unvisit); | ||
cache[schema.key][id] = _schema$denormalize[0]; | ||
found = _schema$denormalize[1]; | ||
} | ||
return [cache[schema.key][id], found]; | ||
}; | ||
var getUnvisit = function getUnvisit(entities) { | ||
var cache = {}; | ||
var getEntity = getEntities(entities); | ||
return [function unvisit(input, schema) { | ||
if (!schema) return [input, true]; | ||
if (typeof schema === 'object' && (!schema.denormalize || typeof schema.denormalize !== 'function')) { | ||
var method = Array.isArray(schema) ? denormalize : _denormalize; | ||
return method(schema, input, unvisit); | ||
} // null is considered intentional, thus always 'found' as true | ||
if (input === null) { | ||
return [input, true]; | ||
} | ||
if (typeof schema.getId === 'function' && typeof schema.normalize === 'function') { | ||
// unvisitEntity just can't handle undefined | ||
if (input === undefined) { | ||
return [input, false]; | ||
} | ||
return unvisitEntity(input, schema, unvisit, getEntity, cache); | ||
} | ||
if (typeof schema.denormalize === 'function') { | ||
return schema.denormalize(input, unvisit); | ||
} | ||
return [input, true]; | ||
}, cache]; | ||
}; | ||
var getEntities = function getEntities(entities) { | ||
var isImmutable$1 = isImmutable(entities); | ||
return function (entityOrId, schema) { | ||
var schemaKey = schema.key; | ||
if (typeof entityOrId === 'object') { | ||
return entityOrId; | ||
} | ||
if (isImmutable$1) { | ||
return entities.getIn([schemaKey, entityOrId.toString()]); | ||
} | ||
return entities[schemaKey] && entities[schemaKey][entityOrId]; | ||
}; | ||
}; | ||
var denormalize$1 = function denormalize(input, schema, entities) { | ||
/* istanbul ignore next */ | ||
// eslint-disable-next-line no-undef | ||
if (process.env.NODE_ENV !== 'production' && schema === undefined) throw new Error('shema needed'); | ||
if (typeof input !== 'undefined') { | ||
var _getUnvisit = getUnvisit(entities), | ||
unvisit = _getUnvisit[0], | ||
cache = _getUnvisit[1]; | ||
return [].concat(unvisit(input, schema), [cache]); | ||
} | ||
return [undefined, false, {}]; | ||
}; | ||
var getDefaultGetId = function getDefaultGetId(idAttribute) { | ||
return function (input) { | ||
return isImmutable(input) ? input.get(idAttribute) : input[idAttribute]; | ||
}; | ||
}; | ||
var EntitySchema = | ||
/*#__PURE__*/ | ||
function () { | ||
function EntitySchema(key, definition, options) { | ||
if (definition === void 0) { | ||
definition = {}; | ||
} | ||
if (options === void 0) { | ||
options = {}; | ||
} | ||
if (!key || typeof key !== 'string') { | ||
throw new Error("Expected a string key for Entity, but found " + key + "."); | ||
} | ||
var _options = options, | ||
_options$idAttribute = _options.idAttribute, | ||
idAttribute = _options$idAttribute === void 0 ? 'id' : _options$idAttribute, | ||
_options$mergeStrateg = _options.mergeStrategy, | ||
mergeStrategy = _options$mergeStrateg === void 0 ? function (entityA, entityB) { | ||
return Object.assign({}, entityA, {}, entityB); | ||
} : _options$mergeStrateg, | ||
_options$processStrat = _options.processStrategy, | ||
processStrategy = _options$processStrat === void 0 ? function (input) { | ||
return Object.assign({}, input); | ||
} : _options$processStrat; | ||
this._key = key; | ||
this._getId = typeof idAttribute === 'function' ? idAttribute : getDefaultGetId(idAttribute); | ||
this._idAttribute = idAttribute; | ||
this._mergeStrategy = mergeStrategy; | ||
this._processStrategy = processStrategy; | ||
this.define(definition); | ||
} | ||
var _proto = EntitySchema.prototype; | ||
_proto.define = function define(definition) { | ||
this.schema = Object.keys(definition).reduce(function (entitySchema, key) { | ||
var _Object$assign; | ||
var schema = definition[key]; | ||
return Object.assign({}, entitySchema, (_Object$assign = {}, _Object$assign[key] = schema, _Object$assign)); | ||
}, this.schema || {}); | ||
}; | ||
_proto.getId = function getId(input, parent, key) { | ||
return this._getId(input, parent, key); | ||
}; | ||
_proto.merge = function merge(entityA, entityB) { | ||
return this._mergeStrategy(entityA, entityB); | ||
}; | ||
_proto.normalize = function normalize(input, parent, key, visit, addEntity, visitedEntities) { | ||
var _this = this; | ||
var id = this.getId(input, parent, key); | ||
var entityType = this.key; | ||
if (!(entityType in visitedEntities)) { | ||
visitedEntities[entityType] = {}; | ||
} | ||
if (!(id in visitedEntities[entityType])) { | ||
visitedEntities[entityType][id] = []; | ||
} | ||
if (visitedEntities[entityType][id].some(function (entity) { | ||
return entity === input; | ||
})) { | ||
return id; | ||
} | ||
visitedEntities[entityType][id].push(input); | ||
var processedEntity = this._processStrategy(input, parent, key); | ||
Object.keys(this.schema).forEach(function (key) { | ||
if (Object.hasOwnProperty.call(processedEntity, key) && typeof processedEntity[key] === 'object') { | ||
var schema = _this.schema[key]; | ||
processedEntity[key] = visit(processedEntity[key], processedEntity, key, schema, addEntity, visitedEntities); | ||
} | ||
}); | ||
addEntity(this, processedEntity, input, parent, key); | ||
return id; | ||
}; | ||
_proto.denormalize = function denormalize(entity, unvisit) { | ||
var _this2 = this; | ||
if (isImmutable(entity)) { | ||
return denormalizeImmutable(this.schema, entity, unvisit); | ||
} | ||
var found = true; | ||
Object.keys(this.schema).forEach(function (key) { | ||
var schema = _this2.schema[key]; | ||
var _unvisit = unvisit(entity[key], schema), | ||
value = _unvisit[0], | ||
foundItem = _unvisit[1]; | ||
if (!foundItem) { | ||
found = false; | ||
} | ||
if (Object.hasOwnProperty.call(entity, key)) { | ||
entity[key] = value; | ||
} | ||
}); | ||
return [entity, found]; | ||
}; | ||
_createClass(EntitySchema, [{ | ||
key: "key", | ||
get: function get() { | ||
return this._key; | ||
} | ||
}, { | ||
key: "idAttribute", | ||
get: function get() { | ||
return this._idAttribute; | ||
} | ||
}]); | ||
return EntitySchema; | ||
}(); | ||
var UnionSchema = | ||
/*#__PURE__*/ | ||
function (_PolymorphicSchema) { | ||
_inheritsLoose(UnionSchema, _PolymorphicSchema); | ||
function UnionSchema(definition, schemaAttribute) { | ||
if (!schemaAttribute) { | ||
throw new Error('Expected option "schemaAttribute" not found on UnionSchema.'); | ||
} | ||
return _PolymorphicSchema.call(this, definition, schemaAttribute) || this; | ||
} | ||
var _proto = UnionSchema.prototype; | ||
_proto.normalize = function normalize(input, parent, key, visit, addEntity, visitedEntities) { | ||
return this.normalizeValue(input, parent, key, visit, addEntity, visitedEntities); | ||
}; | ||
_proto.denormalize = function denormalize(input, unvisit) { | ||
return this.denormalizeValue(input, unvisit); | ||
}; | ||
return UnionSchema; | ||
}(PolymorphicSchema); | ||
var ValuesSchema = | ||
/*#__PURE__*/ | ||
function (_PolymorphicSchema) { | ||
_inheritsLoose(ValuesSchema, _PolymorphicSchema); | ||
function ValuesSchema() { | ||
return _PolymorphicSchema.apply(this, arguments) || this; | ||
} | ||
var _proto = ValuesSchema.prototype; | ||
_proto.normalize = function normalize(input, parent, key, visit, addEntity, visitedEntities) { | ||
var _this = this; | ||
return Object.keys(input).reduce(function (output, key, index) { | ||
var _Object$assign; | ||
var value = input[key]; | ||
return value !== undefined && value !== null ? Object.assign({}, output, (_Object$assign = {}, _Object$assign[key] = _this.normalizeValue(value, input, key, visit, addEntity, visitedEntities), _Object$assign)) : output; | ||
}, {}); | ||
}; | ||
_proto.denormalize = function denormalize(input, unvisit) { | ||
var _this2 = this; | ||
var found = true; | ||
return [Object.keys(input).reduce(function (output, key) { | ||
var _Object$assign2; | ||
var entityOrId = input[key]; | ||
var _this2$denormalizeVal = _this2.denormalizeValue(entityOrId, unvisit), | ||
value = _this2$denormalizeVal[0], | ||
foundItem = _this2$denormalizeVal[1]; | ||
if (!foundItem) { | ||
found = false; | ||
} | ||
return Object.assign({}, output, (_Object$assign2 = {}, _Object$assign2[key] = value, _Object$assign2)); | ||
}, {}), found]; | ||
}; | ||
return ValuesSchema; | ||
}(PolymorphicSchema); | ||
var visit = function visit(value, parent, key, schema, addEntity, visitedEntities) { | ||
@@ -606,100 +701,2 @@ if (typeof value !== 'object' || !value || !schema) { | ||
var unvisitEntity = function unvisitEntity(id, schema, unvisit, getEntity, cache) { | ||
var entity = getEntity(id, schema); | ||
if (typeof entity !== 'object' || entity === null) { | ||
return [entity, false]; | ||
} | ||
if (!cache[schema.key]) { | ||
cache[schema.key] = {}; | ||
} | ||
var found = true; | ||
if (!cache[schema.key][id]) { | ||
// Ensure we don't mutate it non-immutable objects | ||
var entityCopy = isImmutable(entity) ? entity : Object.assign({}, entity); // 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; | ||
var _schema$denormalize = schema.denormalize(entityCopy, unvisit); | ||
cache[schema.key][id] = _schema$denormalize[0]; | ||
found = _schema$denormalize[1]; | ||
} | ||
return [cache[schema.key][id], found]; | ||
}; | ||
var getUnvisit = function getUnvisit(entities) { | ||
var cache = {}; | ||
var getEntity = getEntities(entities); | ||
return [function unvisit(input, schema) { | ||
if (!schema) return [input, true]; | ||
if (typeof schema === 'object' && (!schema.denormalize || typeof schema.denormalize !== 'function')) { | ||
var method = Array.isArray(schema) ? denormalize : _denormalize; | ||
return method(schema, input, unvisit); | ||
} // null is considered intentional, thus always 'found' as true | ||
if (input === null) { | ||
return [input, true]; | ||
} | ||
if (typeof schema.getId === 'function' && typeof schema.normalize === 'function') { | ||
// unvisitEntity just can't handle undefined | ||
if (input === undefined) { | ||
return [input, false]; | ||
} | ||
return unvisitEntity(input, schema, unvisit, getEntity, cache); | ||
} | ||
if (typeof schema.denormalize === 'function') { | ||
return schema.denormalize(input, unvisit); | ||
} | ||
return [input, true]; | ||
}, cache]; | ||
}; | ||
var getEntities = function getEntities(entities) { | ||
var isImmutable$1 = isImmutable(entities); | ||
return function (entityOrId, schema) { | ||
var schemaKey = schema.key; | ||
if (typeof entityOrId === 'object') { | ||
return entityOrId; | ||
} | ||
if (isImmutable$1) { | ||
return entities.getIn([schemaKey, entityOrId.toString()]); | ||
} | ||
return entities[schemaKey] && entities[schemaKey][entityOrId]; | ||
}; | ||
}; | ||
var denormalizeAndTracked = function denormalizeAndTracked(input, schema, entities) { | ||
/* istanbul ignore next */ | ||
// eslint-disable-next-line no-undef | ||
if (process.env.NODE_ENV !== 'production' && schema === undefined) throw new Error('shema needed'); | ||
if (typeof input !== 'undefined') { | ||
var _getUnvisit = getUnvisit(entities), | ||
unvisit = _getUnvisit[0], | ||
cache = _getUnvisit[1]; | ||
return [].concat(unvisit(input, schema), [cache]); | ||
} | ||
return [undefined, false, {}]; | ||
}; | ||
var denormalize$1 = function denormalize(input, schema, entities) { | ||
return denormalizeAndTracked(input, schema, entities).slice(0, 2); | ||
}; | ||
export { denormalize$1 as denormalize, denormalizeAndTracked, normalize$1 as normalize, schema }; | ||
export { denormalize$1 as denormalize, normalize$1 as normalize, schema }; |
@@ -1,1 +0,1 @@ | ||
function e(e){return!(!e||"function"!=typeof e.hasOwnProperty||!(Object.hasOwnProperty.call(e,"__ownerID")||e._map&&Object.hasOwnProperty.call(e._map,"__ownerID")))}function t(e,t,r){let n=!0;return[Object.keys(e).reduce((t,i)=>{const s=""+i,[o,c]=r(t.get(s),e[s]);return c||(n=!1),t.has(s)?t.set(s,o):t},t),n]}class r{constructor(e,t){t&&(this._schemaAttribute="string"==typeof t?e=>e[t]:t),this.define(e)}get isSingleSchema(){return!this._schemaAttribute}define(e){this.schema=e}getSchemaAttribute(e,t,r){return!this.isSingleSchema&&this._schemaAttribute(e,t,r)}inferSchema(e,t,r){if(this.isSingleSchema)return this.schema;const n=this.getSchemaAttribute(e,t,r);return this.schema[n]}normalizeValue(e,t,r,n,i,s){const o=this.inferSchema(e,t,r);if(!o)return e;const c=n(e,t,r,o,i,s);return this.isSingleSchema||null==c?c:{id:c,schema:this.getSchemaAttribute(e,t,r)}}denormalizeValue(t,r){const n=e(t)?t.get("schema"):t.schema;return this.isSingleSchema||n?r((e(t)?t.get("id"):t.id)||t,this.isSingleSchema?this.schema:this.schema[n]):[t,!0]}}const n=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]},i=e=>Array.isArray(e)?e:Object.keys(e).map(t=>e[t]),s=(e,t,r,s,o,c,a)=>(e=n(e),i(t).map((t,n)=>o(t,r,s,e,c,a))),o=(e,t,r)=>{e=n(e);let i=!0;return void 0===t&&e&&([,i]=r(void 0,e)),[t&&t.map?t.map(t=>r(t,e)).filter(([,e])=>e).map(([e])=>e):t,i]};const c=(e,t,r,n,i,s,o)=>{const c=Object.assign({},t);return Object.keys(e).forEach(r=>{const n=e[r],a=i(t[r],t,r,n,s,o);null==a?delete c[r]:c[r]=a}),c},a=(r,n,i)=>{if(e(n))return t(r,n,i);const s=Object.assign({},n);let o=!0;return Object.keys(r).forEach(e=>{const[t,n]=i(s[e],r[e]);void 0!==s[e]&&(s[e]=t),n||(o=!1)}),[s,o]};const u=(e,t,r,n,i,o)=>{if("object"!=typeof e||!e||!n)return e;if("object"==typeof n&&(!n.normalize||"function"!=typeof n.normalize)){return(Array.isArray(n)?s:c)(n,e,t,r,u,i,o)}return n.normalize(e,t,r,u,i,o)},h={Array:class extends r{normalize(e,t,r,n,s,o){return i(e).map((e,i)=>this.normalizeValue(e,t,r,n,s,o)).filter(e=>null!=e)}denormalize(e,t){let r=!0;return void 0===e&&this.schema&&([,r]=t(void 0,this.schema)),[e&&e.map?e.map(e=>this.denormalizeValue(e,t)).filter(([,e])=>e).map(([e])=>e):e,r]}},Entity:class{constructor(t,r={},n={}){if(!t||"string"!=typeof t)throw new Error("Expected a string key for Entity, but found "+t+".");const{idAttribute:i="id",mergeStrategy:s=((e,t)=>Object.assign({},e,{},t)),processStrategy:o=(e=>Object.assign({},e))}=n;this._key=t,this._getId="function"==typeof i?i:(t=>r=>e(r)?r.get(t):r[t])(i),this._idAttribute=i,this._mergeStrategy=s,this._processStrategy=o,this.define(r)}get key(){return this._key}get idAttribute(){return this._idAttribute}define(e){this.schema=Object.keys(e).reduce((t,r)=>{const n=e[r];return Object.assign({},t,{[r]:n})},this.schema||{})}getId(e,t,r){return this._getId(e,t,r)}merge(e,t){return this._mergeStrategy(e,t)}normalize(e,t,r,n,i,s){const o=this.getId(e,t,r),c=this.key;if(c in s||(s[c]={}),o in s[c]||(s[c][o]=[]),s[c][o].some(t=>t===e))return o;s[c][o].push(e);const a=this._processStrategy(e,t,r);return Object.keys(this.schema).forEach(e=>{if(Object.hasOwnProperty.call(a,e)&&"object"==typeof a[e]){const t=this.schema[e];a[e]=n(a[e],a,e,t,i,s)}}),i(this,a,e,t,r),o}denormalize(r,n){if(e(r))return t(this.schema,r,n);let i=!0;return Object.keys(this.schema).forEach(e=>{const t=this.schema[e],[s,o]=n(r[e],t);o||(i=!1),Object.hasOwnProperty.call(r,e)&&(r[e]=s)}),[r,i]}},Object:class{constructor(e){this.define(e)}define(e){this.schema=Object.keys(e).reduce((t,r)=>{const n=e[r];return Object.assign({},t,{[r]:n})},this.schema||{})}normalize(...e){return c(this.schema,...e)}denormalize(...e){return a(this.schema,...e)}},Union:class extends r{constructor(e,t){if(!t)throw new Error('Expected option "schemaAttribute" not found on UnionSchema.');super(e,t)}normalize(e,t,r,n,i,s){return this.normalizeValue(e,t,r,n,i,s)}denormalize(e,t){return this.denormalizeValue(e,t)}},Values:class extends r{normalize(e,t,r,n,i,s){return Object.keys(e).reduce((t,r,o)=>{const c=e[r];return null!=c?Object.assign({},t,{[r]:this.normalizeValue(c,e,r,n,i,s)}):t},{})}denormalize(e,t){let r=!0;return[Object.keys(e).reduce((n,i)=>{const s=e[i],[o,c]=this.denormalizeValue(s,t);return c||(r=!1),Object.assign({},n,{[i]:o})},{}),r]}}};const l=(e,t)=>{const r=function(e){return["object","function"].includes(typeof e)?"object":typeof e}(t);if(null===e||typeof e!==r)throw new Error('Unexpected input given to normalize. Expected type to be "'+r+'", found "'+(null===e?"null":typeof e)+'".');const n={},i={},s=((e,t)=>(r,n,i,s,o)=>{const c=r.key,a=r.getId(i,s,o);c in e||(e[c]={});const u=e[c][a];if(e[c][a]=u?r.merge(u,n):n,Array.isArray(r.indexes)){const n=e[c][a];c in t||(t[c]={});for(const e of r.indexes){e in t[c]||(t[c][e]={});const r=t[c][e];u&&delete r[u[e]],e in n?r[n[e]]=a:"production"!==process.env.NODE_ENV&&console.warn("Index not found in entity. Indexes must be top-level members of your entity.\nIndex: "+e+"\nEntity: "+JSON.stringify(n,void 0,2))}}})(n,i);return{entities:n,indexes:i,result:u(e,e,void 0,t,s,{})}},m=t=>{const r={},n=d(t);return[function t(i,s){if(!s)return[i,!0];if("object"==typeof s&&(!s.denormalize||"function"!=typeof s.denormalize)){return(Array.isArray(s)?o:a)(s,i,t)}return null===i?[i,!0]:"function"==typeof s.getId&&"function"==typeof s.normalize?void 0===i?[i,!1]:((t,r,n,i,s)=>{const o=i(t,r);if("object"!=typeof o||null===o)return[o,!1];s[r.key]||(s[r.key]={});let c=!0;if(!s[r.key][t]){const i=e(o)?o:Object.assign({},o);s[r.key][t]=i,[s[r.key][t],c]=r.denormalize(i,n)}return[s[r.key][t],c]})(i,s,t,n,r):"function"==typeof s.denormalize?s.denormalize(i,t):[i,!0]},r]},d=t=>{const r=e(t);return(e,n)=>{const i=n.key;return"object"==typeof e?e:r?t.getIn([i,e.toString()]):t[i]&&t[i][e]}},f=(e,t,r)=>{if("production"!==process.env.NODE_ENV&&void 0===t)throw new Error("shema needed");if(void 0!==e){const[n,i]=m(r);return[...n(e,t),i]}return[void 0,!1,{}]},y=(e,t,r)=>f(e,t,r).slice(0,2);export{y as denormalize,f as denormalizeAndTracked,l as normalize,h as schema}; | ||
function e(e){return!(!e||"function"!=typeof e.hasOwnProperty||!(Object.hasOwnProperty.call(e,"__ownerID")||e._map&&Object.hasOwnProperty.call(e._map,"__ownerID")))}function t(e,t,n){let r=!0;return[Object.keys(e).reduce((t,i)=>{const s=""+i,[o,c]=n(t.get(s),e[s]);return c||(r=!1),t.has(s)?t.set(s,o):t},t),r]}class n{constructor(e,t){t&&(this._schemaAttribute="string"==typeof t?e=>e[t]:t),this.define(e)}get isSingleSchema(){return!this._schemaAttribute}define(e){this.schema=e}getSchemaAttribute(e,t,n){return!this.isSingleSchema&&this._schemaAttribute(e,t,n)}inferSchema(e,t,n){if(this.isSingleSchema)return this.schema;const r=this.getSchemaAttribute(e,t,n);return this.schema[r]}normalizeValue(e,t,n,r,i,s){const o=this.inferSchema(e,t,n);if(!o)return e;const c=r(e,t,n,o,i,s);return this.isSingleSchema||null==c?c:{id:c,schema:this.getSchemaAttribute(e,t,n)}}denormalizeValue(t,n){const r=e(t)?t.get("schema"):t.schema;return this.isSingleSchema||r?n((this.isSingleSchema?void 0:e(t)?t.get("id"):t.id)||t,this.isSingleSchema?this.schema:this.schema[r]):[t,!0]}}const r=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]},i=e=>Array.isArray(e)?e:Object.keys(e).map(t=>e[t]),s=(e,t,n,s,o,c,a)=>(e=r(e),i(t).map((t,r)=>o(t,n,s,e,c,a))),o=(e,t,n)=>{e=r(e);let i=!0;return void 0===t&&e&&([,i]=n(void 0,e)),[t&&t.map?t.map(t=>n(t,e)).filter(([,e])=>e).map(([e])=>e):t,i]};const c=(e,t,n,r,i,s,o)=>{const c=Object.assign({},t);return Object.keys(e).forEach(n=>{const r=e[n],a=i(t[n],t,n,r,s,o);null==a?delete c[n]:c[n]=a}),c},a=(n,r,i)=>{if(e(r))return t(n,r,i);const s=Object.assign({},r);let o=!0;return Object.keys(n).forEach(e=>{const[t,r]=i(s[e],n[e]);void 0!==s[e]&&(s[e]=t),r||(o=!1)}),[s,o]};const u=t=>{const n={},r=h(t);return[function t(i,s){if(!s)return[i,!0];if("object"==typeof s&&(!s.denormalize||"function"!=typeof s.denormalize)){return(Array.isArray(s)?o:a)(s,i,t)}return null===i?[i,!0]:"function"==typeof s.getId&&"function"==typeof s.normalize?void 0===i?[i,!1]:((t,n,r,i,s)=>{const o=i(t,n);if("object"!=typeof o||null===o)return[o,!1];s[n.key]||(s[n.key]={});let c=!0;if(!s[n.key][t]){const i=e(o)?o:Object.assign({},o);s[n.key][t]=i,[s[n.key][t],c]=n.denormalize(i,r)}return[s[n.key][t],c]})(i,s,t,r,n):"function"==typeof s.denormalize?s.denormalize(i,t):[i,!0]},n]},h=t=>{const n=e(t);return(e,r)=>{const i=r.key;return"object"==typeof e?e:n?t.getIn([i,e.toString()]):t[i]&&t[i][e]}},l=(e,t,n)=>{if("production"!==process.env.NODE_ENV&&void 0===t)throw new Error("shema needed");if(void 0!==e){const[r,i]=u(n);return[...r(e,t),i]}return[void 0,!1,{}]};const m=(e,t,n,r,i,o)=>{if("object"!=typeof e||!e||!r)return e;if("object"==typeof r&&(!r.normalize||"function"!=typeof r.normalize)){return(Array.isArray(r)?s:c)(r,e,t,n,m,i,o)}return r.normalize(e,t,n,m,i,o)},d={Array:class extends n{normalize(e,t,n,r,s,o){return i(e).map((e,i)=>this.normalizeValue(e,t,n,r,s,o)).filter(e=>null!=e)}denormalize(e,t){let n=!0;return void 0===e&&this.schema&&([,n]=t(void 0,this.schema)),[e&&e.map?e.map(e=>this.denormalizeValue(e,t)).filter(([,e])=>e).map(([e])=>e):e,n]}},Entity:class{constructor(t,n={},r={}){if(!t||"string"!=typeof t)throw new Error("Expected a string key for Entity, but found "+t+".");const{idAttribute:i="id",mergeStrategy:s=((e,t)=>Object.assign({},e,{},t)),processStrategy:o=(e=>Object.assign({},e))}=r;this._key=t,this._getId="function"==typeof i?i:(t=>n=>e(n)?n.get(t):n[t])(i),this._idAttribute=i,this._mergeStrategy=s,this._processStrategy=o,this.define(n)}get key(){return this._key}get idAttribute(){return this._idAttribute}define(e){this.schema=Object.keys(e).reduce((t,n)=>{const r=e[n];return Object.assign({},t,{[n]:r})},this.schema||{})}getId(e,t,n){return this._getId(e,t,n)}merge(e,t){return this._mergeStrategy(e,t)}normalize(e,t,n,r,i,s){const o=this.getId(e,t,n),c=this.key;if(c in s||(s[c]={}),o in s[c]||(s[c][o]=[]),s[c][o].some(t=>t===e))return o;s[c][o].push(e);const a=this._processStrategy(e,t,n);return Object.keys(this.schema).forEach(e=>{if(Object.hasOwnProperty.call(a,e)&&"object"==typeof a[e]){const t=this.schema[e];a[e]=r(a[e],a,e,t,i,s)}}),i(this,a,e,t,n),o}denormalize(n,r){if(e(n))return t(this.schema,n,r);let i=!0;return Object.keys(this.schema).forEach(e=>{const t=this.schema[e],[s,o]=r(n[e],t);o||(i=!1),Object.hasOwnProperty.call(n,e)&&(n[e]=s)}),[n,i]}},Object:class{constructor(e){this.define(e)}define(e){this.schema=Object.keys(e).reduce((t,n)=>{const r=e[n];return Object.assign({},t,{[n]:r})},this.schema||{})}normalize(...e){return c(this.schema,...e)}denormalize(...e){return a(this.schema,...e)}},Union:class extends n{constructor(e,t){if(!t)throw new Error('Expected option "schemaAttribute" not found on UnionSchema.');super(e,t)}normalize(e,t,n,r,i,s){return this.normalizeValue(e,t,n,r,i,s)}denormalize(e,t){return this.denormalizeValue(e,t)}},Values:class extends n{normalize(e,t,n,r,i,s){return Object.keys(e).reduce((t,n,o)=>{const c=e[n];return null!=c?Object.assign({},t,{[n]:this.normalizeValue(c,e,n,r,i,s)}):t},{})}denormalize(e,t){let n=!0;return[Object.keys(e).reduce((r,i)=>{const s=e[i],[o,c]=this.denormalizeValue(s,t);return c||(n=!1),Object.assign({},r,{[i]:o})},{}),n]}}};const f=(e,t)=>{const n=function(e){return["object","function"].includes(typeof e)?"object":typeof e}(t);if(null===e||typeof e!==n)throw new Error('Unexpected input given to normalize. Expected type to be "'+n+'", found "'+(null===e?"null":typeof e)+'".');const r={},i={},s=((e,t)=>(n,r,i,s,o)=>{const c=n.key,a=n.getId(i,s,o);c in e||(e[c]={});const u=e[c][a];if(e[c][a]=u?n.merge(u,r):r,Array.isArray(n.indexes)){const r=e[c][a];c in t||(t[c]={});for(const e of n.indexes){e in t[c]||(t[c][e]={});const n=t[c][e];u&&delete n[u[e]],e in r?n[r[e]]=a:"production"!==process.env.NODE_ENV&&console.warn("Index not found in entity. Indexes must be top-level members of your entity.\nIndex: "+e+"\nEntity: "+JSON.stringify(r,void 0,2))}}})(r,i);return{entities:r,indexes:i,result:m(e,e,void 0,t,s,{})}};export{l as denormalize,f as normalize,d as schema}; |
@@ -54,2 +54,8 @@ 'use strict'; | ||
function _inheritsLoose(subClass, superClass) { | ||
subClass.prototype = Object.create(superClass.prototype); | ||
subClass.prototype.constructor = subClass; | ||
subClass.__proto__ = superClass; | ||
} | ||
function _defineProperties(target, props) { | ||
@@ -71,143 +77,2 @@ for (var i = 0; i < props.length; i++) { | ||
var getDefaultGetId = function getDefaultGetId(idAttribute) { | ||
return function (input) { | ||
return isImmutable(input) ? input.get(idAttribute) : input[idAttribute]; | ||
}; | ||
}; | ||
var EntitySchema = | ||
/*#__PURE__*/ | ||
function () { | ||
function EntitySchema(key, definition, options) { | ||
if (definition === void 0) { | ||
definition = {}; | ||
} | ||
if (options === void 0) { | ||
options = {}; | ||
} | ||
if (!key || typeof key !== 'string') { | ||
throw new Error("Expected a string key for Entity, but found " + key + "."); | ||
} | ||
var _options = options, | ||
_options$idAttribute = _options.idAttribute, | ||
idAttribute = _options$idAttribute === void 0 ? 'id' : _options$idAttribute, | ||
_options$mergeStrateg = _options.mergeStrategy, | ||
mergeStrategy = _options$mergeStrateg === void 0 ? function (entityA, entityB) { | ||
return Object.assign({}, entityA, {}, entityB); | ||
} : _options$mergeStrateg, | ||
_options$processStrat = _options.processStrategy, | ||
processStrategy = _options$processStrat === void 0 ? function (input) { | ||
return Object.assign({}, input); | ||
} : _options$processStrat; | ||
this._key = key; | ||
this._getId = typeof idAttribute === 'function' ? idAttribute : getDefaultGetId(idAttribute); | ||
this._idAttribute = idAttribute; | ||
this._mergeStrategy = mergeStrategy; | ||
this._processStrategy = processStrategy; | ||
this.define(definition); | ||
} | ||
var _proto = EntitySchema.prototype; | ||
_proto.define = function define(definition) { | ||
this.schema = Object.keys(definition).reduce(function (entitySchema, key) { | ||
var _Object$assign; | ||
var schema = definition[key]; | ||
return Object.assign({}, entitySchema, (_Object$assign = {}, _Object$assign[key] = schema, _Object$assign)); | ||
}, this.schema || {}); | ||
}; | ||
_proto.getId = function getId(input, parent, key) { | ||
return this._getId(input, parent, key); | ||
}; | ||
_proto.merge = function merge(entityA, entityB) { | ||
return this._mergeStrategy(entityA, entityB); | ||
}; | ||
_proto.normalize = function normalize(input, parent, key, visit, addEntity, visitedEntities) { | ||
var _this = this; | ||
var id = this.getId(input, parent, key); | ||
var entityType = this.key; | ||
if (!(entityType in visitedEntities)) { | ||
visitedEntities[entityType] = {}; | ||
} | ||
if (!(id in visitedEntities[entityType])) { | ||
visitedEntities[entityType][id] = []; | ||
} | ||
if (visitedEntities[entityType][id].some(function (entity) { | ||
return entity === input; | ||
})) { | ||
return id; | ||
} | ||
visitedEntities[entityType][id].push(input); | ||
var processedEntity = this._processStrategy(input, parent, key); | ||
Object.keys(this.schema).forEach(function (key) { | ||
if (Object.hasOwnProperty.call(processedEntity, key) && typeof processedEntity[key] === 'object') { | ||
var schema = _this.schema[key]; | ||
processedEntity[key] = visit(processedEntity[key], processedEntity, key, schema, addEntity, visitedEntities); | ||
} | ||
}); | ||
addEntity(this, processedEntity, input, parent, key); | ||
return id; | ||
}; | ||
_proto.denormalize = function denormalize(entity, unvisit) { | ||
var _this2 = this; | ||
if (isImmutable(entity)) { | ||
return denormalizeImmutable(this.schema, entity, unvisit); | ||
} | ||
var found = true; | ||
Object.keys(this.schema).forEach(function (key) { | ||
var schema = _this2.schema[key]; | ||
var _unvisit = unvisit(entity[key], schema), | ||
value = _unvisit[0], | ||
foundItem = _unvisit[1]; | ||
if (!foundItem) { | ||
found = false; | ||
} | ||
if (Object.hasOwnProperty.call(entity, key)) { | ||
entity[key] = value; | ||
} | ||
}); | ||
return [entity, found]; | ||
}; | ||
_createClass(EntitySchema, [{ | ||
key: "key", | ||
get: function get() { | ||
return this._key; | ||
} | ||
}, { | ||
key: "idAttribute", | ||
get: function get() { | ||
return this._idAttribute; | ||
} | ||
}]); | ||
return EntitySchema; | ||
}(); | ||
function _inheritsLoose(subClass, superClass) { | ||
subClass.prototype = Object.create(superClass.prototype); | ||
subClass.prototype.constructor = subClass; | ||
subClass.__proto__ = superClass; | ||
} | ||
var PolymorphicSchema = | ||
@@ -266,3 +131,3 @@ /*#__PURE__*/ | ||
var id = isImmutable(value) ? value.get('id') : value.id; | ||
var id = this.isSingleSchema ? undefined : isImmutable(value) ? value.get('id') : value.id; | ||
var schema = this.isSingleSchema ? this.schema : this.schema[schemaKey]; | ||
@@ -282,74 +147,2 @@ return unvisit(id || value, schema); | ||
var UnionSchema = | ||
/*#__PURE__*/ | ||
function (_PolymorphicSchema) { | ||
_inheritsLoose(UnionSchema, _PolymorphicSchema); | ||
function UnionSchema(definition, schemaAttribute) { | ||
if (!schemaAttribute) { | ||
throw new Error('Expected option "schemaAttribute" not found on UnionSchema.'); | ||
} | ||
return _PolymorphicSchema.call(this, definition, schemaAttribute) || this; | ||
} | ||
var _proto = UnionSchema.prototype; | ||
_proto.normalize = function normalize(input, parent, key, visit, addEntity, visitedEntities) { | ||
return this.normalizeValue(input, parent, key, visit, addEntity, visitedEntities); | ||
}; | ||
_proto.denormalize = function denormalize(input, unvisit) { | ||
return this.denormalizeValue(input, unvisit); | ||
}; | ||
return UnionSchema; | ||
}(PolymorphicSchema); | ||
var ValuesSchema = | ||
/*#__PURE__*/ | ||
function (_PolymorphicSchema) { | ||
_inheritsLoose(ValuesSchema, _PolymorphicSchema); | ||
function ValuesSchema() { | ||
return _PolymorphicSchema.apply(this, arguments) || this; | ||
} | ||
var _proto = ValuesSchema.prototype; | ||
_proto.normalize = function normalize(input, parent, key, visit, addEntity, visitedEntities) { | ||
var _this = this; | ||
return Object.keys(input).reduce(function (output, key, index) { | ||
var _Object$assign; | ||
var value = input[key]; | ||
return value !== undefined && value !== null ? Object.assign({}, output, (_Object$assign = {}, _Object$assign[key] = _this.normalizeValue(value, input, key, visit, addEntity, visitedEntities), _Object$assign)) : output; | ||
}, {}); | ||
}; | ||
_proto.denormalize = function denormalize(input, unvisit) { | ||
var _this2 = this; | ||
var found = true; | ||
return [Object.keys(input).reduce(function (output, key) { | ||
var _Object$assign2; | ||
var entityOrId = input[key]; | ||
var _this2$denormalizeVal = _this2.denormalizeValue(entityOrId, unvisit), | ||
value = _this2$denormalizeVal[0], | ||
foundItem = _this2$denormalizeVal[1]; | ||
if (!foundItem) { | ||
found = false; | ||
} | ||
return Object.assign({}, output, (_Object$assign2 = {}, _Object$assign2[key] = value, _Object$assign2)); | ||
}, {}), found]; | ||
}; | ||
return ValuesSchema; | ||
}(PolymorphicSchema); | ||
var validateSchema = function validateSchema(definition) { | ||
@@ -523,2 +316,304 @@ var isArray = Array.isArray(definition); | ||
var unvisitEntity = function unvisitEntity(id, schema, unvisit, getEntity, cache) { | ||
var entity = getEntity(id, schema); | ||
if (typeof entity !== 'object' || entity === null) { | ||
return [entity, false]; | ||
} | ||
if (!cache[schema.key]) { | ||
cache[schema.key] = {}; | ||
} | ||
var found = true; | ||
if (!cache[schema.key][id]) { | ||
// Ensure we don't mutate it non-immutable objects | ||
var entityCopy = isImmutable(entity) ? entity : Object.assign({}, entity); // 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; | ||
var _schema$denormalize = schema.denormalize(entityCopy, unvisit); | ||
cache[schema.key][id] = _schema$denormalize[0]; | ||
found = _schema$denormalize[1]; | ||
} | ||
return [cache[schema.key][id], found]; | ||
}; | ||
var getUnvisit = function getUnvisit(entities) { | ||
var cache = {}; | ||
var getEntity = getEntities(entities); | ||
return [function unvisit(input, schema) { | ||
if (!schema) return [input, true]; | ||
if (typeof schema === 'object' && (!schema.denormalize || typeof schema.denormalize !== 'function')) { | ||
var method = Array.isArray(schema) ? denormalize : _denormalize; | ||
return method(schema, input, unvisit); | ||
} // null is considered intentional, thus always 'found' as true | ||
if (input === null) { | ||
return [input, true]; | ||
} | ||
if (typeof schema.getId === 'function' && typeof schema.normalize === 'function') { | ||
// unvisitEntity just can't handle undefined | ||
if (input === undefined) { | ||
return [input, false]; | ||
} | ||
return unvisitEntity(input, schema, unvisit, getEntity, cache); | ||
} | ||
if (typeof schema.denormalize === 'function') { | ||
return schema.denormalize(input, unvisit); | ||
} | ||
return [input, true]; | ||
}, cache]; | ||
}; | ||
var getEntities = function getEntities(entities) { | ||
var isImmutable$1 = isImmutable(entities); | ||
return function (entityOrId, schema) { | ||
var schemaKey = schema.key; | ||
if (typeof entityOrId === 'object') { | ||
return entityOrId; | ||
} | ||
if (isImmutable$1) { | ||
return entities.getIn([schemaKey, entityOrId.toString()]); | ||
} | ||
return entities[schemaKey] && entities[schemaKey][entityOrId]; | ||
}; | ||
}; | ||
var denormalize$1 = function denormalize(input, schema, entities) { | ||
/* istanbul ignore next */ | ||
// eslint-disable-next-line no-undef | ||
if (process.env.NODE_ENV !== 'production' && schema === undefined) throw new Error('shema needed'); | ||
if (typeof input !== 'undefined') { | ||
var _getUnvisit = getUnvisit(entities), | ||
unvisit = _getUnvisit[0], | ||
cache = _getUnvisit[1]; | ||
return [].concat(unvisit(input, schema), [cache]); | ||
} | ||
return [undefined, false, {}]; | ||
}; | ||
var getDefaultGetId = function getDefaultGetId(idAttribute) { | ||
return function (input) { | ||
return isImmutable(input) ? input.get(idAttribute) : input[idAttribute]; | ||
}; | ||
}; | ||
var EntitySchema = | ||
/*#__PURE__*/ | ||
function () { | ||
function EntitySchema(key, definition, options) { | ||
if (definition === void 0) { | ||
definition = {}; | ||
} | ||
if (options === void 0) { | ||
options = {}; | ||
} | ||
if (!key || typeof key !== 'string') { | ||
throw new Error("Expected a string key for Entity, but found " + key + "."); | ||
} | ||
var _options = options, | ||
_options$idAttribute = _options.idAttribute, | ||
idAttribute = _options$idAttribute === void 0 ? 'id' : _options$idAttribute, | ||
_options$mergeStrateg = _options.mergeStrategy, | ||
mergeStrategy = _options$mergeStrateg === void 0 ? function (entityA, entityB) { | ||
return Object.assign({}, entityA, {}, entityB); | ||
} : _options$mergeStrateg, | ||
_options$processStrat = _options.processStrategy, | ||
processStrategy = _options$processStrat === void 0 ? function (input) { | ||
return Object.assign({}, input); | ||
} : _options$processStrat; | ||
this._key = key; | ||
this._getId = typeof idAttribute === 'function' ? idAttribute : getDefaultGetId(idAttribute); | ||
this._idAttribute = idAttribute; | ||
this._mergeStrategy = mergeStrategy; | ||
this._processStrategy = processStrategy; | ||
this.define(definition); | ||
} | ||
var _proto = EntitySchema.prototype; | ||
_proto.define = function define(definition) { | ||
this.schema = Object.keys(definition).reduce(function (entitySchema, key) { | ||
var _Object$assign; | ||
var schema = definition[key]; | ||
return Object.assign({}, entitySchema, (_Object$assign = {}, _Object$assign[key] = schema, _Object$assign)); | ||
}, this.schema || {}); | ||
}; | ||
_proto.getId = function getId(input, parent, key) { | ||
return this._getId(input, parent, key); | ||
}; | ||
_proto.merge = function merge(entityA, entityB) { | ||
return this._mergeStrategy(entityA, entityB); | ||
}; | ||
_proto.normalize = function normalize(input, parent, key, visit, addEntity, visitedEntities) { | ||
var _this = this; | ||
var id = this.getId(input, parent, key); | ||
var entityType = this.key; | ||
if (!(entityType in visitedEntities)) { | ||
visitedEntities[entityType] = {}; | ||
} | ||
if (!(id in visitedEntities[entityType])) { | ||
visitedEntities[entityType][id] = []; | ||
} | ||
if (visitedEntities[entityType][id].some(function (entity) { | ||
return entity === input; | ||
})) { | ||
return id; | ||
} | ||
visitedEntities[entityType][id].push(input); | ||
var processedEntity = this._processStrategy(input, parent, key); | ||
Object.keys(this.schema).forEach(function (key) { | ||
if (Object.hasOwnProperty.call(processedEntity, key) && typeof processedEntity[key] === 'object') { | ||
var schema = _this.schema[key]; | ||
processedEntity[key] = visit(processedEntity[key], processedEntity, key, schema, addEntity, visitedEntities); | ||
} | ||
}); | ||
addEntity(this, processedEntity, input, parent, key); | ||
return id; | ||
}; | ||
_proto.denormalize = function denormalize(entity, unvisit) { | ||
var _this2 = this; | ||
if (isImmutable(entity)) { | ||
return denormalizeImmutable(this.schema, entity, unvisit); | ||
} | ||
var found = true; | ||
Object.keys(this.schema).forEach(function (key) { | ||
var schema = _this2.schema[key]; | ||
var _unvisit = unvisit(entity[key], schema), | ||
value = _unvisit[0], | ||
foundItem = _unvisit[1]; | ||
if (!foundItem) { | ||
found = false; | ||
} | ||
if (Object.hasOwnProperty.call(entity, key)) { | ||
entity[key] = value; | ||
} | ||
}); | ||
return [entity, found]; | ||
}; | ||
_createClass(EntitySchema, [{ | ||
key: "key", | ||
get: function get() { | ||
return this._key; | ||
} | ||
}, { | ||
key: "idAttribute", | ||
get: function get() { | ||
return this._idAttribute; | ||
} | ||
}]); | ||
return EntitySchema; | ||
}(); | ||
var UnionSchema = | ||
/*#__PURE__*/ | ||
function (_PolymorphicSchema) { | ||
_inheritsLoose(UnionSchema, _PolymorphicSchema); | ||
function UnionSchema(definition, schemaAttribute) { | ||
if (!schemaAttribute) { | ||
throw new Error('Expected option "schemaAttribute" not found on UnionSchema.'); | ||
} | ||
return _PolymorphicSchema.call(this, definition, schemaAttribute) || this; | ||
} | ||
var _proto = UnionSchema.prototype; | ||
_proto.normalize = function normalize(input, parent, key, visit, addEntity, visitedEntities) { | ||
return this.normalizeValue(input, parent, key, visit, addEntity, visitedEntities); | ||
}; | ||
_proto.denormalize = function denormalize(input, unvisit) { | ||
return this.denormalizeValue(input, unvisit); | ||
}; | ||
return UnionSchema; | ||
}(PolymorphicSchema); | ||
var ValuesSchema = | ||
/*#__PURE__*/ | ||
function (_PolymorphicSchema) { | ||
_inheritsLoose(ValuesSchema, _PolymorphicSchema); | ||
function ValuesSchema() { | ||
return _PolymorphicSchema.apply(this, arguments) || this; | ||
} | ||
var _proto = ValuesSchema.prototype; | ||
_proto.normalize = function normalize(input, parent, key, visit, addEntity, visitedEntities) { | ||
var _this = this; | ||
return Object.keys(input).reduce(function (output, key, index) { | ||
var _Object$assign; | ||
var value = input[key]; | ||
return value !== undefined && value !== null ? Object.assign({}, output, (_Object$assign = {}, _Object$assign[key] = _this.normalizeValue(value, input, key, visit, addEntity, visitedEntities), _Object$assign)) : output; | ||
}, {}); | ||
}; | ||
_proto.denormalize = function denormalize(input, unvisit) { | ||
var _this2 = this; | ||
var found = true; | ||
return [Object.keys(input).reduce(function (output, key) { | ||
var _Object$assign2; | ||
var entityOrId = input[key]; | ||
var _this2$denormalizeVal = _this2.denormalizeValue(entityOrId, unvisit), | ||
value = _this2$denormalizeVal[0], | ||
foundItem = _this2$denormalizeVal[1]; | ||
if (!foundItem) { | ||
found = false; | ||
} | ||
return Object.assign({}, output, (_Object$assign2 = {}, _Object$assign2[key] = value, _Object$assign2)); | ||
}, {}), found]; | ||
}; | ||
return ValuesSchema; | ||
}(PolymorphicSchema); | ||
var visit = function visit(value, parent, key, schema, addEntity, visitedEntities) { | ||
@@ -630,103 +725,4 @@ if (typeof value !== 'object' || !value || !schema) { | ||
var unvisitEntity = function unvisitEntity(id, schema, unvisit, getEntity, cache) { | ||
var entity = getEntity(id, schema); | ||
if (typeof entity !== 'object' || entity === null) { | ||
return [entity, false]; | ||
} | ||
if (!cache[schema.key]) { | ||
cache[schema.key] = {}; | ||
} | ||
var found = true; | ||
if (!cache[schema.key][id]) { | ||
// Ensure we don't mutate it non-immutable objects | ||
var entityCopy = isImmutable(entity) ? entity : Object.assign({}, entity); // 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; | ||
var _schema$denormalize = schema.denormalize(entityCopy, unvisit); | ||
cache[schema.key][id] = _schema$denormalize[0]; | ||
found = _schema$denormalize[1]; | ||
} | ||
return [cache[schema.key][id], found]; | ||
}; | ||
var getUnvisit = function getUnvisit(entities) { | ||
var cache = {}; | ||
var getEntity = getEntities(entities); | ||
return [function unvisit(input, schema) { | ||
if (!schema) return [input, true]; | ||
if (typeof schema === 'object' && (!schema.denormalize || typeof schema.denormalize !== 'function')) { | ||
var method = Array.isArray(schema) ? denormalize : _denormalize; | ||
return method(schema, input, unvisit); | ||
} // null is considered intentional, thus always 'found' as true | ||
if (input === null) { | ||
return [input, true]; | ||
} | ||
if (typeof schema.getId === 'function' && typeof schema.normalize === 'function') { | ||
// unvisitEntity just can't handle undefined | ||
if (input === undefined) { | ||
return [input, false]; | ||
} | ||
return unvisitEntity(input, schema, unvisit, getEntity, cache); | ||
} | ||
if (typeof schema.denormalize === 'function') { | ||
return schema.denormalize(input, unvisit); | ||
} | ||
return [input, true]; | ||
}, cache]; | ||
}; | ||
var getEntities = function getEntities(entities) { | ||
var isImmutable$1 = isImmutable(entities); | ||
return function (entityOrId, schema) { | ||
var schemaKey = schema.key; | ||
if (typeof entityOrId === 'object') { | ||
return entityOrId; | ||
} | ||
if (isImmutable$1) { | ||
return entities.getIn([schemaKey, entityOrId.toString()]); | ||
} | ||
return entities[schemaKey] && entities[schemaKey][entityOrId]; | ||
}; | ||
}; | ||
var denormalizeAndTracked = function denormalizeAndTracked(input, schema, entities) { | ||
/* istanbul ignore next */ | ||
// eslint-disable-next-line no-undef | ||
if (process.env.NODE_ENV !== 'production' && schema === undefined) throw new Error('shema needed'); | ||
if (typeof input !== 'undefined') { | ||
var _getUnvisit = getUnvisit(entities), | ||
unvisit = _getUnvisit[0], | ||
cache = _getUnvisit[1]; | ||
return [].concat(unvisit(input, schema), [cache]); | ||
} | ||
return [undefined, false, {}]; | ||
}; | ||
var denormalize$1 = function denormalize(input, schema, entities) { | ||
return denormalizeAndTracked(input, schema, entities).slice(0, 2); | ||
}; | ||
exports.denormalize = denormalize$1; | ||
exports.denormalizeAndTracked = denormalizeAndTracked; | ||
exports.normalize = normalize$1; | ||
exports.schema = schema; |
@@ -1,1 +0,1 @@ | ||
"use strict";function e(e){return!(!e||"function"!=typeof e.hasOwnProperty||!(Object.hasOwnProperty.call(e,"__ownerID")||e._map&&Object.hasOwnProperty.call(e._map,"__ownerID")))}function t(e,t,r){let n=!0;return[Object.keys(e).reduce((t,i)=>{const s=""+i,[o,c]=r(t.get(s),e[s]);return c||(n=!1),t.has(s)?t.set(s,o):t},t),n]}Object.defineProperty(exports,"__esModule",{value:!0});class r{constructor(e,t){t&&(this._schemaAttribute="string"==typeof t?e=>e[t]:t),this.define(e)}get isSingleSchema(){return!this._schemaAttribute}define(e){this.schema=e}getSchemaAttribute(e,t,r){return!this.isSingleSchema&&this._schemaAttribute(e,t,r)}inferSchema(e,t,r){if(this.isSingleSchema)return this.schema;const n=this.getSchemaAttribute(e,t,r);return this.schema[n]}normalizeValue(e,t,r,n,i,s){const o=this.inferSchema(e,t,r);if(!o)return e;const c=n(e,t,r,o,i,s);return this.isSingleSchema||null==c?c:{id:c,schema:this.getSchemaAttribute(e,t,r)}}denormalizeValue(t,r){const n=e(t)?t.get("schema"):t.schema;return this.isSingleSchema||n?r((e(t)?t.get("id"):t.id)||t,this.isSingleSchema?this.schema:this.schema[n]):[t,!0]}}const n=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]},i=e=>Array.isArray(e)?e:Object.keys(e).map(t=>e[t]),s=(e,t,r,s,o,c,a)=>(e=n(e),i(t).map((t,n)=>o(t,r,s,e,c,a))),o=(e,t,r)=>{e=n(e);let i=!0;return void 0===t&&e&&([,i]=r(void 0,e)),[t&&t.map?t.map(t=>r(t,e)).filter(([,e])=>e).map(([e])=>e):t,i]};const c=(e,t,r,n,i,s,o)=>{const c=Object.assign({},t);return Object.keys(e).forEach(r=>{const n=e[r],a=i(t[r],t,r,n,s,o);null==a?delete c[r]:c[r]=a}),c},a=(r,n,i)=>{if(e(n))return t(r,n,i);const s=Object.assign({},n);let o=!0;return Object.keys(r).forEach(e=>{const[t,n]=i(s[e],r[e]);void 0!==s[e]&&(s[e]=t),n||(o=!1)}),[s,o]};const u=(e,t,r,n,i,o)=>{if("object"!=typeof e||!e||!n)return e;if("object"==typeof n&&(!n.normalize||"function"!=typeof n.normalize)){return(Array.isArray(n)?s:c)(n,e,t,r,u,i,o)}return n.normalize(e,t,r,u,i,o)},h={Array:class extends r{normalize(e,t,r,n,s,o){return i(e).map((e,i)=>this.normalizeValue(e,t,r,n,s,o)).filter(e=>null!=e)}denormalize(e,t){let r=!0;return void 0===e&&this.schema&&([,r]=t(void 0,this.schema)),[e&&e.map?e.map(e=>this.denormalizeValue(e,t)).filter(([,e])=>e).map(([e])=>e):e,r]}},Entity:class{constructor(t,r={},n={}){if(!t||"string"!=typeof t)throw new Error("Expected a string key for Entity, but found "+t+".");const{idAttribute:i="id",mergeStrategy:s=((e,t)=>Object.assign({},e,{},t)),processStrategy:o=(e=>Object.assign({},e))}=n;this._key=t,this._getId="function"==typeof i?i:(t=>r=>e(r)?r.get(t):r[t])(i),this._idAttribute=i,this._mergeStrategy=s,this._processStrategy=o,this.define(r)}get key(){return this._key}get idAttribute(){return this._idAttribute}define(e){this.schema=Object.keys(e).reduce((t,r)=>{const n=e[r];return Object.assign({},t,{[r]:n})},this.schema||{})}getId(e,t,r){return this._getId(e,t,r)}merge(e,t){return this._mergeStrategy(e,t)}normalize(e,t,r,n,i,s){const o=this.getId(e,t,r),c=this.key;if(c in s||(s[c]={}),o in s[c]||(s[c][o]=[]),s[c][o].some(t=>t===e))return o;s[c][o].push(e);const a=this._processStrategy(e,t,r);return Object.keys(this.schema).forEach(e=>{if(Object.hasOwnProperty.call(a,e)&&"object"==typeof a[e]){const t=this.schema[e];a[e]=n(a[e],a,e,t,i,s)}}),i(this,a,e,t,r),o}denormalize(r,n){if(e(r))return t(this.schema,r,n);let i=!0;return Object.keys(this.schema).forEach(e=>{const t=this.schema[e],[s,o]=n(r[e],t);o||(i=!1),Object.hasOwnProperty.call(r,e)&&(r[e]=s)}),[r,i]}},Object:class{constructor(e){this.define(e)}define(e){this.schema=Object.keys(e).reduce((t,r)=>{const n=e[r];return Object.assign({},t,{[r]:n})},this.schema||{})}normalize(...e){return c(this.schema,...e)}denormalize(...e){return a(this.schema,...e)}},Union:class extends r{constructor(e,t){if(!t)throw new Error('Expected option "schemaAttribute" not found on UnionSchema.');super(e,t)}normalize(e,t,r,n,i,s){return this.normalizeValue(e,t,r,n,i,s)}denormalize(e,t){return this.denormalizeValue(e,t)}},Values:class extends r{normalize(e,t,r,n,i,s){return Object.keys(e).reduce((t,r,o)=>{const c=e[r];return null!=c?Object.assign({},t,{[r]:this.normalizeValue(c,e,r,n,i,s)}):t},{})}denormalize(e,t){let r=!0;return[Object.keys(e).reduce((n,i)=>{const s=e[i],[o,c]=this.denormalizeValue(s,t);return c||(r=!1),Object.assign({},n,{[i]:o})},{}),r]}}};const l=t=>{const r={},n=m(t);return[function t(i,s){if(!s)return[i,!0];if("object"==typeof s&&(!s.denormalize||"function"!=typeof s.denormalize)){return(Array.isArray(s)?o:a)(s,i,t)}return null===i?[i,!0]:"function"==typeof s.getId&&"function"==typeof s.normalize?void 0===i?[i,!1]:((t,r,n,i,s)=>{const o=i(t,r);if("object"!=typeof o||null===o)return[o,!1];s[r.key]||(s[r.key]={});let c=!0;if(!s[r.key][t]){const i=e(o)?o:Object.assign({},o);s[r.key][t]=i,[s[r.key][t],c]=r.denormalize(i,n)}return[s[r.key][t],c]})(i,s,t,n,r):"function"==typeof s.denormalize?s.denormalize(i,t):[i,!0]},r]},m=t=>{const r=e(t);return(e,n)=>{const i=n.key;return"object"==typeof e?e:r?t.getIn([i,e.toString()]):t[i]&&t[i][e]}},d=(e,t,r)=>{if("production"!==process.env.NODE_ENV&&void 0===t)throw new Error("shema needed");if(void 0!==e){const[n,i]=l(r);return[...n(e,t),i]}return[void 0,!1,{}]};exports.denormalize=(e,t,r)=>d(e,t,r).slice(0,2),exports.denormalizeAndTracked=d,exports.normalize=(e,t)=>{const r=function(e){return["object","function"].includes(typeof e)?"object":typeof e}(t);if(null===e||typeof e!==r)throw new Error('Unexpected input given to normalize. Expected type to be "'+r+'", found "'+(null===e?"null":typeof e)+'".');const n={},i={},s=((e,t)=>(r,n,i,s,o)=>{const c=r.key,a=r.getId(i,s,o);c in e||(e[c]={});const u=e[c][a];if(e[c][a]=u?r.merge(u,n):n,Array.isArray(r.indexes)){const n=e[c][a];c in t||(t[c]={});for(const e of r.indexes){e in t[c]||(t[c][e]={});const r=t[c][e];u&&delete r[u[e]],e in n?r[n[e]]=a:"production"!==process.env.NODE_ENV&&console.warn("Index not found in entity. Indexes must be top-level members of your entity.\nIndex: "+e+"\nEntity: "+JSON.stringify(n,void 0,2))}}})(n,i);return{entities:n,indexes:i,result:u(e,e,void 0,t,s,{})}},exports.schema=h; | ||
"use strict";function e(e){return!(!e||"function"!=typeof e.hasOwnProperty||!(Object.hasOwnProperty.call(e,"__ownerID")||e._map&&Object.hasOwnProperty.call(e._map,"__ownerID")))}function t(e,t,r){let n=!0;return[Object.keys(e).reduce((t,i)=>{const s=""+i,[o,c]=r(t.get(s),e[s]);return c||(n=!1),t.has(s)?t.set(s,o):t},t),n]}Object.defineProperty(exports,"__esModule",{value:!0});class r{constructor(e,t){t&&(this._schemaAttribute="string"==typeof t?e=>e[t]:t),this.define(e)}get isSingleSchema(){return!this._schemaAttribute}define(e){this.schema=e}getSchemaAttribute(e,t,r){return!this.isSingleSchema&&this._schemaAttribute(e,t,r)}inferSchema(e,t,r){if(this.isSingleSchema)return this.schema;const n=this.getSchemaAttribute(e,t,r);return this.schema[n]}normalizeValue(e,t,r,n,i,s){const o=this.inferSchema(e,t,r);if(!o)return e;const c=n(e,t,r,o,i,s);return this.isSingleSchema||null==c?c:{id:c,schema:this.getSchemaAttribute(e,t,r)}}denormalizeValue(t,r){const n=e(t)?t.get("schema"):t.schema;return this.isSingleSchema||n?r((this.isSingleSchema?void 0:e(t)?t.get("id"):t.id)||t,this.isSingleSchema?this.schema:this.schema[n]):[t,!0]}}const n=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]},i=e=>Array.isArray(e)?e:Object.keys(e).map(t=>e[t]),s=(e,t,r,s,o,c,a)=>(e=n(e),i(t).map((t,n)=>o(t,r,s,e,c,a))),o=(e,t,r)=>{e=n(e);let i=!0;return void 0===t&&e&&([,i]=r(void 0,e)),[t&&t.map?t.map(t=>r(t,e)).filter(([,e])=>e).map(([e])=>e):t,i]};const c=(e,t,r,n,i,s,o)=>{const c=Object.assign({},t);return Object.keys(e).forEach(r=>{const n=e[r],a=i(t[r],t,r,n,s,o);null==a?delete c[r]:c[r]=a}),c},a=(r,n,i)=>{if(e(n))return t(r,n,i);const s=Object.assign({},n);let o=!0;return Object.keys(r).forEach(e=>{const[t,n]=i(s[e],r[e]);void 0!==s[e]&&(s[e]=t),n||(o=!1)}),[s,o]};const u=t=>{const r={},n=h(t);return[function t(i,s){if(!s)return[i,!0];if("object"==typeof s&&(!s.denormalize||"function"!=typeof s.denormalize)){return(Array.isArray(s)?o:a)(s,i,t)}return null===i?[i,!0]:"function"==typeof s.getId&&"function"==typeof s.normalize?void 0===i?[i,!1]:((t,r,n,i,s)=>{const o=i(t,r);if("object"!=typeof o||null===o)return[o,!1];s[r.key]||(s[r.key]={});let c=!0;if(!s[r.key][t]){const i=e(o)?o:Object.assign({},o);s[r.key][t]=i,[s[r.key][t],c]=r.denormalize(i,n)}return[s[r.key][t],c]})(i,s,t,n,r):"function"==typeof s.denormalize?s.denormalize(i,t):[i,!0]},r]},h=t=>{const r=e(t);return(e,n)=>{const i=n.key;return"object"==typeof e?e:r?t.getIn([i,e.toString()]):t[i]&&t[i][e]}};const l=(e,t,r,n,i,o)=>{if("object"!=typeof e||!e||!n)return e;if("object"==typeof n&&(!n.normalize||"function"!=typeof n.normalize)){return(Array.isArray(n)?s:c)(n,e,t,r,l,i,o)}return n.normalize(e,t,r,l,i,o)},m={Array:class extends r{normalize(e,t,r,n,s,o){return i(e).map((e,i)=>this.normalizeValue(e,t,r,n,s,o)).filter(e=>null!=e)}denormalize(e,t){let r=!0;return void 0===e&&this.schema&&([,r]=t(void 0,this.schema)),[e&&e.map?e.map(e=>this.denormalizeValue(e,t)).filter(([,e])=>e).map(([e])=>e):e,r]}},Entity:class{constructor(t,r={},n={}){if(!t||"string"!=typeof t)throw new Error("Expected a string key for Entity, but found "+t+".");const{idAttribute:i="id",mergeStrategy:s=((e,t)=>Object.assign({},e,{},t)),processStrategy:o=(e=>Object.assign({},e))}=n;this._key=t,this._getId="function"==typeof i?i:(t=>r=>e(r)?r.get(t):r[t])(i),this._idAttribute=i,this._mergeStrategy=s,this._processStrategy=o,this.define(r)}get key(){return this._key}get idAttribute(){return this._idAttribute}define(e){this.schema=Object.keys(e).reduce((t,r)=>{const n=e[r];return Object.assign({},t,{[r]:n})},this.schema||{})}getId(e,t,r){return this._getId(e,t,r)}merge(e,t){return this._mergeStrategy(e,t)}normalize(e,t,r,n,i,s){const o=this.getId(e,t,r),c=this.key;if(c in s||(s[c]={}),o in s[c]||(s[c][o]=[]),s[c][o].some(t=>t===e))return o;s[c][o].push(e);const a=this._processStrategy(e,t,r);return Object.keys(this.schema).forEach(e=>{if(Object.hasOwnProperty.call(a,e)&&"object"==typeof a[e]){const t=this.schema[e];a[e]=n(a[e],a,e,t,i,s)}}),i(this,a,e,t,r),o}denormalize(r,n){if(e(r))return t(this.schema,r,n);let i=!0;return Object.keys(this.schema).forEach(e=>{const t=this.schema[e],[s,o]=n(r[e],t);o||(i=!1),Object.hasOwnProperty.call(r,e)&&(r[e]=s)}),[r,i]}},Object:class{constructor(e){this.define(e)}define(e){this.schema=Object.keys(e).reduce((t,r)=>{const n=e[r];return Object.assign({},t,{[r]:n})},this.schema||{})}normalize(...e){return c(this.schema,...e)}denormalize(...e){return a(this.schema,...e)}},Union:class extends r{constructor(e,t){if(!t)throw new Error('Expected option "schemaAttribute" not found on UnionSchema.');super(e,t)}normalize(e,t,r,n,i,s){return this.normalizeValue(e,t,r,n,i,s)}denormalize(e,t){return this.denormalizeValue(e,t)}},Values:class extends r{normalize(e,t,r,n,i,s){return Object.keys(e).reduce((t,r,o)=>{const c=e[r];return null!=c?Object.assign({},t,{[r]:this.normalizeValue(c,e,r,n,i,s)}):t},{})}denormalize(e,t){let r=!0;return[Object.keys(e).reduce((n,i)=>{const s=e[i],[o,c]=this.denormalizeValue(s,t);return c||(r=!1),Object.assign({},n,{[i]:o})},{}),r]}}};exports.denormalize=(e,t,r)=>{if("production"!==process.env.NODE_ENV&&void 0===t)throw new Error("shema needed");if(void 0!==e){const[n,i]=u(r);return[...n(e,t),i]}return[void 0,!1,{}]},exports.normalize=(e,t)=>{const r=function(e){return["object","function"].includes(typeof e)?"object":typeof e}(t);if(null===e||typeof e!==r)throw new Error('Unexpected input given to normalize. Expected type to be "'+r+'", found "'+(null===e?"null":typeof e)+'".');const n={},i={},s=((e,t)=>(r,n,i,s,o)=>{const c=r.key,a=r.getId(i,s,o);c in e||(e[c]={});const u=e[c][a];if(e[c][a]=u?r.merge(u,n):n,Array.isArray(r.indexes)){const n=e[c][a];c in t||(t[c]={});for(const e of r.indexes){e in t[c]||(t[c][e]={});const r=t[c][e];u&&delete r[u[e]],e in n?r[n[e]]=a:"production"!==process.env.NODE_ENV&&console.warn("Index not found in entity. Indexes must be top-level members of your entity.\nIndex: "+e+"\nEntity: "+JSON.stringify(n,void 0,2))}}})(n,i);return{entities:n,indexes:i,result:l(e,e,void 0,t,s,{})}},exports.schema=m; |
@@ -56,2 +56,8 @@ (function (global, factory) { | ||
function _inheritsLoose(subClass, superClass) { | ||
subClass.prototype = Object.create(superClass.prototype); | ||
subClass.prototype.constructor = subClass; | ||
subClass.__proto__ = superClass; | ||
} | ||
function _defineProperties(target, props) { | ||
@@ -73,143 +79,2 @@ for (var i = 0; i < props.length; i++) { | ||
var getDefaultGetId = function getDefaultGetId(idAttribute) { | ||
return function (input) { | ||
return isImmutable(input) ? input.get(idAttribute) : input[idAttribute]; | ||
}; | ||
}; | ||
var EntitySchema = | ||
/*#__PURE__*/ | ||
function () { | ||
function EntitySchema(key, definition, options) { | ||
if (definition === void 0) { | ||
definition = {}; | ||
} | ||
if (options === void 0) { | ||
options = {}; | ||
} | ||
if (!key || typeof key !== 'string') { | ||
throw new Error("Expected a string key for Entity, but found " + key + "."); | ||
} | ||
var _options = options, | ||
_options$idAttribute = _options.idAttribute, | ||
idAttribute = _options$idAttribute === void 0 ? 'id' : _options$idAttribute, | ||
_options$mergeStrateg = _options.mergeStrategy, | ||
mergeStrategy = _options$mergeStrateg === void 0 ? function (entityA, entityB) { | ||
return Object.assign({}, entityA, {}, entityB); | ||
} : _options$mergeStrateg, | ||
_options$processStrat = _options.processStrategy, | ||
processStrategy = _options$processStrat === void 0 ? function (input) { | ||
return Object.assign({}, input); | ||
} : _options$processStrat; | ||
this._key = key; | ||
this._getId = typeof idAttribute === 'function' ? idAttribute : getDefaultGetId(idAttribute); | ||
this._idAttribute = idAttribute; | ||
this._mergeStrategy = mergeStrategy; | ||
this._processStrategy = processStrategy; | ||
this.define(definition); | ||
} | ||
var _proto = EntitySchema.prototype; | ||
_proto.define = function define(definition) { | ||
this.schema = Object.keys(definition).reduce(function (entitySchema, key) { | ||
var _Object$assign; | ||
var schema = definition[key]; | ||
return Object.assign({}, entitySchema, (_Object$assign = {}, _Object$assign[key] = schema, _Object$assign)); | ||
}, this.schema || {}); | ||
}; | ||
_proto.getId = function getId(input, parent, key) { | ||
return this._getId(input, parent, key); | ||
}; | ||
_proto.merge = function merge(entityA, entityB) { | ||
return this._mergeStrategy(entityA, entityB); | ||
}; | ||
_proto.normalize = function normalize(input, parent, key, visit, addEntity, visitedEntities) { | ||
var _this = this; | ||
var id = this.getId(input, parent, key); | ||
var entityType = this.key; | ||
if (!(entityType in visitedEntities)) { | ||
visitedEntities[entityType] = {}; | ||
} | ||
if (!(id in visitedEntities[entityType])) { | ||
visitedEntities[entityType][id] = []; | ||
} | ||
if (visitedEntities[entityType][id].some(function (entity) { | ||
return entity === input; | ||
})) { | ||
return id; | ||
} | ||
visitedEntities[entityType][id].push(input); | ||
var processedEntity = this._processStrategy(input, parent, key); | ||
Object.keys(this.schema).forEach(function (key) { | ||
if (Object.hasOwnProperty.call(processedEntity, key) && typeof processedEntity[key] === 'object') { | ||
var schema = _this.schema[key]; | ||
processedEntity[key] = visit(processedEntity[key], processedEntity, key, schema, addEntity, visitedEntities); | ||
} | ||
}); | ||
addEntity(this, processedEntity, input, parent, key); | ||
return id; | ||
}; | ||
_proto.denormalize = function denormalize(entity, unvisit) { | ||
var _this2 = this; | ||
if (isImmutable(entity)) { | ||
return denormalizeImmutable(this.schema, entity, unvisit); | ||
} | ||
var found = true; | ||
Object.keys(this.schema).forEach(function (key) { | ||
var schema = _this2.schema[key]; | ||
var _unvisit = unvisit(entity[key], schema), | ||
value = _unvisit[0], | ||
foundItem = _unvisit[1]; | ||
if (!foundItem) { | ||
found = false; | ||
} | ||
if (Object.hasOwnProperty.call(entity, key)) { | ||
entity[key] = value; | ||
} | ||
}); | ||
return [entity, found]; | ||
}; | ||
_createClass(EntitySchema, [{ | ||
key: "key", | ||
get: function get() { | ||
return this._key; | ||
} | ||
}, { | ||
key: "idAttribute", | ||
get: function get() { | ||
return this._idAttribute; | ||
} | ||
}]); | ||
return EntitySchema; | ||
}(); | ||
function _inheritsLoose(subClass, superClass) { | ||
subClass.prototype = Object.create(superClass.prototype); | ||
subClass.prototype.constructor = subClass; | ||
subClass.__proto__ = superClass; | ||
} | ||
var PolymorphicSchema = | ||
@@ -268,3 +133,3 @@ /*#__PURE__*/ | ||
var id = isImmutable(value) ? value.get('id') : value.id; | ||
var id = this.isSingleSchema ? undefined : isImmutable(value) ? value.get('id') : value.id; | ||
var schema = this.isSingleSchema ? this.schema : this.schema[schemaKey]; | ||
@@ -284,74 +149,2 @@ return unvisit(id || value, schema); | ||
var UnionSchema = | ||
/*#__PURE__*/ | ||
function (_PolymorphicSchema) { | ||
_inheritsLoose(UnionSchema, _PolymorphicSchema); | ||
function UnionSchema(definition, schemaAttribute) { | ||
if (!schemaAttribute) { | ||
throw new Error('Expected option "schemaAttribute" not found on UnionSchema.'); | ||
} | ||
return _PolymorphicSchema.call(this, definition, schemaAttribute) || this; | ||
} | ||
var _proto = UnionSchema.prototype; | ||
_proto.normalize = function normalize(input, parent, key, visit, addEntity, visitedEntities) { | ||
return this.normalizeValue(input, parent, key, visit, addEntity, visitedEntities); | ||
}; | ||
_proto.denormalize = function denormalize(input, unvisit) { | ||
return this.denormalizeValue(input, unvisit); | ||
}; | ||
return UnionSchema; | ||
}(PolymorphicSchema); | ||
var ValuesSchema = | ||
/*#__PURE__*/ | ||
function (_PolymorphicSchema) { | ||
_inheritsLoose(ValuesSchema, _PolymorphicSchema); | ||
function ValuesSchema() { | ||
return _PolymorphicSchema.apply(this, arguments) || this; | ||
} | ||
var _proto = ValuesSchema.prototype; | ||
_proto.normalize = function normalize(input, parent, key, visit, addEntity, visitedEntities) { | ||
var _this = this; | ||
return Object.keys(input).reduce(function (output, key, index) { | ||
var _Object$assign; | ||
var value = input[key]; | ||
return value !== undefined && value !== null ? Object.assign({}, output, (_Object$assign = {}, _Object$assign[key] = _this.normalizeValue(value, input, key, visit, addEntity, visitedEntities), _Object$assign)) : output; | ||
}, {}); | ||
}; | ||
_proto.denormalize = function denormalize(input, unvisit) { | ||
var _this2 = this; | ||
var found = true; | ||
return [Object.keys(input).reduce(function (output, key) { | ||
var _Object$assign2; | ||
var entityOrId = input[key]; | ||
var _this2$denormalizeVal = _this2.denormalizeValue(entityOrId, unvisit), | ||
value = _this2$denormalizeVal[0], | ||
foundItem = _this2$denormalizeVal[1]; | ||
if (!foundItem) { | ||
found = false; | ||
} | ||
return Object.assign({}, output, (_Object$assign2 = {}, _Object$assign2[key] = value, _Object$assign2)); | ||
}, {}), found]; | ||
}; | ||
return ValuesSchema; | ||
}(PolymorphicSchema); | ||
var validateSchema = function validateSchema(definition) { | ||
@@ -525,2 +318,304 @@ var isArray = Array.isArray(definition); | ||
var unvisitEntity = function unvisitEntity(id, schema, unvisit, getEntity, cache) { | ||
var entity = getEntity(id, schema); | ||
if (typeof entity !== 'object' || entity === null) { | ||
return [entity, false]; | ||
} | ||
if (!cache[schema.key]) { | ||
cache[schema.key] = {}; | ||
} | ||
var found = true; | ||
if (!cache[schema.key][id]) { | ||
// Ensure we don't mutate it non-immutable objects | ||
var entityCopy = isImmutable(entity) ? entity : Object.assign({}, entity); // 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; | ||
var _schema$denormalize = schema.denormalize(entityCopy, unvisit); | ||
cache[schema.key][id] = _schema$denormalize[0]; | ||
found = _schema$denormalize[1]; | ||
} | ||
return [cache[schema.key][id], found]; | ||
}; | ||
var getUnvisit = function getUnvisit(entities) { | ||
var cache = {}; | ||
var getEntity = getEntities(entities); | ||
return [function unvisit(input, schema) { | ||
if (!schema) return [input, true]; | ||
if (typeof schema === 'object' && (!schema.denormalize || typeof schema.denormalize !== 'function')) { | ||
var method = Array.isArray(schema) ? denormalize : _denormalize; | ||
return method(schema, input, unvisit); | ||
} // null is considered intentional, thus always 'found' as true | ||
if (input === null) { | ||
return [input, true]; | ||
} | ||
if (typeof schema.getId === 'function' && typeof schema.normalize === 'function') { | ||
// unvisitEntity just can't handle undefined | ||
if (input === undefined) { | ||
return [input, false]; | ||
} | ||
return unvisitEntity(input, schema, unvisit, getEntity, cache); | ||
} | ||
if (typeof schema.denormalize === 'function') { | ||
return schema.denormalize(input, unvisit); | ||
} | ||
return [input, true]; | ||
}, cache]; | ||
}; | ||
var getEntities = function getEntities(entities) { | ||
var isImmutable$1 = isImmutable(entities); | ||
return function (entityOrId, schema) { | ||
var schemaKey = schema.key; | ||
if (typeof entityOrId === 'object') { | ||
return entityOrId; | ||
} | ||
if (isImmutable$1) { | ||
return entities.getIn([schemaKey, entityOrId.toString()]); | ||
} | ||
return entities[schemaKey] && entities[schemaKey][entityOrId]; | ||
}; | ||
}; | ||
var denormalize$1 = function denormalize(input, schema, entities) { | ||
/* istanbul ignore next */ | ||
// eslint-disable-next-line no-undef | ||
if (process.env.NODE_ENV !== 'production' && schema === undefined) throw new Error('shema needed'); | ||
if (typeof input !== 'undefined') { | ||
var _getUnvisit = getUnvisit(entities), | ||
unvisit = _getUnvisit[0], | ||
cache = _getUnvisit[1]; | ||
return [].concat(unvisit(input, schema), [cache]); | ||
} | ||
return [undefined, false, {}]; | ||
}; | ||
var getDefaultGetId = function getDefaultGetId(idAttribute) { | ||
return function (input) { | ||
return isImmutable(input) ? input.get(idAttribute) : input[idAttribute]; | ||
}; | ||
}; | ||
var EntitySchema = | ||
/*#__PURE__*/ | ||
function () { | ||
function EntitySchema(key, definition, options) { | ||
if (definition === void 0) { | ||
definition = {}; | ||
} | ||
if (options === void 0) { | ||
options = {}; | ||
} | ||
if (!key || typeof key !== 'string') { | ||
throw new Error("Expected a string key for Entity, but found " + key + "."); | ||
} | ||
var _options = options, | ||
_options$idAttribute = _options.idAttribute, | ||
idAttribute = _options$idAttribute === void 0 ? 'id' : _options$idAttribute, | ||
_options$mergeStrateg = _options.mergeStrategy, | ||
mergeStrategy = _options$mergeStrateg === void 0 ? function (entityA, entityB) { | ||
return Object.assign({}, entityA, {}, entityB); | ||
} : _options$mergeStrateg, | ||
_options$processStrat = _options.processStrategy, | ||
processStrategy = _options$processStrat === void 0 ? function (input) { | ||
return Object.assign({}, input); | ||
} : _options$processStrat; | ||
this._key = key; | ||
this._getId = typeof idAttribute === 'function' ? idAttribute : getDefaultGetId(idAttribute); | ||
this._idAttribute = idAttribute; | ||
this._mergeStrategy = mergeStrategy; | ||
this._processStrategy = processStrategy; | ||
this.define(definition); | ||
} | ||
var _proto = EntitySchema.prototype; | ||
_proto.define = function define(definition) { | ||
this.schema = Object.keys(definition).reduce(function (entitySchema, key) { | ||
var _Object$assign; | ||
var schema = definition[key]; | ||
return Object.assign({}, entitySchema, (_Object$assign = {}, _Object$assign[key] = schema, _Object$assign)); | ||
}, this.schema || {}); | ||
}; | ||
_proto.getId = function getId(input, parent, key) { | ||
return this._getId(input, parent, key); | ||
}; | ||
_proto.merge = function merge(entityA, entityB) { | ||
return this._mergeStrategy(entityA, entityB); | ||
}; | ||
_proto.normalize = function normalize(input, parent, key, visit, addEntity, visitedEntities) { | ||
var _this = this; | ||
var id = this.getId(input, parent, key); | ||
var entityType = this.key; | ||
if (!(entityType in visitedEntities)) { | ||
visitedEntities[entityType] = {}; | ||
} | ||
if (!(id in visitedEntities[entityType])) { | ||
visitedEntities[entityType][id] = []; | ||
} | ||
if (visitedEntities[entityType][id].some(function (entity) { | ||
return entity === input; | ||
})) { | ||
return id; | ||
} | ||
visitedEntities[entityType][id].push(input); | ||
var processedEntity = this._processStrategy(input, parent, key); | ||
Object.keys(this.schema).forEach(function (key) { | ||
if (Object.hasOwnProperty.call(processedEntity, key) && typeof processedEntity[key] === 'object') { | ||
var schema = _this.schema[key]; | ||
processedEntity[key] = visit(processedEntity[key], processedEntity, key, schema, addEntity, visitedEntities); | ||
} | ||
}); | ||
addEntity(this, processedEntity, input, parent, key); | ||
return id; | ||
}; | ||
_proto.denormalize = function denormalize(entity, unvisit) { | ||
var _this2 = this; | ||
if (isImmutable(entity)) { | ||
return denormalizeImmutable(this.schema, entity, unvisit); | ||
} | ||
var found = true; | ||
Object.keys(this.schema).forEach(function (key) { | ||
var schema = _this2.schema[key]; | ||
var _unvisit = unvisit(entity[key], schema), | ||
value = _unvisit[0], | ||
foundItem = _unvisit[1]; | ||
if (!foundItem) { | ||
found = false; | ||
} | ||
if (Object.hasOwnProperty.call(entity, key)) { | ||
entity[key] = value; | ||
} | ||
}); | ||
return [entity, found]; | ||
}; | ||
_createClass(EntitySchema, [{ | ||
key: "key", | ||
get: function get() { | ||
return this._key; | ||
} | ||
}, { | ||
key: "idAttribute", | ||
get: function get() { | ||
return this._idAttribute; | ||
} | ||
}]); | ||
return EntitySchema; | ||
}(); | ||
var UnionSchema = | ||
/*#__PURE__*/ | ||
function (_PolymorphicSchema) { | ||
_inheritsLoose(UnionSchema, _PolymorphicSchema); | ||
function UnionSchema(definition, schemaAttribute) { | ||
if (!schemaAttribute) { | ||
throw new Error('Expected option "schemaAttribute" not found on UnionSchema.'); | ||
} | ||
return _PolymorphicSchema.call(this, definition, schemaAttribute) || this; | ||
} | ||
var _proto = UnionSchema.prototype; | ||
_proto.normalize = function normalize(input, parent, key, visit, addEntity, visitedEntities) { | ||
return this.normalizeValue(input, parent, key, visit, addEntity, visitedEntities); | ||
}; | ||
_proto.denormalize = function denormalize(input, unvisit) { | ||
return this.denormalizeValue(input, unvisit); | ||
}; | ||
return UnionSchema; | ||
}(PolymorphicSchema); | ||
var ValuesSchema = | ||
/*#__PURE__*/ | ||
function (_PolymorphicSchema) { | ||
_inheritsLoose(ValuesSchema, _PolymorphicSchema); | ||
function ValuesSchema() { | ||
return _PolymorphicSchema.apply(this, arguments) || this; | ||
} | ||
var _proto = ValuesSchema.prototype; | ||
_proto.normalize = function normalize(input, parent, key, visit, addEntity, visitedEntities) { | ||
var _this = this; | ||
return Object.keys(input).reduce(function (output, key, index) { | ||
var _Object$assign; | ||
var value = input[key]; | ||
return value !== undefined && value !== null ? Object.assign({}, output, (_Object$assign = {}, _Object$assign[key] = _this.normalizeValue(value, input, key, visit, addEntity, visitedEntities), _Object$assign)) : output; | ||
}, {}); | ||
}; | ||
_proto.denormalize = function denormalize(input, unvisit) { | ||
var _this2 = this; | ||
var found = true; | ||
return [Object.keys(input).reduce(function (output, key) { | ||
var _Object$assign2; | ||
var entityOrId = input[key]; | ||
var _this2$denormalizeVal = _this2.denormalizeValue(entityOrId, unvisit), | ||
value = _this2$denormalizeVal[0], | ||
foundItem = _this2$denormalizeVal[1]; | ||
if (!foundItem) { | ||
found = false; | ||
} | ||
return Object.assign({}, output, (_Object$assign2 = {}, _Object$assign2[key] = value, _Object$assign2)); | ||
}, {}), found]; | ||
}; | ||
return ValuesSchema; | ||
}(PolymorphicSchema); | ||
var visit = function visit(value, parent, key, schema, addEntity, visitedEntities) { | ||
@@ -632,102 +727,3 @@ if (typeof value !== 'object' || !value || !schema) { | ||
var unvisitEntity = function unvisitEntity(id, schema, unvisit, getEntity, cache) { | ||
var entity = getEntity(id, schema); | ||
if (typeof entity !== 'object' || entity === null) { | ||
return [entity, false]; | ||
} | ||
if (!cache[schema.key]) { | ||
cache[schema.key] = {}; | ||
} | ||
var found = true; | ||
if (!cache[schema.key][id]) { | ||
// Ensure we don't mutate it non-immutable objects | ||
var entityCopy = isImmutable(entity) ? entity : Object.assign({}, entity); // 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; | ||
var _schema$denormalize = schema.denormalize(entityCopy, unvisit); | ||
cache[schema.key][id] = _schema$denormalize[0]; | ||
found = _schema$denormalize[1]; | ||
} | ||
return [cache[schema.key][id], found]; | ||
}; | ||
var getUnvisit = function getUnvisit(entities) { | ||
var cache = {}; | ||
var getEntity = getEntities(entities); | ||
return [function unvisit(input, schema) { | ||
if (!schema) return [input, true]; | ||
if (typeof schema === 'object' && (!schema.denormalize || typeof schema.denormalize !== 'function')) { | ||
var method = Array.isArray(schema) ? denormalize : _denormalize; | ||
return method(schema, input, unvisit); | ||
} // null is considered intentional, thus always 'found' as true | ||
if (input === null) { | ||
return [input, true]; | ||
} | ||
if (typeof schema.getId === 'function' && typeof schema.normalize === 'function') { | ||
// unvisitEntity just can't handle undefined | ||
if (input === undefined) { | ||
return [input, false]; | ||
} | ||
return unvisitEntity(input, schema, unvisit, getEntity, cache); | ||
} | ||
if (typeof schema.denormalize === 'function') { | ||
return schema.denormalize(input, unvisit); | ||
} | ||
return [input, true]; | ||
}, cache]; | ||
}; | ||
var getEntities = function getEntities(entities) { | ||
var isImmutable$1 = isImmutable(entities); | ||
return function (entityOrId, schema) { | ||
var schemaKey = schema.key; | ||
if (typeof entityOrId === 'object') { | ||
return entityOrId; | ||
} | ||
if (isImmutable$1) { | ||
return entities.getIn([schemaKey, entityOrId.toString()]); | ||
} | ||
return entities[schemaKey] && entities[schemaKey][entityOrId]; | ||
}; | ||
}; | ||
var denormalizeAndTracked = function denormalizeAndTracked(input, schema, entities) { | ||
/* istanbul ignore next */ | ||
// eslint-disable-next-line no-undef | ||
if (process.env.NODE_ENV !== 'production' && schema === undefined) throw new Error('shema needed'); | ||
if (typeof input !== 'undefined') { | ||
var _getUnvisit = getUnvisit(entities), | ||
unvisit = _getUnvisit[0], | ||
cache = _getUnvisit[1]; | ||
return [].concat(unvisit(input, schema), [cache]); | ||
} | ||
return [undefined, false, {}]; | ||
}; | ||
var denormalize$1 = function denormalize(input, schema, entities) { | ||
return denormalizeAndTracked(input, schema, entities).slice(0, 2); | ||
}; | ||
exports.denormalize = denormalize$1; | ||
exports.denormalizeAndTracked = denormalizeAndTracked; | ||
exports.normalize = normalize$1; | ||
@@ -734,0 +730,0 @@ exports.schema = schema; |
@@ -1,1 +0,1 @@ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e=e||self).rest_hooks_normalizr={})}(this,(function(e){"use strict";function t(e){return!(!e||"function"!=typeof e.hasOwnProperty||!(Object.hasOwnProperty.call(e,"__ownerID")||e._map&&Object.hasOwnProperty.call(e._map,"__ownerID")))}function n(e,t,n){let r=!0;return[Object.keys(e).reduce((t,i)=>{const s=""+i,[o,c]=n(t.get(s),e[s]);return c||(r=!1),t.has(s)?t.set(s,o):t},t),r]}class r{constructor(e,t){t&&(this._schemaAttribute="string"==typeof t?e=>e[t]:t),this.define(e)}get isSingleSchema(){return!this._schemaAttribute}define(e){this.schema=e}getSchemaAttribute(e,t,n){return!this.isSingleSchema&&this._schemaAttribute(e,t,n)}inferSchema(e,t,n){if(this.isSingleSchema)return this.schema;const r=this.getSchemaAttribute(e,t,n);return this.schema[r]}normalizeValue(e,t,n,r,i,s){const o=this.inferSchema(e,t,n);if(!o)return e;const c=r(e,t,n,o,i,s);return this.isSingleSchema||null==c?c:{id:c,schema:this.getSchemaAttribute(e,t,n)}}denormalizeValue(e,n){const r=t(e)?e.get("schema"):e.schema;return this.isSingleSchema||r?n((t(e)?e.get("id"):e.id)||e,this.isSingleSchema?this.schema:this.schema[r]):[e,!0]}}const i=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]},s=e=>Array.isArray(e)?e:Object.keys(e).map(t=>e[t]),o=(e,t,n,r,o,c,a)=>(e=i(e),s(t).map((t,i)=>o(t,n,r,e,c,a))),c=(e,t,n)=>{e=i(e);let r=!0;return void 0===t&&e&&([,r]=n(void 0,e)),[t&&t.map?t.map(t=>n(t,e)).filter(([,e])=>e).map(([e])=>e):t,r]};const a=(e,t,n,r,i,s,o)=>{const c=Object.assign({},t);return Object.keys(e).forEach(n=>{const r=e[n],a=i(t[n],t,n,r,s,o);null==a?delete c[n]:c[n]=a}),c},u=(e,r,i)=>{if(t(r))return n(e,r,i);const s=Object.assign({},r);let o=!0;return Object.keys(e).forEach(t=>{const[n,r]=i(s[t],e[t]);void 0!==s[t]&&(s[t]=n),r||(o=!1)}),[s,o]};const h=(e,t,n,r,i,s)=>{if("object"!=typeof e||!e||!r)return e;if("object"==typeof r&&(!r.normalize||"function"!=typeof r.normalize)){return(Array.isArray(r)?o:a)(r,e,t,n,h,i,s)}return r.normalize(e,t,n,h,i,s)},l={Array:class extends r{normalize(e,t,n,r,i,o){return s(e).map((e,s)=>this.normalizeValue(e,t,n,r,i,o)).filter(e=>null!=e)}denormalize(e,t){let n=!0;return void 0===e&&this.schema&&([,n]=t(void 0,this.schema)),[e&&e.map?e.map(e=>this.denormalizeValue(e,t)).filter(([,e])=>e).map(([e])=>e):e,n]}},Entity:class{constructor(e,n={},r={}){if(!e||"string"!=typeof e)throw new Error("Expected a string key for Entity, but found "+e+".");const{idAttribute:i="id",mergeStrategy:s=((e,t)=>Object.assign({},e,{},t)),processStrategy:o=(e=>Object.assign({},e))}=r;this._key=e,this._getId="function"==typeof i?i:(e=>n=>t(n)?n.get(e):n[e])(i),this._idAttribute=i,this._mergeStrategy=s,this._processStrategy=o,this.define(n)}get key(){return this._key}get idAttribute(){return this._idAttribute}define(e){this.schema=Object.keys(e).reduce((t,n)=>{const r=e[n];return Object.assign({},t,{[n]:r})},this.schema||{})}getId(e,t,n){return this._getId(e,t,n)}merge(e,t){return this._mergeStrategy(e,t)}normalize(e,t,n,r,i,s){const o=this.getId(e,t,n),c=this.key;if(c in s||(s[c]={}),o in s[c]||(s[c][o]=[]),s[c][o].some(t=>t===e))return o;s[c][o].push(e);const a=this._processStrategy(e,t,n);return Object.keys(this.schema).forEach(e=>{if(Object.hasOwnProperty.call(a,e)&&"object"==typeof a[e]){const t=this.schema[e];a[e]=r(a[e],a,e,t,i,s)}}),i(this,a,e,t,n),o}denormalize(e,r){if(t(e))return n(this.schema,e,r);let i=!0;return Object.keys(this.schema).forEach(t=>{const n=this.schema[t],[s,o]=r(e[t],n);o||(i=!1),Object.hasOwnProperty.call(e,t)&&(e[t]=s)}),[e,i]}},Object:class{constructor(e){this.define(e)}define(e){this.schema=Object.keys(e).reduce((t,n)=>{const r=e[n];return Object.assign({},t,{[n]:r})},this.schema||{})}normalize(...e){return a(this.schema,...e)}denormalize(...e){return u(this.schema,...e)}},Union:class extends r{constructor(e,t){if(!t)throw new Error('Expected option "schemaAttribute" not found on UnionSchema.');super(e,t)}normalize(e,t,n,r,i,s){return this.normalizeValue(e,t,n,r,i,s)}denormalize(e,t){return this.denormalizeValue(e,t)}},Values:class extends r{normalize(e,t,n,r,i,s){return Object.keys(e).reduce((t,n,o)=>{const c=e[n];return null!=c?Object.assign({},t,{[n]:this.normalizeValue(c,e,n,r,i,s)}):t},{})}denormalize(e,t){let n=!0;return[Object.keys(e).reduce((r,i)=>{const s=e[i],[o,c]=this.denormalizeValue(s,t);return c||(n=!1),Object.assign({},r,{[i]:o})},{}),n]}}};const m=e=>{const n={},r=d(e);return[function e(i,s){if(!s)return[i,!0];if("object"==typeof s&&(!s.denormalize||"function"!=typeof s.denormalize)){return(Array.isArray(s)?c:u)(s,i,e)}return null===i?[i,!0]:"function"==typeof s.getId&&"function"==typeof s.normalize?void 0===i?[i,!1]:((e,n,r,i,s)=>{const o=i(e,n);if("object"!=typeof o||null===o)return[o,!1];s[n.key]||(s[n.key]={});let c=!0;if(!s[n.key][e]){const i=t(o)?o:Object.assign({},o);s[n.key][e]=i,[s[n.key][e],c]=n.denormalize(i,r)}return[s[n.key][e],c]})(i,s,e,r,n):"function"==typeof s.denormalize?s.denormalize(i,e):[i,!0]},n]},d=e=>{const n=t(e);return(t,r)=>{const i=r.key;return"object"==typeof t?t:n?e.getIn([i,t.toString()]):e[i]&&e[i][t]}},f=(e,t,n)=>{if("production"!==process.env.NODE_ENV&&void 0===t)throw new Error("shema needed");if(void 0!==e){const[r,i]=m(n);return[...r(e,t),i]}return[void 0,!1,{}]};e.denormalize=(e,t,n)=>f(e,t,n).slice(0,2),e.denormalizeAndTracked=f,e.normalize=(e,t)=>{const n=function(e){return["object","function"].includes(typeof e)?"object":typeof e}(t);if(null===e||typeof e!==n)throw new Error('Unexpected input given to normalize. Expected type to be "'+n+'", found "'+(null===e?"null":typeof e)+'".');const r={},i={},s=((e,t)=>(n,r,i,s,o)=>{const c=n.key,a=n.getId(i,s,o);c in e||(e[c]={});const u=e[c][a];if(e[c][a]=u?n.merge(u,r):r,Array.isArray(n.indexes)){const r=e[c][a];c in t||(t[c]={});for(const e of n.indexes){e in t[c]||(t[c][e]={});const n=t[c][e];u&&delete n[u[e]],e in r?n[r[e]]=a:"production"!==process.env.NODE_ENV&&console.warn("Index not found in entity. Indexes must be top-level members of your entity.\nIndex: "+e+"\nEntity: "+JSON.stringify(r,void 0,2))}}})(r,i);return{entities:r,indexes:i,result:h(e,e,void 0,t,s,{})}},e.schema=l,Object.defineProperty(e,"__esModule",{value:!0})})); | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e=e||self).rest_hooks_normalizr={})}(this,(function(e){"use strict";function t(e){return!(!e||"function"!=typeof e.hasOwnProperty||!(Object.hasOwnProperty.call(e,"__ownerID")||e._map&&Object.hasOwnProperty.call(e._map,"__ownerID")))}function n(e,t,n){let r=!0;return[Object.keys(e).reduce((t,i)=>{const s=""+i,[o,c]=n(t.get(s),e[s]);return c||(r=!1),t.has(s)?t.set(s,o):t},t),r]}class r{constructor(e,t){t&&(this._schemaAttribute="string"==typeof t?e=>e[t]:t),this.define(e)}get isSingleSchema(){return!this._schemaAttribute}define(e){this.schema=e}getSchemaAttribute(e,t,n){return!this.isSingleSchema&&this._schemaAttribute(e,t,n)}inferSchema(e,t,n){if(this.isSingleSchema)return this.schema;const r=this.getSchemaAttribute(e,t,n);return this.schema[r]}normalizeValue(e,t,n,r,i,s){const o=this.inferSchema(e,t,n);if(!o)return e;const c=r(e,t,n,o,i,s);return this.isSingleSchema||null==c?c:{id:c,schema:this.getSchemaAttribute(e,t,n)}}denormalizeValue(e,n){const r=t(e)?e.get("schema"):e.schema;return this.isSingleSchema||r?n((this.isSingleSchema?void 0:t(e)?e.get("id"):e.id)||e,this.isSingleSchema?this.schema:this.schema[r]):[e,!0]}}const i=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]},s=e=>Array.isArray(e)?e:Object.keys(e).map(t=>e[t]),o=(e,t,n,r,o,c,a)=>(e=i(e),s(t).map((t,i)=>o(t,n,r,e,c,a))),c=(e,t,n)=>{e=i(e);let r=!0;return void 0===t&&e&&([,r]=n(void 0,e)),[t&&t.map?t.map(t=>n(t,e)).filter(([,e])=>e).map(([e])=>e):t,r]};const a=(e,t,n,r,i,s,o)=>{const c=Object.assign({},t);return Object.keys(e).forEach(n=>{const r=e[n],a=i(t[n],t,n,r,s,o);null==a?delete c[n]:c[n]=a}),c},u=(e,r,i)=>{if(t(r))return n(e,r,i);const s=Object.assign({},r);let o=!0;return Object.keys(e).forEach(t=>{const[n,r]=i(s[t],e[t]);void 0!==s[t]&&(s[t]=n),r||(o=!1)}),[s,o]};const h=e=>{const n={},r=l(e);return[function e(i,s){if(!s)return[i,!0];if("object"==typeof s&&(!s.denormalize||"function"!=typeof s.denormalize)){return(Array.isArray(s)?c:u)(s,i,e)}return null===i?[i,!0]:"function"==typeof s.getId&&"function"==typeof s.normalize?void 0===i?[i,!1]:((e,n,r,i,s)=>{const o=i(e,n);if("object"!=typeof o||null===o)return[o,!1];s[n.key]||(s[n.key]={});let c=!0;if(!s[n.key][e]){const i=t(o)?o:Object.assign({},o);s[n.key][e]=i,[s[n.key][e],c]=n.denormalize(i,r)}return[s[n.key][e],c]})(i,s,e,r,n):"function"==typeof s.denormalize?s.denormalize(i,e):[i,!0]},n]},l=e=>{const n=t(e);return(t,r)=>{const i=r.key;return"object"==typeof t?t:n?e.getIn([i,t.toString()]):e[i]&&e[i][t]}};const m=(e,t,n,r,i,s)=>{if("object"!=typeof e||!e||!r)return e;if("object"==typeof r&&(!r.normalize||"function"!=typeof r.normalize)){return(Array.isArray(r)?o:a)(r,e,t,n,m,i,s)}return r.normalize(e,t,n,m,i,s)},d={Array:class extends r{normalize(e,t,n,r,i,o){return s(e).map((e,s)=>this.normalizeValue(e,t,n,r,i,o)).filter(e=>null!=e)}denormalize(e,t){let n=!0;return void 0===e&&this.schema&&([,n]=t(void 0,this.schema)),[e&&e.map?e.map(e=>this.denormalizeValue(e,t)).filter(([,e])=>e).map(([e])=>e):e,n]}},Entity:class{constructor(e,n={},r={}){if(!e||"string"!=typeof e)throw new Error("Expected a string key for Entity, but found "+e+".");const{idAttribute:i="id",mergeStrategy:s=((e,t)=>Object.assign({},e,{},t)),processStrategy:o=(e=>Object.assign({},e))}=r;this._key=e,this._getId="function"==typeof i?i:(e=>n=>t(n)?n.get(e):n[e])(i),this._idAttribute=i,this._mergeStrategy=s,this._processStrategy=o,this.define(n)}get key(){return this._key}get idAttribute(){return this._idAttribute}define(e){this.schema=Object.keys(e).reduce((t,n)=>{const r=e[n];return Object.assign({},t,{[n]:r})},this.schema||{})}getId(e,t,n){return this._getId(e,t,n)}merge(e,t){return this._mergeStrategy(e,t)}normalize(e,t,n,r,i,s){const o=this.getId(e,t,n),c=this.key;if(c in s||(s[c]={}),o in s[c]||(s[c][o]=[]),s[c][o].some(t=>t===e))return o;s[c][o].push(e);const a=this._processStrategy(e,t,n);return Object.keys(this.schema).forEach(e=>{if(Object.hasOwnProperty.call(a,e)&&"object"==typeof a[e]){const t=this.schema[e];a[e]=r(a[e],a,e,t,i,s)}}),i(this,a,e,t,n),o}denormalize(e,r){if(t(e))return n(this.schema,e,r);let i=!0;return Object.keys(this.schema).forEach(t=>{const n=this.schema[t],[s,o]=r(e[t],n);o||(i=!1),Object.hasOwnProperty.call(e,t)&&(e[t]=s)}),[e,i]}},Object:class{constructor(e){this.define(e)}define(e){this.schema=Object.keys(e).reduce((t,n)=>{const r=e[n];return Object.assign({},t,{[n]:r})},this.schema||{})}normalize(...e){return a(this.schema,...e)}denormalize(...e){return u(this.schema,...e)}},Union:class extends r{constructor(e,t){if(!t)throw new Error('Expected option "schemaAttribute" not found on UnionSchema.');super(e,t)}normalize(e,t,n,r,i,s){return this.normalizeValue(e,t,n,r,i,s)}denormalize(e,t){return this.denormalizeValue(e,t)}},Values:class extends r{normalize(e,t,n,r,i,s){return Object.keys(e).reduce((t,n,o)=>{const c=e[n];return null!=c?Object.assign({},t,{[n]:this.normalizeValue(c,e,n,r,i,s)}):t},{})}denormalize(e,t){let n=!0;return[Object.keys(e).reduce((r,i)=>{const s=e[i],[o,c]=this.denormalizeValue(s,t);return c||(n=!1),Object.assign({},r,{[i]:o})},{}),n]}}};e.denormalize=(e,t,n)=>{if("production"!==process.env.NODE_ENV&&void 0===t)throw new Error("shema needed");if(void 0!==e){const[r,i]=h(n);return[...r(e,t),i]}return[void 0,!1,{}]},e.normalize=(e,t)=>{const n=function(e){return["object","function"].includes(typeof e)?"object":typeof e}(t);if(null===e||typeof e!==n)throw new Error('Unexpected input given to normalize. Expected type to be "'+n+'", found "'+(null===e?"null":typeof e)+'".');const r={},i={},s=((e,t)=>(n,r,i,s,o)=>{const c=n.key,a=n.getId(i,s,o);c in e||(e[c]={});const u=e[c][a];if(e[c][a]=u?n.merge(u,r):r,Array.isArray(n.indexes)){const r=e[c][a];c in t||(t[c]={});for(const e of n.indexes){e in t[c]||(t[c][e]={});const n=t[c][e];u&&delete n[u[e]],e in r?n[r[e]]=a:"production"!==process.env.NODE_ENV&&console.warn("Index not found in entity. Indexes must be top-level members of your entity.\nIndex: "+e+"\nEntity: "+JSON.stringify(r,void 0,2))}}})(r,i);return{entities:r,indexes:i,result:m(e,e,void 0,t,s,{})}},e.schema=d,Object.defineProperty(e,"__esModule",{value:!0})})); |
{ | ||
"name": "@rest-hooks/normalizr", | ||
"version": "4.3.3", | ||
"version": "5.0.0", | ||
"description": "Normalizes and denormalizes JSON according to schema for Redux and Flux applications", | ||
@@ -29,3 +29,3 @@ "homepage": "https://github.com/coinbase/rest-hooks/tree/master/packages/normalizr#readme", | ||
"main": "dist/normalizr.js", | ||
"module": "dist/normalizr.es.js", | ||
"module": "dist/normalizr.es.min.js", | ||
"typings": "src/index.d.ts", | ||
@@ -64,3 +64,3 @@ "sideEffects": false, | ||
}, | ||
"gitHead": "75849f63bea3fe910e8f2202306e5ab42de913a8" | ||
"gitHead": "3da6f649bd4f81f85be01742f02e13713abcbf5e" | ||
} |
@@ -335,10 +335,4 @@ declare namespace schema { | ||
entities: any, | ||
): [Denormalize<S>, true] | [DenormalizeNullable<S>, false]; | ||
export function denormalizeAndTracked<S extends Schema>( | ||
input: any, | ||
schema: S, | ||
entities: any, | ||
): | ||
| [Denormalize<S>, true, { [key: string]: { [key: string]: any } }] | ||
| [DenormalizeNullable<S>, false, { [key: string]: { [key: string]: any } }]; |
160106
3241