function denormalize(input, schema, entities) { if (!input) { return input; } return getUnvisit(entities)(input, schema); }
...
if (!cache[schema.key][id]) {
// Ensure we don't mutate it non-immutable objects
var entityCopy = isImmutable(entity) ? entity : _extends({}, 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] = schema.denormalize(entityCopy, unvisit);
}
return cache[schema.key][id];
};
var getUnvisit = function getUnvisit(entities) {
var cache = {};
...
function normalize(input, schema) { if (!input || (typeof input === 'undefined' ? 'undefined' : _typeof(input)) !== 'object') { throw new Error('Unexpected input given to normalize. Expected type to be "object", found "' + (typeof input === 'undefined' ? ' undefined' : _typeof(input)) + '".'); } var entities = {}; var addEntity = addEntities(entities); var result = visit(input, input, null, schema, addEntity); return { entities: entities, result: result }; }
...
}
if ((typeof schema === 'undefined' ? 'undefined' : _typeof(schema)) === 'object' && (!schema
.normalize || typeof schema.normalize !== 'function')) {
var method = Array.isArray(schema) ? normalize$2 : _normalize;
return method(schema, value, parent, key, visit, addEntity);
}
return schema.normalize(value, parent, key, visit, addEntity);
};
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)) {
...
function ArraySchema() { _classCallCheck(this, ArraySchema); return _possibleConstructorReturn(this, (ArraySchema.__proto__ || Object.getPrototypeOf(ArraySchema)).apply(this, arguments)); }
n/a
function EntitySchema(key) { var definition = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; _classCallCheck(this, EntitySchema); if (!key || typeof key !== 'string') { throw new Error('Expected a string key for Entity, but found ' + key + '.'); } var _options$idAttribute = options.idAttribute, idAttribute = _options$idAttribute === undefined ? 'id' : _options$idAttribute, _options$mergeStrateg = options.mergeStrategy, mergeStrategy = _options$mergeStrateg === undefined ? function (entityA, entityB) { return _extends({}, entityA, entityB); } : _options$mergeStrateg, _options$processStrat = options.processStrategy, processStrategy = _options$processStrat === undefined ? function (input) { return _extends({}, input); } : _options$processStrat; this._key = key; this._getId = typeof idAttribute === 'function' ? idAttribute : getDefaultGetId(idAttribute); this._idAttribute = idAttribute; this._mergeStrategy = mergeStrategy; this._processStrategy = processStrategy; this.define(definition); }
...
We have two nested entity types within our `article`: `users` and `comments`. Using various `schema`, we can normalize all three
entity types down:
```js
import { normalize, schema } from 'normalizr';
// Define a users schema
const user = new schema.Entity('users');
// Define your comments schema
const comment = new schema.Entity('comments', {
commenter: user
});
// Define your article
...
function ObjectSchema(definition) { _classCallCheck(this, ObjectSchema); this.define(definition); }
n/a
function UnionSchema(definition, schemaAttribute) { _classCallCheck(this, UnionSchema); if (!schemaAttribute) { throw new Error('Expected option "schemaAttribute" not found on UnionSchema.'); } return _possibleConstructorReturn(this, (UnionSchema.__proto__ || Object.getPrototypeOf(UnionSchema)).call(this, definition, schemaAttribute )); }
n/a
function ValuesSchema() { _classCallCheck(this, ValuesSchema); return _possibleConstructorReturn(this, (ValuesSchema.__proto__ || Object.getPrototypeOf(ValuesSchema)).apply(this, arguments)); }
n/a