Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions hal.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,18 +148,30 @@
* @see Link
*/
Resource.prototype.link = function (link) {
var forceArray = false;
var self = this;
if (arguments.length > 1) {
link = Link(arguments[0], arguments[1]);
var href = arguments[1];
var rel = arguments[0];
if (Array.isArray(href)) {
forceArray = true;
href.forEach(function(h) {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally, I prefer using map and reduce over a plain forEach. I think this might look really nice if you do it.

Maybe use one map to build a list of Links, and make a single call to linkGroupPlus.

link = Link(rel, h);
self._links[link.rel] = linkGroupPlus(self._links[link.rel], link, forceArray);

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe you could do linkGroupPlus(self._links[link.rel], [link]) and get rid of the forceArray. This way you wouldn't even have to change implementation of linkGroupPlus.

});
return this;
}
link = Link(arguments[0], href);
}

this._links[link.rel] = linkGroupPlus(this._links[link.rel], link);
this._links[link.rel] = linkGroupPlus(this._links[link.rel], link, forceArray);

return this;
};

function linkGroupPlus(group, newLink) {
function linkGroupPlus(group, newLink, forceArray) {
if (!group) {
return newLink;
return forceArray ? [newLink] : newLink;
}
if (Array.isArray(group)) {
return group.concat(newLink);
Expand Down
18 changes: 18 additions & 0 deletions test/hal.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,24 @@ describe('HAL', function () {
expect(_.pluck(res._links.admin, 'href')).to.deep.equal(['/user/john', '/user/jane']);
expect(_.pluck(res._links.admin, 'rel')).to.deep.equal(['admin', 'admin']);
});
it('should force a single link to be an array', function() {
var res = new hal.Resource({});
res.link('admin', ['/user/john']);

expect(res._links).to.have.property('admin');
expect(res._links.admin).to.be.an('Array');
expect(_.pluck(res._links.admin, 'href')).to.deep.equal(['/user/john']);
expect(_.pluck(res._links.admin, 'rel')).to.deep.equal(['admin']);
});
it('should add two links with the same rel in one line', function() {
var res = new hal.Resource({});
res.link('admin', ['/user/john','/user/jane']);

expect(res._links).to.have.property('admin');
expect(res._links.admin).to.be.an('Array');
expect(_.pluck(res._links.admin, 'href')).to.deep.equal(['/user/john', '/user/jane']);
expect(_.pluck(res._links.admin, 'rel')).to.deep.equal(['admin', 'admin']);
});
it('should embed resource', function () {
var res = new hal.Resource({}, 'href');
var sub = new hal.Resource({}, 'href2');
Expand Down