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

Commit

Permalink
Remove entity key inferring function option.
Browse files Browse the repository at this point in the history
  • Loading branch information
paularmstrong committed Dec 29, 2016
1 parent b4ad95f commit 2522bb9
Show file tree
Hide file tree
Showing 7 changed files with 8 additions and 106 deletions.
6 changes: 1 addition & 5 deletions docs/api.md
Expand Up @@ -71,11 +71,7 @@ const normalizedData = normalize(data, myArray);

### `Entity(key, definition = {}, options = {})`

* `key`: **required** The key name under which all entities of this type will be listed in the normalized response. Can be a string or a function that returns a string.
As a function, accepts the following arguments, in order:
* `value`: The input value of the entity.
* `parent`: The parent object of the input array.
* `key`: The key at which the input array appears on the parent object.
* `key`: **required** The key name under which all entities of this type will be listed in the normalized response. Must be a string name.
* `definition`: A definition of the nested entities found within this entity. Defaults to empty object.
You *do not* need to define any keys in your entity other than those that hold nested entities. All other values will be copied to the normalized entity's output.
* `options`:
Expand Down
42 changes: 0 additions & 42 deletions src/__tests__/__snapshots__/index.test.js.snap
Expand Up @@ -171,45 +171,3 @@ Array [
],
]
`;

exports[`normalize uses the non-normalized input when getting the key for an entity 1`] = `
Object {
"entities": Object {
"user_recommendation": Object {
"123": Object {
"id": "123",
"user": "456",
},
},
"users": Object {
"456": Object {
"id": "456",
"type": "user_recommendation",
},
},
},
"result": "123",
}
`;

exports[`normalize uses the non-normalized input when getting the key for an entity 2`] = `
Array [
Array [
Object {
"id": "123",
"user": Object {
"id": "456",
"type": "user_recommendation",
},
},
Object {
"id": "123",
"user": Object {
"id": "456",
"type": "user_recommendation",
},
},
null,
],
]
`;
12 changes: 1 addition & 11 deletions src/__tests__/index.test.js
Expand Up @@ -90,7 +90,7 @@ describe('normalize', () => {
addEntity(this, entity, parent, key);
return {
uuid: this.getId(entity),
schema: this.getKey(input, parent, key)
schema: this.key
};
}
}
Expand All @@ -105,16 +105,6 @@ describe('normalize', () => {
}, mySchema)).toMatchSnapshot();
});

it('uses the non-normalized input when getting the key for an entity', () => {
const userEntity = new schema.Entity('users');
const inferKeyFn = jest.fn((nonNormalized, parent, key) => nonNormalized.user.type);
const recommendation = new schema.Entity(inferKeyFn, { user: userEntity });
expect(normalize({
id: '123', user: { id: '456', type: 'user_recommendation' }
}, recommendation)).toMatchSnapshot();
expect(inferKeyFn.mock.calls).toMatchSnapshot();
});

it('uses the non-normalized input when getting the ID for an entity', () => {
const userEntity = new schema.Entity('users');
const idAttributeFn = jest.fn((nonNormalized, parent, key) => nonNormalized.user.id);
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Expand Up @@ -21,7 +21,7 @@ const visit = (value, parent, key, schema, addEntity) => {
};

const addEntities = (entities) => (schema, processedEntity, value, parent, key) => {
const schemaKey = schema.getKey(value, parent, key);
const schemaKey = schema.key;
const id = schema.getId(value, parent, key);
if (!(schemaKey in entities)) {
entities[schemaKey] = {};
Expand Down
11 changes: 5 additions & 6 deletions src/schemas/Entity.js
@@ -1,8 +1,7 @@
export default class EntitySchema {
constructor(key, definition = {}, options = {}) {
const keyIsFunction = typeof key === 'function';
if (!key || typeof key !== 'string' && !keyIsFunction) {
throw new Error('A string or function is required to return the entity key.');
if (!key || typeof key !== 'string') {
throw new Error(`Expected a string key for Entity, but found ${key}.`);
}

const {
Expand All @@ -13,15 +12,15 @@ export default class EntitySchema {
processStrategy = (input) => ({ ...input })
} = options;

this._getKey = keyIsFunction ? key : () => key;
this._key = key;
this._getId = typeof idAttribute === 'function' ? idAttribute : (input) => input[idAttribute];
this._mergeStrategy = mergeStrategy;
this._processStrategy = processStrategy;
this.define(definition);
}

getKey(input, parent, key) {
return this._getKey(input, parent, key);
get key() {
return this._key;
}

define(definition) {
Expand Down
7 changes: 0 additions & 7 deletions src/schemas/__tests__/Entity.test.js
Expand Up @@ -15,13 +15,6 @@ describe(schema.Entity.name, () => {
it('key name must be a string', () => {
expect(() => new schema.Entity(42)).toThrow();
});

it('can use a function to infer the key', () => {
const inferKey = jest.fn(() => 'tacos');
const entity = new schema.Entity(inferKey);
expect(normalize({ foo: { id: '4', name: 'bar' } }, { foo: entity })).toMatchSnapshot();
expect(inferKey.mock.calls).toMatchSnapshot();
});
});

describe('idAttribute', () => {
Expand Down
34 changes: 0 additions & 34 deletions src/schemas/__tests__/__snapshots__/Entity.test.js.snap
Expand Up @@ -54,40 +54,6 @@ Object {
}
`;

exports[`EntitySchema key can use a function to infer the key 1`] = `
Object {
"entities": Object {
"tacos": Object {
"4": Object {
"id": "4",
"name": "bar",
},
},
},
"result": Object {
"foo": "4",
},
}
`;

exports[`EntitySchema key can use a function to infer the key 2`] = `
Array [
Array [
Object {
"id": "4",
"name": "bar",
},
Object {
"foo": Object {
"id": "4",
"name": "bar",
},
},
"foo",
],
]
`;

exports[`EntitySchema mergeStrategy can use a custom merging strategy 1`] = `
Object {
"entities": Object {
Expand Down

0 comments on commit 2522bb9

Please sign in to comment.