@rest-hooks/normalizr
Advanced tools
Comparing version 4.0.0 to 4.1.0
@@ -1,53 +0,21 @@ | ||
# v3.4.0 | ||
# Change Log | ||
* **Changed** Now built with Babel 7 | ||
* **Added** Support for circular references (gh-335) | ||
* **Added** Symbols are valid keys for Entity keys (gh-369) | ||
* **Added/Changed** Typescript definitions include generics for `normalize` (gh-363) | ||
* **Fixed** denormalization skipping of falsy valued ids used in `Object` schemas (gh-345) | ||
* **Chore** Update dev dependencies | ||
* **Chore** Added Greenkeeper | ||
All notable changes to this project will be documented in this file. | ||
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines. | ||
# v3.3.0 | ||
## 4.1.0 (2019-12-31) | ||
* **Added** ES Module builds | ||
* **Fixed** type error with typescript on array+object shorthand (gh-322) | ||
# v3.2.0 | ||
### 🚀 Features | ||
* **Added** Support denormalizing from Immutable entities (gh-228) | ||
* **Added** Brought back `get idAttribute()` to `schema.Entity` (gh-226) | ||
* **Fixed** Gracefully handle missing data in `denormalize` (gh-232) | ||
* **Fixed** Prevent infinite recursion in `denormalize` (gh-220) | ||
* Support string schemas ([#222](https://github.com/coinbase/rest-hooks/issues/222)) ([3f025de](https://github.com/coinbase/rest-hooks/commit/3f025def8de1d949962f99c4411bca86a2e22b1f)) | ||
# v3.1.0 | ||
* **Added** `denormalize`. (gh-214) | ||
* **Changed** No longer requires all input in a polymorphic schema (`Array`, `Union`, `Values`) have a matching schema definition. (gh-208) | ||
* **Changed** Builds do both rollup and plain babel file conversions. `"main"` property in package.json points to babel-converted files. | ||
### 🏠 Internal | ||
# v3.0.0 | ||
The entire normalizr package has been rewritten from v2.x for this version. Please refer to the [documentation](/docs) for all changes. | ||
## Added | ||
* `schema.Entity` | ||
* `processStrategy` for modifying `Entity` objects before they're moved to the `entities` stack. | ||
* `mergeStrategy` for merging with multiple entities with the same ID. | ||
* Added `schema.Object`, with a shorthand of `{}` | ||
* Added `schema.Array`, with a shorthand of `[ schema ]` | ||
## Changed | ||
* `Schema` has been moved to a `schema` namespace, available at `schema.Entity` | ||
* `arrayOf` has been replaced by `schema.Array` or `[]` | ||
* `unionOf` has been replaced by `schema.Union` | ||
* `valuesOf` has been replaced by `schema.Values` | ||
## Removed | ||
* `normalize` no longer accepts an optional `options` argument. All options are assigned at the schema level. | ||
* Entity schema no longer accepts `defaults` as an option. Use a custom `processStrategy` option to apply defaults as needed. | ||
* `assignEntity` has been replaced by `processStrategy` | ||
* `meta` option. See `processStrategy` | ||
* **deps:** bump extend in /packages/normalizr/examples/redux ([#218](https://github.com/coinbase/rest-hooks/issues/218)) ([96f31bf](https://github.com/coinbase/rest-hooks/commit/96f31bf78bb218994ce7538a48178fc3a8440354)) | ||
* **deps:** bump sshpk in /packages/normalizr/examples/redux ([#217](https://github.com/coinbase/rest-hooks/issues/217)) ([56e4a16](https://github.com/coinbase/rest-hooks/commit/56e4a16bbfb861e3acfdb7ccec353991fb07e2ab)) | ||
* **deps:** bump stringstream in /packages/normalizr/examples/redux ([#216](https://github.com/coinbase/rest-hooks/issues/216)) ([f57648a](https://github.com/coinbase/rest-hooks/commit/f57648a3ed77ca4f4592bf655fe40f045b024b77)) | ||
* **deps:** bump tar in /packages/normalizr/examples/redux ([#220](https://github.com/coinbase/rest-hooks/issues/220)) ([1575c65](https://github.com/coinbase/rest-hooks/commit/1575c655d9fdc2bc0034095ab935aed47a439d6c)) | ||
* **deps:** bump tough-cookie in /packages/normalizr/examples/redux ([#221](https://github.com/coinbase/rest-hooks/issues/221)) ([2088197](https://github.com/coinbase/rest-hooks/commit/20881979e883b96def82948bce3583dc02f3f66b)) | ||
* Move normalizr code into repo ([#212](https://github.com/coinbase/rest-hooks/issues/212)) ([7d290f4](https://github.com/coinbase/rest-hooks/commit/7d290f404016073b64b4cddfc723e3591241b358)) |
@@ -16,4 +16,4 @@ define(['exports'], function (exports) { 'use strict'; | ||
function isImmutable(object) { | ||
return !!(object && typeof object.hasOwnProperty === 'function' && (object.hasOwnProperty('__ownerID') || // Immutable.Map | ||
object._map && object._map.hasOwnProperty('__ownerID'))); // Immutable.Record | ||
return !!(object && typeof object.hasOwnProperty === 'function' && (Object.hasOwnProperty.call(object, '__ownerID') || // Immutable.Map | ||
object._map && Object.hasOwnProperty.call(object._map, '__ownerID'))); // Immutable.Record | ||
} | ||
@@ -31,9 +31,12 @@ /** | ||
function denormalizeImmutable(schema, input, unvisit) { | ||
let found = true; | ||
return [Object.keys(schema).reduce((object, key) => { | ||
var found = true; | ||
return [Object.keys(schema).reduce(function (object, key) { | ||
// Immutable maps cast keys to strings on write so we need to ensure | ||
// we're accessing them using string keys. | ||
const stringKey = "" + key; | ||
const [item, foundItem] = unvisit(object.get(stringKey), schema[stringKey]); | ||
var stringKey = "" + key; | ||
var _unvisit = unvisit(object.get(stringKey), schema[stringKey]), | ||
item = _unvisit[0], | ||
foundItem = _unvisit[1]; | ||
if (!foundItem) { | ||
@@ -51,6 +54,36 @@ found = false; | ||
const getDefaultGetId = idAttribute => input => isImmutable(input) ? input.get(idAttribute) : input[idAttribute]; | ||
function _defineProperties(target, props) { | ||
for (var i = 0; i < props.length; i++) { | ||
var descriptor = props[i]; | ||
descriptor.enumerable = descriptor.enumerable || false; | ||
descriptor.configurable = true; | ||
if ("value" in descriptor) descriptor.writable = true; | ||
Object.defineProperty(target, descriptor.key, descriptor); | ||
} | ||
} | ||
class EntitySchema { | ||
constructor(key, definition = {}, options = {}) { | ||
function _createClass(Constructor, protoProps, staticProps) { | ||
if (protoProps) _defineProperties(Constructor.prototype, protoProps); | ||
if (staticProps) _defineProperties(Constructor, staticProps); | ||
return Constructor; | ||
} | ||
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') { | ||
@@ -60,9 +93,13 @@ throw new Error("Expected a string key for Entity, but found " + key + "."); | ||
const { | ||
idAttribute = 'id', | ||
mergeStrategy = (entityA, entityB) => { | ||
return Object.assign({}, entityA, {}, entityB); | ||
}, | ||
processStrategy = input => Object.assign({}, input) | ||
} = options; | ||
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; | ||
@@ -76,31 +113,27 @@ this._getId = typeof idAttribute === 'function' ? idAttribute : getDefaultGetId(idAttribute); | ||
get key() { | ||
return this._key; | ||
} | ||
var _proto = EntitySchema.prototype; | ||
get idAttribute() { | ||
return this._idAttribute; | ||
} | ||
_proto.define = function define(definition) { | ||
this.schema = Object.keys(definition).reduce(function (entitySchema, key) { | ||
var _Object$assign; | ||
define(definition) { | ||
this.schema = Object.keys(definition).reduce((entitySchema, key) => { | ||
const schema = definition[key]; | ||
return Object.assign({}, entitySchema, { | ||
[key]: schema | ||
}); | ||
var schema = definition[key]; | ||
return Object.assign({}, entitySchema, (_Object$assign = {}, _Object$assign[key] = schema, _Object$assign)); | ||
}, this.schema || {}); | ||
} | ||
}; | ||
getId(input, parent, key) { | ||
_proto.getId = function getId(input, parent, key) { | ||
return this._getId(input, parent, key); | ||
} | ||
}; | ||
merge(entityA, entityB) { | ||
_proto.merge = function merge(entityA, entityB) { | ||
return this._mergeStrategy(entityA, entityB); | ||
} | ||
}; | ||
normalize(input, parent, key, visit, addEntity, visitedEntities) { | ||
const id = this.getId(input, parent, key); | ||
const entityType = this.key; | ||
_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)) { | ||
@@ -114,3 +147,5 @@ visitedEntities[entityType] = {}; | ||
if (visitedEntities[entityType][id].some(entity => entity === input)) { | ||
if (visitedEntities[entityType][id].some(function (entity) { | ||
return entity === input; | ||
})) { | ||
return id; | ||
@@ -121,7 +156,7 @@ } | ||
const processedEntity = this._processStrategy(input, parent, key); | ||
var processedEntity = this._processStrategy(input, parent, key); | ||
Object.keys(this.schema).forEach(key => { | ||
if (processedEntity.hasOwnProperty(key) && typeof processedEntity[key] === 'object') { | ||
const schema = this.schema[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); | ||
@@ -132,5 +167,7 @@ } | ||
return id; | ||
} | ||
}; | ||
denormalize(entity, unvisit) { | ||
_proto.denormalize = function denormalize(entity, unvisit) { | ||
var _this2 = this; | ||
if (isImmutable(entity)) { | ||
@@ -140,7 +177,10 @@ return denormalizeImmutable(this.schema, entity, unvisit); | ||
let found = true; | ||
Object.keys(this.schema).forEach(key => { | ||
const schema = this.schema[key]; | ||
const [value, foundItem] = unvisit(entity[key], schema); | ||
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) { | ||
@@ -150,3 +190,3 @@ found = false; | ||
if (entity.hasOwnProperty(key)) { | ||
if (Object.hasOwnProperty.call(entity, key)) { | ||
entity[key] = value; | ||
@@ -156,10 +196,33 @@ } | ||
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; | ||
} | ||
class PolymorphicSchema { | ||
constructor(definition, schemaAttribute) { | ||
var PolymorphicSchema = | ||
/*#__PURE__*/ | ||
function () { | ||
function PolymorphicSchema(definition, schemaAttribute) { | ||
if (schemaAttribute) { | ||
this._schemaAttribute = typeof schemaAttribute === 'string' ? input => input[schemaAttribute] : schemaAttribute; | ||
this._schemaAttribute = typeof schemaAttribute === 'string' ? function (input) { | ||
return input[schemaAttribute]; | ||
} : schemaAttribute; | ||
} | ||
@@ -170,15 +233,13 @@ | ||
get isSingleSchema() { | ||
return !this._schemaAttribute; | ||
} | ||
var _proto = PolymorphicSchema.prototype; | ||
define(definition) { | ||
_proto.define = function define(definition) { | ||
this.schema = definition; | ||
} | ||
}; | ||
getSchemaAttribute(input, parent, key) { | ||
_proto.getSchemaAttribute = function getSchemaAttribute(input, parent, key) { | ||
return !this.isSingleSchema && this._schemaAttribute(input, parent, key); | ||
} | ||
}; | ||
inferSchema(input, parent, key) { | ||
_proto.inferSchema = function inferSchema(input, parent, key) { | ||
if (this.isSingleSchema) { | ||
@@ -188,8 +249,8 @@ return this.schema; | ||
const attr = this.getSchemaAttribute(input, parent, key); | ||
var attr = this.getSchemaAttribute(input, parent, key); | ||
return this.schema[attr]; | ||
} | ||
}; | ||
normalizeValue(value, parent, key, visit, addEntity, visitedEntities) { | ||
const schema = this.inferSchema(value, parent, key); | ||
_proto.normalizeValue = function normalizeValue(value, parent, key, visit, addEntity, visitedEntities) { | ||
var schema = this.inferSchema(value, parent, key); | ||
@@ -200,3 +261,3 @@ if (!schema) { | ||
const normalizedValue = visit(value, parent, key, schema, addEntity, visitedEntities); | ||
var normalizedValue = visit(value, parent, key, schema, addEntity, visitedEntities); | ||
return this.isSingleSchema || normalizedValue === undefined || normalizedValue === null ? normalizedValue : { | ||
@@ -206,6 +267,6 @@ id: normalizedValue, | ||
}; | ||
} | ||
}; | ||
denormalizeValue(value, unvisit) { | ||
const schemaKey = isImmutable(value) ? value.get('schema') : value.schema; | ||
_proto.denormalizeValue = function denormalizeValue(value, unvisit) { | ||
var schemaKey = isImmutable(value) ? value.get('schema') : value.schema; | ||
@@ -216,11 +277,23 @@ if (!this.isSingleSchema && !schemaKey) { | ||
const id = isImmutable(value) ? value.get('id') : value.id; | ||
const schema = this.isSingleSchema ? this.schema : this.schema[schemaKey]; | ||
var id = isImmutable(value) ? value.get('id') : value.id; | ||
var schema = this.isSingleSchema ? this.schema : this.schema[schemaKey]; | ||
return unvisit(id || value, schema); | ||
} | ||
}; | ||
} | ||
_createClass(PolymorphicSchema, [{ | ||
key: "isSingleSchema", | ||
get: function get() { | ||
return !this._schemaAttribute; | ||
} | ||
}]); | ||
class UnionSchema extends PolymorphicSchema { | ||
constructor(definition, schemaAttribute) { | ||
return PolymorphicSchema; | ||
}(); | ||
var UnionSchema = | ||
/*#__PURE__*/ | ||
function (_PolymorphicSchema) { | ||
_inheritsLoose(UnionSchema, _PolymorphicSchema); | ||
function UnionSchema(definition, schemaAttribute) { | ||
if (!schemaAttribute) { | ||
@@ -230,31 +303,53 @@ throw new Error('Expected option "schemaAttribute" not found on UnionSchema.'); | ||
super(definition, schemaAttribute); | ||
return _PolymorphicSchema.call(this, definition, schemaAttribute) || this; | ||
} | ||
normalize(input, parent, key, visit, addEntity, visitedEntities) { | ||
var _proto = UnionSchema.prototype; | ||
_proto.normalize = function normalize(input, parent, key, visit, addEntity, visitedEntities) { | ||
return this.normalizeValue(input, parent, key, visit, addEntity, visitedEntities); | ||
} | ||
}; | ||
denormalize(input, unvisit) { | ||
_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; | ||
class ValuesSchema extends PolymorphicSchema { | ||
normalize(input, parent, key, visit, addEntity, visitedEntities) { | ||
return Object.keys(input).reduce((output, key, index) => { | ||
const value = input[key]; | ||
return value !== undefined && value !== null ? Object.assign({}, output, { | ||
[key]: this.normalizeValue(value, input, key, visit, addEntity, visitedEntities) | ||
}) : output; | ||
_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; | ||
}, {}); | ||
} | ||
}; | ||
denormalize(input, unvisit) { | ||
let found = true; | ||
return [Object.keys(input).reduce((output, key) => { | ||
const entityOrId = input[key]; | ||
const [value, foundItem] = this.denormalizeValue(entityOrId, unvisit); | ||
_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) { | ||
@@ -264,12 +359,11 @@ found = false; | ||
return Object.assign({}, output, { | ||
[key]: value | ||
}); | ||
return Object.assign({}, output, (_Object$assign2 = {}, _Object$assign2[key] = value, _Object$assign2)); | ||
}, {}), found]; | ||
} | ||
}; | ||
} | ||
return ValuesSchema; | ||
}(PolymorphicSchema); | ||
const validateSchema = definition => { | ||
const isArray = Array.isArray(definition); | ||
var validateSchema = function validateSchema(definition) { | ||
var isArray = Array.isArray(definition); | ||
@@ -283,44 +377,90 @@ if (isArray && definition.length > 1) { | ||
const getValues = input => Array.isArray(input) ? input : Object.keys(input).map(key => input[key]); | ||
var getValues = function getValues(input) { | ||
return Array.isArray(input) ? input : Object.keys(input).map(function (key) { | ||
return input[key]; | ||
}); | ||
}; | ||
const normalize = (schema, input, parent, key, visit, addEntity, visitedEntities) => { | ||
var normalize = function normalize(schema, input, parent, key, visit, addEntity, visitedEntities) { | ||
schema = validateSchema(schema); | ||
const values = getValues(input); // Special case: Arrays pass *their* parent on to their children, since there | ||
var values = getValues(input); // Special case: Arrays pass *their* parent on to their children, since there | ||
// is not any special information that can be gathered from themselves directly | ||
return values.map((value, index) => visit(value, parent, key, schema, addEntity, visitedEntities)); | ||
return values.map(function (value, index) { | ||
return visit(value, parent, key, schema, addEntity, visitedEntities); | ||
}); | ||
}; | ||
const denormalize = (schema, input, unvisit) => { | ||
var denormalize = function denormalize(schema, input, unvisit) { | ||
schema = validateSchema(schema); | ||
let found = true; | ||
var found = true; | ||
if (input === undefined && schema) { | ||
[, found] = unvisit(undefined, schema); | ||
var _unvisit = unvisit(undefined, schema); | ||
found = _unvisit[1]; | ||
} | ||
return [input && input.map ? input.map(entityOrId => unvisit(entityOrId, schema)).filter(([, foundItem]) => foundItem).map(([value]) => value) : input, found]; | ||
return [input && input.map ? input.map(function (entityOrId) { | ||
return unvisit(entityOrId, schema); | ||
}).filter(function (_ref) { | ||
var foundItem = _ref[1]; | ||
return foundItem; | ||
}).map(function (_ref2) { | ||
var value = _ref2[0]; | ||
return value; | ||
}) : input, found]; | ||
}; | ||
class ArraySchema extends PolymorphicSchema { | ||
normalize(input, parent, key, visit, addEntity, visitedEntities) { | ||
const values = getValues(input); | ||
return values.map((value, index) => this.normalizeValue(value, parent, key, visit, addEntity, visitedEntities)).filter(value => value !== undefined && value !== null); | ||
var ArraySchema = | ||
/*#__PURE__*/ | ||
function (_PolymorphicSchema) { | ||
_inheritsLoose(ArraySchema, _PolymorphicSchema); | ||
function ArraySchema() { | ||
return _PolymorphicSchema.apply(this, arguments) || this; | ||
} | ||
denormalize(input, unvisit) { | ||
let found = true; | ||
var _proto = ArraySchema.prototype; | ||
_proto.normalize = function normalize(input, parent, key, visit, addEntity, visitedEntities) { | ||
var _this = this; | ||
var values = getValues(input); | ||
return values.map(function (value, index) { | ||
return _this.normalizeValue(value, parent, key, visit, addEntity, visitedEntities); | ||
}).filter(function (value) { | ||
return value !== undefined && value !== null; | ||
}); | ||
}; | ||
_proto.denormalize = function denormalize(input, unvisit) { | ||
var _this2 = this; | ||
var found = true; | ||
if (input === undefined && this.schema) { | ||
[, found] = unvisit(undefined, this.schema); | ||
var _unvisit2 = unvisit(undefined, this.schema); | ||
found = _unvisit2[1]; | ||
} | ||
return [input && input.map ? input.map(entityOrId => this.denormalizeValue(entityOrId, unvisit)).filter(([, foundItem]) => foundItem).map(([value]) => value) : input, found]; | ||
} | ||
return [input && input.map ? input.map(function (entityOrId) { | ||
return _this2.denormalizeValue(entityOrId, unvisit); | ||
}).filter(function (_ref3) { | ||
var foundItem = _ref3[1]; | ||
return foundItem; | ||
}).map(function (_ref4) { | ||
var value = _ref4[0]; | ||
return value; | ||
}) : input, found]; | ||
}; | ||
} | ||
return ArraySchema; | ||
}(PolymorphicSchema); | ||
const normalize$1 = (schema, input, parent, key, visit, addEntity, visitedEntities) => { | ||
const object = Object.assign({}, input); | ||
Object.keys(schema).forEach(key => { | ||
const localSchema = schema[key]; | ||
const value = visit(input[key], input, key, localSchema, addEntity, visitedEntities); | ||
var _normalize = function normalize(schema, input, parent, key, visit, addEntity, visitedEntities) { | ||
var object = Object.assign({}, input); | ||
Object.keys(schema).forEach(function (key) { | ||
var localSchema = schema[key]; | ||
var value = visit(input[key], input, key, localSchema, addEntity, visitedEntities); | ||
@@ -335,3 +475,4 @@ if (value === undefined || value === null) { | ||
}; | ||
const denormalize$1 = (schema, input, unvisit) => { | ||
var _denormalize = function denormalize(schema, input, unvisit) { | ||
if (isImmutable(input)) { | ||
@@ -341,6 +482,8 @@ return denormalizeImmutable(schema, input, unvisit); | ||
const object = Object.assign({}, input); | ||
let found = true; | ||
Object.keys(schema).forEach(key => { | ||
const [item, foundItem] = unvisit(object[key], schema[key]); | ||
var object = Object.assign({}, input); | ||
var found = true; | ||
Object.keys(schema).forEach(function (key) { | ||
var _unvisit = unvisit(object[key], schema[key]), | ||
item = _unvisit[0], | ||
foundItem = _unvisit[1]; | ||
@@ -357,27 +500,41 @@ if (object[key] != null) { | ||
}; | ||
class ObjectSchema { | ||
constructor(definition) { | ||
var ObjectSchema = | ||
/*#__PURE__*/ | ||
function () { | ||
function ObjectSchema(definition) { | ||
this.define(definition); | ||
} | ||
define(definition) { | ||
this.schema = Object.keys(definition).reduce((entitySchema, key) => { | ||
const schema = definition[key]; | ||
return Object.assign({}, entitySchema, { | ||
[key]: schema | ||
}); | ||
var _proto = ObjectSchema.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 || {}); | ||
} | ||
}; | ||
normalize(...args) { | ||
return normalize$1(this.schema, ...args); | ||
} | ||
_proto.normalize = function normalize() { | ||
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { | ||
args[_key] = arguments[_key]; | ||
} | ||
denormalize(...args) { | ||
return denormalize$1(this.schema, ...args); | ||
} | ||
return _normalize.apply(void 0, [this.schema].concat(args)); | ||
}; | ||
} | ||
_proto.denormalize = function denormalize() { | ||
for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { | ||
args[_key2] = arguments[_key2]; | ||
} | ||
const visit = (value, parent, key, schema, addEntity, visitedEntities) => { | ||
return _denormalize.apply(void 0, [this.schema].concat(args)); | ||
}; | ||
return ObjectSchema; | ||
}(); | ||
var visit = function visit(value, parent, key, schema, addEntity, visitedEntities) { | ||
if (typeof value !== 'object' || !value) { | ||
@@ -388,3 +545,3 @@ return value; | ||
if (typeof schema === 'object' && (!schema.normalize || typeof schema.normalize !== 'function')) { | ||
const method = Array.isArray(schema) ? normalize : normalize$1; | ||
var method = Array.isArray(schema) ? normalize : _normalize; | ||
return method(schema, value, parent, key, visit, addEntity, visitedEntities); | ||
@@ -396,20 +553,22 @@ } | ||
const addEntities = entities => (schema, processedEntity, value, parent, key) => { | ||
const schemaKey = schema.key; | ||
const id = schema.getId(value, parent, key); | ||
var addEntities = function addEntities(entities) { | ||
return function (schema, processedEntity, value, parent, key) { | ||
var schemaKey = schema.key; | ||
var id = schema.getId(value, parent, key); | ||
if (!(schemaKey in entities)) { | ||
entities[schemaKey] = {}; | ||
} | ||
if (!(schemaKey in entities)) { | ||
entities[schemaKey] = {}; | ||
} | ||
const existingEntity = entities[schemaKey][id]; | ||
var existingEntity = entities[schemaKey][id]; | ||
if (existingEntity) { | ||
entities[schemaKey][id] = schema.merge(existingEntity, processedEntity); | ||
} else { | ||
entities[schemaKey][id] = processedEntity; | ||
} | ||
if (existingEntity) { | ||
entities[schemaKey][id] = schema.merge(existingEntity, processedEntity); | ||
} else { | ||
entities[schemaKey][id] = processedEntity; | ||
} | ||
}; | ||
}; | ||
const schema = { | ||
var schema = { | ||
Array: ArraySchema, | ||
@@ -421,19 +580,19 @@ Entity: EntitySchema, | ||
}; | ||
const normalize$2 = (input, schema) => { | ||
if (!input || typeof input !== 'object') { | ||
throw new Error("Unexpected input given to normalize. Expected type to be \"object\", found \"" + (input === null ? 'null' : typeof input) + "\"."); | ||
var normalize$1 = function normalize(input, schema) { | ||
if (input === null || typeof input !== typeof schema) { | ||
throw new Error("Unexpected input given to normalize. Expected type to be \"" + (schema === null ? 'null' : typeof schema) + "\", found \"" + (input === null ? 'null' : typeof input) + "\"."); | ||
} | ||
const entities = {}; | ||
const addEntity = addEntities(entities); | ||
const visitedEntities = {}; | ||
const result = visit(input, input, null, schema, addEntity, visitedEntities); | ||
var entities = {}; | ||
var addEntity = addEntities(entities); | ||
var visitedEntities = {}; | ||
var result = visit(input, input, null, schema, addEntity, visitedEntities); | ||
return { | ||
entities, | ||
result | ||
entities: entities, | ||
result: result | ||
}; | ||
}; | ||
const unvisitEntity = (id, schema, unvisit, getEntity, cache) => { | ||
const entity = getEntity(id, schema); | ||
var unvisitEntity = function unvisitEntity(id, schema, unvisit, getEntity, cache) { | ||
var entity = getEntity(id, schema); | ||
@@ -448,11 +607,15 @@ if (typeof entity !== 'object' || entity === null) { | ||
let found = true; | ||
var found = true; | ||
if (!cache[schema.key][id]) { | ||
// Ensure we don't mutate it non-immutable objects | ||
const entityCopy = isImmutable(entity) ? entity : Object.assign({}, entity); // Need to set this first so that if it is referenced further within the | ||
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; | ||
[cache[schema.key][id], found] = schema.denormalize(entityCopy, unvisit); | ||
var _schema$denormalize = schema.denormalize(entityCopy, unvisit); | ||
cache[schema.key][id] = _schema$denormalize[0]; | ||
found = _schema$denormalize[1]; | ||
} | ||
@@ -463,8 +626,8 @@ | ||
const getUnvisit = entities => { | ||
const cache = {}; | ||
const getEntity = getEntities(entities); | ||
var getUnvisit = function getUnvisit(entities) { | ||
var cache = {}; | ||
var getEntity = getEntities(entities); | ||
return function unvisit(input, schema) { | ||
if (typeof schema === 'object' && (!schema.denormalize || typeof schema.denormalize !== 'function')) { | ||
const method = Array.isArray(schema) ? denormalize : denormalize$1; | ||
var method = Array.isArray(schema) ? denormalize : _denormalize; | ||
return method(schema, input, unvisit); | ||
@@ -495,6 +658,6 @@ } // null is considered intentional, thus always 'found' as true | ||
const getEntities = entities => { | ||
const isImmutable$1 = isImmutable(entities); | ||
return (entityOrId, schema) => { | ||
const schemaKey = schema.key; | ||
var getEntities = function getEntities(entities) { | ||
var isImmutable$1 = isImmutable(entities); | ||
return function (entityOrId, schema) { | ||
var schemaKey = schema.key; | ||
@@ -513,3 +676,3 @@ if (typeof entityOrId === 'object') { | ||
const denormalize$2 = (input, schema, entities) => { | ||
var denormalize$1 = function denormalize(input, schema, entities) { | ||
if (typeof input !== 'undefined') { | ||
@@ -522,4 +685,4 @@ return getUnvisit(entities)(input, schema); | ||
exports.denormalize = denormalize$2; | ||
exports.normalize = normalize$2; | ||
exports.denormalize = denormalize$1; | ||
exports.normalize = normalize$1; | ||
exports.schema = schema; | ||
@@ -526,0 +689,0 @@ |
@@ -1,1 +0,1 @@ | ||
define(["exports"],(function(e){"use strict";function t(e){return!(!e||"function"!=typeof e.hasOwnProperty||!(e.hasOwnProperty("__ownerID")||e._map&&e._map.hasOwnProperty("__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={},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(a.hasOwnProperty(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),e.hasOwnProperty(t)&&(e[t]=i)}),[e,s]}}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,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 o=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]},c=e=>Array.isArray(e)?e:Object.keys(e).map(t=>e[t]),a=(e,t,r,n,s,i,a)=>{return e=o(e),c(t).map((t,o)=>s(t,r,n,e,i,a))},h=(e,t,r)=>{e=o(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},l=(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 m=(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)?a:u)(n,e,t,r,m,s,i)}return n.normalize(e,t,r,m,s,i)},d={Array:class extends i{normalize(e,t,r,n,s,i){return c(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: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 l(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,s,i){return this.normalizeValue(e,t,r,n,s,i)}denormalize(e,t){return this.denormalizeValue(e,t)}},Values:class extends i{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]}}},f=e=>{const r={},n=y(e);return function e(i,o){if("object"==typeof o&&(!o.denormalize||"function"!=typeof o.denormalize)){return(Array.isArray(o)?h:l)(o,i,e)}return null===i?[i,!0]:o instanceof s?void 0===i?[i,!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]})(i,o,e,n,r):"function"==typeof o.denormalize?o.denormalize(i,e):[i,!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]}};e.denormalize=(e,t,r)=>void 0!==e?f(r)(e,t):[void 0,!1],e.normalize=(e,t)=>{if(!e||"object"!=typeof e)throw new Error('Unexpected input given to normalize. Expected type to be "object", found "'+(null===e?"null":typeof e)+'".');const r={},n=(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})(r);return{entities:r,result:m(e,e,null,t,n,{})}},e.schema=d,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 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={},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]}}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,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 o=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]},c=e=>Array.isArray(e)?e:Object.keys(e).map(t=>e[t]),a=(e,t,r,n,s,i,a)=>{return e=o(e),c(t).map((t,o)=>s(t,r,n,e,i,a))},u=(e,t,r)=>{e=o(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 h=(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},l=(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 m=(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)?a:h)(n,e,t,r,m,s,i)}return n.normalize(e,t,r,m,s,i)},f={Array:class extends i{normalize(e,t,r,n,s,i){return c(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: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 h(this.schema,...e)}denormalize(...e){return l(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,s,i){return this.normalizeValue(e,t,r,n,s,i)}denormalize(e,t){return this.denormalizeValue(e,t)}},Values:class extends i{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]}}},y=e=>{const r={},n=d(e);return function e(i,o){if("object"==typeof o&&(!o.denormalize||"function"!=typeof o.denormalize)){return(Array.isArray(o)?u:l)(o,i,e)}return null===i?[i,!0]:o instanceof s?void 0===i?[i,!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]})(i,o,e,n,r):"function"==typeof o.denormalize?o.denormalize(i,e):[i,!0]}},d=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]}};e.denormalize=(e,t,r)=>void 0!==e?y(r)(e,t):[void 0,!1],e.normalize=(e,t)=>{if(null===e||typeof e!=typeof t)throw new Error('Unexpected input given to normalize. Expected type to be "'+(null===t?"null":typeof t)+'", found "'+(null===e?"null":typeof e)+'".');const r={},n=(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})(r);return{entities:r,result:m(e,e,null,t,n,{})}},e.schema=f,Object.defineProperty(e,"__esModule",{value:!0})})); |
@@ -17,4 +17,4 @@ var rest_hooks_normalizr = (function (exports) { | ||
function isImmutable(object) { | ||
return !!(object && typeof object.hasOwnProperty === 'function' && (object.hasOwnProperty('__ownerID') || // Immutable.Map | ||
object._map && object._map.hasOwnProperty('__ownerID'))); // Immutable.Record | ||
return !!(object && typeof object.hasOwnProperty === 'function' && (Object.hasOwnProperty.call(object, '__ownerID') || // Immutable.Map | ||
object._map && Object.hasOwnProperty.call(object._map, '__ownerID'))); // Immutable.Record | ||
} | ||
@@ -32,9 +32,12 @@ /** | ||
function denormalizeImmutable(schema, input, unvisit) { | ||
let found = true; | ||
return [Object.keys(schema).reduce((object, key) => { | ||
var found = true; | ||
return [Object.keys(schema).reduce(function (object, key) { | ||
// Immutable maps cast keys to strings on write so we need to ensure | ||
// we're accessing them using string keys. | ||
const stringKey = "" + key; | ||
const [item, foundItem] = unvisit(object.get(stringKey), schema[stringKey]); | ||
var stringKey = "" + key; | ||
var _unvisit = unvisit(object.get(stringKey), schema[stringKey]), | ||
item = _unvisit[0], | ||
foundItem = _unvisit[1]; | ||
if (!foundItem) { | ||
@@ -52,6 +55,36 @@ found = false; | ||
const getDefaultGetId = idAttribute => input => isImmutable(input) ? input.get(idAttribute) : input[idAttribute]; | ||
function _defineProperties(target, props) { | ||
for (var i = 0; i < props.length; i++) { | ||
var descriptor = props[i]; | ||
descriptor.enumerable = descriptor.enumerable || false; | ||
descriptor.configurable = true; | ||
if ("value" in descriptor) descriptor.writable = true; | ||
Object.defineProperty(target, descriptor.key, descriptor); | ||
} | ||
} | ||
class EntitySchema { | ||
constructor(key, definition = {}, options = {}) { | ||
function _createClass(Constructor, protoProps, staticProps) { | ||
if (protoProps) _defineProperties(Constructor.prototype, protoProps); | ||
if (staticProps) _defineProperties(Constructor, staticProps); | ||
return Constructor; | ||
} | ||
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') { | ||
@@ -61,9 +94,13 @@ throw new Error("Expected a string key for Entity, but found " + key + "."); | ||
const { | ||
idAttribute = 'id', | ||
mergeStrategy = (entityA, entityB) => { | ||
return Object.assign({}, entityA, {}, entityB); | ||
}, | ||
processStrategy = input => Object.assign({}, input) | ||
} = options; | ||
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; | ||
@@ -77,31 +114,27 @@ this._getId = typeof idAttribute === 'function' ? idAttribute : getDefaultGetId(idAttribute); | ||
get key() { | ||
return this._key; | ||
} | ||
var _proto = EntitySchema.prototype; | ||
get idAttribute() { | ||
return this._idAttribute; | ||
} | ||
_proto.define = function define(definition) { | ||
this.schema = Object.keys(definition).reduce(function (entitySchema, key) { | ||
var _Object$assign; | ||
define(definition) { | ||
this.schema = Object.keys(definition).reduce((entitySchema, key) => { | ||
const schema = definition[key]; | ||
return Object.assign({}, entitySchema, { | ||
[key]: schema | ||
}); | ||
var schema = definition[key]; | ||
return Object.assign({}, entitySchema, (_Object$assign = {}, _Object$assign[key] = schema, _Object$assign)); | ||
}, this.schema || {}); | ||
} | ||
}; | ||
getId(input, parent, key) { | ||
_proto.getId = function getId(input, parent, key) { | ||
return this._getId(input, parent, key); | ||
} | ||
}; | ||
merge(entityA, entityB) { | ||
_proto.merge = function merge(entityA, entityB) { | ||
return this._mergeStrategy(entityA, entityB); | ||
} | ||
}; | ||
normalize(input, parent, key, visit, addEntity, visitedEntities) { | ||
const id = this.getId(input, parent, key); | ||
const entityType = this.key; | ||
_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)) { | ||
@@ -115,3 +148,5 @@ visitedEntities[entityType] = {}; | ||
if (visitedEntities[entityType][id].some(entity => entity === input)) { | ||
if (visitedEntities[entityType][id].some(function (entity) { | ||
return entity === input; | ||
})) { | ||
return id; | ||
@@ -122,7 +157,7 @@ } | ||
const processedEntity = this._processStrategy(input, parent, key); | ||
var processedEntity = this._processStrategy(input, parent, key); | ||
Object.keys(this.schema).forEach(key => { | ||
if (processedEntity.hasOwnProperty(key) && typeof processedEntity[key] === 'object') { | ||
const schema = this.schema[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); | ||
@@ -133,5 +168,7 @@ } | ||
return id; | ||
} | ||
}; | ||
denormalize(entity, unvisit) { | ||
_proto.denormalize = function denormalize(entity, unvisit) { | ||
var _this2 = this; | ||
if (isImmutable(entity)) { | ||
@@ -141,7 +178,10 @@ return denormalizeImmutable(this.schema, entity, unvisit); | ||
let found = true; | ||
Object.keys(this.schema).forEach(key => { | ||
const schema = this.schema[key]; | ||
const [value, foundItem] = unvisit(entity[key], schema); | ||
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) { | ||
@@ -151,3 +191,3 @@ found = false; | ||
if (entity.hasOwnProperty(key)) { | ||
if (Object.hasOwnProperty.call(entity, key)) { | ||
entity[key] = value; | ||
@@ -157,10 +197,33 @@ } | ||
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; | ||
} | ||
class PolymorphicSchema { | ||
constructor(definition, schemaAttribute) { | ||
var PolymorphicSchema = | ||
/*#__PURE__*/ | ||
function () { | ||
function PolymorphicSchema(definition, schemaAttribute) { | ||
if (schemaAttribute) { | ||
this._schemaAttribute = typeof schemaAttribute === 'string' ? input => input[schemaAttribute] : schemaAttribute; | ||
this._schemaAttribute = typeof schemaAttribute === 'string' ? function (input) { | ||
return input[schemaAttribute]; | ||
} : schemaAttribute; | ||
} | ||
@@ -171,15 +234,13 @@ | ||
get isSingleSchema() { | ||
return !this._schemaAttribute; | ||
} | ||
var _proto = PolymorphicSchema.prototype; | ||
define(definition) { | ||
_proto.define = function define(definition) { | ||
this.schema = definition; | ||
} | ||
}; | ||
getSchemaAttribute(input, parent, key) { | ||
_proto.getSchemaAttribute = function getSchemaAttribute(input, parent, key) { | ||
return !this.isSingleSchema && this._schemaAttribute(input, parent, key); | ||
} | ||
}; | ||
inferSchema(input, parent, key) { | ||
_proto.inferSchema = function inferSchema(input, parent, key) { | ||
if (this.isSingleSchema) { | ||
@@ -189,8 +250,8 @@ return this.schema; | ||
const attr = this.getSchemaAttribute(input, parent, key); | ||
var attr = this.getSchemaAttribute(input, parent, key); | ||
return this.schema[attr]; | ||
} | ||
}; | ||
normalizeValue(value, parent, key, visit, addEntity, visitedEntities) { | ||
const schema = this.inferSchema(value, parent, key); | ||
_proto.normalizeValue = function normalizeValue(value, parent, key, visit, addEntity, visitedEntities) { | ||
var schema = this.inferSchema(value, parent, key); | ||
@@ -201,3 +262,3 @@ if (!schema) { | ||
const normalizedValue = visit(value, parent, key, schema, addEntity, visitedEntities); | ||
var normalizedValue = visit(value, parent, key, schema, addEntity, visitedEntities); | ||
return this.isSingleSchema || normalizedValue === undefined || normalizedValue === null ? normalizedValue : { | ||
@@ -207,6 +268,6 @@ id: normalizedValue, | ||
}; | ||
} | ||
}; | ||
denormalizeValue(value, unvisit) { | ||
const schemaKey = isImmutable(value) ? value.get('schema') : value.schema; | ||
_proto.denormalizeValue = function denormalizeValue(value, unvisit) { | ||
var schemaKey = isImmutable(value) ? value.get('schema') : value.schema; | ||
@@ -217,11 +278,23 @@ if (!this.isSingleSchema && !schemaKey) { | ||
const id = isImmutable(value) ? value.get('id') : value.id; | ||
const schema = this.isSingleSchema ? this.schema : this.schema[schemaKey]; | ||
var id = isImmutable(value) ? value.get('id') : value.id; | ||
var schema = this.isSingleSchema ? this.schema : this.schema[schemaKey]; | ||
return unvisit(id || value, schema); | ||
} | ||
}; | ||
} | ||
_createClass(PolymorphicSchema, [{ | ||
key: "isSingleSchema", | ||
get: function get() { | ||
return !this._schemaAttribute; | ||
} | ||
}]); | ||
class UnionSchema extends PolymorphicSchema { | ||
constructor(definition, schemaAttribute) { | ||
return PolymorphicSchema; | ||
}(); | ||
var UnionSchema = | ||
/*#__PURE__*/ | ||
function (_PolymorphicSchema) { | ||
_inheritsLoose(UnionSchema, _PolymorphicSchema); | ||
function UnionSchema(definition, schemaAttribute) { | ||
if (!schemaAttribute) { | ||
@@ -231,31 +304,53 @@ throw new Error('Expected option "schemaAttribute" not found on UnionSchema.'); | ||
super(definition, schemaAttribute); | ||
return _PolymorphicSchema.call(this, definition, schemaAttribute) || this; | ||
} | ||
normalize(input, parent, key, visit, addEntity, visitedEntities) { | ||
var _proto = UnionSchema.prototype; | ||
_proto.normalize = function normalize(input, parent, key, visit, addEntity, visitedEntities) { | ||
return this.normalizeValue(input, parent, key, visit, addEntity, visitedEntities); | ||
} | ||
}; | ||
denormalize(input, unvisit) { | ||
_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; | ||
class ValuesSchema extends PolymorphicSchema { | ||
normalize(input, parent, key, visit, addEntity, visitedEntities) { | ||
return Object.keys(input).reduce((output, key, index) => { | ||
const value = input[key]; | ||
return value !== undefined && value !== null ? Object.assign({}, output, { | ||
[key]: this.normalizeValue(value, input, key, visit, addEntity, visitedEntities) | ||
}) : output; | ||
_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; | ||
}, {}); | ||
} | ||
}; | ||
denormalize(input, unvisit) { | ||
let found = true; | ||
return [Object.keys(input).reduce((output, key) => { | ||
const entityOrId = input[key]; | ||
const [value, foundItem] = this.denormalizeValue(entityOrId, unvisit); | ||
_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) { | ||
@@ -265,12 +360,11 @@ found = false; | ||
return Object.assign({}, output, { | ||
[key]: value | ||
}); | ||
return Object.assign({}, output, (_Object$assign2 = {}, _Object$assign2[key] = value, _Object$assign2)); | ||
}, {}), found]; | ||
} | ||
}; | ||
} | ||
return ValuesSchema; | ||
}(PolymorphicSchema); | ||
const validateSchema = definition => { | ||
const isArray = Array.isArray(definition); | ||
var validateSchema = function validateSchema(definition) { | ||
var isArray = Array.isArray(definition); | ||
@@ -284,44 +378,90 @@ if (isArray && definition.length > 1) { | ||
const getValues = input => Array.isArray(input) ? input : Object.keys(input).map(key => input[key]); | ||
var getValues = function getValues(input) { | ||
return Array.isArray(input) ? input : Object.keys(input).map(function (key) { | ||
return input[key]; | ||
}); | ||
}; | ||
const normalize = (schema, input, parent, key, visit, addEntity, visitedEntities) => { | ||
var normalize = function normalize(schema, input, parent, key, visit, addEntity, visitedEntities) { | ||
schema = validateSchema(schema); | ||
const values = getValues(input); // Special case: Arrays pass *their* parent on to their children, since there | ||
var values = getValues(input); // Special case: Arrays pass *their* parent on to their children, since there | ||
// is not any special information that can be gathered from themselves directly | ||
return values.map((value, index) => visit(value, parent, key, schema, addEntity, visitedEntities)); | ||
return values.map(function (value, index) { | ||
return visit(value, parent, key, schema, addEntity, visitedEntities); | ||
}); | ||
}; | ||
const denormalize = (schema, input, unvisit) => { | ||
var denormalize = function denormalize(schema, input, unvisit) { | ||
schema = validateSchema(schema); | ||
let found = true; | ||
var found = true; | ||
if (input === undefined && schema) { | ||
[, found] = unvisit(undefined, schema); | ||
var _unvisit = unvisit(undefined, schema); | ||
found = _unvisit[1]; | ||
} | ||
return [input && input.map ? input.map(entityOrId => unvisit(entityOrId, schema)).filter(([, foundItem]) => foundItem).map(([value]) => value) : input, found]; | ||
return [input && input.map ? input.map(function (entityOrId) { | ||
return unvisit(entityOrId, schema); | ||
}).filter(function (_ref) { | ||
var foundItem = _ref[1]; | ||
return foundItem; | ||
}).map(function (_ref2) { | ||
var value = _ref2[0]; | ||
return value; | ||
}) : input, found]; | ||
}; | ||
class ArraySchema extends PolymorphicSchema { | ||
normalize(input, parent, key, visit, addEntity, visitedEntities) { | ||
const values = getValues(input); | ||
return values.map((value, index) => this.normalizeValue(value, parent, key, visit, addEntity, visitedEntities)).filter(value => value !== undefined && value !== null); | ||
var ArraySchema = | ||
/*#__PURE__*/ | ||
function (_PolymorphicSchema) { | ||
_inheritsLoose(ArraySchema, _PolymorphicSchema); | ||
function ArraySchema() { | ||
return _PolymorphicSchema.apply(this, arguments) || this; | ||
} | ||
denormalize(input, unvisit) { | ||
let found = true; | ||
var _proto = ArraySchema.prototype; | ||
_proto.normalize = function normalize(input, parent, key, visit, addEntity, visitedEntities) { | ||
var _this = this; | ||
var values = getValues(input); | ||
return values.map(function (value, index) { | ||
return _this.normalizeValue(value, parent, key, visit, addEntity, visitedEntities); | ||
}).filter(function (value) { | ||
return value !== undefined && value !== null; | ||
}); | ||
}; | ||
_proto.denormalize = function denormalize(input, unvisit) { | ||
var _this2 = this; | ||
var found = true; | ||
if (input === undefined && this.schema) { | ||
[, found] = unvisit(undefined, this.schema); | ||
var _unvisit2 = unvisit(undefined, this.schema); | ||
found = _unvisit2[1]; | ||
} | ||
return [input && input.map ? input.map(entityOrId => this.denormalizeValue(entityOrId, unvisit)).filter(([, foundItem]) => foundItem).map(([value]) => value) : input, found]; | ||
} | ||
return [input && input.map ? input.map(function (entityOrId) { | ||
return _this2.denormalizeValue(entityOrId, unvisit); | ||
}).filter(function (_ref3) { | ||
var foundItem = _ref3[1]; | ||
return foundItem; | ||
}).map(function (_ref4) { | ||
var value = _ref4[0]; | ||
return value; | ||
}) : input, found]; | ||
}; | ||
} | ||
return ArraySchema; | ||
}(PolymorphicSchema); | ||
const normalize$1 = (schema, input, parent, key, visit, addEntity, visitedEntities) => { | ||
const object = Object.assign({}, input); | ||
Object.keys(schema).forEach(key => { | ||
const localSchema = schema[key]; | ||
const value = visit(input[key], input, key, localSchema, addEntity, visitedEntities); | ||
var _normalize = function normalize(schema, input, parent, key, visit, addEntity, visitedEntities) { | ||
var object = Object.assign({}, input); | ||
Object.keys(schema).forEach(function (key) { | ||
var localSchema = schema[key]; | ||
var value = visit(input[key], input, key, localSchema, addEntity, visitedEntities); | ||
@@ -336,3 +476,4 @@ if (value === undefined || value === null) { | ||
}; | ||
const denormalize$1 = (schema, input, unvisit) => { | ||
var _denormalize = function denormalize(schema, input, unvisit) { | ||
if (isImmutable(input)) { | ||
@@ -342,6 +483,8 @@ return denormalizeImmutable(schema, input, unvisit); | ||
const object = Object.assign({}, input); | ||
let found = true; | ||
Object.keys(schema).forEach(key => { | ||
const [item, foundItem] = unvisit(object[key], schema[key]); | ||
var object = Object.assign({}, input); | ||
var found = true; | ||
Object.keys(schema).forEach(function (key) { | ||
var _unvisit = unvisit(object[key], schema[key]), | ||
item = _unvisit[0], | ||
foundItem = _unvisit[1]; | ||
@@ -358,27 +501,41 @@ if (object[key] != null) { | ||
}; | ||
class ObjectSchema { | ||
constructor(definition) { | ||
var ObjectSchema = | ||
/*#__PURE__*/ | ||
function () { | ||
function ObjectSchema(definition) { | ||
this.define(definition); | ||
} | ||
define(definition) { | ||
this.schema = Object.keys(definition).reduce((entitySchema, key) => { | ||
const schema = definition[key]; | ||
return Object.assign({}, entitySchema, { | ||
[key]: schema | ||
}); | ||
var _proto = ObjectSchema.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 || {}); | ||
} | ||
}; | ||
normalize(...args) { | ||
return normalize$1(this.schema, ...args); | ||
} | ||
_proto.normalize = function normalize() { | ||
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { | ||
args[_key] = arguments[_key]; | ||
} | ||
denormalize(...args) { | ||
return denormalize$1(this.schema, ...args); | ||
} | ||
return _normalize.apply(void 0, [this.schema].concat(args)); | ||
}; | ||
} | ||
_proto.denormalize = function denormalize() { | ||
for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { | ||
args[_key2] = arguments[_key2]; | ||
} | ||
const visit = (value, parent, key, schema, addEntity, visitedEntities) => { | ||
return _denormalize.apply(void 0, [this.schema].concat(args)); | ||
}; | ||
return ObjectSchema; | ||
}(); | ||
var visit = function visit(value, parent, key, schema, addEntity, visitedEntities) { | ||
if (typeof value !== 'object' || !value) { | ||
@@ -389,3 +546,3 @@ return value; | ||
if (typeof schema === 'object' && (!schema.normalize || typeof schema.normalize !== 'function')) { | ||
const method = Array.isArray(schema) ? normalize : normalize$1; | ||
var method = Array.isArray(schema) ? normalize : _normalize; | ||
return method(schema, value, parent, key, visit, addEntity, visitedEntities); | ||
@@ -397,20 +554,22 @@ } | ||
const addEntities = entities => (schema, processedEntity, value, parent, key) => { | ||
const schemaKey = schema.key; | ||
const id = schema.getId(value, parent, key); | ||
var addEntities = function addEntities(entities) { | ||
return function (schema, processedEntity, value, parent, key) { | ||
var schemaKey = schema.key; | ||
var id = schema.getId(value, parent, key); | ||
if (!(schemaKey in entities)) { | ||
entities[schemaKey] = {}; | ||
} | ||
if (!(schemaKey in entities)) { | ||
entities[schemaKey] = {}; | ||
} | ||
const existingEntity = entities[schemaKey][id]; | ||
var existingEntity = entities[schemaKey][id]; | ||
if (existingEntity) { | ||
entities[schemaKey][id] = schema.merge(existingEntity, processedEntity); | ||
} else { | ||
entities[schemaKey][id] = processedEntity; | ||
} | ||
if (existingEntity) { | ||
entities[schemaKey][id] = schema.merge(existingEntity, processedEntity); | ||
} else { | ||
entities[schemaKey][id] = processedEntity; | ||
} | ||
}; | ||
}; | ||
const schema = { | ||
var schema = { | ||
Array: ArraySchema, | ||
@@ -422,19 +581,19 @@ Entity: EntitySchema, | ||
}; | ||
const normalize$2 = (input, schema) => { | ||
if (!input || typeof input !== 'object') { | ||
throw new Error("Unexpected input given to normalize. Expected type to be \"object\", found \"" + (input === null ? 'null' : typeof input) + "\"."); | ||
var normalize$1 = function normalize(input, schema) { | ||
if (input === null || typeof input !== typeof schema) { | ||
throw new Error("Unexpected input given to normalize. Expected type to be \"" + (schema === null ? 'null' : typeof schema) + "\", found \"" + (input === null ? 'null' : typeof input) + "\"."); | ||
} | ||
const entities = {}; | ||
const addEntity = addEntities(entities); | ||
const visitedEntities = {}; | ||
const result = visit(input, input, null, schema, addEntity, visitedEntities); | ||
var entities = {}; | ||
var addEntity = addEntities(entities); | ||
var visitedEntities = {}; | ||
var result = visit(input, input, null, schema, addEntity, visitedEntities); | ||
return { | ||
entities, | ||
result | ||
entities: entities, | ||
result: result | ||
}; | ||
}; | ||
const unvisitEntity = (id, schema, unvisit, getEntity, cache) => { | ||
const entity = getEntity(id, schema); | ||
var unvisitEntity = function unvisitEntity(id, schema, unvisit, getEntity, cache) { | ||
var entity = getEntity(id, schema); | ||
@@ -449,11 +608,15 @@ if (typeof entity !== 'object' || entity === null) { | ||
let found = true; | ||
var found = true; | ||
if (!cache[schema.key][id]) { | ||
// Ensure we don't mutate it non-immutable objects | ||
const entityCopy = isImmutable(entity) ? entity : Object.assign({}, entity); // Need to set this first so that if it is referenced further within the | ||
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; | ||
[cache[schema.key][id], found] = schema.denormalize(entityCopy, unvisit); | ||
var _schema$denormalize = schema.denormalize(entityCopy, unvisit); | ||
cache[schema.key][id] = _schema$denormalize[0]; | ||
found = _schema$denormalize[1]; | ||
} | ||
@@ -464,8 +627,8 @@ | ||
const getUnvisit = entities => { | ||
const cache = {}; | ||
const getEntity = getEntities(entities); | ||
var getUnvisit = function getUnvisit(entities) { | ||
var cache = {}; | ||
var getEntity = getEntities(entities); | ||
return function unvisit(input, schema) { | ||
if (typeof schema === 'object' && (!schema.denormalize || typeof schema.denormalize !== 'function')) { | ||
const method = Array.isArray(schema) ? denormalize : denormalize$1; | ||
var method = Array.isArray(schema) ? denormalize : _denormalize; | ||
return method(schema, input, unvisit); | ||
@@ -496,6 +659,6 @@ } // null is considered intentional, thus always 'found' as true | ||
const getEntities = entities => { | ||
const isImmutable$1 = isImmutable(entities); | ||
return (entityOrId, schema) => { | ||
const schemaKey = schema.key; | ||
var getEntities = function getEntities(entities) { | ||
var isImmutable$1 = isImmutable(entities); | ||
return function (entityOrId, schema) { | ||
var schemaKey = schema.key; | ||
@@ -514,3 +677,3 @@ if (typeof entityOrId === 'object') { | ||
const denormalize$2 = (input, schema, entities) => { | ||
var denormalize$1 = function denormalize(input, schema, entities) { | ||
if (typeof input !== 'undefined') { | ||
@@ -523,4 +686,4 @@ return getUnvisit(entities)(input, schema); | ||
exports.denormalize = denormalize$2; | ||
exports.normalize = normalize$2; | ||
exports.denormalize = denormalize$1; | ||
exports.normalize = normalize$1; | ||
exports.schema = schema; | ||
@@ -527,0 +690,0 @@ |
@@ -1,1 +0,1 @@ | ||
var rest_hooks_normalizr=function(e){"use strict";function t(e){return!(!e||"function"!=typeof e.hasOwnProperty||!(e.hasOwnProperty("__ownerID")||e._map&&e._map.hasOwnProperty("__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={},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(a.hasOwnProperty(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),e.hasOwnProperty(t)&&(e[t]=i)}),[e,s]}}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,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 o=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]},c=e=>Array.isArray(e)?e:Object.keys(e).map(t=>e[t]),a=(e,t,r,n,s,i,a)=>{return e=o(e),c(t).map((t,o)=>s(t,r,n,e,i,a))},h=(e,t,r)=>{e=o(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},m=(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)?a:u)(n,e,t,r,l,s,i)}return n.normalize(e,t,r,l,s,i)},f={Array:class extends i{normalize(e,t,r,n,s,i){return c(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: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 m(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,s,i){return this.normalizeValue(e,t,r,n,s,i)}denormalize(e,t){return this.denormalizeValue(e,t)}},Values:class extends i{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]}}},y=e=>{const r={},n=d(e);return function e(i,o){if("object"==typeof o&&(!o.denormalize||"function"!=typeof o.denormalize)){return(Array.isArray(o)?h:m)(o,i,e)}return null===i?[i,!0]:o instanceof s?void 0===i?[i,!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]})(i,o,e,n,r):"function"==typeof o.denormalize?o.denormalize(i,e):[i,!0]}},d=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?y(r)(e,t):[void 0,!1],e.normalize=(e,t)=>{if(!e||"object"!=typeof e)throw new Error('Unexpected input given to normalize. Expected type to be "object", found "'+(null===e?"null":typeof e)+'".');const r={},n=(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})(r);return{entities:r,result:l(e,e,null,t,n,{})}},e.schema=f,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,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={},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]}}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,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 o=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]},c=e=>Array.isArray(e)?e:Object.keys(e).map(t=>e[t]),a=(e,t,r,n,s,i,a)=>{return e=o(e),c(t).map((t,o)=>s(t,r,n,e,i,a))},u=(e,t,r)=>{e=o(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 h=(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},l=(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 m=(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)?a:h)(n,e,t,r,m,s,i)}return n.normalize(e,t,r,m,s,i)},f={Array:class extends i{normalize(e,t,r,n,s,i){return c(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: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 h(this.schema,...e)}denormalize(...e){return l(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,s,i){return this.normalizeValue(e,t,r,n,s,i)}denormalize(e,t){return this.denormalizeValue(e,t)}},Values:class extends i{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]}}},y=e=>{const r={},n=d(e);return function e(i,o){if("object"==typeof o&&(!o.denormalize||"function"!=typeof o.denormalize)){return(Array.isArray(o)?u:l)(o,i,e)}return null===i?[i,!0]:o instanceof s?void 0===i?[i,!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]})(i,o,e,n,r):"function"==typeof o.denormalize?o.denormalize(i,e):[i,!0]}},d=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?y(r)(e,t):[void 0,!1],e.normalize=(e,t)=>{if(null===e||typeof e!=typeof t)throw new Error('Unexpected input given to normalize. Expected type to be "'+(null===t?"null":typeof t)+'", found "'+(null===e?"null":typeof e)+'".');const r={},n=(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})(r);return{entities:r,result:m(e,e,null,t,n,{})}},e.schema=f,e}({}); |
@@ -0,1 +1,4 @@ | ||
import _createClass from '@babel/runtime/helpers/esm/createClass'; | ||
import _inheritsLoose from '@babel/runtime/helpers/esm/inheritsLoose'; | ||
/** | ||
@@ -14,4 +17,4 @@ * Helpers to enable Immutable compatibility *without* bringing in | ||
function isImmutable(object) { | ||
return !!(object && typeof object.hasOwnProperty === 'function' && (object.hasOwnProperty('__ownerID') || // Immutable.Map | ||
object._map && object._map.hasOwnProperty('__ownerID'))); // Immutable.Record | ||
return !!(object && typeof object.hasOwnProperty === 'function' && (Object.hasOwnProperty.call(object, '__ownerID') || // Immutable.Map | ||
object._map && Object.hasOwnProperty.call(object._map, '__ownerID'))); // Immutable.Record | ||
} | ||
@@ -29,9 +32,12 @@ /** | ||
function denormalizeImmutable(schema, input, unvisit) { | ||
let found = true; | ||
return [Object.keys(schema).reduce((object, key) => { | ||
var found = true; | ||
return [Object.keys(schema).reduce(function (object, key) { | ||
// Immutable maps cast keys to strings on write so we need to ensure | ||
// we're accessing them using string keys. | ||
const stringKey = "" + key; | ||
const [item, foundItem] = unvisit(object.get(stringKey), schema[stringKey]); | ||
var stringKey = "" + key; | ||
var _unvisit = unvisit(object.get(stringKey), schema[stringKey]), | ||
item = _unvisit[0], | ||
foundItem = _unvisit[1]; | ||
if (!foundItem) { | ||
@@ -49,6 +55,20 @@ found = false; | ||
const getDefaultGetId = idAttribute => input => isImmutable(input) ? input.get(idAttribute) : input[idAttribute]; | ||
var getDefaultGetId = function getDefaultGetId(idAttribute) { | ||
return function (input) { | ||
return isImmutable(input) ? input.get(idAttribute) : input[idAttribute]; | ||
}; | ||
}; | ||
class EntitySchema { | ||
constructor(key, definition = {}, options = {}) { | ||
var EntitySchema = | ||
/*#__PURE__*/ | ||
function () { | ||
function EntitySchema(key, definition, options) { | ||
if (definition === void 0) { | ||
definition = {}; | ||
} | ||
if (options === void 0) { | ||
options = {}; | ||
} | ||
if (!key || typeof key !== 'string') { | ||
@@ -58,9 +78,13 @@ throw new Error("Expected a string key for Entity, but found " + key + "."); | ||
const { | ||
idAttribute = 'id', | ||
mergeStrategy = (entityA, entityB) => { | ||
return Object.assign({}, entityA, {}, entityB); | ||
}, | ||
processStrategy = input => Object.assign({}, input) | ||
} = options; | ||
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; | ||
@@ -74,31 +98,27 @@ this._getId = typeof idAttribute === 'function' ? idAttribute : getDefaultGetId(idAttribute); | ||
get key() { | ||
return this._key; | ||
} | ||
var _proto = EntitySchema.prototype; | ||
get idAttribute() { | ||
return this._idAttribute; | ||
} | ||
_proto.define = function define(definition) { | ||
this.schema = Object.keys(definition).reduce(function (entitySchema, key) { | ||
var _Object$assign; | ||
define(definition) { | ||
this.schema = Object.keys(definition).reduce((entitySchema, key) => { | ||
const schema = definition[key]; | ||
return Object.assign({}, entitySchema, { | ||
[key]: schema | ||
}); | ||
var schema = definition[key]; | ||
return Object.assign({}, entitySchema, (_Object$assign = {}, _Object$assign[key] = schema, _Object$assign)); | ||
}, this.schema || {}); | ||
} | ||
}; | ||
getId(input, parent, key) { | ||
_proto.getId = function getId(input, parent, key) { | ||
return this._getId(input, parent, key); | ||
} | ||
}; | ||
merge(entityA, entityB) { | ||
_proto.merge = function merge(entityA, entityB) { | ||
return this._mergeStrategy(entityA, entityB); | ||
} | ||
}; | ||
normalize(input, parent, key, visit, addEntity, visitedEntities) { | ||
const id = this.getId(input, parent, key); | ||
const entityType = this.key; | ||
_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)) { | ||
@@ -112,3 +132,5 @@ visitedEntities[entityType] = {}; | ||
if (visitedEntities[entityType][id].some(entity => entity === input)) { | ||
if (visitedEntities[entityType][id].some(function (entity) { | ||
return entity === input; | ||
})) { | ||
return id; | ||
@@ -119,7 +141,7 @@ } | ||
const processedEntity = this._processStrategy(input, parent, key); | ||
var processedEntity = this._processStrategy(input, parent, key); | ||
Object.keys(this.schema).forEach(key => { | ||
if (processedEntity.hasOwnProperty(key) && typeof processedEntity[key] === 'object') { | ||
const schema = this.schema[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); | ||
@@ -130,5 +152,7 @@ } | ||
return id; | ||
} | ||
}; | ||
denormalize(entity, unvisit) { | ||
_proto.denormalize = function denormalize(entity, unvisit) { | ||
var _this2 = this; | ||
if (isImmutable(entity)) { | ||
@@ -138,7 +162,10 @@ return denormalizeImmutable(this.schema, entity, unvisit); | ||
let found = true; | ||
Object.keys(this.schema).forEach(key => { | ||
const schema = this.schema[key]; | ||
const [value, foundItem] = unvisit(entity[key], schema); | ||
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) { | ||
@@ -148,3 +175,3 @@ found = false; | ||
if (entity.hasOwnProperty(key)) { | ||
if (Object.hasOwnProperty.call(entity, key)) { | ||
entity[key] = value; | ||
@@ -154,10 +181,27 @@ } | ||
return [entity, found]; | ||
} | ||
}; | ||
} | ||
_createClass(EntitySchema, [{ | ||
key: "key", | ||
get: function get() { | ||
return this._key; | ||
} | ||
}, { | ||
key: "idAttribute", | ||
get: function get() { | ||
return this._idAttribute; | ||
} | ||
}]); | ||
class PolymorphicSchema { | ||
constructor(definition, schemaAttribute) { | ||
return EntitySchema; | ||
}(); | ||
var PolymorphicSchema = | ||
/*#__PURE__*/ | ||
function () { | ||
function PolymorphicSchema(definition, schemaAttribute) { | ||
if (schemaAttribute) { | ||
this._schemaAttribute = typeof schemaAttribute === 'string' ? input => input[schemaAttribute] : schemaAttribute; | ||
this._schemaAttribute = typeof schemaAttribute === 'string' ? function (input) { | ||
return input[schemaAttribute]; | ||
} : schemaAttribute; | ||
} | ||
@@ -168,15 +212,13 @@ | ||
get isSingleSchema() { | ||
return !this._schemaAttribute; | ||
} | ||
var _proto = PolymorphicSchema.prototype; | ||
define(definition) { | ||
_proto.define = function define(definition) { | ||
this.schema = definition; | ||
} | ||
}; | ||
getSchemaAttribute(input, parent, key) { | ||
_proto.getSchemaAttribute = function getSchemaAttribute(input, parent, key) { | ||
return !this.isSingleSchema && this._schemaAttribute(input, parent, key); | ||
} | ||
}; | ||
inferSchema(input, parent, key) { | ||
_proto.inferSchema = function inferSchema(input, parent, key) { | ||
if (this.isSingleSchema) { | ||
@@ -186,8 +228,8 @@ return this.schema; | ||
const attr = this.getSchemaAttribute(input, parent, key); | ||
var attr = this.getSchemaAttribute(input, parent, key); | ||
return this.schema[attr]; | ||
} | ||
}; | ||
normalizeValue(value, parent, key, visit, addEntity, visitedEntities) { | ||
const schema = this.inferSchema(value, parent, key); | ||
_proto.normalizeValue = function normalizeValue(value, parent, key, visit, addEntity, visitedEntities) { | ||
var schema = this.inferSchema(value, parent, key); | ||
@@ -198,3 +240,3 @@ if (!schema) { | ||
const normalizedValue = visit(value, parent, key, schema, addEntity, visitedEntities); | ||
var normalizedValue = visit(value, parent, key, schema, addEntity, visitedEntities); | ||
return this.isSingleSchema || normalizedValue === undefined || normalizedValue === null ? normalizedValue : { | ||
@@ -204,6 +246,6 @@ id: normalizedValue, | ||
}; | ||
} | ||
}; | ||
denormalizeValue(value, unvisit) { | ||
const schemaKey = isImmutable(value) ? value.get('schema') : value.schema; | ||
_proto.denormalizeValue = function denormalizeValue(value, unvisit) { | ||
var schemaKey = isImmutable(value) ? value.get('schema') : value.schema; | ||
@@ -214,11 +256,23 @@ if (!this.isSingleSchema && !schemaKey) { | ||
const id = isImmutable(value) ? value.get('id') : value.id; | ||
const schema = this.isSingleSchema ? this.schema : this.schema[schemaKey]; | ||
var id = isImmutable(value) ? value.get('id') : value.id; | ||
var schema = this.isSingleSchema ? this.schema : this.schema[schemaKey]; | ||
return unvisit(id || value, schema); | ||
} | ||
}; | ||
} | ||
_createClass(PolymorphicSchema, [{ | ||
key: "isSingleSchema", | ||
get: function get() { | ||
return !this._schemaAttribute; | ||
} | ||
}]); | ||
class UnionSchema extends PolymorphicSchema { | ||
constructor(definition, schemaAttribute) { | ||
return PolymorphicSchema; | ||
}(); | ||
var UnionSchema = | ||
/*#__PURE__*/ | ||
function (_PolymorphicSchema) { | ||
_inheritsLoose(UnionSchema, _PolymorphicSchema); | ||
function UnionSchema(definition, schemaAttribute) { | ||
if (!schemaAttribute) { | ||
@@ -228,31 +282,53 @@ throw new Error('Expected option "schemaAttribute" not found on UnionSchema.'); | ||
super(definition, schemaAttribute); | ||
return _PolymorphicSchema.call(this, definition, schemaAttribute) || this; | ||
} | ||
normalize(input, parent, key, visit, addEntity, visitedEntities) { | ||
var _proto = UnionSchema.prototype; | ||
_proto.normalize = function normalize(input, parent, key, visit, addEntity, visitedEntities) { | ||
return this.normalizeValue(input, parent, key, visit, addEntity, visitedEntities); | ||
} | ||
}; | ||
denormalize(input, unvisit) { | ||
_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; | ||
class ValuesSchema extends PolymorphicSchema { | ||
normalize(input, parent, key, visit, addEntity, visitedEntities) { | ||
return Object.keys(input).reduce((output, key, index) => { | ||
const value = input[key]; | ||
return value !== undefined && value !== null ? Object.assign({}, output, { | ||
[key]: this.normalizeValue(value, input, key, visit, addEntity, visitedEntities) | ||
}) : output; | ||
_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; | ||
}, {}); | ||
} | ||
}; | ||
denormalize(input, unvisit) { | ||
let found = true; | ||
return [Object.keys(input).reduce((output, key) => { | ||
const entityOrId = input[key]; | ||
const [value, foundItem] = this.denormalizeValue(entityOrId, unvisit); | ||
_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) { | ||
@@ -262,12 +338,11 @@ found = false; | ||
return Object.assign({}, output, { | ||
[key]: value | ||
}); | ||
return Object.assign({}, output, (_Object$assign2 = {}, _Object$assign2[key] = value, _Object$assign2)); | ||
}, {}), found]; | ||
} | ||
}; | ||
} | ||
return ValuesSchema; | ||
}(PolymorphicSchema); | ||
const validateSchema = definition => { | ||
const isArray = Array.isArray(definition); | ||
var validateSchema = function validateSchema(definition) { | ||
var isArray = Array.isArray(definition); | ||
@@ -281,44 +356,90 @@ if (isArray && definition.length > 1) { | ||
const getValues = input => Array.isArray(input) ? input : Object.keys(input).map(key => input[key]); | ||
var getValues = function getValues(input) { | ||
return Array.isArray(input) ? input : Object.keys(input).map(function (key) { | ||
return input[key]; | ||
}); | ||
}; | ||
const normalize = (schema, input, parent, key, visit, addEntity, visitedEntities) => { | ||
var normalize = function normalize(schema, input, parent, key, visit, addEntity, visitedEntities) { | ||
schema = validateSchema(schema); | ||
const values = getValues(input); // Special case: Arrays pass *their* parent on to their children, since there | ||
var values = getValues(input); // Special case: Arrays pass *their* parent on to their children, since there | ||
// is not any special information that can be gathered from themselves directly | ||
return values.map((value, index) => visit(value, parent, key, schema, addEntity, visitedEntities)); | ||
return values.map(function (value, index) { | ||
return visit(value, parent, key, schema, addEntity, visitedEntities); | ||
}); | ||
}; | ||
const denormalize = (schema, input, unvisit) => { | ||
var denormalize = function denormalize(schema, input, unvisit) { | ||
schema = validateSchema(schema); | ||
let found = true; | ||
var found = true; | ||
if (input === undefined && schema) { | ||
[, found] = unvisit(undefined, schema); | ||
var _unvisit = unvisit(undefined, schema); | ||
found = _unvisit[1]; | ||
} | ||
return [input && input.map ? input.map(entityOrId => unvisit(entityOrId, schema)).filter(([, foundItem]) => foundItem).map(([value]) => value) : input, found]; | ||
return [input && input.map ? input.map(function (entityOrId) { | ||
return unvisit(entityOrId, schema); | ||
}).filter(function (_ref) { | ||
var foundItem = _ref[1]; | ||
return foundItem; | ||
}).map(function (_ref2) { | ||
var value = _ref2[0]; | ||
return value; | ||
}) : input, found]; | ||
}; | ||
class ArraySchema extends PolymorphicSchema { | ||
normalize(input, parent, key, visit, addEntity, visitedEntities) { | ||
const values = getValues(input); | ||
return values.map((value, index) => this.normalizeValue(value, parent, key, visit, addEntity, visitedEntities)).filter(value => value !== undefined && value !== null); | ||
var ArraySchema = | ||
/*#__PURE__*/ | ||
function (_PolymorphicSchema) { | ||
_inheritsLoose(ArraySchema, _PolymorphicSchema); | ||
function ArraySchema() { | ||
return _PolymorphicSchema.apply(this, arguments) || this; | ||
} | ||
denormalize(input, unvisit) { | ||
let found = true; | ||
var _proto = ArraySchema.prototype; | ||
_proto.normalize = function normalize(input, parent, key, visit, addEntity, visitedEntities) { | ||
var _this = this; | ||
var values = getValues(input); | ||
return values.map(function (value, index) { | ||
return _this.normalizeValue(value, parent, key, visit, addEntity, visitedEntities); | ||
}).filter(function (value) { | ||
return value !== undefined && value !== null; | ||
}); | ||
}; | ||
_proto.denormalize = function denormalize(input, unvisit) { | ||
var _this2 = this; | ||
var found = true; | ||
if (input === undefined && this.schema) { | ||
[, found] = unvisit(undefined, this.schema); | ||
var _unvisit2 = unvisit(undefined, this.schema); | ||
found = _unvisit2[1]; | ||
} | ||
return [input && input.map ? input.map(entityOrId => this.denormalizeValue(entityOrId, unvisit)).filter(([, foundItem]) => foundItem).map(([value]) => value) : input, found]; | ||
} | ||
return [input && input.map ? input.map(function (entityOrId) { | ||
return _this2.denormalizeValue(entityOrId, unvisit); | ||
}).filter(function (_ref3) { | ||
var foundItem = _ref3[1]; | ||
return foundItem; | ||
}).map(function (_ref4) { | ||
var value = _ref4[0]; | ||
return value; | ||
}) : input, found]; | ||
}; | ||
} | ||
return ArraySchema; | ||
}(PolymorphicSchema); | ||
const normalize$1 = (schema, input, parent, key, visit, addEntity, visitedEntities) => { | ||
const object = Object.assign({}, input); | ||
Object.keys(schema).forEach(key => { | ||
const localSchema = schema[key]; | ||
const value = visit(input[key], input, key, localSchema, addEntity, visitedEntities); | ||
var _normalize = function normalize(schema, input, parent, key, visit, addEntity, visitedEntities) { | ||
var object = Object.assign({}, input); | ||
Object.keys(schema).forEach(function (key) { | ||
var localSchema = schema[key]; | ||
var value = visit(input[key], input, key, localSchema, addEntity, visitedEntities); | ||
@@ -333,3 +454,4 @@ if (value === undefined || value === null) { | ||
}; | ||
const denormalize$1 = (schema, input, unvisit) => { | ||
var _denormalize = function denormalize(schema, input, unvisit) { | ||
if (isImmutable(input)) { | ||
@@ -339,6 +461,8 @@ return denormalizeImmutable(schema, input, unvisit); | ||
const object = Object.assign({}, input); | ||
let found = true; | ||
Object.keys(schema).forEach(key => { | ||
const [item, foundItem] = unvisit(object[key], schema[key]); | ||
var object = Object.assign({}, input); | ||
var found = true; | ||
Object.keys(schema).forEach(function (key) { | ||
var _unvisit = unvisit(object[key], schema[key]), | ||
item = _unvisit[0], | ||
foundItem = _unvisit[1]; | ||
@@ -355,27 +479,41 @@ if (object[key] != null) { | ||
}; | ||
class ObjectSchema { | ||
constructor(definition) { | ||
var ObjectSchema = | ||
/*#__PURE__*/ | ||
function () { | ||
function ObjectSchema(definition) { | ||
this.define(definition); | ||
} | ||
define(definition) { | ||
this.schema = Object.keys(definition).reduce((entitySchema, key) => { | ||
const schema = definition[key]; | ||
return Object.assign({}, entitySchema, { | ||
[key]: schema | ||
}); | ||
var _proto = ObjectSchema.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 || {}); | ||
} | ||
}; | ||
normalize(...args) { | ||
return normalize$1(this.schema, ...args); | ||
} | ||
_proto.normalize = function normalize() { | ||
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { | ||
args[_key] = arguments[_key]; | ||
} | ||
denormalize(...args) { | ||
return denormalize$1(this.schema, ...args); | ||
} | ||
return _normalize.apply(void 0, [this.schema].concat(args)); | ||
}; | ||
} | ||
_proto.denormalize = function denormalize() { | ||
for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { | ||
args[_key2] = arguments[_key2]; | ||
} | ||
const visit = (value, parent, key, schema, addEntity, visitedEntities) => { | ||
return _denormalize.apply(void 0, [this.schema].concat(args)); | ||
}; | ||
return ObjectSchema; | ||
}(); | ||
var visit = function visit(value, parent, key, schema, addEntity, visitedEntities) { | ||
if (typeof value !== 'object' || !value) { | ||
@@ -386,3 +524,3 @@ return value; | ||
if (typeof schema === 'object' && (!schema.normalize || typeof schema.normalize !== 'function')) { | ||
const method = Array.isArray(schema) ? normalize : normalize$1; | ||
var method = Array.isArray(schema) ? normalize : _normalize; | ||
return method(schema, value, parent, key, visit, addEntity, visitedEntities); | ||
@@ -394,20 +532,22 @@ } | ||
const addEntities = entities => (schema, processedEntity, value, parent, key) => { | ||
const schemaKey = schema.key; | ||
const id = schema.getId(value, parent, key); | ||
var addEntities = function addEntities(entities) { | ||
return function (schema, processedEntity, value, parent, key) { | ||
var schemaKey = schema.key; | ||
var id = schema.getId(value, parent, key); | ||
if (!(schemaKey in entities)) { | ||
entities[schemaKey] = {}; | ||
} | ||
if (!(schemaKey in entities)) { | ||
entities[schemaKey] = {}; | ||
} | ||
const existingEntity = entities[schemaKey][id]; | ||
var existingEntity = entities[schemaKey][id]; | ||
if (existingEntity) { | ||
entities[schemaKey][id] = schema.merge(existingEntity, processedEntity); | ||
} else { | ||
entities[schemaKey][id] = processedEntity; | ||
} | ||
if (existingEntity) { | ||
entities[schemaKey][id] = schema.merge(existingEntity, processedEntity); | ||
} else { | ||
entities[schemaKey][id] = processedEntity; | ||
} | ||
}; | ||
}; | ||
const schema = { | ||
var schema = { | ||
Array: ArraySchema, | ||
@@ -419,19 +559,19 @@ Entity: EntitySchema, | ||
}; | ||
const normalize$2 = (input, schema) => { | ||
if (!input || typeof input !== 'object') { | ||
throw new Error("Unexpected input given to normalize. Expected type to be \"object\", found \"" + (input === null ? 'null' : typeof input) + "\"."); | ||
var normalize$1 = function normalize(input, schema) { | ||
if (input === null || typeof input !== typeof schema) { | ||
throw new Error("Unexpected input given to normalize. Expected type to be \"" + (schema === null ? 'null' : typeof schema) + "\", found \"" + (input === null ? 'null' : typeof input) + "\"."); | ||
} | ||
const entities = {}; | ||
const addEntity = addEntities(entities); | ||
const visitedEntities = {}; | ||
const result = visit(input, input, null, schema, addEntity, visitedEntities); | ||
var entities = {}; | ||
var addEntity = addEntities(entities); | ||
var visitedEntities = {}; | ||
var result = visit(input, input, null, schema, addEntity, visitedEntities); | ||
return { | ||
entities, | ||
result | ||
entities: entities, | ||
result: result | ||
}; | ||
}; | ||
const unvisitEntity = (id, schema, unvisit, getEntity, cache) => { | ||
const entity = getEntity(id, schema); | ||
var unvisitEntity = function unvisitEntity(id, schema, unvisit, getEntity, cache) { | ||
var entity = getEntity(id, schema); | ||
@@ -446,11 +586,15 @@ if (typeof entity !== 'object' || entity === null) { | ||
let found = true; | ||
var found = true; | ||
if (!cache[schema.key][id]) { | ||
// Ensure we don't mutate it non-immutable objects | ||
const entityCopy = isImmutable(entity) ? entity : Object.assign({}, entity); // Need to set this first so that if it is referenced further within the | ||
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; | ||
[cache[schema.key][id], found] = schema.denormalize(entityCopy, unvisit); | ||
var _schema$denormalize = schema.denormalize(entityCopy, unvisit); | ||
cache[schema.key][id] = _schema$denormalize[0]; | ||
found = _schema$denormalize[1]; | ||
} | ||
@@ -461,8 +605,8 @@ | ||
const getUnvisit = entities => { | ||
const cache = {}; | ||
const getEntity = getEntities(entities); | ||
var getUnvisit = function getUnvisit(entities) { | ||
var cache = {}; | ||
var getEntity = getEntities(entities); | ||
return function unvisit(input, schema) { | ||
if (typeof schema === 'object' && (!schema.denormalize || typeof schema.denormalize !== 'function')) { | ||
const method = Array.isArray(schema) ? denormalize : denormalize$1; | ||
var method = Array.isArray(schema) ? denormalize : _denormalize; | ||
return method(schema, input, unvisit); | ||
@@ -493,6 +637,6 @@ } // null is considered intentional, thus always 'found' as true | ||
const getEntities = entities => { | ||
const isImmutable$1 = isImmutable(entities); | ||
return (entityOrId, schema) => { | ||
const schemaKey = schema.key; | ||
var getEntities = function getEntities(entities) { | ||
var isImmutable$1 = isImmutable(entities); | ||
return function (entityOrId, schema) { | ||
var schemaKey = schema.key; | ||
@@ -511,3 +655,3 @@ if (typeof entityOrId === 'object') { | ||
const denormalize$2 = (input, schema, entities) => { | ||
var denormalize$1 = function denormalize(input, schema, entities) { | ||
if (typeof input !== 'undefined') { | ||
@@ -520,2 +664,2 @@ return getUnvisit(entities)(input, schema); | ||
export { denormalize$2 as denormalize, normalize$2 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||!(e.hasOwnProperty("__ownerID")||e._map&&e._map.hasOwnProperty("__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={},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(a.hasOwnProperty(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),r.hasOwnProperty(e)&&(r[e]=i)}),[r,s]}}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(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 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 h=(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 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:h)(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:n,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 h(this.schema,...e)}denormalize(...e){return u(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]}}},y=(e,t)=>{if(!e||"object"!=typeof e)throw new Error('Unexpected input given to normalize. Expected type to be "object", found "'+(null===e?"null":typeof e)+'".');const r={},n=(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})(r);return{entities:r,result:l(e,e,null,t,n,{})}},d=t=>{const r={},s=f(t);return function t(i,o){if("object"==typeof o&&(!o.denormalize||"function"!=typeof o.denormalize)){return(Array.isArray(o)?a:u)(o,i,t)}return null===i?[i,!0]:o instanceof n?void 0===i?[i,!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]})(i,o,t,s,r):"function"==typeof o.denormalize?o.denormalize(i,t):[i,!0]}},f=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]}},g=(e,t,r)=>void 0!==e?d(r)(e,t):[void 0,!1];export{g as denormalize,y as normalize,m 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,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={},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]}}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(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 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 h=(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 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:h)(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:n,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 h(this.schema,...e)}denormalize(...e){return u(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]}}},y=(e,t)=>{if(null===e||typeof e!=typeof t)throw new Error('Unexpected input given to normalize. Expected type to be "'+(null===t?"null":typeof t)+'", found "'+(null===e?"null":typeof e)+'".');const r={},n=(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})(r);return{entities:r,result:l(e,e,null,t,n,{})}},f=t=>{const r={},s=d(t);return function t(i,o){if("object"==typeof o&&(!o.denormalize||"function"!=typeof o.denormalize)){return(Array.isArray(o)?a:u)(o,i,t)}return null===i?[i,!0]:o instanceof n?void 0===i?[i,!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]})(i,o,t,s,r):"function"==typeof o.denormalize?o.denormalize(i,t):[i,!0]}},d=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]}},g=(e,t,r)=>void 0!==e?f(r)(e,t):[void 0,!1];export{g as denormalize,y as normalize,m as schema}; |
@@ -18,4 +18,4 @@ 'use strict'; | ||
function isImmutable(object) { | ||
return !!(object && typeof object.hasOwnProperty === 'function' && (object.hasOwnProperty('__ownerID') || // Immutable.Map | ||
object._map && object._map.hasOwnProperty('__ownerID'))); // Immutable.Record | ||
return !!(object && typeof object.hasOwnProperty === 'function' && (Object.hasOwnProperty.call(object, '__ownerID') || // Immutable.Map | ||
object._map && Object.hasOwnProperty.call(object._map, '__ownerID'))); // Immutable.Record | ||
} | ||
@@ -33,9 +33,12 @@ /** | ||
function denormalizeImmutable(schema, input, unvisit) { | ||
let found = true; | ||
return [Object.keys(schema).reduce((object, key) => { | ||
var found = true; | ||
return [Object.keys(schema).reduce(function (object, key) { | ||
// Immutable maps cast keys to strings on write so we need to ensure | ||
// we're accessing them using string keys. | ||
const stringKey = "" + key; | ||
const [item, foundItem] = unvisit(object.get(stringKey), schema[stringKey]); | ||
var stringKey = "" + key; | ||
var _unvisit = unvisit(object.get(stringKey), schema[stringKey]), | ||
item = _unvisit[0], | ||
foundItem = _unvisit[1]; | ||
if (!foundItem) { | ||
@@ -53,6 +56,36 @@ found = false; | ||
const getDefaultGetId = idAttribute => input => isImmutable(input) ? input.get(idAttribute) : input[idAttribute]; | ||
function _defineProperties(target, props) { | ||
for (var i = 0; i < props.length; i++) { | ||
var descriptor = props[i]; | ||
descriptor.enumerable = descriptor.enumerable || false; | ||
descriptor.configurable = true; | ||
if ("value" in descriptor) descriptor.writable = true; | ||
Object.defineProperty(target, descriptor.key, descriptor); | ||
} | ||
} | ||
class EntitySchema { | ||
constructor(key, definition = {}, options = {}) { | ||
function _createClass(Constructor, protoProps, staticProps) { | ||
if (protoProps) _defineProperties(Constructor.prototype, protoProps); | ||
if (staticProps) _defineProperties(Constructor, staticProps); | ||
return Constructor; | ||
} | ||
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') { | ||
@@ -62,9 +95,13 @@ throw new Error("Expected a string key for Entity, but found " + key + "."); | ||
const { | ||
idAttribute = 'id', | ||
mergeStrategy = (entityA, entityB) => { | ||
return Object.assign({}, entityA, {}, entityB); | ||
}, | ||
processStrategy = input => Object.assign({}, input) | ||
} = options; | ||
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; | ||
@@ -78,31 +115,27 @@ this._getId = typeof idAttribute === 'function' ? idAttribute : getDefaultGetId(idAttribute); | ||
get key() { | ||
return this._key; | ||
} | ||
var _proto = EntitySchema.prototype; | ||
get idAttribute() { | ||
return this._idAttribute; | ||
} | ||
_proto.define = function define(definition) { | ||
this.schema = Object.keys(definition).reduce(function (entitySchema, key) { | ||
var _Object$assign; | ||
define(definition) { | ||
this.schema = Object.keys(definition).reduce((entitySchema, key) => { | ||
const schema = definition[key]; | ||
return Object.assign({}, entitySchema, { | ||
[key]: schema | ||
}); | ||
var schema = definition[key]; | ||
return Object.assign({}, entitySchema, (_Object$assign = {}, _Object$assign[key] = schema, _Object$assign)); | ||
}, this.schema || {}); | ||
} | ||
}; | ||
getId(input, parent, key) { | ||
_proto.getId = function getId(input, parent, key) { | ||
return this._getId(input, parent, key); | ||
} | ||
}; | ||
merge(entityA, entityB) { | ||
_proto.merge = function merge(entityA, entityB) { | ||
return this._mergeStrategy(entityA, entityB); | ||
} | ||
}; | ||
normalize(input, parent, key, visit, addEntity, visitedEntities) { | ||
const id = this.getId(input, parent, key); | ||
const entityType = this.key; | ||
_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)) { | ||
@@ -116,3 +149,5 @@ visitedEntities[entityType] = {}; | ||
if (visitedEntities[entityType][id].some(entity => entity === input)) { | ||
if (visitedEntities[entityType][id].some(function (entity) { | ||
return entity === input; | ||
})) { | ||
return id; | ||
@@ -123,7 +158,7 @@ } | ||
const processedEntity = this._processStrategy(input, parent, key); | ||
var processedEntity = this._processStrategy(input, parent, key); | ||
Object.keys(this.schema).forEach(key => { | ||
if (processedEntity.hasOwnProperty(key) && typeof processedEntity[key] === 'object') { | ||
const schema = this.schema[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); | ||
@@ -134,5 +169,7 @@ } | ||
return id; | ||
} | ||
}; | ||
denormalize(entity, unvisit) { | ||
_proto.denormalize = function denormalize(entity, unvisit) { | ||
var _this2 = this; | ||
if (isImmutable(entity)) { | ||
@@ -142,7 +179,10 @@ return denormalizeImmutable(this.schema, entity, unvisit); | ||
let found = true; | ||
Object.keys(this.schema).forEach(key => { | ||
const schema = this.schema[key]; | ||
const [value, foundItem] = unvisit(entity[key], schema); | ||
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) { | ||
@@ -152,3 +192,3 @@ found = false; | ||
if (entity.hasOwnProperty(key)) { | ||
if (Object.hasOwnProperty.call(entity, key)) { | ||
entity[key] = value; | ||
@@ -158,10 +198,33 @@ } | ||
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; | ||
} | ||
class PolymorphicSchema { | ||
constructor(definition, schemaAttribute) { | ||
var PolymorphicSchema = | ||
/*#__PURE__*/ | ||
function () { | ||
function PolymorphicSchema(definition, schemaAttribute) { | ||
if (schemaAttribute) { | ||
this._schemaAttribute = typeof schemaAttribute === 'string' ? input => input[schemaAttribute] : schemaAttribute; | ||
this._schemaAttribute = typeof schemaAttribute === 'string' ? function (input) { | ||
return input[schemaAttribute]; | ||
} : schemaAttribute; | ||
} | ||
@@ -172,15 +235,13 @@ | ||
get isSingleSchema() { | ||
return !this._schemaAttribute; | ||
} | ||
var _proto = PolymorphicSchema.prototype; | ||
define(definition) { | ||
_proto.define = function define(definition) { | ||
this.schema = definition; | ||
} | ||
}; | ||
getSchemaAttribute(input, parent, key) { | ||
_proto.getSchemaAttribute = function getSchemaAttribute(input, parent, key) { | ||
return !this.isSingleSchema && this._schemaAttribute(input, parent, key); | ||
} | ||
}; | ||
inferSchema(input, parent, key) { | ||
_proto.inferSchema = function inferSchema(input, parent, key) { | ||
if (this.isSingleSchema) { | ||
@@ -190,8 +251,8 @@ return this.schema; | ||
const attr = this.getSchemaAttribute(input, parent, key); | ||
var attr = this.getSchemaAttribute(input, parent, key); | ||
return this.schema[attr]; | ||
} | ||
}; | ||
normalizeValue(value, parent, key, visit, addEntity, visitedEntities) { | ||
const schema = this.inferSchema(value, parent, key); | ||
_proto.normalizeValue = function normalizeValue(value, parent, key, visit, addEntity, visitedEntities) { | ||
var schema = this.inferSchema(value, parent, key); | ||
@@ -202,3 +263,3 @@ if (!schema) { | ||
const normalizedValue = visit(value, parent, key, schema, addEntity, visitedEntities); | ||
var normalizedValue = visit(value, parent, key, schema, addEntity, visitedEntities); | ||
return this.isSingleSchema || normalizedValue === undefined || normalizedValue === null ? normalizedValue : { | ||
@@ -208,6 +269,6 @@ id: normalizedValue, | ||
}; | ||
} | ||
}; | ||
denormalizeValue(value, unvisit) { | ||
const schemaKey = isImmutable(value) ? value.get('schema') : value.schema; | ||
_proto.denormalizeValue = function denormalizeValue(value, unvisit) { | ||
var schemaKey = isImmutable(value) ? value.get('schema') : value.schema; | ||
@@ -218,11 +279,23 @@ if (!this.isSingleSchema && !schemaKey) { | ||
const id = isImmutable(value) ? value.get('id') : value.id; | ||
const schema = this.isSingleSchema ? this.schema : this.schema[schemaKey]; | ||
var id = isImmutable(value) ? value.get('id') : value.id; | ||
var schema = this.isSingleSchema ? this.schema : this.schema[schemaKey]; | ||
return unvisit(id || value, schema); | ||
} | ||
}; | ||
} | ||
_createClass(PolymorphicSchema, [{ | ||
key: "isSingleSchema", | ||
get: function get() { | ||
return !this._schemaAttribute; | ||
} | ||
}]); | ||
class UnionSchema extends PolymorphicSchema { | ||
constructor(definition, schemaAttribute) { | ||
return PolymorphicSchema; | ||
}(); | ||
var UnionSchema = | ||
/*#__PURE__*/ | ||
function (_PolymorphicSchema) { | ||
_inheritsLoose(UnionSchema, _PolymorphicSchema); | ||
function UnionSchema(definition, schemaAttribute) { | ||
if (!schemaAttribute) { | ||
@@ -232,31 +305,53 @@ throw new Error('Expected option "schemaAttribute" not found on UnionSchema.'); | ||
super(definition, schemaAttribute); | ||
return _PolymorphicSchema.call(this, definition, schemaAttribute) || this; | ||
} | ||
normalize(input, parent, key, visit, addEntity, visitedEntities) { | ||
var _proto = UnionSchema.prototype; | ||
_proto.normalize = function normalize(input, parent, key, visit, addEntity, visitedEntities) { | ||
return this.normalizeValue(input, parent, key, visit, addEntity, visitedEntities); | ||
} | ||
}; | ||
denormalize(input, unvisit) { | ||
_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; | ||
class ValuesSchema extends PolymorphicSchema { | ||
normalize(input, parent, key, visit, addEntity, visitedEntities) { | ||
return Object.keys(input).reduce((output, key, index) => { | ||
const value = input[key]; | ||
return value !== undefined && value !== null ? Object.assign({}, output, { | ||
[key]: this.normalizeValue(value, input, key, visit, addEntity, visitedEntities) | ||
}) : output; | ||
_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; | ||
}, {}); | ||
} | ||
}; | ||
denormalize(input, unvisit) { | ||
let found = true; | ||
return [Object.keys(input).reduce((output, key) => { | ||
const entityOrId = input[key]; | ||
const [value, foundItem] = this.denormalizeValue(entityOrId, unvisit); | ||
_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) { | ||
@@ -266,12 +361,11 @@ found = false; | ||
return Object.assign({}, output, { | ||
[key]: value | ||
}); | ||
return Object.assign({}, output, (_Object$assign2 = {}, _Object$assign2[key] = value, _Object$assign2)); | ||
}, {}), found]; | ||
} | ||
}; | ||
} | ||
return ValuesSchema; | ||
}(PolymorphicSchema); | ||
const validateSchema = definition => { | ||
const isArray = Array.isArray(definition); | ||
var validateSchema = function validateSchema(definition) { | ||
var isArray = Array.isArray(definition); | ||
@@ -285,44 +379,90 @@ if (isArray && definition.length > 1) { | ||
const getValues = input => Array.isArray(input) ? input : Object.keys(input).map(key => input[key]); | ||
var getValues = function getValues(input) { | ||
return Array.isArray(input) ? input : Object.keys(input).map(function (key) { | ||
return input[key]; | ||
}); | ||
}; | ||
const normalize = (schema, input, parent, key, visit, addEntity, visitedEntities) => { | ||
var normalize = function normalize(schema, input, parent, key, visit, addEntity, visitedEntities) { | ||
schema = validateSchema(schema); | ||
const values = getValues(input); // Special case: Arrays pass *their* parent on to their children, since there | ||
var values = getValues(input); // Special case: Arrays pass *their* parent on to their children, since there | ||
// is not any special information that can be gathered from themselves directly | ||
return values.map((value, index) => visit(value, parent, key, schema, addEntity, visitedEntities)); | ||
return values.map(function (value, index) { | ||
return visit(value, parent, key, schema, addEntity, visitedEntities); | ||
}); | ||
}; | ||
const denormalize = (schema, input, unvisit) => { | ||
var denormalize = function denormalize(schema, input, unvisit) { | ||
schema = validateSchema(schema); | ||
let found = true; | ||
var found = true; | ||
if (input === undefined && schema) { | ||
[, found] = unvisit(undefined, schema); | ||
var _unvisit = unvisit(undefined, schema); | ||
found = _unvisit[1]; | ||
} | ||
return [input && input.map ? input.map(entityOrId => unvisit(entityOrId, schema)).filter(([, foundItem]) => foundItem).map(([value]) => value) : input, found]; | ||
return [input && input.map ? input.map(function (entityOrId) { | ||
return unvisit(entityOrId, schema); | ||
}).filter(function (_ref) { | ||
var foundItem = _ref[1]; | ||
return foundItem; | ||
}).map(function (_ref2) { | ||
var value = _ref2[0]; | ||
return value; | ||
}) : input, found]; | ||
}; | ||
class ArraySchema extends PolymorphicSchema { | ||
normalize(input, parent, key, visit, addEntity, visitedEntities) { | ||
const values = getValues(input); | ||
return values.map((value, index) => this.normalizeValue(value, parent, key, visit, addEntity, visitedEntities)).filter(value => value !== undefined && value !== null); | ||
var ArraySchema = | ||
/*#__PURE__*/ | ||
function (_PolymorphicSchema) { | ||
_inheritsLoose(ArraySchema, _PolymorphicSchema); | ||
function ArraySchema() { | ||
return _PolymorphicSchema.apply(this, arguments) || this; | ||
} | ||
denormalize(input, unvisit) { | ||
let found = true; | ||
var _proto = ArraySchema.prototype; | ||
_proto.normalize = function normalize(input, parent, key, visit, addEntity, visitedEntities) { | ||
var _this = this; | ||
var values = getValues(input); | ||
return values.map(function (value, index) { | ||
return _this.normalizeValue(value, parent, key, visit, addEntity, visitedEntities); | ||
}).filter(function (value) { | ||
return value !== undefined && value !== null; | ||
}); | ||
}; | ||
_proto.denormalize = function denormalize(input, unvisit) { | ||
var _this2 = this; | ||
var found = true; | ||
if (input === undefined && this.schema) { | ||
[, found] = unvisit(undefined, this.schema); | ||
var _unvisit2 = unvisit(undefined, this.schema); | ||
found = _unvisit2[1]; | ||
} | ||
return [input && input.map ? input.map(entityOrId => this.denormalizeValue(entityOrId, unvisit)).filter(([, foundItem]) => foundItem).map(([value]) => value) : input, found]; | ||
} | ||
return [input && input.map ? input.map(function (entityOrId) { | ||
return _this2.denormalizeValue(entityOrId, unvisit); | ||
}).filter(function (_ref3) { | ||
var foundItem = _ref3[1]; | ||
return foundItem; | ||
}).map(function (_ref4) { | ||
var value = _ref4[0]; | ||
return value; | ||
}) : input, found]; | ||
}; | ||
} | ||
return ArraySchema; | ||
}(PolymorphicSchema); | ||
const normalize$1 = (schema, input, parent, key, visit, addEntity, visitedEntities) => { | ||
const object = Object.assign({}, input); | ||
Object.keys(schema).forEach(key => { | ||
const localSchema = schema[key]; | ||
const value = visit(input[key], input, key, localSchema, addEntity, visitedEntities); | ||
var _normalize = function normalize(schema, input, parent, key, visit, addEntity, visitedEntities) { | ||
var object = Object.assign({}, input); | ||
Object.keys(schema).forEach(function (key) { | ||
var localSchema = schema[key]; | ||
var value = visit(input[key], input, key, localSchema, addEntity, visitedEntities); | ||
@@ -337,3 +477,4 @@ if (value === undefined || value === null) { | ||
}; | ||
const denormalize$1 = (schema, input, unvisit) => { | ||
var _denormalize = function denormalize(schema, input, unvisit) { | ||
if (isImmutable(input)) { | ||
@@ -343,6 +484,8 @@ return denormalizeImmutable(schema, input, unvisit); | ||
const object = Object.assign({}, input); | ||
let found = true; | ||
Object.keys(schema).forEach(key => { | ||
const [item, foundItem] = unvisit(object[key], schema[key]); | ||
var object = Object.assign({}, input); | ||
var found = true; | ||
Object.keys(schema).forEach(function (key) { | ||
var _unvisit = unvisit(object[key], schema[key]), | ||
item = _unvisit[0], | ||
foundItem = _unvisit[1]; | ||
@@ -359,27 +502,41 @@ if (object[key] != null) { | ||
}; | ||
class ObjectSchema { | ||
constructor(definition) { | ||
var ObjectSchema = | ||
/*#__PURE__*/ | ||
function () { | ||
function ObjectSchema(definition) { | ||
this.define(definition); | ||
} | ||
define(definition) { | ||
this.schema = Object.keys(definition).reduce((entitySchema, key) => { | ||
const schema = definition[key]; | ||
return Object.assign({}, entitySchema, { | ||
[key]: schema | ||
}); | ||
var _proto = ObjectSchema.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 || {}); | ||
} | ||
}; | ||
normalize(...args) { | ||
return normalize$1(this.schema, ...args); | ||
} | ||
_proto.normalize = function normalize() { | ||
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { | ||
args[_key] = arguments[_key]; | ||
} | ||
denormalize(...args) { | ||
return denormalize$1(this.schema, ...args); | ||
} | ||
return _normalize.apply(void 0, [this.schema].concat(args)); | ||
}; | ||
} | ||
_proto.denormalize = function denormalize() { | ||
for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { | ||
args[_key2] = arguments[_key2]; | ||
} | ||
const visit = (value, parent, key, schema, addEntity, visitedEntities) => { | ||
return _denormalize.apply(void 0, [this.schema].concat(args)); | ||
}; | ||
return ObjectSchema; | ||
}(); | ||
var visit = function visit(value, parent, key, schema, addEntity, visitedEntities) { | ||
if (typeof value !== 'object' || !value) { | ||
@@ -390,3 +547,3 @@ return value; | ||
if (typeof schema === 'object' && (!schema.normalize || typeof schema.normalize !== 'function')) { | ||
const method = Array.isArray(schema) ? normalize : normalize$1; | ||
var method = Array.isArray(schema) ? normalize : _normalize; | ||
return method(schema, value, parent, key, visit, addEntity, visitedEntities); | ||
@@ -398,20 +555,22 @@ } | ||
const addEntities = entities => (schema, processedEntity, value, parent, key) => { | ||
const schemaKey = schema.key; | ||
const id = schema.getId(value, parent, key); | ||
var addEntities = function addEntities(entities) { | ||
return function (schema, processedEntity, value, parent, key) { | ||
var schemaKey = schema.key; | ||
var id = schema.getId(value, parent, key); | ||
if (!(schemaKey in entities)) { | ||
entities[schemaKey] = {}; | ||
} | ||
if (!(schemaKey in entities)) { | ||
entities[schemaKey] = {}; | ||
} | ||
const existingEntity = entities[schemaKey][id]; | ||
var existingEntity = entities[schemaKey][id]; | ||
if (existingEntity) { | ||
entities[schemaKey][id] = schema.merge(existingEntity, processedEntity); | ||
} else { | ||
entities[schemaKey][id] = processedEntity; | ||
} | ||
if (existingEntity) { | ||
entities[schemaKey][id] = schema.merge(existingEntity, processedEntity); | ||
} else { | ||
entities[schemaKey][id] = processedEntity; | ||
} | ||
}; | ||
}; | ||
const schema = { | ||
var schema = { | ||
Array: ArraySchema, | ||
@@ -423,19 +582,19 @@ Entity: EntitySchema, | ||
}; | ||
const normalize$2 = (input, schema) => { | ||
if (!input || typeof input !== 'object') { | ||
throw new Error("Unexpected input given to normalize. Expected type to be \"object\", found \"" + (input === null ? 'null' : typeof input) + "\"."); | ||
var normalize$1 = function normalize(input, schema) { | ||
if (input === null || typeof input !== typeof schema) { | ||
throw new Error("Unexpected input given to normalize. Expected type to be \"" + (schema === null ? 'null' : typeof schema) + "\", found \"" + (input === null ? 'null' : typeof input) + "\"."); | ||
} | ||
const entities = {}; | ||
const addEntity = addEntities(entities); | ||
const visitedEntities = {}; | ||
const result = visit(input, input, null, schema, addEntity, visitedEntities); | ||
var entities = {}; | ||
var addEntity = addEntities(entities); | ||
var visitedEntities = {}; | ||
var result = visit(input, input, null, schema, addEntity, visitedEntities); | ||
return { | ||
entities, | ||
result | ||
entities: entities, | ||
result: result | ||
}; | ||
}; | ||
const unvisitEntity = (id, schema, unvisit, getEntity, cache) => { | ||
const entity = getEntity(id, schema); | ||
var unvisitEntity = function unvisitEntity(id, schema, unvisit, getEntity, cache) { | ||
var entity = getEntity(id, schema); | ||
@@ -450,11 +609,15 @@ if (typeof entity !== 'object' || entity === null) { | ||
let found = true; | ||
var found = true; | ||
if (!cache[schema.key][id]) { | ||
// Ensure we don't mutate it non-immutable objects | ||
const entityCopy = isImmutable(entity) ? entity : Object.assign({}, entity); // Need to set this first so that if it is referenced further within the | ||
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; | ||
[cache[schema.key][id], found] = schema.denormalize(entityCopy, unvisit); | ||
var _schema$denormalize = schema.denormalize(entityCopy, unvisit); | ||
cache[schema.key][id] = _schema$denormalize[0]; | ||
found = _schema$denormalize[1]; | ||
} | ||
@@ -465,8 +628,8 @@ | ||
const getUnvisit = entities => { | ||
const cache = {}; | ||
const getEntity = getEntities(entities); | ||
var getUnvisit = function getUnvisit(entities) { | ||
var cache = {}; | ||
var getEntity = getEntities(entities); | ||
return function unvisit(input, schema) { | ||
if (typeof schema === 'object' && (!schema.denormalize || typeof schema.denormalize !== 'function')) { | ||
const method = Array.isArray(schema) ? denormalize : denormalize$1; | ||
var method = Array.isArray(schema) ? denormalize : _denormalize; | ||
return method(schema, input, unvisit); | ||
@@ -497,6 +660,6 @@ } // null is considered intentional, thus always 'found' as true | ||
const getEntities = entities => { | ||
const isImmutable$1 = isImmutable(entities); | ||
return (entityOrId, schema) => { | ||
const schemaKey = schema.key; | ||
var getEntities = function getEntities(entities) { | ||
var isImmutable$1 = isImmutable(entities); | ||
return function (entityOrId, schema) { | ||
var schemaKey = schema.key; | ||
@@ -515,3 +678,3 @@ if (typeof entityOrId === 'object') { | ||
const denormalize$2 = (input, schema, entities) => { | ||
var denormalize$1 = function denormalize(input, schema, entities) { | ||
if (typeof input !== 'undefined') { | ||
@@ -524,4 +687,4 @@ return getUnvisit(entities)(input, schema); | ||
exports.denormalize = denormalize$2; | ||
exports.normalize = normalize$2; | ||
exports.denormalize = denormalize$1; | ||
exports.normalize = normalize$1; | ||
exports.schema = schema; |
@@ -1,1 +0,1 @@ | ||
"use strict";function isImmutable(e){return!(!e||"function"!=typeof e.hasOwnProperty||!(e.hasOwnProperty("__ownerID")||e._map&&e._map.hasOwnProperty("__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(c.hasOwnProperty(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),e.hasOwnProperty(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},normalize$2=(e,t)=>{if(!e||"object"!=typeof e)throw new Error('Unexpected input given to normalize. Expected type to be "object", found "'+(null===e?"null":typeof e)+'".');const r={},i=addEntities(r);return{entities:r,result:visit(e,e,null,t,i,{})}},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]:n instanceof EntitySchema?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 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},normalize$2=(e,t)=>{if(null===e||typeof e!=typeof t)throw new Error('Unexpected input given to normalize. Expected type to be "'+(null===t?"null":typeof t)+'", found "'+(null===e?"null":typeof e)+'".');const r={},i=addEntities(r);return{entities:r,result:visit(e,e,null,t,i,{})}},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]:n instanceof EntitySchema?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; |
@@ -20,4 +20,4 @@ (function (global, factory) { | ||
function isImmutable(object) { | ||
return !!(object && typeof object.hasOwnProperty === 'function' && (object.hasOwnProperty('__ownerID') || // Immutable.Map | ||
object._map && object._map.hasOwnProperty('__ownerID'))); // Immutable.Record | ||
return !!(object && typeof object.hasOwnProperty === 'function' && (Object.hasOwnProperty.call(object, '__ownerID') || // Immutable.Map | ||
object._map && Object.hasOwnProperty.call(object._map, '__ownerID'))); // Immutable.Record | ||
} | ||
@@ -35,9 +35,12 @@ /** | ||
function denormalizeImmutable(schema, input, unvisit) { | ||
let found = true; | ||
return [Object.keys(schema).reduce((object, key) => { | ||
var found = true; | ||
return [Object.keys(schema).reduce(function (object, key) { | ||
// Immutable maps cast keys to strings on write so we need to ensure | ||
// we're accessing them using string keys. | ||
const stringKey = "" + key; | ||
const [item, foundItem] = unvisit(object.get(stringKey), schema[stringKey]); | ||
var stringKey = "" + key; | ||
var _unvisit = unvisit(object.get(stringKey), schema[stringKey]), | ||
item = _unvisit[0], | ||
foundItem = _unvisit[1]; | ||
if (!foundItem) { | ||
@@ -55,6 +58,36 @@ found = false; | ||
const getDefaultGetId = idAttribute => input => isImmutable(input) ? input.get(idAttribute) : input[idAttribute]; | ||
function _defineProperties(target, props) { | ||
for (var i = 0; i < props.length; i++) { | ||
var descriptor = props[i]; | ||
descriptor.enumerable = descriptor.enumerable || false; | ||
descriptor.configurable = true; | ||
if ("value" in descriptor) descriptor.writable = true; | ||
Object.defineProperty(target, descriptor.key, descriptor); | ||
} | ||
} | ||
class EntitySchema { | ||
constructor(key, definition = {}, options = {}) { | ||
function _createClass(Constructor, protoProps, staticProps) { | ||
if (protoProps) _defineProperties(Constructor.prototype, protoProps); | ||
if (staticProps) _defineProperties(Constructor, staticProps); | ||
return Constructor; | ||
} | ||
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') { | ||
@@ -64,9 +97,13 @@ throw new Error("Expected a string key for Entity, but found " + key + "."); | ||
const { | ||
idAttribute = 'id', | ||
mergeStrategy = (entityA, entityB) => { | ||
return Object.assign({}, entityA, {}, entityB); | ||
}, | ||
processStrategy = input => Object.assign({}, input) | ||
} = options; | ||
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; | ||
@@ -80,31 +117,27 @@ this._getId = typeof idAttribute === 'function' ? idAttribute : getDefaultGetId(idAttribute); | ||
get key() { | ||
return this._key; | ||
} | ||
var _proto = EntitySchema.prototype; | ||
get idAttribute() { | ||
return this._idAttribute; | ||
} | ||
_proto.define = function define(definition) { | ||
this.schema = Object.keys(definition).reduce(function (entitySchema, key) { | ||
var _Object$assign; | ||
define(definition) { | ||
this.schema = Object.keys(definition).reduce((entitySchema, key) => { | ||
const schema = definition[key]; | ||
return Object.assign({}, entitySchema, { | ||
[key]: schema | ||
}); | ||
var schema = definition[key]; | ||
return Object.assign({}, entitySchema, (_Object$assign = {}, _Object$assign[key] = schema, _Object$assign)); | ||
}, this.schema || {}); | ||
} | ||
}; | ||
getId(input, parent, key) { | ||
_proto.getId = function getId(input, parent, key) { | ||
return this._getId(input, parent, key); | ||
} | ||
}; | ||
merge(entityA, entityB) { | ||
_proto.merge = function merge(entityA, entityB) { | ||
return this._mergeStrategy(entityA, entityB); | ||
} | ||
}; | ||
normalize(input, parent, key, visit, addEntity, visitedEntities) { | ||
const id = this.getId(input, parent, key); | ||
const entityType = this.key; | ||
_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)) { | ||
@@ -118,3 +151,5 @@ visitedEntities[entityType] = {}; | ||
if (visitedEntities[entityType][id].some(entity => entity === input)) { | ||
if (visitedEntities[entityType][id].some(function (entity) { | ||
return entity === input; | ||
})) { | ||
return id; | ||
@@ -125,7 +160,7 @@ } | ||
const processedEntity = this._processStrategy(input, parent, key); | ||
var processedEntity = this._processStrategy(input, parent, key); | ||
Object.keys(this.schema).forEach(key => { | ||
if (processedEntity.hasOwnProperty(key) && typeof processedEntity[key] === 'object') { | ||
const schema = this.schema[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); | ||
@@ -136,5 +171,7 @@ } | ||
return id; | ||
} | ||
}; | ||
denormalize(entity, unvisit) { | ||
_proto.denormalize = function denormalize(entity, unvisit) { | ||
var _this2 = this; | ||
if (isImmutable(entity)) { | ||
@@ -144,7 +181,10 @@ return denormalizeImmutable(this.schema, entity, unvisit); | ||
let found = true; | ||
Object.keys(this.schema).forEach(key => { | ||
const schema = this.schema[key]; | ||
const [value, foundItem] = unvisit(entity[key], schema); | ||
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) { | ||
@@ -154,3 +194,3 @@ found = false; | ||
if (entity.hasOwnProperty(key)) { | ||
if (Object.hasOwnProperty.call(entity, key)) { | ||
entity[key] = value; | ||
@@ -160,10 +200,33 @@ } | ||
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; | ||
} | ||
class PolymorphicSchema { | ||
constructor(definition, schemaAttribute) { | ||
var PolymorphicSchema = | ||
/*#__PURE__*/ | ||
function () { | ||
function PolymorphicSchema(definition, schemaAttribute) { | ||
if (schemaAttribute) { | ||
this._schemaAttribute = typeof schemaAttribute === 'string' ? input => input[schemaAttribute] : schemaAttribute; | ||
this._schemaAttribute = typeof schemaAttribute === 'string' ? function (input) { | ||
return input[schemaAttribute]; | ||
} : schemaAttribute; | ||
} | ||
@@ -174,15 +237,13 @@ | ||
get isSingleSchema() { | ||
return !this._schemaAttribute; | ||
} | ||
var _proto = PolymorphicSchema.prototype; | ||
define(definition) { | ||
_proto.define = function define(definition) { | ||
this.schema = definition; | ||
} | ||
}; | ||
getSchemaAttribute(input, parent, key) { | ||
_proto.getSchemaAttribute = function getSchemaAttribute(input, parent, key) { | ||
return !this.isSingleSchema && this._schemaAttribute(input, parent, key); | ||
} | ||
}; | ||
inferSchema(input, parent, key) { | ||
_proto.inferSchema = function inferSchema(input, parent, key) { | ||
if (this.isSingleSchema) { | ||
@@ -192,8 +253,8 @@ return this.schema; | ||
const attr = this.getSchemaAttribute(input, parent, key); | ||
var attr = this.getSchemaAttribute(input, parent, key); | ||
return this.schema[attr]; | ||
} | ||
}; | ||
normalizeValue(value, parent, key, visit, addEntity, visitedEntities) { | ||
const schema = this.inferSchema(value, parent, key); | ||
_proto.normalizeValue = function normalizeValue(value, parent, key, visit, addEntity, visitedEntities) { | ||
var schema = this.inferSchema(value, parent, key); | ||
@@ -204,3 +265,3 @@ if (!schema) { | ||
const normalizedValue = visit(value, parent, key, schema, addEntity, visitedEntities); | ||
var normalizedValue = visit(value, parent, key, schema, addEntity, visitedEntities); | ||
return this.isSingleSchema || normalizedValue === undefined || normalizedValue === null ? normalizedValue : { | ||
@@ -210,6 +271,6 @@ id: normalizedValue, | ||
}; | ||
} | ||
}; | ||
denormalizeValue(value, unvisit) { | ||
const schemaKey = isImmutable(value) ? value.get('schema') : value.schema; | ||
_proto.denormalizeValue = function denormalizeValue(value, unvisit) { | ||
var schemaKey = isImmutable(value) ? value.get('schema') : value.schema; | ||
@@ -220,11 +281,23 @@ if (!this.isSingleSchema && !schemaKey) { | ||
const id = isImmutable(value) ? value.get('id') : value.id; | ||
const schema = this.isSingleSchema ? this.schema : this.schema[schemaKey]; | ||
var id = isImmutable(value) ? value.get('id') : value.id; | ||
var schema = this.isSingleSchema ? this.schema : this.schema[schemaKey]; | ||
return unvisit(id || value, schema); | ||
} | ||
}; | ||
} | ||
_createClass(PolymorphicSchema, [{ | ||
key: "isSingleSchema", | ||
get: function get() { | ||
return !this._schemaAttribute; | ||
} | ||
}]); | ||
class UnionSchema extends PolymorphicSchema { | ||
constructor(definition, schemaAttribute) { | ||
return PolymorphicSchema; | ||
}(); | ||
var UnionSchema = | ||
/*#__PURE__*/ | ||
function (_PolymorphicSchema) { | ||
_inheritsLoose(UnionSchema, _PolymorphicSchema); | ||
function UnionSchema(definition, schemaAttribute) { | ||
if (!schemaAttribute) { | ||
@@ -234,31 +307,53 @@ throw new Error('Expected option "schemaAttribute" not found on UnionSchema.'); | ||
super(definition, schemaAttribute); | ||
return _PolymorphicSchema.call(this, definition, schemaAttribute) || this; | ||
} | ||
normalize(input, parent, key, visit, addEntity, visitedEntities) { | ||
var _proto = UnionSchema.prototype; | ||
_proto.normalize = function normalize(input, parent, key, visit, addEntity, visitedEntities) { | ||
return this.normalizeValue(input, parent, key, visit, addEntity, visitedEntities); | ||
} | ||
}; | ||
denormalize(input, unvisit) { | ||
_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; | ||
class ValuesSchema extends PolymorphicSchema { | ||
normalize(input, parent, key, visit, addEntity, visitedEntities) { | ||
return Object.keys(input).reduce((output, key, index) => { | ||
const value = input[key]; | ||
return value !== undefined && value !== null ? Object.assign({}, output, { | ||
[key]: this.normalizeValue(value, input, key, visit, addEntity, visitedEntities) | ||
}) : output; | ||
_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; | ||
}, {}); | ||
} | ||
}; | ||
denormalize(input, unvisit) { | ||
let found = true; | ||
return [Object.keys(input).reduce((output, key) => { | ||
const entityOrId = input[key]; | ||
const [value, foundItem] = this.denormalizeValue(entityOrId, unvisit); | ||
_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) { | ||
@@ -268,12 +363,11 @@ found = false; | ||
return Object.assign({}, output, { | ||
[key]: value | ||
}); | ||
return Object.assign({}, output, (_Object$assign2 = {}, _Object$assign2[key] = value, _Object$assign2)); | ||
}, {}), found]; | ||
} | ||
}; | ||
} | ||
return ValuesSchema; | ||
}(PolymorphicSchema); | ||
const validateSchema = definition => { | ||
const isArray = Array.isArray(definition); | ||
var validateSchema = function validateSchema(definition) { | ||
var isArray = Array.isArray(definition); | ||
@@ -287,44 +381,90 @@ if (isArray && definition.length > 1) { | ||
const getValues = input => Array.isArray(input) ? input : Object.keys(input).map(key => input[key]); | ||
var getValues = function getValues(input) { | ||
return Array.isArray(input) ? input : Object.keys(input).map(function (key) { | ||
return input[key]; | ||
}); | ||
}; | ||
const normalize = (schema, input, parent, key, visit, addEntity, visitedEntities) => { | ||
var normalize = function normalize(schema, input, parent, key, visit, addEntity, visitedEntities) { | ||
schema = validateSchema(schema); | ||
const values = getValues(input); // Special case: Arrays pass *their* parent on to their children, since there | ||
var values = getValues(input); // Special case: Arrays pass *their* parent on to their children, since there | ||
// is not any special information that can be gathered from themselves directly | ||
return values.map((value, index) => visit(value, parent, key, schema, addEntity, visitedEntities)); | ||
return values.map(function (value, index) { | ||
return visit(value, parent, key, schema, addEntity, visitedEntities); | ||
}); | ||
}; | ||
const denormalize = (schema, input, unvisit) => { | ||
var denormalize = function denormalize(schema, input, unvisit) { | ||
schema = validateSchema(schema); | ||
let found = true; | ||
var found = true; | ||
if (input === undefined && schema) { | ||
[, found] = unvisit(undefined, schema); | ||
var _unvisit = unvisit(undefined, schema); | ||
found = _unvisit[1]; | ||
} | ||
return [input && input.map ? input.map(entityOrId => unvisit(entityOrId, schema)).filter(([, foundItem]) => foundItem).map(([value]) => value) : input, found]; | ||
return [input && input.map ? input.map(function (entityOrId) { | ||
return unvisit(entityOrId, schema); | ||
}).filter(function (_ref) { | ||
var foundItem = _ref[1]; | ||
return foundItem; | ||
}).map(function (_ref2) { | ||
var value = _ref2[0]; | ||
return value; | ||
}) : input, found]; | ||
}; | ||
class ArraySchema extends PolymorphicSchema { | ||
normalize(input, parent, key, visit, addEntity, visitedEntities) { | ||
const values = getValues(input); | ||
return values.map((value, index) => this.normalizeValue(value, parent, key, visit, addEntity, visitedEntities)).filter(value => value !== undefined && value !== null); | ||
var ArraySchema = | ||
/*#__PURE__*/ | ||
function (_PolymorphicSchema) { | ||
_inheritsLoose(ArraySchema, _PolymorphicSchema); | ||
function ArraySchema() { | ||
return _PolymorphicSchema.apply(this, arguments) || this; | ||
} | ||
denormalize(input, unvisit) { | ||
let found = true; | ||
var _proto = ArraySchema.prototype; | ||
_proto.normalize = function normalize(input, parent, key, visit, addEntity, visitedEntities) { | ||
var _this = this; | ||
var values = getValues(input); | ||
return values.map(function (value, index) { | ||
return _this.normalizeValue(value, parent, key, visit, addEntity, visitedEntities); | ||
}).filter(function (value) { | ||
return value !== undefined && value !== null; | ||
}); | ||
}; | ||
_proto.denormalize = function denormalize(input, unvisit) { | ||
var _this2 = this; | ||
var found = true; | ||
if (input === undefined && this.schema) { | ||
[, found] = unvisit(undefined, this.schema); | ||
var _unvisit2 = unvisit(undefined, this.schema); | ||
found = _unvisit2[1]; | ||
} | ||
return [input && input.map ? input.map(entityOrId => this.denormalizeValue(entityOrId, unvisit)).filter(([, foundItem]) => foundItem).map(([value]) => value) : input, found]; | ||
} | ||
return [input && input.map ? input.map(function (entityOrId) { | ||
return _this2.denormalizeValue(entityOrId, unvisit); | ||
}).filter(function (_ref3) { | ||
var foundItem = _ref3[1]; | ||
return foundItem; | ||
}).map(function (_ref4) { | ||
var value = _ref4[0]; | ||
return value; | ||
}) : input, found]; | ||
}; | ||
} | ||
return ArraySchema; | ||
}(PolymorphicSchema); | ||
const normalize$1 = (schema, input, parent, key, visit, addEntity, visitedEntities) => { | ||
const object = Object.assign({}, input); | ||
Object.keys(schema).forEach(key => { | ||
const localSchema = schema[key]; | ||
const value = visit(input[key], input, key, localSchema, addEntity, visitedEntities); | ||
var _normalize = function normalize(schema, input, parent, key, visit, addEntity, visitedEntities) { | ||
var object = Object.assign({}, input); | ||
Object.keys(schema).forEach(function (key) { | ||
var localSchema = schema[key]; | ||
var value = visit(input[key], input, key, localSchema, addEntity, visitedEntities); | ||
@@ -339,3 +479,4 @@ if (value === undefined || value === null) { | ||
}; | ||
const denormalize$1 = (schema, input, unvisit) => { | ||
var _denormalize = function denormalize(schema, input, unvisit) { | ||
if (isImmutable(input)) { | ||
@@ -345,6 +486,8 @@ return denormalizeImmutable(schema, input, unvisit); | ||
const object = Object.assign({}, input); | ||
let found = true; | ||
Object.keys(schema).forEach(key => { | ||
const [item, foundItem] = unvisit(object[key], schema[key]); | ||
var object = Object.assign({}, input); | ||
var found = true; | ||
Object.keys(schema).forEach(function (key) { | ||
var _unvisit = unvisit(object[key], schema[key]), | ||
item = _unvisit[0], | ||
foundItem = _unvisit[1]; | ||
@@ -361,27 +504,41 @@ if (object[key] != null) { | ||
}; | ||
class ObjectSchema { | ||
constructor(definition) { | ||
var ObjectSchema = | ||
/*#__PURE__*/ | ||
function () { | ||
function ObjectSchema(definition) { | ||
this.define(definition); | ||
} | ||
define(definition) { | ||
this.schema = Object.keys(definition).reduce((entitySchema, key) => { | ||
const schema = definition[key]; | ||
return Object.assign({}, entitySchema, { | ||
[key]: schema | ||
}); | ||
var _proto = ObjectSchema.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 || {}); | ||
} | ||
}; | ||
normalize(...args) { | ||
return normalize$1(this.schema, ...args); | ||
} | ||
_proto.normalize = function normalize() { | ||
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { | ||
args[_key] = arguments[_key]; | ||
} | ||
denormalize(...args) { | ||
return denormalize$1(this.schema, ...args); | ||
} | ||
return _normalize.apply(void 0, [this.schema].concat(args)); | ||
}; | ||
} | ||
_proto.denormalize = function denormalize() { | ||
for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { | ||
args[_key2] = arguments[_key2]; | ||
} | ||
const visit = (value, parent, key, schema, addEntity, visitedEntities) => { | ||
return _denormalize.apply(void 0, [this.schema].concat(args)); | ||
}; | ||
return ObjectSchema; | ||
}(); | ||
var visit = function visit(value, parent, key, schema, addEntity, visitedEntities) { | ||
if (typeof value !== 'object' || !value) { | ||
@@ -392,3 +549,3 @@ return value; | ||
if (typeof schema === 'object' && (!schema.normalize || typeof schema.normalize !== 'function')) { | ||
const method = Array.isArray(schema) ? normalize : normalize$1; | ||
var method = Array.isArray(schema) ? normalize : _normalize; | ||
return method(schema, value, parent, key, visit, addEntity, visitedEntities); | ||
@@ -400,20 +557,22 @@ } | ||
const addEntities = entities => (schema, processedEntity, value, parent, key) => { | ||
const schemaKey = schema.key; | ||
const id = schema.getId(value, parent, key); | ||
var addEntities = function addEntities(entities) { | ||
return function (schema, processedEntity, value, parent, key) { | ||
var schemaKey = schema.key; | ||
var id = schema.getId(value, parent, key); | ||
if (!(schemaKey in entities)) { | ||
entities[schemaKey] = {}; | ||
} | ||
if (!(schemaKey in entities)) { | ||
entities[schemaKey] = {}; | ||
} | ||
const existingEntity = entities[schemaKey][id]; | ||
var existingEntity = entities[schemaKey][id]; | ||
if (existingEntity) { | ||
entities[schemaKey][id] = schema.merge(existingEntity, processedEntity); | ||
} else { | ||
entities[schemaKey][id] = processedEntity; | ||
} | ||
if (existingEntity) { | ||
entities[schemaKey][id] = schema.merge(existingEntity, processedEntity); | ||
} else { | ||
entities[schemaKey][id] = processedEntity; | ||
} | ||
}; | ||
}; | ||
const schema = { | ||
var schema = { | ||
Array: ArraySchema, | ||
@@ -425,19 +584,19 @@ Entity: EntitySchema, | ||
}; | ||
const normalize$2 = (input, schema) => { | ||
if (!input || typeof input !== 'object') { | ||
throw new Error("Unexpected input given to normalize. Expected type to be \"object\", found \"" + (input === null ? 'null' : typeof input) + "\"."); | ||
var normalize$1 = function normalize(input, schema) { | ||
if (input === null || typeof input !== typeof schema) { | ||
throw new Error("Unexpected input given to normalize. Expected type to be \"" + (schema === null ? 'null' : typeof schema) + "\", found \"" + (input === null ? 'null' : typeof input) + "\"."); | ||
} | ||
const entities = {}; | ||
const addEntity = addEntities(entities); | ||
const visitedEntities = {}; | ||
const result = visit(input, input, null, schema, addEntity, visitedEntities); | ||
var entities = {}; | ||
var addEntity = addEntities(entities); | ||
var visitedEntities = {}; | ||
var result = visit(input, input, null, schema, addEntity, visitedEntities); | ||
return { | ||
entities, | ||
result | ||
entities: entities, | ||
result: result | ||
}; | ||
}; | ||
const unvisitEntity = (id, schema, unvisit, getEntity, cache) => { | ||
const entity = getEntity(id, schema); | ||
var unvisitEntity = function unvisitEntity(id, schema, unvisit, getEntity, cache) { | ||
var entity = getEntity(id, schema); | ||
@@ -452,11 +611,15 @@ if (typeof entity !== 'object' || entity === null) { | ||
let found = true; | ||
var found = true; | ||
if (!cache[schema.key][id]) { | ||
// Ensure we don't mutate it non-immutable objects | ||
const entityCopy = isImmutable(entity) ? entity : Object.assign({}, entity); // Need to set this first so that if it is referenced further within the | ||
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; | ||
[cache[schema.key][id], found] = schema.denormalize(entityCopy, unvisit); | ||
var _schema$denormalize = schema.denormalize(entityCopy, unvisit); | ||
cache[schema.key][id] = _schema$denormalize[0]; | ||
found = _schema$denormalize[1]; | ||
} | ||
@@ -467,8 +630,8 @@ | ||
const getUnvisit = entities => { | ||
const cache = {}; | ||
const getEntity = getEntities(entities); | ||
var getUnvisit = function getUnvisit(entities) { | ||
var cache = {}; | ||
var getEntity = getEntities(entities); | ||
return function unvisit(input, schema) { | ||
if (typeof schema === 'object' && (!schema.denormalize || typeof schema.denormalize !== 'function')) { | ||
const method = Array.isArray(schema) ? denormalize : denormalize$1; | ||
var method = Array.isArray(schema) ? denormalize : _denormalize; | ||
return method(schema, input, unvisit); | ||
@@ -499,6 +662,6 @@ } // null is considered intentional, thus always 'found' as true | ||
const getEntities = entities => { | ||
const isImmutable$1 = isImmutable(entities); | ||
return (entityOrId, schema) => { | ||
const schemaKey = schema.key; | ||
var getEntities = function getEntities(entities) { | ||
var isImmutable$1 = isImmutable(entities); | ||
return function (entityOrId, schema) { | ||
var schemaKey = schema.key; | ||
@@ -517,3 +680,3 @@ if (typeof entityOrId === 'object') { | ||
const denormalize$2 = (input, schema, entities) => { | ||
var denormalize$1 = function denormalize(input, schema, entities) { | ||
if (typeof input !== 'undefined') { | ||
@@ -526,4 +689,4 @@ return getUnvisit(entities)(input, schema); | ||
exports.denormalize = denormalize$2; | ||
exports.normalize = normalize$2; | ||
exports.denormalize = denormalize$1; | ||
exports.normalize = normalize$1; | ||
exports.schema = schema; | ||
@@ -530,0 +693,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||!(e.hasOwnProperty("__ownerID")||e._map&&e._map.hasOwnProperty("__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={},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(a.hasOwnProperty(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),e.hasOwnProperty(t)&&(e[t]=i)}),[e,s]}}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,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 o=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]},c=e=>Array.isArray(e)?e:Object.keys(e).map(t=>e[t]),a=(e,t,r,n,s,i,a)=>{return e=o(e),c(t).map((t,o)=>s(t,r,n,e,i,a))},u=(e,t,r)=>{e=o(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 h=(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},l=(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 m=(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)?a:h)(n,e,t,r,m,s,i)}return n.normalize(e,t,r,m,s,i)},f={Array:class extends i{normalize(e,t,r,n,s,i){return c(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: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 h(this.schema,...e)}denormalize(...e){return l(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,s,i){return this.normalizeValue(e,t,r,n,s,i)}denormalize(e,t){return this.denormalizeValue(e,t)}},Values:class extends i{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]}}},d=e=>{const r={},n=y(e);return function e(i,o){if("object"==typeof o&&(!o.denormalize||"function"!=typeof o.denormalize)){return(Array.isArray(o)?u:l)(o,i,e)}return null===i?[i,!0]:o instanceof s?void 0===i?[i,!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]})(i,o,e,n,r):"function"==typeof o.denormalize?o.denormalize(i,e):[i,!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]}};e.denormalize=(e,t,r)=>void 0!==e?d(r)(e,t):[void 0,!1],e.normalize=(e,t)=>{if(!e||"object"!=typeof e)throw new Error('Unexpected input given to normalize. Expected type to be "object", found "'+(null===e?"null":typeof e)+'".');const r={},n=(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})(r);return{entities:r,result:m(e,e,null,t,n,{})}},e.schema=f,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 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={},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]}}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,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 o=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]},c=e=>Array.isArray(e)?e:Object.keys(e).map(t=>e[t]),a=(e,t,r,n,s,i,a)=>{return e=o(e),c(t).map((t,o)=>s(t,r,n,e,i,a))},u=(e,t,r)=>{e=o(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 h=(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},l=(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 m=(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)?a:h)(n,e,t,r,m,s,i)}return n.normalize(e,t,r,m,s,i)},f={Array:class extends i{normalize(e,t,r,n,s,i){return c(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: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 h(this.schema,...e)}denormalize(...e){return l(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,s,i){return this.normalizeValue(e,t,r,n,s,i)}denormalize(e,t){return this.denormalizeValue(e,t)}},Values:class extends i{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]}}},d=e=>{const r={},n=y(e);return function e(i,o){if("object"==typeof o&&(!o.denormalize||"function"!=typeof o.denormalize)){return(Array.isArray(o)?u:l)(o,i,e)}return null===i?[i,!0]:o instanceof s?void 0===i?[i,!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]})(i,o,e,n,r):"function"==typeof o.denormalize?o.denormalize(i,e):[i,!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]}};e.denormalize=(e,t,r)=>void 0!==e?d(r)(e,t):[void 0,!1],e.normalize=(e,t)=>{if(null===e||typeof e!=typeof t)throw new Error('Unexpected input given to normalize. Expected type to be "'+(null===t?"null":typeof t)+'", found "'+(null===e?"null":typeof e)+'".');const r={},n=(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})(r);return{entities:r,result:m(e,e,null,t,n,{})}},e.schema=f,Object.defineProperty(e,"__esModule",{value:!0})})); |
{ | ||
"name": "@rest-hooks/normalizr", | ||
"version": "4.0.0", | ||
"version": "4.1.0", | ||
"description": "Normalizes and denormalizes JSON according to schema for Redux and Flux applications", | ||
"homepage": "https://github.com/coinbase/rest-hooks/tree/master/packages/normalizr#readme", | ||
"bugs": { | ||
"url": "https://github.com/ntucker/normalizr/issues" | ||
"url": "https://github.com/coinbase/rest-hooks/issues" | ||
}, | ||
"homepage": "https://github.com/ntucker/normalizr", | ||
"repository": { | ||
"url": "https://github.com/ntucker/normalizr.git", | ||
"type": "git" | ||
"type": "git", | ||
"url": "git+ssh://git@github.com:coinbase/rest-hooks.git", | ||
"directory": "packages/normalizr" | ||
}, | ||
@@ -23,3 +24,3 @@ "keywords": [ | ||
"dist/", | ||
"index.d.ts", | ||
"src/index.d.ts", | ||
"LICENSE", | ||
@@ -30,3 +31,3 @@ "README.md" | ||
"module": "dist/normalizr.es.js", | ||
"typings": "index.d.ts", | ||
"typings": "src/index.d.ts", | ||
"sideEffects": false, | ||
@@ -38,4 +39,2 @@ "scripts": { | ||
"clean": "rimraf dist", | ||
"flow": "flow", | ||
"flow:ci": "flow check", | ||
"lint": "yarn lint:cmd --fix", | ||
@@ -59,40 +58,11 @@ "lint:ci": "yarn lint:cmd", | ||
"license": "MIT", | ||
"browserslist": [ | ||
"> 1%", | ||
"Firefox ESR", | ||
"not dead", | ||
"not ie<12", | ||
"not OperaMini all" | ||
], | ||
"devDependencies": { | ||
"@anansi/babel-preset": "^0.19.3", | ||
"@babel/core": "^7.7.2", | ||
"babel-core": "^7.0.0-bridge.0", | ||
"babel-eslint": "^10.0.3", | ||
"babel-jest": "^24.9.0", | ||
"coveralls": "^3.0.3", | ||
"eslint": "^5.16.0", | ||
"eslint-config-prettier": "^5.0.0", | ||
"eslint-plugin-jest": "^22.6.4", | ||
"eslint-plugin-json": "^1.4.0", | ||
"eslint-plugin-prettier": "^3.1.0", | ||
"flow-bin": "^0.101.0", | ||
"husky": "^2.3.0", | ||
"coveralls": "^3.0.9", | ||
"immutable": "^3.8.1", | ||
"jest": "^24.8.0", | ||
"lint-staged": "^8.1.7", | ||
"npm-run-all": "^4.1.5", | ||
"prettier": "^1.19.1", | ||
"rimraf": "^2.6.3", | ||
"rollup": "^1.26.3", | ||
"rollup-plugin-babel": "^4.3.3", | ||
"rollup-plugin-commonjs": "^10.1.0", | ||
"rollup-plugin-filesize": "^6.2.1", | ||
"rollup-plugin-node-resolve": "^5.2.0", | ||
"rollup-plugin-terser": "^5.1.2", | ||
"typescript": "^3.7.2" | ||
"npm-run-all": "^4.1.5" | ||
}, | ||
"dependencies": { | ||
"@babel/runtime": "^7.7.0" | ||
} | ||
}, | ||
"gitHead": "7b86389ec9b44d8e2b8248c80616cd95fe6e90d8" | ||
} |
@@ -1,2 +0,3 @@ | ||
# normalizr [![build status](https://img.shields.io/travis/ntucker/normalizr/master.svg?style=flat-square)](https://travis-ci.org/ntucker/normalizr) [![Coverage Status](https://img.shields.io/coveralls/ntucker/normalizr/master.svg?style=flat-square)](https://coveralls.io/github/ntucker/normalizr?branch=master) [![npm version](https://img.shields.io/npm/v/@rest-hooks/normalizr.svg?style=flat-square)](https://www.npmjs.com/package/@rest-hooks/normalizr) [![npm downloads](https://img.shields.io/npm/dm/@rest-hooks/normalizr.svg?style=flat-square)](https://www.npmjs.com/package/@rest-hooks/normalizr) [![Greenkeeper badge](https://badges.greenkeeper.io/ntucker/normalizr.svg)](https://greenkeeper.io/) | ||
# normalizr | ||
[![CircleCI](https://circleci.com/gh/coinbase/rest-hooks.svg?style=shield)](https://circleci.com/gh/coinbase/rest-hooks) [![Coverage Status](https://img.shields.io/coveralls/ntucker/normalizr/master.svg?style=flat-square)](https://coveralls.io/github/ntucker/normalizr?branch=master) [![npm version](https://img.shields.io/npm/v/@rest-hooks/normalizr.svg?style=flat-square)](https://www.npmjs.com/package/@rest-hooks/normalizr) [![npm downloads](https://img.shields.io/npm/dm/@rest-hooks/normalizr.svg?style=flat-square)](https://www.npmjs.com/package/@rest-hooks/normalizr) | ||
@@ -3,0 +4,0 @@ ## Install |
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
145163
3
3034
121
1