Skip to content

Commit 07c5836

Browse files
authored
Merge pull request yury-dymov#12 from lvauvillier/relationships
Relationships normalization
2 parents 4e2cb69 + 79b0b95 commit 07c5836

File tree

2 files changed

+409
-197
lines changed

2 files changed

+409
-197
lines changed

src/normalize.js

Lines changed: 37 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import camelCase from 'lodash/camelCase';
22
import isArray from 'lodash/isArray';
33
import isNull from 'lodash/isNull';
4-
import join from 'lodash/join';
54
import keys from 'lodash/keys';
65
import merge from 'lodash/merge';
76

@@ -13,7 +12,37 @@ function wrap(json) {
1312
return [json];
1413
}
1514

16-
function extract(json, { camelizeKeys }) {
15+
function extractRelationships(relationships, { camelizeKeys }) {
16+
const ret = {};
17+
keys(relationships).forEach((key) => {
18+
const relationship = relationships[key];
19+
const name = camelizeKeys ? camelCase(key) : key;
20+
ret[name] = {};
21+
22+
if (typeof relationship.data !== 'undefined') {
23+
if (isArray(relationship.data)) {
24+
ret[name].data = relationship.data.map(e => ({
25+
id: e.id,
26+
type: camelizeKeys ? camelCase(e.type) : e.type,
27+
}));
28+
} else if (!isNull(relationship.data)) {
29+
ret[name].data = {
30+
id: relationship.data.id,
31+
type: camelizeKeys ? camelCase(relationship.data.type) : relationship.data.type,
32+
};
33+
} else {
34+
ret[name].data = relationship.data;
35+
}
36+
}
37+
38+
if (relationship.links) {
39+
ret[name].links = relationship.links;
40+
}
41+
});
42+
return ret;
43+
}
44+
45+
function extractEntities(json, { camelizeKeys }) {
1746
const ret = {};
1847

1948
wrap(json).forEach((elem) => {
@@ -35,33 +64,8 @@ function extract(json, { camelizeKeys }) {
3564
}
3665

3766
if (elem.relationships) {
38-
wrap(elem.relationships).forEach((relationship) => {
39-
const mp = {};
40-
41-
wrap(relationship).forEach((object) => {
42-
keys(object).forEach((key) => {
43-
mp[camelizeKeys ? camelCase(key) : key] = {};
44-
45-
if (typeof object[key].data !== 'undefined' && !isNull(object[key].data)) {
46-
if (wrap(object[key].data).length > 0) {
47-
const ids = wrap(object[key].data).map(el => el.id);
48-
const relType = wrap(object[key].data)[0].type;
49-
50-
mp[camelizeKeys ? camelCase(key) : key] = {
51-
id: ids.length === 1 ? ids[0].toString() : join(ids, ','),
52-
type: camelizeKeys ? camelCase(relType) : relType,
53-
};
54-
}
55-
}
56-
57-
if (object[key].links) {
58-
mp[camelizeKeys ? camelCase(key) : key].links = object[key].links;
59-
}
60-
});
61-
});
62-
63-
ret[type][elem.id].relationships = mp;
64-
});
67+
ret[type][elem.id].relationships =
68+
extractRelationships(elem.relationships, { camelizeKeys });
6569
}
6670
});
6771

@@ -99,21 +103,8 @@ function extractMetaData(json, endpoint, { camelizeKeys, filterEndpoint }) {
99103
const pObject = { id: object.id, type: camelizeKeys ? camelCase(object.type) : object.type };
100104

101105
if (object.relationships) {
102-
keys(object.relationships).forEach((key) => {
103-
pObject.relationships = pObject.relationships || {};
104-
105-
if (typeof object.relationships[key].data !== 'undefined' && !isNull(object.relationships[key].data)) {
106-
if (wrap(object.relationships[key].data).length > 0) {
107-
const ids = wrap(object.relationships[key].data).map(elem => elem.id);
108-
const type = wrap(object.relationships[key].data)[0].type;
109-
110-
pObject.relationships[camelizeKeys ? camelCase(key) : key] = {
111-
type: camelizeKeys ? camelCase(type) : type,
112-
id: join(ids, ','),
113-
};
114-
}
115-
}
116-
});
106+
pObject.relationships =
107+
extractRelationships(object.relationships, { camelizeKeys });
117108
}
118109

119110
meta.push(pObject);
@@ -148,11 +139,11 @@ export default function normalize(json, opts = {}) {
148139
}
149140

150141
if (json.data) {
151-
merge(ret, extract(json.data, { camelizeKeys }));
142+
merge(ret, extractEntities(json.data, { camelizeKeys }));
152143
}
153144

154145
if (json.included) {
155-
merge(ret, extract(json.included, { camelizeKeys }));
146+
merge(ret, extractEntities(json.included, { camelizeKeys }));
156147
}
157148

158149
if (endpoint) {

0 commit comments

Comments
 (0)