diff --git a/dist/jsonApiNormalize.js b/dist/jsonApiNormalize.js index 3fd9ed4..afc0aa2 100644 --- a/dist/jsonApiNormalize.js +++ b/dist/jsonApiNormalize.js @@ -137,7 +137,7 @@ function getEntityValue(property, entity) { - return ['id', 'type'].indexOf(property) >= 0 ? entity[property] : entity.attributes[property]; + return ['id', 'type', 'links'].indexOf(property) >= 0 ? entity[property] : entity.attributes[property]; } @@ -187,7 +187,7 @@ if (temp.length === 0) { return undefined; - } else if (temp.length === 1) { + } else if (temp.length === 1 && !isArray(relationData)) { return temp[0]; } else { return temp; diff --git a/dist/jsonApiNormalize.min.js b/dist/jsonApiNormalize.min.js index c3d32e3..6305a9a 100644 --- a/dist/jsonApiNormalize.min.js +++ b/dist/jsonApiNormalize.min.js @@ -1 +1 @@ -!function(a,b){"function"==typeof define&&define.amd?define([],b):"object"==typeof module&&module.exports?module.exports=b():a.jsonApiNormalize=b()}(this,function(){function a(d,e){if(!(this instanceof a)){var f=new a(d);return e?f.get(e):f}if("string"==typeof d&&(d=JSON.parse(d)),!d||!d.data)throw new Error("Invalid json api normalizer input");this.dataset=d,this.isCollection=c(d.data),this.haystack=b(d.included,this.isCollection?d.data:[d.data])}function b(a,b){var c=[],e=a||[];return d(e,function(a){c.push(a.type+"@"+a.id)}),d(b,function(a){c.indexOf(a.type+"@"+a.id)<0&&e.push(a)}),e}function c(a){return a instanceof Array}function d(a,b,d){if(c(a))for(var e=a.length,f=0;f=0?b[a]:b.attributes[a]}function i(a,b,d,f){var g=d.relationships&&d.relationships[a]&&d.relationships[a].data,h=g&&k(g,f);return c(h)?e(h,function(a){return j(b,a,f)}):h?j(b,h,f):void 0}function j(b,c,d){return n.call(new a({data:c,included:d}),b)}function k(a,b){var f=[],g=c(a)?e(a,function(a){return a.type+"@"+a.id}):[a.type+"@"+a.id];return d(b,function(a){g.indexOf(a.type+"@"+a.id)>=0&&f.push(a)}),0===f.length?void 0:1===f.length?f[0]:f}function l(a){for(var b={},c=b,d=a.length,e=0;e1?f(b,l(c)):b[a]=void 0}),b}function n(a){return this.isCollection?e(this.dataset.data,function(b){return g(a,b,this.haystack)},this):g(a,this.dataset.data,this.haystack)}return a.prototype.get=function(a){var b=n.call(this,m(c(a)?a:[a]));return this.isCollection?c(a)?b:e(b,function(b){return b[a]}):c(a)?b:b[a]},a}); \ No newline at end of file +!function(a,b){"function"==typeof define&&define.amd?define([],b):"object"==typeof module&&module.exports?module.exports=b():a.jsonApiNormalize=b()}(this,function(){function a(d,e){if(!(this instanceof a)){var f=new a(d);return e?f.get(e):f}if("string"==typeof d&&(d=JSON.parse(d)),!d||!d.data)throw new Error("Invalid json api normalizer input");this.dataset=d,this.isCollection=c(d.data),this.haystack=b(d.included,this.isCollection?d.data:[d.data])}function b(a,b){var c=[],e=a||[];return d(e,function(a){c.push(a.type+"@"+a.id)}),d(b,function(a){c.indexOf(a.type+"@"+a.id)<0&&e.push(a)}),e}function c(a){return a instanceof Array}function d(a,b,d){if(c(a))for(var e=a.length,f=0;f=0?b[a]:b.attributes[a]}function i(a,b,d,f){var g=d.relationships&&d.relationships[a]&&d.relationships[a].data,h=g&&k(g,f);return c(h)?e(h,function(a){return j(b,a,f)}):h?j(b,h,f):void 0}function j(b,c,d){return n.call(new a({data:c,included:d}),b)}function k(a,b){var f=[],g=c(a)?e(a,function(a){return a.type+"@"+a.id}):[a.type+"@"+a.id];return d(b,function(a){g.indexOf(a.type+"@"+a.id)>=0&&f.push(a)}),0===f.length?void 0:1!==f.length||c(a)?f:f[0]}function l(a){for(var b={},c=b,d=a.length,e=0;e1?f(b,l(c)):b[a]=void 0}),b}function n(a){return this.isCollection?e(this.dataset.data,function(b){return g(a,b,this.haystack)},this):g(a,this.dataset.data,this.haystack)}return a.prototype.get=function(a){var b=n.call(this,m(c(a)?a:[a]));return this.isCollection?c(a)?b:e(b,function(b){return b[a]}):c(a)?b:b[a]},a}); \ No newline at end of file diff --git a/src/jsonApiNormalize.js b/src/jsonApiNormalize.js index a190380..afc0aa2 100644 --- a/src/jsonApiNormalize.js +++ b/src/jsonApiNormalize.js @@ -137,7 +137,7 @@ function getEntityValue(property, entity) { - return ['id', 'type'].indexOf(property) >= 0 ? entity[property] : entity.attributes[property]; + return ['id', 'type', 'links'].indexOf(property) >= 0 ? entity[property] : entity.attributes[property]; } diff --git a/test/index.js b/test/index.js index 15ba1d4..725ed60 100644 --- a/test/index.js +++ b/test/index.js @@ -26,6 +26,9 @@ beforeEach(function() { comments: {data: [ {id: '1', 'type': 'comment'} ]} + }, + links: { + self: 'http://example.com/articles/1' } }], included: [{ @@ -37,6 +40,9 @@ beforeEach(function() { }, relationships: { boss: {data: {'id': '42', 'type': 'user'}}, + }, + links: { + self: 'http://example.com/users/42' } }, { type: 'tag', @@ -202,4 +208,28 @@ describe("Json api normalize", function() { }); + it('retrieves top level and nested links', function() { + + var data = normalize(testCollection).get([ + 'id', + 'links', + 'author.id', + 'author.links', + ]); + + assert.deepEqual(data, [{ + id: '1', + links: { + self: 'http://example.com/articles/1' + }, + author: { + id: '42', + links: { + self: 'http://example.com/users/42' + } + }, + }]); + + }); + }); \ No newline at end of file