@rest-hooks/normalizr
Advanced tools
Comparing version 5.0.0 to 5.0.1
@@ -6,2 +6,11 @@ # Change Log | ||
### [5.0.1](https://github.com/coinbase/rest-hooks/compare/@rest-hooks/normalizr@5.0.0...@rest-hooks/normalizr@5.0.1) (2020-01-29) | ||
### 🐛 Bug Fix | ||
* ES export is non-min version ([4b04b62](https://github.com/coinbase/rest-hooks/commit/4b04b629e67cce295c82743cb6d3a6d9f99df506)) | ||
## [5.0.0](https://github.com/coinbase/rest-hooks/compare/@rest-hooks/normalizr@4.3.3...@rest-hooks/normalizr@5.0.0) (2020-01-29) | ||
@@ -8,0 +17,0 @@ |
@@ -1,4 +0,1 @@ | ||
import _inheritsLoose from '@babel/runtime/helpers/esm/inheritsLoose'; | ||
import _createClass from '@babel/runtime/helpers/esm/createClass'; | ||
/** | ||
@@ -31,12 +28,9 @@ * Helpers to enable Immutable compatibility *without* bringing in | ||
function denormalizeImmutable(schema, input, unvisit) { | ||
var found = true; | ||
return [Object.keys(schema).reduce(function (object, key) { | ||
let found = true; | ||
return [Object.keys(schema).reduce((object, key) => { | ||
// Immutable maps cast keys to strings on write so we need to ensure | ||
// we're accessing them using string keys. | ||
var stringKey = "" + key; | ||
const stringKey = "" + key; | ||
const [item, foundItem] = unvisit(object.get(stringKey), schema[stringKey]); | ||
var _unvisit = unvisit(object.get(stringKey), schema[stringKey]), | ||
item = _unvisit[0], | ||
foundItem = _unvisit[1]; | ||
if (!foundItem) { | ||
@@ -54,10 +48,6 @@ found = false; | ||
var PolymorphicSchema = | ||
/*#__PURE__*/ | ||
function () { | ||
function PolymorphicSchema(definition, schemaAttribute) { | ||
class PolymorphicSchema { | ||
constructor(definition, schemaAttribute) { | ||
if (schemaAttribute) { | ||
this._schemaAttribute = typeof schemaAttribute === 'string' ? function (input) { | ||
return input[schemaAttribute]; | ||
} : schemaAttribute; | ||
this._schemaAttribute = typeof schemaAttribute === 'string' ? input => input[schemaAttribute] : schemaAttribute; | ||
} | ||
@@ -68,13 +58,15 @@ | ||
var _proto = PolymorphicSchema.prototype; | ||
get isSingleSchema() { | ||
return !this._schemaAttribute; | ||
} | ||
_proto.define = function define(definition) { | ||
define(definition) { | ||
this.schema = definition; | ||
}; | ||
} | ||
_proto.getSchemaAttribute = function getSchemaAttribute(input, parent, key) { | ||
getSchemaAttribute(input, parent, key) { | ||
return !this.isSingleSchema && this._schemaAttribute(input, parent, key); | ||
}; | ||
} | ||
_proto.inferSchema = function inferSchema(input, parent, key) { | ||
inferSchema(input, parent, key) { | ||
if (this.isSingleSchema) { | ||
@@ -84,8 +76,8 @@ return this.schema; | ||
var attr = this.getSchemaAttribute(input, parent, key); | ||
const attr = this.getSchemaAttribute(input, parent, key); | ||
return this.schema[attr]; | ||
}; | ||
} | ||
_proto.normalizeValue = function normalizeValue(value, parent, key, visit, addEntity, visitedEntities) { | ||
var schema = this.inferSchema(value, parent, key); | ||
normalizeValue(value, parent, key, visit, addEntity, visitedEntities) { | ||
const schema = this.inferSchema(value, parent, key); | ||
@@ -96,3 +88,3 @@ if (!schema) { | ||
var normalizedValue = visit(value, parent, key, schema, addEntity, visitedEntities); | ||
const normalizedValue = visit(value, parent, key, schema, addEntity, visitedEntities); | ||
return this.isSingleSchema || normalizedValue === undefined || normalizedValue === null ? normalizedValue : { | ||
@@ -102,6 +94,6 @@ id: normalizedValue, | ||
}; | ||
}; | ||
} | ||
_proto.denormalizeValue = function denormalizeValue(value, unvisit) { | ||
var schemaKey = isImmutable(value) ? value.get('schema') : value.schema; | ||
denormalizeValue(value, unvisit) { | ||
const schemaKey = isImmutable(value) ? value.get('schema') : value.schema; | ||
@@ -112,20 +104,12 @@ if (!this.isSingleSchema && !schemaKey) { | ||
var id = this.isSingleSchema ? undefined : isImmutable(value) ? value.get('id') : value.id; | ||
var schema = this.isSingleSchema ? this.schema : this.schema[schemaKey]; | ||
const id = this.isSingleSchema ? undefined : isImmutable(value) ? value.get('id') : value.id; | ||
const schema = this.isSingleSchema ? this.schema : this.schema[schemaKey]; | ||
return unvisit(id || value, schema); | ||
}; | ||
} | ||
_createClass(PolymorphicSchema, [{ | ||
key: "isSingleSchema", | ||
get: function get() { | ||
return !this._schemaAttribute; | ||
} | ||
}]); | ||
} | ||
return PolymorphicSchema; | ||
}(); | ||
const validateSchema = definition => { | ||
const isArray = Array.isArray(definition); | ||
var validateSchema = function validateSchema(definition) { | ||
var isArray = Array.isArray(definition); | ||
if (isArray && definition.length > 1) { | ||
@@ -138,90 +122,44 @@ throw new Error("Expected schema definition to be a single schema, but found " + definition.length + "."); | ||
var getValues = function getValues(input) { | ||
return Array.isArray(input) ? input : Object.keys(input).map(function (key) { | ||
return input[key]; | ||
}); | ||
}; | ||
const getValues = input => Array.isArray(input) ? input : Object.keys(input).map(key => input[key]); | ||
var normalize = function normalize(schema, input, parent, key, visit, addEntity, visitedEntities) { | ||
const normalize = (schema, input, parent, key, visit, addEntity, visitedEntities) => { | ||
schema = validateSchema(schema); | ||
var values = getValues(input); // Special case: Arrays pass *their* parent on to their children, since there | ||
const 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(function (value, index) { | ||
return visit(value, parent, key, schema, addEntity, visitedEntities); | ||
}); | ||
return values.map((value, index) => visit(value, parent, key, schema, addEntity, visitedEntities)); | ||
}; | ||
var denormalize = function denormalize(schema, input, unvisit) { | ||
const denormalize = (schema, input, unvisit) => { | ||
schema = validateSchema(schema); | ||
var found = true; | ||
let found = true; | ||
if (input === undefined && schema) { | ||
var _unvisit = unvisit(undefined, schema); | ||
found = _unvisit[1]; | ||
[, found] = unvisit(undefined, schema); | ||
} | ||
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]; | ||
return [input && input.map ? input.map(entityOrId => unvisit(entityOrId, schema)).filter(([, foundItem]) => foundItem).map(([value]) => value) : input, found]; | ||
}; | ||
var ArraySchema = | ||
/*#__PURE__*/ | ||
function (_PolymorphicSchema) { | ||
_inheritsLoose(ArraySchema, _PolymorphicSchema); | ||
function ArraySchema() { | ||
return _PolymorphicSchema.apply(this, arguments) || this; | ||
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 _proto = ArraySchema.prototype; | ||
denormalize(input, unvisit) { | ||
let found = true; | ||
_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) { | ||
var _unvisit2 = unvisit(undefined, this.schema); | ||
found = _unvisit2[1]; | ||
[, found] = unvisit(undefined, this.schema); | ||
} | ||
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 [input && input.map ? input.map(entityOrId => this.denormalizeValue(entityOrId, unvisit)).filter(([, foundItem]) => foundItem).map(([value]) => value) : input, found]; | ||
} | ||
return ArraySchema; | ||
}(PolymorphicSchema); | ||
} | ||
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); | ||
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); | ||
@@ -236,4 +174,3 @@ if (value === undefined || value === null) { | ||
}; | ||
var _denormalize = function denormalize(schema, input, unvisit) { | ||
const denormalize$1 = (schema, input, unvisit) => { | ||
if (isImmutable(input)) { | ||
@@ -243,8 +180,6 @@ return denormalizeImmutable(schema, input, unvisit); | ||
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]; | ||
const object = Object.assign({}, input); | ||
let found = true; | ||
Object.keys(schema).forEach(key => { | ||
const [item, foundItem] = unvisit(object[key], schema[key]); | ||
@@ -261,43 +196,29 @@ if (object[key] !== undefined) { | ||
}; | ||
var ObjectSchema = | ||
/*#__PURE__*/ | ||
function () { | ||
function ObjectSchema(definition) { | ||
class ObjectSchema { | ||
constructor(definition) { | ||
this.define(definition); | ||
} | ||
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)); | ||
define(definition) { | ||
this.schema = Object.keys(definition).reduce((entitySchema, key) => { | ||
const schema = definition[key]; | ||
return Object.assign({}, entitySchema, { | ||
[key]: schema | ||
}); | ||
}, this.schema || {}); | ||
}; | ||
} | ||
_proto.normalize = function normalize() { | ||
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { | ||
args[_key] = arguments[_key]; | ||
} | ||
normalize(...args) { | ||
return normalize$1(this.schema, ...args); | ||
} | ||
return _normalize.apply(void 0, [this.schema].concat(args)); | ||
}; | ||
denormalize(...args) { | ||
return denormalize$1(this.schema, ...args); | ||
} | ||
_proto.denormalize = function denormalize() { | ||
for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { | ||
args[_key2] = arguments[_key2]; | ||
} | ||
} | ||
return _denormalize.apply(void 0, [this.schema].concat(args)); | ||
}; | ||
const unvisitEntity = (id, schema, unvisit, getEntity, cache) => { | ||
const entity = getEntity(id, schema); | ||
return ObjectSchema; | ||
}(); | ||
var unvisitEntity = function unvisitEntity(id, schema, unvisit, getEntity, cache) { | ||
var entity = getEntity(id, schema); | ||
if (typeof entity !== 'object' || entity === null) { | ||
@@ -311,15 +232,11 @@ return [entity, false]; | ||
var found = true; | ||
let found = true; | ||
if (!cache[schema.key][id]) { | ||
// Ensure we don't mutate it non-immutable objects | ||
var entityCopy = isImmutable(entity) ? entity : Object.assign({}, entity); // Need to set this first so that if it is referenced further within the | ||
const entityCopy = isImmutable(entity) ? entity : Object.assign({}, entity); // Need to set this first so that if it is referenced further within the | ||
// denormalization the reference will already exist. | ||
cache[schema.key][id] = entityCopy; | ||
var _schema$denormalize = schema.denormalize(entityCopy, unvisit); | ||
cache[schema.key][id] = _schema$denormalize[0]; | ||
found = _schema$denormalize[1]; | ||
[cache[schema.key][id], found] = schema.denormalize(entityCopy, unvisit); | ||
} | ||
@@ -330,5 +247,5 @@ | ||
var getUnvisit = function getUnvisit(entities) { | ||
var cache = {}; | ||
var getEntity = getEntities(entities); | ||
const getUnvisit = entities => { | ||
const cache = {}; | ||
const getEntity = getEntities(entities); | ||
return [function unvisit(input, schema) { | ||
@@ -338,3 +255,3 @@ if (!schema) return [input, true]; | ||
if (typeof schema === 'object' && (!schema.denormalize || typeof schema.denormalize !== 'function')) { | ||
var method = Array.isArray(schema) ? denormalize : _denormalize; | ||
const method = Array.isArray(schema) ? denormalize : denormalize$1; | ||
return method(schema, input, unvisit); | ||
@@ -365,6 +282,6 @@ } // null is considered intentional, thus always 'found' as true | ||
var getEntities = function getEntities(entities) { | ||
var isImmutable$1 = isImmutable(entities); | ||
return function (entityOrId, schema) { | ||
var schemaKey = schema.key; | ||
const getEntities = entities => { | ||
const isImmutable$1 = isImmutable(entities); | ||
return (entityOrId, schema) => { | ||
const schemaKey = schema.key; | ||
@@ -383,3 +300,3 @@ if (typeof entityOrId === 'object') { | ||
var denormalize$1 = function denormalize(input, schema, entities) { | ||
const denormalize$2 = (input, schema, entities) => { | ||
/* istanbul ignore next */ | ||
@@ -390,7 +307,4 @@ // eslint-disable-next-line no-undef | ||
if (typeof input !== 'undefined') { | ||
var _getUnvisit = getUnvisit(entities), | ||
unvisit = _getUnvisit[0], | ||
cache = _getUnvisit[1]; | ||
return [].concat(unvisit(input, schema), [cache]); | ||
const [unvisit, cache] = getUnvisit(entities); | ||
return [...unvisit(input, schema), cache]; | ||
} | ||
@@ -401,20 +315,6 @@ | ||
var getDefaultGetId = function getDefaultGetId(idAttribute) { | ||
return function (input) { | ||
return isImmutable(input) ? input.get(idAttribute) : input[idAttribute]; | ||
}; | ||
}; | ||
const getDefaultGetId = idAttribute => input => 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 = {}; | ||
} | ||
class EntitySchema { | ||
constructor(key, definition = {}, options = {}) { | ||
if (!key || typeof key !== 'string') { | ||
@@ -424,13 +324,9 @@ throw new Error("Expected a string key for Entity, but found " + key + "."); | ||
var _options = options, | ||
_options$idAttribute = _options.idAttribute, | ||
idAttribute = _options$idAttribute === void 0 ? 'id' : _options$idAttribute, | ||
_options$mergeStrateg = _options.mergeStrategy, | ||
mergeStrategy = _options$mergeStrateg === void 0 ? function (entityA, entityB) { | ||
return Object.assign({}, entityA, {}, entityB); | ||
} : _options$mergeStrateg, | ||
_options$processStrat = _options.processStrategy, | ||
processStrategy = _options$processStrat === void 0 ? function (input) { | ||
return Object.assign({}, input); | ||
} : _options$processStrat; | ||
const { | ||
idAttribute = 'id', | ||
mergeStrategy = (entityA, entityB) => { | ||
return Object.assign({}, entityA, {}, entityB); | ||
}, | ||
processStrategy = input => Object.assign({}, input) | ||
} = options; | ||
this._key = key; | ||
@@ -444,27 +340,31 @@ this._getId = typeof idAttribute === 'function' ? idAttribute : getDefaultGetId(idAttribute); | ||
var _proto = EntitySchema.prototype; | ||
get key() { | ||
return this._key; | ||
} | ||
_proto.define = function define(definition) { | ||
this.schema = Object.keys(definition).reduce(function (entitySchema, key) { | ||
var _Object$assign; | ||
get idAttribute() { | ||
return this._idAttribute; | ||
} | ||
var schema = definition[key]; | ||
return Object.assign({}, entitySchema, (_Object$assign = {}, _Object$assign[key] = schema, _Object$assign)); | ||
define(definition) { | ||
this.schema = Object.keys(definition).reduce((entitySchema, key) => { | ||
const schema = definition[key]; | ||
return Object.assign({}, entitySchema, { | ||
[key]: schema | ||
}); | ||
}, this.schema || {}); | ||
}; | ||
} | ||
_proto.getId = function getId(input, parent, key) { | ||
getId(input, parent, key) { | ||
return this._getId(input, parent, key); | ||
}; | ||
} | ||
_proto.merge = function merge(entityA, entityB) { | ||
merge(entityA, entityB) { | ||
return this._mergeStrategy(entityA, entityB); | ||
}; | ||
} | ||
_proto.normalize = function normalize(input, parent, key, visit, addEntity, visitedEntities) { | ||
var _this = this; | ||
normalize(input, parent, key, visit, addEntity, visitedEntities) { | ||
const id = this.getId(input, parent, key); | ||
const entityType = this.key; | ||
var id = this.getId(input, parent, key); | ||
var entityType = this.key; | ||
if (!(entityType in visitedEntities)) { | ||
@@ -478,5 +378,3 @@ visitedEntities[entityType] = {}; | ||
if (visitedEntities[entityType][id].some(function (entity) { | ||
return entity === input; | ||
})) { | ||
if (visitedEntities[entityType][id].some(entity => entity === input)) { | ||
return id; | ||
@@ -487,7 +385,7 @@ } | ||
var processedEntity = this._processStrategy(input, parent, key); | ||
const processedEntity = this._processStrategy(input, parent, key); | ||
Object.keys(this.schema).forEach(function (key) { | ||
Object.keys(this.schema).forEach(key => { | ||
if (Object.hasOwnProperty.call(processedEntity, key) && typeof processedEntity[key] === 'object') { | ||
var schema = _this.schema[key]; | ||
const schema = this.schema[key]; | ||
processedEntity[key] = visit(processedEntity[key], processedEntity, key, schema, addEntity, visitedEntities); | ||
@@ -498,7 +396,5 @@ } | ||
return id; | ||
}; | ||
} | ||
_proto.denormalize = function denormalize(entity, unvisit) { | ||
var _this2 = this; | ||
denormalize(entity, unvisit) { | ||
if (isImmutable(entity)) { | ||
@@ -508,10 +404,7 @@ return denormalizeImmutable(this.schema, entity, unvisit); | ||
var found = true; | ||
Object.keys(this.schema).forEach(function (key) { | ||
var schema = _this2.schema[key]; | ||
let found = true; | ||
Object.keys(this.schema).forEach(key => { | ||
const schema = this.schema[key]; | ||
const [value, foundItem] = unvisit(entity[key], schema); | ||
var _unvisit = unvisit(entity[key], schema), | ||
value = _unvisit[0], | ||
foundItem = _unvisit[1]; | ||
if (!foundItem) { | ||
@@ -526,25 +419,8 @@ found = false; | ||
return [entity, found]; | ||
}; | ||
} | ||
_createClass(EntitySchema, [{ | ||
key: "key", | ||
get: function get() { | ||
return this._key; | ||
} | ||
}, { | ||
key: "idAttribute", | ||
get: function get() { | ||
return this._idAttribute; | ||
} | ||
}]); | ||
} | ||
return EntitySchema; | ||
}(); | ||
var UnionSchema = | ||
/*#__PURE__*/ | ||
function (_PolymorphicSchema) { | ||
_inheritsLoose(UnionSchema, _PolymorphicSchema); | ||
function UnionSchema(definition, schemaAttribute) { | ||
class UnionSchema extends PolymorphicSchema { | ||
constructor(definition, schemaAttribute) { | ||
if (!schemaAttribute) { | ||
@@ -554,53 +430,31 @@ throw new Error('Expected option "schemaAttribute" not found on UnionSchema.'); | ||
return _PolymorphicSchema.call(this, definition, schemaAttribute) || this; | ||
super(definition, schemaAttribute); | ||
} | ||
var _proto = UnionSchema.prototype; | ||
_proto.normalize = function normalize(input, parent, key, visit, addEntity, visitedEntities) { | ||
normalize(input, parent, key, visit, addEntity, visitedEntities) { | ||
return this.normalizeValue(input, parent, key, visit, addEntity, visitedEntities); | ||
}; | ||
} | ||
_proto.denormalize = function denormalize(input, unvisit) { | ||
denormalize(input, unvisit) { | ||
return this.denormalizeValue(input, unvisit); | ||
}; | ||
return UnionSchema; | ||
}(PolymorphicSchema); | ||
var ValuesSchema = | ||
/*#__PURE__*/ | ||
function (_PolymorphicSchema) { | ||
_inheritsLoose(ValuesSchema, _PolymorphicSchema); | ||
function ValuesSchema() { | ||
return _PolymorphicSchema.apply(this, arguments) || this; | ||
} | ||
var _proto = ValuesSchema.prototype; | ||
} | ||
_proto.normalize = function normalize(input, parent, key, visit, addEntity, visitedEntities) { | ||
var _this = this; | ||
return Object.keys(input).reduce(function (output, key, index) { | ||
var _Object$assign; | ||
var value = input[key]; | ||
return value !== undefined && value !== null ? Object.assign({}, output, (_Object$assign = {}, _Object$assign[key] = _this.normalizeValue(value, input, key, visit, addEntity, visitedEntities), _Object$assign)) : output; | ||
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.denormalize = function denormalize(input, unvisit) { | ||
var _this2 = this; | ||
denormalize(input, unvisit) { | ||
let found = true; | ||
return [Object.keys(input).reduce((output, key) => { | ||
const entityOrId = input[key]; | ||
const [value, foundItem] = this.denormalizeValue(entityOrId, unvisit); | ||
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) { | ||
@@ -610,10 +464,11 @@ found = false; | ||
return Object.assign({}, output, (_Object$assign2 = {}, _Object$assign2[key] = value, _Object$assign2)); | ||
return Object.assign({}, output, { | ||
[key]: value | ||
}); | ||
}, {}), found]; | ||
}; | ||
} | ||
return ValuesSchema; | ||
}(PolymorphicSchema); | ||
} | ||
var visit = function visit(value, parent, key, schema, addEntity, visitedEntities) { | ||
const visit = (value, parent, key, schema, addEntity, visitedEntities) => { | ||
if (typeof value !== 'object' || !value || !schema) { | ||
@@ -624,3 +479,3 @@ return value; | ||
if (typeof schema === 'object' && (!schema.normalize || typeof schema.normalize !== 'function')) { | ||
var method = Array.isArray(schema) ? normalize : _normalize; | ||
const method = Array.isArray(schema) ? normalize : normalize$1; | ||
return method(schema, value, parent, key, visit, addEntity, visitedEntities); | ||
@@ -632,65 +487,50 @@ } | ||
var addEntities = function addEntities(entities, indexes) { | ||
return function (schema, processedEntity, value, parent, key) { | ||
var schemaKey = schema.key; | ||
var id = schema.getId(value, parent, key); | ||
const addEntities = (entities, indexes) => (schema, processedEntity, value, parent, key) => { | ||
const schemaKey = schema.key; | ||
const id = schema.getId(value, parent, key); | ||
if (!(schemaKey in entities)) { | ||
entities[schemaKey] = {}; | ||
} | ||
if (!(schemaKey in entities)) { | ||
entities[schemaKey] = {}; | ||
} | ||
var existingEntity = entities[schemaKey][id]; | ||
const existingEntity = entities[schemaKey][id]; | ||
if (existingEntity) { | ||
entities[schemaKey][id] = schema.merge(existingEntity, processedEntity); | ||
} else { | ||
entities[schemaKey][id] = processedEntity; | ||
} // update index | ||
if (existingEntity) { | ||
entities[schemaKey][id] = schema.merge(existingEntity, processedEntity); | ||
} else { | ||
entities[schemaKey][id] = processedEntity; | ||
} // update index | ||
if (Array.isArray(schema.indexes)) { | ||
var entity = entities[schemaKey][id]; | ||
if (Array.isArray(schema.indexes)) { | ||
const entity = entities[schemaKey][id]; | ||
if (!(schemaKey in indexes)) { | ||
indexes[schemaKey] = {}; | ||
if (!(schemaKey in indexes)) { | ||
indexes[schemaKey] = {}; | ||
} | ||
for (const index of schema.indexes) { | ||
if (!(index in indexes[schemaKey])) { | ||
indexes[schemaKey][index] = {}; | ||
} | ||
for (var _iterator = schema.indexes, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) { | ||
var _ref; | ||
const indexMap = indexes[schemaKey][index]; | ||
if (_isArray) { | ||
if (_i >= _iterator.length) break; | ||
_ref = _iterator[_i++]; | ||
} else { | ||
_i = _iterator.next(); | ||
if (_i.done) break; | ||
_ref = _i.value; | ||
} | ||
if (existingEntity) { | ||
delete indexMap[existingEntity[index]]; | ||
} | ||
var index = _ref; | ||
if (!(index in indexes[schemaKey])) { | ||
indexes[schemaKey][index] = {}; | ||
if (index in entity) { | ||
indexMap[entity[index]] = id; | ||
} | ||
/* istanbul ignore next */ | ||
else if ( // eslint-disable-next-line no-undef | ||
process.env.NODE_ENV !== 'production') { | ||
console.warn("Index not found in entity. Indexes must be top-level members of your entity.\nIndex: " + index + "\nEntity: " + JSON.stringify(entity, undefined, 2)); | ||
} | ||
var indexMap = indexes[schemaKey][index]; | ||
if (existingEntity) { | ||
delete indexMap[existingEntity[index]]; | ||
} | ||
if (index in entity) { | ||
indexMap[entity[index]] = id; | ||
} | ||
/* istanbul ignore next */ | ||
else if ( // eslint-disable-next-line no-undef | ||
process.env.NODE_ENV !== 'production') { | ||
console.warn("Index not found in entity. Indexes must be top-level members of your entity.\nIndex: " + index + "\nEntity: " + JSON.stringify(entity, undefined, 2)); | ||
} | ||
} | ||
} | ||
}; | ||
} | ||
}; | ||
var schema = { | ||
const schema = { | ||
Array: ArraySchema, | ||
@@ -707,4 +547,4 @@ Entity: EntitySchema, | ||
var normalize$1 = function normalize(input, schema) { | ||
var schemaType = expectedSchemaType(schema); | ||
const normalize$2 = (input, schema) => { | ||
const schemaType = expectedSchemaType(schema); | ||
@@ -715,14 +555,14 @@ if (input === null || typeof input !== schemaType) { | ||
var entities = {}; | ||
var indexes = {}; | ||
var addEntity = addEntities(entities, indexes); | ||
var visitedEntities = {}; | ||
var result = visit(input, input, undefined, schema, addEntity, visitedEntities); | ||
const entities = {}; | ||
const indexes = {}; | ||
const addEntity = addEntities(entities, indexes); | ||
const visitedEntities = {}; | ||
const result = visit(input, input, undefined, schema, addEntity, visitedEntities); | ||
return { | ||
entities: entities, | ||
indexes: indexes, | ||
result: result | ||
entities, | ||
indexes, | ||
result | ||
}; | ||
}; | ||
export { denormalize$1 as denormalize, normalize$1 as normalize, schema }; | ||
export { denormalize$2 as denormalize, normalize$2 as normalize, schema }; |
{ | ||
"name": "@rest-hooks/normalizr", | ||
"version": "5.0.0", | ||
"version": "5.0.1", | ||
"description": "Normalizes and denormalizes JSON according to schema for Redux and Flux applications", | ||
@@ -29,3 +29,3 @@ "homepage": "https://github.com/coinbase/rest-hooks/tree/master/packages/normalizr#readme", | ||
"main": "dist/normalizr.js", | ||
"module": "dist/normalizr.es.min.js", | ||
"module": "dist/normalizr.es.js", | ||
"typings": "src/index.d.ts", | ||
@@ -37,2 +37,4 @@ "sideEffects": false, | ||
"build:production": "NODE_ENV=production rollup -c", | ||
"build:es_development": "BROWSERSLIST_ENV=production NODE_ENV=development rollup -c", | ||
"build:es_production": "BROWSERSLIST_ENV=production NODE_ENV=production rollup -c", | ||
"clean": "rimraf dist", | ||
@@ -65,3 +67,3 @@ "lint": "yarn lint:cmd --fix", | ||
}, | ||
"gitHead": "3da6f649bd4f81f85be01742f02e13713abcbf5e" | ||
"gitHead": "e09ce052c86d094bad18b57ba1baee60c556b9e6" | ||
} |
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
0
155821
3120