@rest-hooks/normalizr
Advanced tools
Comparing version 4.2.0 to 4.3.0
@@ -6,2 +6,11 @@ # Change Log | ||
## [4.3.0](https://github.com/coinbase/rest-hooks/compare/@rest-hooks/normalizr@4.2.0...@rest-hooks/normalizr@4.3.0) (2020-01-17) | ||
### 🚀 Features | ||
* Add indexes to Entity for improved performance ([#237](https://github.com/coinbase/rest-hooks/issues/237)) ([a2339f0](https://github.com/coinbase/rest-hooks/commit/a2339f0e61e9446da87af85440061b060ad0f444)) | ||
## [4.2.0](https://github.com/coinbase/rest-hooks/compare/@rest-hooks/normalizr@4.1.1...@rest-hooks/normalizr@4.2.0) (2020-01-06) | ||
@@ -8,0 +17,0 @@ |
@@ -530,3 +530,3 @@ define(['exports'], function (exports) { 'use strict'; | ||
var addEntities = function addEntities(entities) { | ||
var addEntities = function addEntities(entities, indexes) { | ||
return function (schema, processedEntity, value, parent, key) { | ||
@@ -546,2 +546,45 @@ var schemaKey = schema.key; | ||
entities[schemaKey][id] = processedEntity; | ||
} // update index | ||
if (Array.isArray(schema.indexes)) { | ||
var entity = entities[schemaKey][id]; | ||
if (!(schemaKey in indexes)) { | ||
indexes[schemaKey] = {}; | ||
} | ||
for (var _iterator = schema.indexes, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) { | ||
var _ref; | ||
if (_isArray) { | ||
if (_i >= _iterator.length) break; | ||
_ref = _iterator[_i++]; | ||
} else { | ||
_i = _iterator.next(); | ||
if (_i.done) break; | ||
_ref = _i.value; | ||
} | ||
var index = _ref; | ||
if (!(index in indexes[schemaKey])) { | ||
indexes[schemaKey][index] = {}; | ||
} | ||
var indexMap = indexes[schemaKey][index]; | ||
if (existingEntity) { | ||
delete indexMap[existingEntity[index]]; | ||
} | ||
if (index in entity) { | ||
indexMap[entity[index]] = id; | ||
} | ||
/* istanbul ignore next */ | ||
else if ( // eslint-disable-next-line no-undef | ||
process.env.NODE_ENV !== 'production') { | ||
console.warn("Index not found in entity. Indexes must be top-level members of your entity.\nIndex: " + index + "\nEntity: " + JSON.stringify(entity, undefined, 2)); | ||
} | ||
} | ||
} | ||
@@ -571,3 +614,4 @@ }; | ||
var entities = {}; | ||
var addEntity = addEntities(entities); | ||
var indexes = {}; | ||
var addEntity = addEntities(entities, indexes); | ||
var visitedEntities = {}; | ||
@@ -577,2 +621,3 @@ var result = visit(input, input, undefined, schema, addEntity, visitedEntities); | ||
entities: entities, | ||
indexes: indexes, | ||
result: result | ||
@@ -579,0 +624,0 @@ }; |
@@ -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]}const n=e=>r=>t(r)?r.get(e):r[e];class i{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 s=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]},o=e=>Array.isArray(e)?e:Object.keys(e).map(t=>e[t]),c=(e,t,r,n,i,c,a)=>{return e=s(e),o(t).map((t,s)=>i(t,r,n,e,c,a))},a=(e,t,r)=>{e=s(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 u=(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},h=(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]);null!=s[t]&&(s[t]=r),n||(o=!1)}),[s,o]};const l=(e,t,r,n,i,s)=>{if("object"!=typeof e||!e)return e;if("object"==typeof n&&(!n.normalize||"function"!=typeof n.normalize)){return(Array.isArray(n)?c:u)(n,e,t,r,l,i,s)}return n.normalize(e,t,r,l,i,s)},m={Array:class extends i{normalize(e,t,r,n,i,s){return o(e).map((e,o)=>this.normalizeValue(e,t,r,n,i,s)).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,t={},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:n(i),this._idAttribute=i,this._mergeStrategy=s,this._processStrategy=o,this.define(t)}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 u(this.schema,...e)}denormalize(...e){return h(this.schema,...e)}},Union:class extends i{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 i{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 f=e=>{const r={},n=d(e);return function e(i,s){if("object"==typeof s&&(!s.denormalize||"function"!=typeof s.denormalize)){return(Array.isArray(s)?a:h)(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]}},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]}};e.denormalize=(e,t,r)=>void 0!==e?f(r)(e,t):[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=(e=>(t,r,n,i,s)=>{const o=t.key,c=t.getId(n,i,s);o in e||(e[o]={});const a=e[o][c];e[o][c]=a?t.merge(a,r):r})(n);return{entities:n,result:l(e,e,void 0,t,i,{})}},e.schema=m,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((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]);null!=s[t]&&(s[t]=n),r||(o=!1)}),[s,o]};const h=(e,t,n,r,i,s)=>{if("object"!=typeof e||!e)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("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]}},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]}};e.denormalize=(e,t,n)=>void 0!==e?m(n)(e,t):[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:h(e,e,void 0,t,s,{})}},e.schema=l,Object.defineProperty(e,"__esModule",{value:!0})})); |
@@ -531,3 +531,3 @@ var rest_hooks_normalizr = (function (exports) { | ||
var addEntities = function addEntities(entities) { | ||
var addEntities = function addEntities(entities, indexes) { | ||
return function (schema, processedEntity, value, parent, key) { | ||
@@ -547,2 +547,45 @@ var schemaKey = schema.key; | ||
entities[schemaKey][id] = processedEntity; | ||
} // update index | ||
if (Array.isArray(schema.indexes)) { | ||
var entity = entities[schemaKey][id]; | ||
if (!(schemaKey in indexes)) { | ||
indexes[schemaKey] = {}; | ||
} | ||
for (var _iterator = schema.indexes, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) { | ||
var _ref; | ||
if (_isArray) { | ||
if (_i >= _iterator.length) break; | ||
_ref = _iterator[_i++]; | ||
} else { | ||
_i = _iterator.next(); | ||
if (_i.done) break; | ||
_ref = _i.value; | ||
} | ||
var index = _ref; | ||
if (!(index in indexes[schemaKey])) { | ||
indexes[schemaKey][index] = {}; | ||
} | ||
var indexMap = indexes[schemaKey][index]; | ||
if (existingEntity) { | ||
delete indexMap[existingEntity[index]]; | ||
} | ||
if (index in entity) { | ||
indexMap[entity[index]] = id; | ||
} | ||
/* istanbul ignore next */ | ||
else if ( // eslint-disable-next-line no-undef | ||
process.env.NODE_ENV !== 'production') { | ||
console.warn("Index not found in entity. Indexes must be top-level members of your entity.\nIndex: " + index + "\nEntity: " + JSON.stringify(entity, undefined, 2)); | ||
} | ||
} | ||
} | ||
@@ -572,3 +615,4 @@ }; | ||
var entities = {}; | ||
var addEntity = addEntities(entities); | ||
var indexes = {}; | ||
var addEntity = addEntities(entities, indexes); | ||
var visitedEntities = {}; | ||
@@ -578,2 +622,3 @@ var result = visit(input, input, undefined, schema, addEntity, visitedEntities); | ||
entities: entities, | ||
indexes: indexes, | ||
result: result | ||
@@ -580,0 +625,0 @@ }; |
@@ -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,s)=>{const i=""+s,[o,c]=r(t.get(i),e[i]);return c||(n=!1),t.has(i)?t.set(i,o):t},t),n]}const n=e=>r=>t(r)?r.get(e):r[e];class s{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,s,i){const o=this.inferSchema(e,t,r);if(!o)return e;const c=n(e,t,r,o,s,i);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]},o=e=>Array.isArray(e)?e:Object.keys(e).map(t=>e[t]),c=(e,t,r,n,s,c,a)=>{return e=i(e),o(t).map((t,i)=>s(t,r,n,e,c,a))},a=(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 u=(e,t,r,n,s,i,o)=>{const c=Object.assign({},t);return Object.keys(e).forEach(r=>{const n=e[r],a=s(t[r],t,r,n,i,o);null==a?delete c[r]:c[r]=a}),c},h=(e,n,s)=>{if(t(n))return r(e,n,s);const i=Object.assign({},n);let o=!0;return Object.keys(e).forEach(t=>{const[r,n]=s(i[t],e[t]);null!=i[t]&&(i[t]=r),n||(o=!1)}),[i,o]};const l=(e,t,r,n,s,i)=>{if("object"!=typeof e||!e)return e;if("object"==typeof n&&(!n.normalize||"function"!=typeof n.normalize)){return(Array.isArray(n)?c:u)(n,e,t,r,l,s,i)}return n.normalize(e,t,r,l,s,i)},m={Array:class extends s{normalize(e,t,r,n,s,i){return o(e).map((e,o)=>this.normalizeValue(e,t,r,n,s,i)).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,t={},r={}){if(!e||"string"!=typeof e)throw new Error("Expected a string key for Entity, but found "+e+".");const{idAttribute:s="id",mergeStrategy:i=((e,t)=>Object.assign({},e,{},t)),processStrategy:o=(e=>Object.assign({},e))}=r;this._key=e,this._getId="function"==typeof s?s:n(s),this._idAttribute=s,this._mergeStrategy=i,this._processStrategy=o,this.define(t)}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,s,i){const o=this.getId(e,t,r),c=this.key;if(c in i||(i[c]={}),o in i[c]||(i[c][o]=[]),i[c][o].some(t=>t===e))return o;i[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,s,i)}}),s(this,a,e,t,r),o}denormalize(e,n){if(t(e))return r(this.schema,e,n);let s=!0;return Object.keys(this.schema).forEach(t=>{const r=this.schema[t],[i,o]=n(e[t],r);o||(s=!1),Object.hasOwnProperty.call(e,t)&&(e[t]=i)}),[e,s]}},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 u(this.schema,...e)}denormalize(...e){return h(this.schema,...e)}},Union:class extends s{constructor(e,t){if(!t)throw new Error('Expected option "schemaAttribute" not found on UnionSchema.');super(e,t)}normalize(e,t,r,n,s,i){return this.normalizeValue(e,t,r,n,s,i)}denormalize(e,t){return this.denormalizeValue(e,t)}},Values:class extends s{normalize(e,t,r,n,s,i){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,s,i)}):t},{})}denormalize(e,t){let r=!0;return[Object.keys(e).reduce((n,s)=>{const i=e[s],[o,c]=this.denormalizeValue(i,t);return c||(r=!1),Object.assign({},n,{[s]:o})},{}),r]}}};const f=e=>{const r={},n=y(e);return function e(s,i){if("object"==typeof i&&(!i.denormalize||"function"!=typeof i.denormalize)){return(Array.isArray(i)?a:h)(i,s,e)}return null===s?[s,!0]:"function"==typeof i.getId&&"function"==typeof i.normalize?void 0===s?[s,!1]:((e,r,n,s,i)=>{const o=s(e,r);if("object"!=typeof o||null===o)return[o,!1];i[r.key]||(i[r.key]={});let c=!0;if(!i[r.key][e]){const s=t(o)?o:Object.assign({},o);i[r.key][e]=s,[i[r.key][e],c]=r.denormalize(s,n)}return[i[r.key][e],c]})(s,i,e,n,r):"function"==typeof i.denormalize?i.denormalize(s,e):[s,!0]}},y=e=>{const r=t(e);return(t,n)=>{const s=n.key;return"object"==typeof t?t:r?e.getIn([s,t.toString()]):e[s]&&e[s][t]}};return e.denormalize=(e,t,r)=>void 0!==e?f(r)(e,t):[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={},s=(e=>(t,r,n,s,i)=>{const o=t.key,c=t.getId(n,s,i);o in e||(e[o]={});const a=e[o][c];e[o][c]=a?t.merge(a,r):r})(n);return{entities:n,result:l(e,e,void 0,t,s,{})}},e.schema=m,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 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]);null!=s[t]&&(s[t]=n),r||(o=!1)}),[s,o]};const h=(e,t,n,r,i,s)=>{if("object"!=typeof e||!e)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=f(e);return function e(i,s){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]}},f=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]}};return e.denormalize=(e,t,n)=>void 0!==e?m(n)(e,t):[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:h(e,e,void 0,t,s,{})}},e.schema=l,e}({}); |
@@ -509,3 +509,3 @@ import _createClass from '@babel/runtime/helpers/esm/createClass'; | ||
var addEntities = function addEntities(entities) { | ||
var addEntities = function addEntities(entities, indexes) { | ||
return function (schema, processedEntity, value, parent, key) { | ||
@@ -525,2 +525,45 @@ var schemaKey = schema.key; | ||
entities[schemaKey][id] = processedEntity; | ||
} // update index | ||
if (Array.isArray(schema.indexes)) { | ||
var entity = entities[schemaKey][id]; | ||
if (!(schemaKey in indexes)) { | ||
indexes[schemaKey] = {}; | ||
} | ||
for (var _iterator = schema.indexes, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) { | ||
var _ref; | ||
if (_isArray) { | ||
if (_i >= _iterator.length) break; | ||
_ref = _iterator[_i++]; | ||
} else { | ||
_i = _iterator.next(); | ||
if (_i.done) break; | ||
_ref = _i.value; | ||
} | ||
var index = _ref; | ||
if (!(index in indexes[schemaKey])) { | ||
indexes[schemaKey][index] = {}; | ||
} | ||
var indexMap = indexes[schemaKey][index]; | ||
if (existingEntity) { | ||
delete indexMap[existingEntity[index]]; | ||
} | ||
if (index in entity) { | ||
indexMap[entity[index]] = id; | ||
} | ||
/* istanbul ignore next */ | ||
else if ( // eslint-disable-next-line no-undef | ||
process.env.NODE_ENV !== 'production') { | ||
console.warn("Index not found in entity. Indexes must be top-level members of your entity.\nIndex: " + index + "\nEntity: " + JSON.stringify(entity, undefined, 2)); | ||
} | ||
} | ||
} | ||
@@ -550,3 +593,4 @@ }; | ||
var entities = {}; | ||
var addEntity = addEntities(entities); | ||
var indexes = {}; | ||
var addEntity = addEntities(entities, indexes); | ||
var visitedEntities = {}; | ||
@@ -556,2 +600,3 @@ var result = visit(input, input, undefined, schema, addEntity, visitedEntities); | ||
entities: entities, | ||
indexes: indexes, | ||
result: result | ||
@@ -558,0 +603,0 @@ }; |
@@ -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,s)=>{const i=""+s,[o,c]=r(t.get(i),e[i]);return c||(n=!1),t.has(i)?t.set(i,o):t},t),n]}const r=t=>r=>e(r)?r.get(t):r[t];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,s,i){const o=this.inferSchema(e,t,r);if(!o)return e;const c=n(e,t,r,o,s,i);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 s=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]),o=(e,t,r,n,o,c,a)=>{return e=s(e),i(t).map((t,s)=>o(t,r,n,e,c,a))},c=(e,t,r)=>{e=s(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,s,i,o)=>{const c=Object.assign({},t);return Object.keys(e).forEach(r=>{const n=e[r],a=s(t[r],t,r,n,i,o);null==a?delete c[r]:c[r]=a}),c},u=(r,n,s)=>{if(e(n))return t(r,n,s);const i=Object.assign({},n);let o=!0;return Object.keys(r).forEach(e=>{const[t,n]=s(i[e],r[e]);null!=i[e]&&(i[e]=t),n||(o=!1)}),[i,o]};const h=(e,t,r,n,s,i)=>{if("object"!=typeof e||!e)return e;if("object"==typeof n&&(!n.normalize||"function"!=typeof n.normalize)){return(Array.isArray(n)?o:a)(n,e,t,r,h,s,i)}return n.normalize(e,t,r,h,s,i)},l={Array:class extends n{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(e,t={},n={}){if(!e||"string"!=typeof e)throw new Error("Expected a string key for Entity, but found "+e+".");const{idAttribute:s="id",mergeStrategy:i=((e,t)=>Object.assign({},e,{},t)),processStrategy:o=(e=>Object.assign({},e))}=n;this._key=e,this._getId="function"==typeof s?s:r(s),this._idAttribute=s,this._mergeStrategy=i,this._processStrategy=o,this.define(t)}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,s,i){const o=this.getId(e,t,r),c=this.key;if(c in i||(i[c]={}),o in i[c]||(i[c][o]=[]),i[c][o].some(t=>t===e))return o;i[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,s,i)}}),s(this,a,e,t,r),o}denormalize(r,n){if(e(r))return t(this.schema,r,n);let s=!0;return Object.keys(this.schema).forEach(e=>{const t=this.schema[e],[i,o]=n(r[e],t);o||(s=!1),Object.hasOwnProperty.call(r,e)&&(r[e]=i)}),[r,s]}},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,s,i){return this.normalizeValue(e,t,r,n,s,i)}denormalize(e,t){return this.denormalizeValue(e,t)}},Values:class extends n{normalize(e,t,r,n,s,i){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,s,i)}):t},{})}denormalize(e,t){let r=!0;return[Object.keys(e).reduce((n,s)=>{const i=e[s],[o,c]=this.denormalizeValue(i,t);return c||(r=!1),Object.assign({},n,{[s]:o})},{}),r]}}};const m=(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={},s=(e=>(t,r,n,s,i)=>{const o=t.key,c=t.getId(n,s,i);o in e||(e[o]={});const a=e[o][c];e[o][c]=a?t.merge(a,r):r})(n);return{entities:n,result:h(e,e,void 0,t,s,{})}},f=t=>{const r={},n=y(t);return function t(s,i){if("object"==typeof i&&(!i.denormalize||"function"!=typeof i.denormalize)){return(Array.isArray(i)?c:u)(i,s,t)}return null===s?[s,!0]:"function"==typeof i.getId&&"function"==typeof i.normalize?void 0===s?[s,!1]:((t,r,n,s,i)=>{const o=s(t,r);if("object"!=typeof o||null===o)return[o,!1];i[r.key]||(i[r.key]={});let c=!0;if(!i[r.key][t]){const s=e(o)?o:Object.assign({},o);i[r.key][t]=s,[i[r.key][t],c]=r.denormalize(s,n)}return[i[r.key][t],c]})(s,i,t,n,r):"function"==typeof i.denormalize?i.denormalize(s,t):[s,!0]}},y=t=>{const r=e(t);return(e,n)=>{const s=n.key;return"object"==typeof e?e:r?t.getIn([s,e.toString()]):t[s]&&t[s][e]}},d=(e,t,r)=>void 0!==e?f(r)(e,t):[void 0,!1];export{d as denormalize,m as normalize,l 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((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]);null!=s[e]&&(s[e]=t),r||(o=!1)}),[s,o]};const u=(e,t,n,r,i,o)=>{if("object"!=typeof e||!e)return e;if("object"==typeof r&&(!r.normalize||"function"!=typeof r.normalize)){return(Array.isArray(r)?s:c)(r,e,t,n,u,i,o)}return r.normalize(e,t,n,u,i,o)},h={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 l=(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:u(e,e,void 0,t,s,{})}},m=t=>{const n={},r=d(t);return function t(i,s){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]}},d=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]}},f=(e,t,n)=>void 0!==e?m(n)(e,t):[void 0,!1];export{f as denormalize,l as normalize,h as schema}; |
@@ -532,3 +532,3 @@ 'use strict'; | ||
var addEntities = function addEntities(entities) { | ||
var addEntities = function addEntities(entities, indexes) { | ||
return function (schema, processedEntity, value, parent, key) { | ||
@@ -548,2 +548,45 @@ var schemaKey = schema.key; | ||
entities[schemaKey][id] = processedEntity; | ||
} // update index | ||
if (Array.isArray(schema.indexes)) { | ||
var entity = entities[schemaKey][id]; | ||
if (!(schemaKey in indexes)) { | ||
indexes[schemaKey] = {}; | ||
} | ||
for (var _iterator = schema.indexes, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) { | ||
var _ref; | ||
if (_isArray) { | ||
if (_i >= _iterator.length) break; | ||
_ref = _iterator[_i++]; | ||
} else { | ||
_i = _iterator.next(); | ||
if (_i.done) break; | ||
_ref = _i.value; | ||
} | ||
var index = _ref; | ||
if (!(index in indexes[schemaKey])) { | ||
indexes[schemaKey][index] = {}; | ||
} | ||
var indexMap = indexes[schemaKey][index]; | ||
if (existingEntity) { | ||
delete indexMap[existingEntity[index]]; | ||
} | ||
if (index in entity) { | ||
indexMap[entity[index]] = id; | ||
} | ||
/* istanbul ignore next */ | ||
else if ( // eslint-disable-next-line no-undef | ||
process.env.NODE_ENV !== 'production') { | ||
console.warn("Index not found in entity. Indexes must be top-level members of your entity.\nIndex: " + index + "\nEntity: " + JSON.stringify(entity, undefined, 2)); | ||
} | ||
} | ||
} | ||
@@ -573,3 +616,4 @@ }; | ||
var entities = {}; | ||
var addEntity = addEntities(entities); | ||
var indexes = {}; | ||
var addEntity = addEntities(entities, indexes); | ||
var visitedEntities = {}; | ||
@@ -579,2 +623,3 @@ var result = visit(input, input, undefined, schema, addEntity, visitedEntities); | ||
entities: entities, | ||
indexes: indexes, | ||
result: result | ||
@@ -581,0 +626,0 @@ }; |
@@ -1,1 +0,1 @@ | ||
"use strict";function isImmutable(e){return!(!e||"function"!=typeof e.hasOwnProperty||!(Object.hasOwnProperty.call(e,"__ownerID")||e._map&&Object.hasOwnProperty.call(e._map,"__ownerID")))}function denormalizeImmutable(e,t,r){let i=!0;return[Object.keys(e).reduce((t,n)=>{const s=""+n,[a,o]=r(t.get(s),e[s]);return o||(i=!1),t.has(s)?t.set(s,a):t},t),i]}Object.defineProperty(exports,"__esModule",{value:!0});const getDefaultGetId=e=>t=>isImmutable(t)?t.get(e):t[e];class EntitySchema{constructor(e,t={},r={}){if(!e||"string"!=typeof e)throw new Error("Expected a string key for Entity, but found "+e+".");const{idAttribute:i="id",mergeStrategy:n=((e,t)=>Object.assign({},e,{},t)),processStrategy:s=(e=>Object.assign({},e))}=r;this._key=e,this._getId="function"==typeof i?i:getDefaultGetId(i),this._idAttribute=i,this._mergeStrategy=n,this._processStrategy=s,this.define(t)}get key(){return this._key}get idAttribute(){return this._idAttribute}define(e){this.schema=Object.keys(e).reduce((t,r)=>{const i=e[r];return Object.assign({},t,{[r]:i})},this.schema||{})}getId(e,t,r){return this._getId(e,t,r)}merge(e,t){return this._mergeStrategy(e,t)}normalize(e,t,r,i,n,s){const a=this.getId(e,t,r),o=this.key;if(o in s||(s[o]={}),a in s[o]||(s[o][a]=[]),s[o][a].some(t=>t===e))return a;s[o][a].push(e);const c=this._processStrategy(e,t,r);return Object.keys(this.schema).forEach(e=>{if(Object.hasOwnProperty.call(c,e)&&"object"==typeof c[e]){const t=this.schema[e];c[e]=i(c[e],c,e,t,n,s)}}),n(this,c,e,t,r),a}denormalize(e,t){if(isImmutable(e))return denormalizeImmutable(this.schema,e,t);let r=!0;return Object.keys(this.schema).forEach(i=>{const n=this.schema[i],[s,a]=t(e[i],n);a||(r=!1),Object.hasOwnProperty.call(e,i)&&(e[i]=s)}),[e,r]}}class PolymorphicSchema{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 i=this.getSchemaAttribute(e,t,r);return this.schema[i]}normalizeValue(e,t,r,i,n,s){const a=this.inferSchema(e,t,r);if(!a)return e;const o=i(e,t,r,a,n,s);return this.isSingleSchema||null==o?o:{id:o,schema:this.getSchemaAttribute(e,t,r)}}denormalizeValue(e,t){const r=isImmutable(e)?e.get("schema"):e.schema;return this.isSingleSchema||r?t((isImmutable(e)?e.get("id"):e.id)||e,this.isSingleSchema?this.schema:this.schema[r]):[e,!0]}}class UnionSchema extends PolymorphicSchema{constructor(e,t){if(!t)throw new Error('Expected option "schemaAttribute" not found on UnionSchema.');super(e,t)}normalize(e,t,r,i,n,s){return this.normalizeValue(e,t,r,i,n,s)}denormalize(e,t){return this.denormalizeValue(e,t)}}class ValuesSchema extends PolymorphicSchema{normalize(e,t,r,i,n,s){return Object.keys(e).reduce((t,r,a)=>{const o=e[r];return null!=o?Object.assign({},t,{[r]:this.normalizeValue(o,e,r,i,n,s)}):t},{})}denormalize(e,t){let r=!0;return[Object.keys(e).reduce((i,n)=>{const s=e[n],[a,o]=this.denormalizeValue(s,t);return o||(r=!1),Object.assign({},i,{[n]:a})},{}),r]}}const validateSchema=e=>{if(Array.isArray(e)&&e.length>1)throw new Error("Expected schema definition to be a single schema, but found "+e.length+".");return e[0]},getValues=e=>Array.isArray(e)?e:Object.keys(e).map(t=>e[t]),normalize=(e,t,r,i,n,s,a)=>{return e=validateSchema(e),getValues(t).map((t,o)=>n(t,r,i,e,s,a))},denormalize=(e,t,r)=>{e=validateSchema(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]};class ArraySchema extends PolymorphicSchema{normalize(e,t,r,i,n,s){return getValues(e).map((e,a)=>this.normalizeValue(e,t,r,i,n,s)).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]}}const normalize$1=(e,t,r,i,n,s,a)=>{const o=Object.assign({},t);return Object.keys(e).forEach(r=>{const i=e[r],c=n(t[r],t,r,i,s,a);null==c?delete o[r]:o[r]=c}),o},denormalize$1=(e,t,r)=>{if(isImmutable(t))return denormalizeImmutable(e,t,r);const i=Object.assign({},t);let n=!0;return Object.keys(e).forEach(t=>{const[s,a]=r(i[t],e[t]);null!=i[t]&&(i[t]=s),a||(n=!1)}),[i,n]};class ObjectSchema{constructor(e){this.define(e)}define(e){this.schema=Object.keys(e).reduce((t,r)=>{const i=e[r];return Object.assign({},t,{[r]:i})},this.schema||{})}normalize(...e){return normalize$1(this.schema,...e)}denormalize(...e){return denormalize$1(this.schema,...e)}}const visit=(e,t,r,i,n,s)=>{if("object"!=typeof e||!e)return e;if("object"==typeof i&&(!i.normalize||"function"!=typeof i.normalize)){return(Array.isArray(i)?normalize:normalize$1)(i,e,t,r,visit,n,s)}return i.normalize(e,t,r,visit,n,s)},addEntities=e=>(t,r,i,n,s)=>{const a=t.key,o=t.getId(i,n,s);a in e||(e[a]={});const c=e[a][o];e[a][o]=c?t.merge(c,r):r},schema={Array:ArraySchema,Entity:EntitySchema,Object:ObjectSchema,Union:UnionSchema,Values:ValuesSchema};function expectedSchemaType(e){return["object","function"].includes(typeof e)?"object":typeof e}const normalize$2=(e,t)=>{const r=expectedSchemaType(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 i={},n=addEntities(i);return{entities:i,result:visit(e,e,void 0,t,n,{})}},unvisitEntity=(e,t,r,i,n)=>{const s=i(e,t);if("object"!=typeof s||null===s)return[s,!1];n[t.key]||(n[t.key]={});let a=!0;if(!n[t.key][e]){const i=isImmutable(s)?s:Object.assign({},s);n[t.key][e]=i,[n[t.key][e],a]=t.denormalize(i,r)}return[n[t.key][e],a]},getUnvisit=e=>{const t={},r=getEntities(e);return function e(i,n){if("object"==typeof n&&(!n.denormalize||"function"!=typeof n.denormalize)){return(Array.isArray(n)?denormalize:denormalize$1)(n,i,e)}return null===i?[i,!0]:"function"==typeof n.getId&&"function"==typeof n.normalize?void 0===i?[i,!1]:unvisitEntity(i,n,e,r,t):"function"==typeof n.denormalize?n.denormalize(i,e):[i,!0]}},getEntities=e=>{const t=isImmutable(e);return(r,i)=>{const n=i.key;return"object"==typeof r?r:t?e.getIn([n,r.toString()]):e[n]&&e[n][r]}},denormalize$2=(e,t,r)=>void 0!==e?getUnvisit(r)(e,t):[void 0,!1];exports.denormalize=denormalize$2,exports.normalize=normalize$2,exports.schema=schema; | ||
"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,s)=>{const i=""+s,[o,c]=r(t.get(i),e[i]);return c||(n=!1),t.has(i)?t.set(i,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,s,i){const o=this.inferSchema(e,t,r);if(!o)return e;const c=n(e,t,r,o,s,i);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]},s=e=>Array.isArray(e)?e:Object.keys(e).map(t=>e[t]),i=(e,t,r,i,o,c,a)=>(e=n(e),s(t).map((t,n)=>o(t,r,i,e,c,a))),o=(e,t,r)=>{e=n(e);let s=!0;return void 0===t&&e&&([,s]=r(void 0,e)),[t&&t.map?t.map(t=>r(t,e)).filter(([,e])=>e).map(([e])=>e):t,s]};const c=(e,t,r,n,s,i,o)=>{const c=Object.assign({},t);return Object.keys(e).forEach(r=>{const n=e[r],a=s(t[r],t,r,n,i,o);null==a?delete c[r]:c[r]=a}),c},a=(r,n,s)=>{if(e(n))return t(r,n,s);const i=Object.assign({},n);let o=!0;return Object.keys(r).forEach(e=>{const[t,n]=s(i[e],r[e]);null!=i[e]&&(i[e]=t),n||(o=!1)}),[i,o]};const u=(e,t,r,n,s,o)=>{if("object"!=typeof e||!e)return e;if("object"==typeof n&&(!n.normalize||"function"!=typeof n.normalize)){return(Array.isArray(n)?i:c)(n,e,t,r,u,s,o)}return n.normalize(e,t,r,u,s,o)},h={Array:class extends r{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(t,r={},n={}){if(!t||"string"!=typeof t)throw new Error("Expected a string key for Entity, but found "+t+".");const{idAttribute:s="id",mergeStrategy:i=((e,t)=>Object.assign({},e,{},t)),processStrategy:o=(e=>Object.assign({},e))}=n;this._key=t,this._getId="function"==typeof s?s:(t=>r=>e(r)?r.get(t):r[t])(s),this._idAttribute=s,this._mergeStrategy=i,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,s,i){const o=this.getId(e,t,r),c=this.key;if(c in i||(i[c]={}),o in i[c]||(i[c][o]=[]),i[c][o].some(t=>t===e))return o;i[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,s,i)}}),s(this,a,e,t,r),o}denormalize(r,n){if(e(r))return t(this.schema,r,n);let s=!0;return Object.keys(this.schema).forEach(e=>{const t=this.schema[e],[i,o]=n(r[e],t);o||(s=!1),Object.hasOwnProperty.call(r,e)&&(r[e]=i)}),[r,s]}},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,s,i){return this.normalizeValue(e,t,r,n,s,i)}denormalize(e,t){return this.denormalizeValue(e,t)}},Values:class extends r{normalize(e,t,r,n,s,i){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,s,i)}):t},{})}denormalize(e,t){let r=!0;return[Object.keys(e).reduce((n,s)=>{const i=e[s],[o,c]=this.denormalizeValue(i,t);return c||(r=!1),Object.assign({},n,{[s]:o})},{}),r]}}};const l=t=>{const r={},n=m(t);return function t(s,i){if("object"==typeof i&&(!i.denormalize||"function"!=typeof i.denormalize)){return(Array.isArray(i)?o:a)(i,s,t)}return null===s?[s,!0]:"function"==typeof i.getId&&"function"==typeof i.normalize?void 0===s?[s,!1]:((t,r,n,s,i)=>{const o=s(t,r);if("object"!=typeof o||null===o)return[o,!1];i[r.key]||(i[r.key]={});let c=!0;if(!i[r.key][t]){const s=e(o)?o:Object.assign({},o);i[r.key][t]=s,[i[r.key][t],c]=r.denormalize(s,n)}return[i[r.key][t],c]})(s,i,t,n,r):"function"==typeof i.denormalize?i.denormalize(s,t):[s,!0]}},m=t=>{const r=e(t);return(e,n)=>{const s=n.key;return"object"==typeof e?e:r?t.getIn([s,e.toString()]):t[s]&&t[s][e]}};exports.denormalize=(e,t,r)=>void 0!==e?l(r)(e,t):[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={},s={},i=((e,t)=>(r,n,s,i,o)=>{const c=r.key,a=r.getId(s,i,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,s);return{entities:n,indexes:s,result:u(e,e,void 0,t,i,{})}},exports.schema=h; |
@@ -534,3 +534,3 @@ (function (global, factory) { | ||
var addEntities = function addEntities(entities) { | ||
var addEntities = function addEntities(entities, indexes) { | ||
return function (schema, processedEntity, value, parent, key) { | ||
@@ -550,2 +550,45 @@ var schemaKey = schema.key; | ||
entities[schemaKey][id] = processedEntity; | ||
} // update index | ||
if (Array.isArray(schema.indexes)) { | ||
var entity = entities[schemaKey][id]; | ||
if (!(schemaKey in indexes)) { | ||
indexes[schemaKey] = {}; | ||
} | ||
for (var _iterator = schema.indexes, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) { | ||
var _ref; | ||
if (_isArray) { | ||
if (_i >= _iterator.length) break; | ||
_ref = _iterator[_i++]; | ||
} else { | ||
_i = _iterator.next(); | ||
if (_i.done) break; | ||
_ref = _i.value; | ||
} | ||
var index = _ref; | ||
if (!(index in indexes[schemaKey])) { | ||
indexes[schemaKey][index] = {}; | ||
} | ||
var indexMap = indexes[schemaKey][index]; | ||
if (existingEntity) { | ||
delete indexMap[existingEntity[index]]; | ||
} | ||
if (index in entity) { | ||
indexMap[entity[index]] = id; | ||
} | ||
/* istanbul ignore next */ | ||
else if ( // eslint-disable-next-line no-undef | ||
process.env.NODE_ENV !== 'production') { | ||
console.warn("Index not found in entity. Indexes must be top-level members of your entity.\nIndex: " + index + "\nEntity: " + JSON.stringify(entity, undefined, 2)); | ||
} | ||
} | ||
} | ||
@@ -575,3 +618,4 @@ }; | ||
var entities = {}; | ||
var addEntity = addEntities(entities); | ||
var indexes = {}; | ||
var addEntity = addEntities(entities, indexes); | ||
var visitedEntities = {}; | ||
@@ -581,2 +625,3 @@ var result = visit(input, input, undefined, schema, addEntity, visitedEntities); | ||
entities: entities, | ||
indexes: indexes, | ||
result: result | ||
@@ -583,0 +628,0 @@ }; |
@@ -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 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]}const n=e=>r=>t(r)?r.get(e):r[e];class i{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 s=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]},o=e=>Array.isArray(e)?e:Object.keys(e).map(t=>e[t]),c=(e,t,r,n,i,c,a)=>{return e=s(e),o(t).map((t,s)=>i(t,r,n,e,c,a))},a=(e,t,r)=>{e=s(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 u=(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},h=(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]);null!=s[t]&&(s[t]=r),n||(o=!1)}),[s,o]};const l=(e,t,r,n,i,s)=>{if("object"!=typeof e||!e)return e;if("object"==typeof n&&(!n.normalize||"function"!=typeof n.normalize)){return(Array.isArray(n)?c:u)(n,e,t,r,l,i,s)}return n.normalize(e,t,r,l,i,s)},m={Array:class extends i{normalize(e,t,r,n,i,s){return o(e).map((e,o)=>this.normalizeValue(e,t,r,n,i,s)).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,t={},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:n(i),this._idAttribute=i,this._mergeStrategy=s,this._processStrategy=o,this.define(t)}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 u(this.schema,...e)}denormalize(...e){return h(this.schema,...e)}},Union:class extends i{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 i{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 f=e=>{const r={},n=d(e);return function e(i,s){if("object"==typeof s&&(!s.denormalize||"function"!=typeof s.denormalize)){return(Array.isArray(s)?a:h)(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]}},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]}};e.denormalize=(e,t,r)=>void 0!==e?f(r)(e,t):[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=(e=>(t,r,n,i,s)=>{const o=t.key,c=t.getId(n,i,s);o in e||(e[o]={});const a=e[o][c];e[o][c]=a?t.merge(a,r):r})(n);return{entities:n,result:l(e,e,void 0,t,i,{})}},e.schema=m,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((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]);null!=s[t]&&(s[t]=n),r||(o=!1)}),[s,o]};const h=(e,t,n,r,i,s)=>{if("object"!=typeof e||!e)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=f(e);return function e(i,s){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]}},f=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]}};e.denormalize=(e,t,n)=>void 0!==e?m(n)(e,t):[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:h(e,e,void 0,t,s,{})}},e.schema=l,Object.defineProperty(e,"__esModule",{value:!0})})); |
{ | ||
"name": "@rest-hooks/normalizr", | ||
"version": "4.2.0", | ||
"version": "4.3.0", | ||
"description": "Normalizes and denormalizes JSON according to schema for Redux and Flux applications", | ||
@@ -63,3 +63,3 @@ "homepage": "https://github.com/coinbase/rest-hooks/tree/master/packages/normalizr#readme", | ||
}, | ||
"gitHead": "799829ebe78c8a5b9817d8a74eb3951f42469bb2" | ||
"gitHead": "169864670d5651dfd96e2c43acf725d400384ac6" | ||
} |
@@ -313,4 +313,12 @@ declare namespace schema { | ||
export type NormalizedSchema<E, R> = { entities: E; result: R }; | ||
export type NormalizedIndex = { | ||
[entityKey: string]: { [indexName: string]: { [lookup: string]: string } }; | ||
}; | ||
export type NormalizedSchema<E, R> = { | ||
entities: E; | ||
result: R; | ||
indexes: NormalizedIndex; | ||
}; | ||
export function normalize< | ||
@@ -317,0 +325,0 @@ S extends Schema = Schema, |
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
155094
3199
1