Skip to content
Open
Show file tree
Hide file tree
Changes from all 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: 13 additions & 7 deletions hal.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,13 +148,19 @@
* @see Link
*/
Resource.prototype.link = function (link) {
if (arguments.length > 1) {
link = Link(arguments[0], arguments[1]);
}

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

return this;
var self = this;
var rel = arguments.length > 1 ? arguments[0] : link.rel;
var links = arguments.length > 1 ?
Array.isArray(arguments[1]) ?
arguments[1].map(function (h) {
return Link(rel, h)
}) :
Link(rel, arguments[1])
: link;
Copy link
Owner

Choose a reason for hiding this comment

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

These two ternaries in the same expression are hard to follow.

Copy link
Owner

Choose a reason for hiding this comment

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

maybe something like var links = (arguments.length === 1) ? link : extractLinks.apply(this, arguments);

and extractLinks would always receive two arguments. A relation and an href (or array of hrefs).


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

return this;
};

function linkGroupPlus(group, newLink) {
Expand Down
19 changes: 19 additions & 0 deletions test/hal.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,25 @@ 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']);
console.log(res._links);

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