Change function and param naming to communicate the purpose better

This commit is contained in:
Kimmo Puputti 2017-02-22 11:17:45 +02:00
parent d02785cf9d
commit 22878eb755
3 changed files with 32 additions and 32 deletions

View file

@ -1,6 +1,6 @@
/* eslint-disable no-constant-condition, no-console */
import { call, put, take, fork } from 'redux-saga/effects';
import { mergeEntities } from '../util/data';
import { updatedEntities } from '../util/data';
const requestAction = actionType => params => ({ type: actionType, payload: { params } });
@ -44,7 +44,7 @@ const initialState = {
};
const merge = (state, payload) => {
const entities = mergeEntities(state.entities, payload.data);
const entities = updatedEntities(state.entities, payload.data);
return { ...state, entities };
};

View file

@ -1,39 +1,39 @@
/**
* Merge the given relationships objects
* Combine the given relationships objects
*
* See: http://jsonapi.org/format/#document-resource-object-relationships
*/
export const mergeRelationships = (rels1, rels2) => {
if (!rels1 && !rels2) {
export const combinedRelationships = (oldRels, newRels) => {
if (!oldRels && !newRels) {
// Special case to avoid adding an empty relationships object when
// none of the resource objects had any relationships.
return null;
}
return { ...rels1, ...rels2 };
return { ...oldRels, ...newRels };
};
/**
* Merge the given resource objects
* Combine the given resource objects
*
* See: http://jsonapi.org/format/#document-resource-objects
*/
export const mergeResourceObjects = (res1, res2) => {
const { id, type } = res1;
if (res2.id.uuid !== id.uuid || res2.type !== type) {
export const combinedResourceObjects = (oldRes, newRes) => {
const { id, type } = oldRes;
if (newRes.id.uuid !== id.uuid || newRes.type !== type) {
throw new Error('Cannot merge resource objects with different ids or types');
}
const attributes = res2.attributes || res1.attributes;
const attributes = newRes.attributes || oldRes.attributes;
const attrs = attributes ? { attributes } : null;
const relationships = mergeRelationships(res1.relationships, res2.relationships);
const relationships = combinedRelationships(oldRes.relationships, newRes.relationships);
const rels = relationships ? { relationships } : null;
return { id, type, ...attrs, ...rels };
};
/**
* Merge the resource objects form the given api response to the
* Combine the resource objects form the given api response to the
* existing entities.
*/
export const mergeEntities = (oldEntities, apiResponse) => {
export const updatedEntities = (oldEntities, apiResponse) => {
const { data, included = [] } = apiResponse;
const objects = (Array.isArray(data) ? data : [data]).concat(included);
@ -43,7 +43,7 @@ export const mergeEntities = (oldEntities, apiResponse) => {
const { id, type } = curr;
entities[type] = entities[type] || {};
const entity = entities[type][id.uuid];
entities[type][id.uuid] = entity ? mergeResourceObjects(entity, curr) : curr;
entities[type][id.uuid] = entity ? combinedResourceObjects(entity, curr) : curr;
return entities;
},
oldEntities,

View file

@ -1,12 +1,12 @@
import { types } from 'sharetribe-sdk';
import { mergeRelationships, mergeResourceObjects, mergeEntities } from './data';
import { combinedRelationships, combinedResourceObjects, updatedEntities } from './data';
const { UUID } = types;
describe('data utils', () => {
describe('mergeRelationships()', () => {
describe('combinedRelationships()', () => {
it('handles no rels', () => {
expect(mergeRelationships(undefined, undefined)).toEqual(null);
expect(combinedRelationships(undefined, undefined)).toEqual(null);
});
it('takes existings rels if no new rels are given', () => {
@ -19,7 +19,7 @@ describe('data utils', () => {
],
},
};
expect(mergeRelationships(listingRels, undefined)).toEqual(listingRels);
expect(combinedRelationships(listingRels, undefined)).toEqual(listingRels);
});
it('takes new rels if no existing rels are given', () => {
@ -27,7 +27,7 @@ describe('data utils', () => {
author: { data: { id: new UUID('author-id'), type: 'author' } },
images: { data: { id: new UUID('image1'), type: 'image' } },
};
expect(mergeRelationships(undefined, listingRels)).toEqual(listingRels);
expect(combinedRelationships(undefined, listingRels)).toEqual(listingRels);
});
it('merges two nonempty rels', () => {
@ -48,7 +48,7 @@ describe('data utils', () => {
const oldListingRels = { author: authorRel, images: imagesRel };
const newListingRels = { images: newImagesRel, reviews: reviewsRel };
expect(mergeRelationships(oldListingRels, newListingRels)).toEqual({
expect(combinedRelationships(oldListingRels, newListingRels)).toEqual({
author: authorRel,
images: newImagesRel,
reviews: reviewsRel,
@ -56,11 +56,11 @@ describe('data utils', () => {
});
});
describe('mergeResourceObjects()', () => {
describe('combinedResourceObjects()', () => {
it("throws if ids don't match", () => {
const listing1 = { id: new UUID('listing1'), type: 'listing' };
const listing2 = { id: new UUID('listing2'), type: 'listing' };
expect(() => mergeResourceObjects(listing1, listing2)).toThrow(
expect(() => combinedResourceObjects(listing1, listing2)).toThrow(
'Cannot merge resource objects with different ids or types',
);
});
@ -68,7 +68,7 @@ describe('data utils', () => {
it("throws if types don't match", () => {
const listing1 = { id: new UUID('listing1'), type: 'listing' };
const author1 = { id: new UUID('author1'), type: 'author' };
expect(() => mergeResourceObjects(listing1, author1)).toThrow(
expect(() => combinedResourceObjects(listing1, author1)).toThrow(
'Cannot merge resource objects with different ids or types',
);
});
@ -76,7 +76,7 @@ describe('data utils', () => {
it("doesn't add attributes or relationships keys unnecessarily", () => {
const res1 = { id: new UUID('listing1'), type: 'listing' };
const res2 = { id: new UUID('listing1'), type: 'listing' };
const merged = mergeResourceObjects(res1, res2);
const merged = combinedResourceObjects(res1, res2);
expect(merged).toEqual(res1);
const keys = Object.keys(merged).sort();
expect(keys).toEqual(['id', 'type']);
@ -93,7 +93,7 @@ describe('data utils', () => {
type: 'listing',
attributes: { title: 'Changed title', description: 'Some description' },
};
expect(mergeResourceObjects(res1, res2)).toEqual(res2);
expect(combinedResourceObjects(res1, res2)).toEqual(res2);
});
it("keeps old attributes if new does't have any", () => {
@ -102,7 +102,7 @@ describe('data utils', () => {
const res1 = { id: new UUID('listing1'), type: 'listing', attributes: res1Attributes };
const res2 = { id: new UUID('listing1'), type: 'listing', relationships: res2Relationships };
expect(mergeResourceObjects(res1, res2)).toEqual({
expect(combinedResourceObjects(res1, res2)).toEqual({
id: new UUID('listing1'),
type: 'listing',
attributes: res1Attributes,
@ -111,11 +111,11 @@ describe('data utils', () => {
});
});
describe('mergeEntities()', () => {
describe('updatedEntities()', () => {
it('adds a single entity', () => {
const listing1 = { id: new UUID('listing1'), type: 'listing' };
const response = { data: listing1 };
const entities = mergeEntities({}, response);
const entities = updatedEntities({}, response);
expect(entities).toEqual({ listing: { listing1 } });
});
@ -123,7 +123,7 @@ describe('data utils', () => {
const listing1 = { id: new UUID('listing1'), type: 'listing' };
const listing2 = { id: new UUID('listing2'), type: 'listing' };
const response = { data: [listing1, listing2] };
const entities = mergeEntities({}, response);
const entities = updatedEntities({}, response);
expect(entities).toEqual({ listing: { listing1, listing2 } });
});
@ -139,7 +139,7 @@ describe('data utils', () => {
attributes: { title: 'Listing 2 title', description: 'Listing 2 description' },
};
const initialResponse = { data: [listing1, listing2] };
const initialEntities = mergeEntities({}, initialResponse);
const initialEntities = updatedEntities({}, initialResponse);
expect(initialEntities).toEqual({ listing: { listing1, listing2 } });
const author1 = { id: new UUID('author1'), type: 'author' };
const author1WithAttributes = {
@ -155,7 +155,7 @@ describe('data utils', () => {
};
const included = [author1WithAttributes];
const response = { data: [listing2Updated], included };
const entities = mergeEntities(initialEntities, response, true);
const entities = updatedEntities(initialEntities, response, true);
expect(entities).toEqual({
listing: { listing1, listing2: listing2Updated },
author: { author1: author1WithAttributes },