Skip to content

Commit

Permalink
version bump 0.0.6. more tests. handle when host header is unset. fac…
Browse files Browse the repository at this point in the history
…tor out HyperJsonCollection.
  • Loading branch information
cainus committed Dec 8, 2013
1 parent c962500 commit a52fd8a
Show file tree
Hide file tree
Showing 5 changed files with 170 additions and 59 deletions.
61 changes: 61 additions & 0 deletions HyperJsonCollection.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
var HyperJson = require("hyperjson");
var _ = require("underscore");

var HyperJsonCollection = function(obj, key) {
var index, newObj;
obj = _.clone(obj);
if (Array.isArray(obj)) {
if (!!key) {
newObj = {};
index = 0;
_.each(obj, function(item) {
newObj[item[key]] = item;
});
obj = newObj;
}
}
this.obj = {
_items: obj
};
};

HyperJsonCollection.prototype = Object.create(HyperJson.prototype);

HyperJsonCollection.prototype.each = function(cb) {
var i, items, len, x;
items = this.obj._items;
if (Array.isArray(items)) {
len = (!!items ? items.length : 0);
i = 0;
while (i < len) {
items[i] = cb(items[i]);
i++;
}
} else {
for (x in items) {
items[x] = cb(_.clone(items[x]), x);
}
}
return this;
};

HyperJsonCollection.prototype.linkEach = function(rel, cb) {
var i, items, len, x;
items = this.obj._items;
if (Array.isArray(items)) {
len = (!!items ? items.length : 0);
i = 0;
while (i < len) {
items[i] = new HyperJson(_.clone(items[i])).link(rel, cb(items[i], i)).toObject();
i++;
}
} else {
for (x in items) {
items[x] = new HyperJson(_.clone(items[x])).link(rel, cb(items[x], x)).toObject();
}
}
return this;
};


module.exports = HyperJsonCollection;
65 changes: 7 additions & 58 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,62 +1,7 @@
var HyperJson = require("hyperjson");
var urlgrey = require("urlgrey");
var _ = require("underscore");

var HyperJsonCollection = function(obj, key) {
var index, newObj;
obj = _.clone(obj);
if (Array.isArray(obj)) {
if (!!key) {
newObj = {};
index = 0;
_.each(obj, function(item) {
newObj[item[key]] = item;
});
obj = newObj;
}
}
this.obj = {
_items: obj
};
};

HyperJsonCollection.prototype = Object.create(HyperJson.prototype);

HyperJsonCollection.prototype.each = function(cb) {
var i, items, len, x;
items = this.obj._items;
if (Array.isArray(items)) {
len = (!!items ? items.length : 0);
i = 0;
while (i < len) {
items[i] = cb(items[i]);
i++;
}
} else {
for (x in items) {
items[x] = cb(_.clone(items[x]), x);
}
}
return this;
};

HyperJsonCollection.prototype.linkEach = function(rel, cb) {
var i, items, len, x;
items = this.obj._items;
if (Array.isArray(items)) {
len = (!!items ? items.length : 0);
i = 0;
while (i < len) {
items[i] = new HyperJson(_.clone(items[i])).link(rel, cb(items[i], i)).toObject();
i++;
}
} else {
for (x in items) {
items[x] = new HyperJson(_.clone(items[x])).link(rel, cb(items[x], x)).toObject();
}
}
return this;
};
var HyperJsonCollection = require('./HyperJsonCollection');


var send = function(req, res, jsonObj, options) {
Expand All @@ -81,11 +26,15 @@ var addDefaultLinks = function(req, res, json, options) {
}
if (!current._links || !current._links.parent) {
try {
var host = 'localhost';
if (!!req.headers.host){
host = req.headers.host;
}
var base = options.protocol +
'://' +
(req.host || 'localhost');
(req.headers.host || 'localhost');
parent = urlgrey(base)
.hostname(req.headers.host)
.hostname(req.headers.host || 'localhost')
.path(req.url)
.parent()
.toString();
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "hyperjson-connect",
"version": "0.0.5",
"version": "0.0.6",
"description": "connect middleware for hyperjson support",
"main": "index.js",
"scripts": {
Expand Down
82 changes: 82 additions & 0 deletions test/HyperJsonCollection.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
var should = require('should');
var HyperJsonCollection = require('../HyperJsonCollection');

describe("HyperJsonCollection", function(){
describe("#toObject", function(){
it ("should return a json object when given one", function(){
new HyperJsonCollection({thisis : "a test"})
.toObject().should.eql({_items:{thisis:"a test"}});
});
it ("should return a json collection when given an array", function(){
new HyperJsonCollection([{thisis : "a test"}, {thisis : "too"}])
.toObject().should.eql({ _items : [{thisis:"a test"}, {thisis : "too"}]});
});
it ("should return a json collection indexed by a particular property when given an array and property name", function(){
new HyperJsonCollection([{thisis : "a test"}, {thisis : "too"}], 'thisis')
.toObject().should.eql({ _items : {"a test" : {thisis:"a test"}, "too" : {thisis : "too"}}});
});
});
describe("#linkEach", function(){
it ("should allow a cb to be specified for an array collection", function(){
var out = new HyperJsonCollection({thisis : {value : "atest"},
thisisalso : {value : "too"}})
.linkEach('somerel', function(item, key){
return '/asdf/' + key;
})
.toObject();
out._items.thisis._links.should.eql({ somerel : {
href : '/asdf/thisis'
}
});
out._items.thisisalso._links.should.eql({ somerel : {
href : '/asdf/thisisalso'
}
});
});
it ("should allow a cb to be specified for an array collection", function(){
var out = new HyperJsonCollection([{thisis : "atest"}, {thisis : "too"}])
.linkEach('somerel', function(item, key){
return '/asdf/' + item.thisis;
})
.toObject();
out._items[0]._links.should.eql({ somerel : {
href : '/asdf/atest'
}
});
out._items[1]._links.should.eql({ somerel : {
href : '/asdf/too'
}
});
});
});
describe("#each", function(){
it ("should do nothing if there are no _items", function(){
new HyperJsonCollection({thisis : "a test"})
.each(function(item){
item.decorated = true;
return item;
})
.toObject().should.eql({_items:{thisis : "a test"}});
});
it ("should decorate each item in _items if its an array", function(){
new HyperJsonCollection([{thisis : "a test"}, {thisis : "too"}])
.each(function(item){
item.decorated = true;
return item;
})
.toObject().should.eql({ _items : [{thisis:"a test",
decorated : true},
{thisis : "too",
decorated : true}]});
});
it ("should decorate each item in _items if its an object", function(){
new HyperJsonCollection({thisis : {value : "a test"}, andsois : {value : "this"}})
.each(function(item, name){
item.decorated = name;
return item;
})
.toObject().should.eql({ _items : {thisis: {value : "a test", decorated : "thisis"},
andsois : {value : "this", decorated : "andsois"}}});
});
});
});
19 changes: 19 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,24 @@ describe("the middleware", function(){
done();
});
});
it("uses localhost for links if there is no host header", function(done){
app.get("/api/resource", function(req, res){
res.object({"test" : true}).send();
});
request({ uri:baseUrl + '/api/resource',
method:'get'
}, function(err,response,body){
if (err){should.fail(err);}
response.statusCode.should.equal(200);
var expected = {
test: true,
_links: {
parent: {
href: 'http://localhost:1337/api' } } };
JSON.parse(body).should.eql(expected);
done();
});
});
});
describe("with connect", function(){
beforeEach(function(done){
Expand All @@ -171,3 +189,4 @@ describe("the middleware", function(){
});
});
});

0 comments on commit a52fd8a

Please sign in to comment.