diff --git a/hal.js b/hal.js index 1a8e404..00de6aa 100644 --- a/hal.js +++ b/hal.js @@ -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; + + this._links[rel] = linkGroupPlus(this._links[rel], links); + + return this; }; function linkGroupPlus(group, newLink) { diff --git a/test/hal.js b/test/hal.js index 3103366..d36b3e3 100644 --- a/test/hal.js +++ b/test/hal.js @@ -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');