From 8e206c30362f9ccf32dc9dc4dfa9cc7e6ed2119a Mon Sep 17 00:00:00 2001 From: Chad Kouse Date: Fri, 28 Mar 2014 01:33:29 -0400 Subject: [PATCH 1/7] allow forcing a link to be an array --- hal.js | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/hal.js b/hal.js index 1a8e404..6bcaf1e 100644 --- a/hal.js +++ b/hal.js @@ -151,15 +151,21 @@ if (arguments.length > 1) { link = Link(arguments[0], arguments[1]); } + var forceArray = false; + if (arguments.length >= 3) + { + forceArray = arguments[2]; + } + - 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); From d7682ed3998aaba55a43d638cfb1b6dfe60d6afc Mon Sep 17 00:00:00 2001 From: Chad Kouse Date: Fri, 28 Mar 2014 02:09:44 -0400 Subject: [PATCH 2/7] allow forcing a link to be an array --- hal.js | 14 +++++++------- test/hal.js | 8 ++++++++ 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/hal.js b/hal.js index 6bcaf1e..2f20456 100644 --- a/hal.js +++ b/hal.js @@ -148,15 +148,15 @@ * @see Link */ Resource.prototype.link = function (link) { + var forceArray = false; if (arguments.length > 1) { - link = Link(arguments[0], arguments[1]); - } - var forceArray = false; - if (arguments.length >= 3) - { - forceArray = arguments[2]; + var href = arguments[1]; + if (Array.isArray(href)) { + forceArray = true; + href = href[0]; + } + link = Link(arguments[0], href); } - this._links[link.rel] = linkGroupPlus(this._links[link.rel], link, forceArray); diff --git a/test/hal.js b/test/hal.js index 3103366..fcfe9a8 100644 --- a/test/hal.js +++ b/test/hal.js @@ -68,6 +68,14 @@ 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']); + }); it('should embed resource', function () { var res = new hal.Resource({}, 'href'); var sub = new hal.Resource({}, 'href2'); From c70809da373cdff70c189f588fbada4c7858b3ba Mon Sep 17 00:00:00 2001 From: Chad Kouse Date: Fri, 28 Mar 2014 02:23:49 -0400 Subject: [PATCH 3/7] allow mulpitle links to be passed in at once. cleanup --- hal.js | 16 +++++++++++----- test/hal.js | 10 ++++++++++ 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/hal.js b/hal.js index 2f20456..0d5fe40 100644 --- a/hal.js +++ b/hal.js @@ -149,12 +149,18 @@ */ Resource.prototype.link = function (link) { var forceArray = false; + var self = this; if (arguments.length > 1) { - var href = arguments[1]; - if (Array.isArray(href)) { - forceArray = true; - href = href[0]; - } + var href = arguments[1]; + var rel = arguments[0]; + if (Array.isArray(href)) { + forceArray = true; + href.forEach(function(h) { + link = Link(rel, h); + self._links[link.rel] = linkGroupPlus(self._links[link.rel], link, forceArray); + }); + return this; + } link = Link(arguments[0], href); } diff --git a/test/hal.js b/test/hal.js index fcfe9a8..e253f2b 100644 --- a/test/hal.js +++ b/test/hal.js @@ -75,6 +75,16 @@ describe('HAL', function () { 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'); From c2745fa4f63b0f56b058cd48b079ad8505ca5ff6 Mon Sep 17 00:00:00 2001 From: Chad Kouse Date: Thu, 3 Apr 2014 00:31:54 -0400 Subject: [PATCH 4/7] remove cruft --- hal.js | 13 +++++++------ test/hal.js | 1 + 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/hal.js b/hal.js index 0d5fe40..c453fd7 100644 --- a/hal.js +++ b/hal.js @@ -155,23 +155,24 @@ var rel = arguments[0]; if (Array.isArray(href)) { forceArray = true; - href.forEach(function(h) { - link = Link(rel, h); - self._links[link.rel] = linkGroupPlus(self._links[link.rel], link, forceArray); + var links = href.map(function(h) { + return Link(rel, h); }); + + self._links[rel] = linkGroupPlus(self._links[link.rel], links); return this; } link = Link(arguments[0], href); } - this._links[link.rel] = linkGroupPlus(this._links[link.rel], link, forceArray); + this._links[link.rel] = linkGroupPlus(this._links[link.rel], link); return this; }; - function linkGroupPlus(group, newLink, forceArray) { + function linkGroupPlus(group, newLink) { if (!group) { - return forceArray ? [newLink] : newLink; + return newLink; } if (Array.isArray(group)) { return group.concat(newLink); diff --git a/test/hal.js b/test/hal.js index e253f2b..d36b3e3 100644 --- a/test/hal.js +++ b/test/hal.js @@ -71,6 +71,7 @@ describe('HAL', function () { 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'); From 6267fa9f3ced0c457fbfb6a2e7625eb262af6d95 Mon Sep 17 00:00:00 2001 From: Chad Kouse Date: Thu, 3 Apr 2014 00:33:18 -0400 Subject: [PATCH 5/7] remove unused line --- hal.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/hal.js b/hal.js index c453fd7..0eb4d55 100644 --- a/hal.js +++ b/hal.js @@ -148,13 +148,11 @@ * @see Link */ Resource.prototype.link = function (link) { - var forceArray = false; var self = this; if (arguments.length > 1) { + var rel = arguments[0]; var href = arguments[1]; - var rel = arguments[0]; if (Array.isArray(href)) { - forceArray = true; var links = href.map(function(h) { return Link(rel, h); }); From cbe88b88e4694a47ef458242af0377cc6d30cbdb Mon Sep 17 00:00:00 2001 From: Chad Kouse Date: Thu, 3 Apr 2014 00:33:47 -0400 Subject: [PATCH 6/7] formatting --- hal.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hal.js b/hal.js index 0eb4d55..56d4b6a 100644 --- a/hal.js +++ b/hal.js @@ -150,7 +150,7 @@ Resource.prototype.link = function (link) { var self = this; if (arguments.length > 1) { - var rel = arguments[0]; + var rel = arguments[0]; var href = arguments[1]; if (Array.isArray(href)) { var links = href.map(function(h) { From 12194f517a7114036b054a327cda64bf2f6fb946 Mon Sep 17 00:00:00 2001 From: Chad Kouse Date: Thu, 3 Apr 2014 00:47:50 -0400 Subject: [PATCH 7/7] tightening it all up --- hal.js | 31 +++++++++++++------------------ 1 file changed, 13 insertions(+), 18 deletions(-) diff --git a/hal.js b/hal.js index 56d4b6a..00de6aa 100644 --- a/hal.js +++ b/hal.js @@ -148,24 +148,19 @@ * @see Link */ Resource.prototype.link = function (link) { - var self = this; - if (arguments.length > 1) { - var rel = arguments[0]; - var href = arguments[1]; - if (Array.isArray(href)) { - var links = href.map(function(h) { - return Link(rel, h); - }); - - self._links[rel] = linkGroupPlus(self._links[link.rel], links); - return this; - } - link = Link(arguments[0], href); - } - - 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) {