Skip to content

Commit

Permalink
Merge pull request #16 from stephenprater/include-embedded-links-in-n…
Browse files Browse the repository at this point in the history
…ormalization

Include self links in normalization
  • Loading branch information
yury-dymov authored Jun 12, 2017
2 parents 81d7172 + cc8a223 commit 6513698
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 10 deletions.
8 changes: 8 additions & 0 deletions src/normalize.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@ function extractEntities(json, { camelizeKeys }) {
ret[type][elem.id].attributes = elem.attributes;
}

if (elem.links) {
ret[type][elem.id].links = {};

keys(elem.links).forEach((key) => {
ret[type][elem.id].links[key] = elem.links[key];
})
}

if (elem.relationships) {
ret[type][elem.id].relationships =
extractRelationships(elem.relationships, { camelizeKeys });
Expand Down
32 changes: 22 additions & 10 deletions test/normalize.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ describe('data is normalized', () => {
attributes: {
text: 'hello',
number: 3,
},
links: {
self: 'http://www.example.com/post/3'
}
},
{
Expand All @@ -19,6 +22,9 @@ describe('data is normalized', () => {
attributes: {
text: 'hello world',
number: 4,
},
links: {
self: 'http://www.example.com/post/4'
}
}
]
Expand All @@ -31,13 +37,19 @@ describe('data is normalized', () => {
attributes: {
text: 'hello',
number: 3
},
links: {
self: 'http://www.example.com/post/3'
}
},
"4": {
id: 4,
attributes: {
text: 'hello world',
number: 4
},
links: {
self: 'http://www.example.com/post/4'
}
}
}
Expand Down Expand Up @@ -174,7 +186,7 @@ describe('relationships', () => {
}
}]
};

const output = {
post: {
"2620": {
Expand Down Expand Up @@ -211,7 +223,7 @@ describe('relationships', () => {
}
}]
};

const output = {
post: {
"2620": {
Expand Down Expand Up @@ -251,7 +263,7 @@ describe('relationships', () => {
}
}]
};

const output = {
post: {
"2620": {
Expand Down Expand Up @@ -294,7 +306,7 @@ describe('relationships', () => {
}
}]
};

const output = {
post: {
"2620": {
Expand Down Expand Up @@ -349,7 +361,7 @@ describe('relationships', () => {
}
}]
};

const output = {
post: {
"2620": {
Expand Down Expand Up @@ -407,7 +419,7 @@ describe('relationships', () => {
}
}]
};

const output = {
post: {
"2620": {
Expand Down Expand Up @@ -682,7 +694,7 @@ describe('meta', () => {
}
};

expect(isEqual(normalize(json3, { endpoint: 'posts/me' }), output3)).to.be.true;
expect(isEqual(normalize(json3, { endpoint: 'posts/me' }), output3)).to.be.true;
});

it('empty collection', () => {
Expand Down Expand Up @@ -1068,7 +1080,7 @@ describe('lazy loading', () => {
"self": "http://localhost:3000/api/v1/actor/1c9d234b-66c4-411e-b785-955d57db5536/relationships/movie",
"related": "http://localhost:3000/api/v1/actor/1c9d234b-66c4-411e-b785-955d57db5536/movie"
}
},
},
},
type: "question"
}]
Expand All @@ -1087,7 +1099,7 @@ describe('lazy loading', () => {
movie: {
links: {
"self": "http://localhost:3000/api/v1/actor/1c9d234b-66c4-411e-b785-955d57db5536/relationships/movie",
"related": "http://localhost:3000/api/v1/actor/1c9d234b-66c4-411e-b785-955d57db5536/movie"
"related": "http://localhost:3000/api/v1/actor/1c9d234b-66c4-411e-b785-955d57db5536/movie"
}
}
}
Expand All @@ -1097,6 +1109,6 @@ describe('lazy loading', () => {

it('basic test', () => {
const result = normalize(json);
expect(isEqual(result, output)).to.be.true;
expect(isEqual(result, output)).to.be.true;
});
});

0 comments on commit 6513698

Please sign in to comment.