Skip to content
This repository has been archived by the owner on Mar 20, 2022. It is now read-only.

Commit

Permalink
Refactor denormalize/unvisit.
Browse files Browse the repository at this point in the history
Fixes gh-225
  • Loading branch information
paularmstrong committed Jan 30, 2017
1 parent 5b73515 commit b4dd248
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 8 deletions.
8 changes: 6 additions & 2 deletions src/schemas/Array.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ export const normalize = (schema, input, parent, key, visit, addEntity) => {

export const denormalize = (schema, input, unvisit, getDenormalizedEntity) => {
schema = validateSchema(schema);
return input.map((entityOrId) => unvisit(entityOrId, schema, getDenormalizedEntity));
return Array.isArray(input) ?
input.map((entityOrId) => unvisit(entityOrId, schema, getDenormalizedEntity)) :
input;
};

export default class ArraySchema extends PolymorphicSchema {
Expand All @@ -35,6 +37,8 @@ export default class ArraySchema extends PolymorphicSchema {
}

denormalize(input, unvisit, getDenormalizedEntity) {
return input.map((value) => this.denormalizeValue(value, unvisit, getDenormalizedEntity));
return Array.isArray(input) ?
input.map((value) => this.denormalizeValue(value, unvisit, getDenormalizedEntity)) :
input;
}
}
18 changes: 12 additions & 6 deletions src/schemas/Entity.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { denormalize } from './Object';

export default class EntitySchema {
constructor(key, definition = {}, options = {}) {
if (!key || typeof key !== 'string') {
Expand All @@ -16,7 +14,7 @@ export default class EntitySchema {

this._key = key;
this._getId = typeof idAttribute === 'function' ? idAttribute : (input) => input[idAttribute];
this._idAttribute = idAttribute
this._idAttribute = idAttribute;
this._mergeStrategy = mergeStrategy;
this._processStrategy = processStrategy;
this.define(definition);
Expand All @@ -25,9 +23,9 @@ export default class EntitySchema {
get key() {
return this._key;
}

get idAttribute() {
return this._idAttribute
return this._idAttribute;
}

define(definition) {
Expand Down Expand Up @@ -63,6 +61,14 @@ export default class EntitySchema {
if (typeof entity !== 'object') {
return entity;
}
return denormalize(this.schema, entity, unvisit, getDenormalizedEntity);

const processedEntity = { ...entity };
Object.keys(this.schema).forEach((key) => {
if (processedEntity.hasOwnProperty(key)) {
const schema = this.schema[key];
processedEntity[key] = unvisit(processedEntity[key], schema, getDenormalizedEntity);
}
});
return processedEntity;
}
}
31 changes: 31 additions & 0 deletions src/schemas/__tests__/Array.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,21 @@ describe(`${schema.Array.name} denormalization`, () => {
};
expect(denormalize([ 1, 2 ], [ cats ], entities)).toMatchSnapshot();
});

it('returns the input value if is not an array', () => {
const filling = new schema.Entity('fillings');
const taco = new schema.Entity('tacos', { fillings: [ filling ] });
const entities = {
tacos: {
'123': {
id: '123',
fillings: null
}
}
};

expect(denormalize('123', taco, entities)).toMatchSnapshot();
});
});

describe('Class', () => {
Expand Down Expand Up @@ -137,5 +152,21 @@ describe(`${schema.Array.name} denormalization`, () => {

expect(denormalize(input, listSchema, entities)).toMatchSnapshot();
});

it('returns the input value if is not an array', () => {
const filling = new schema.Entity('fillings');
const fillings = new schema.Array(filling);
const taco = new schema.Entity('tacos', { fillings });
const entities = {
tacos: {
'123': {
id: '123',
fillings: null
}
}
};

expect(denormalize('123', taco, entities)).toMatchSnapshot();
});
});
});
14 changes: 14 additions & 0 deletions src/schemas/__tests__/__snapshots__/Array.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ Array [
]
`;

exports[`ArraySchema denormalization Class returns the input value if is not an array 1`] = `
Object {
"fillings": null,
"id": "123",
}
`;

exports[`ArraySchema denormalization Object denormalizes a single entity 1`] = `
Array [
Object {
Expand All @@ -44,6 +51,13 @@ Array [
]
`;

exports[`ArraySchema denormalization Object returns the input value if is not an array 1`] = `
Object {
"fillings": null,
"id": "123",
}
`;

exports[`ArraySchema normalization Class filters out undefined and null normalized values 1`] = `
Object {
"entities": Object {
Expand Down

0 comments on commit b4dd248

Please sign in to comment.