-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
version bump 0.0.6. more tests. handle when host header is unset. fac…
…tor out HyperJsonCollection.
- Loading branch information
Showing
5 changed files
with
170 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"}}}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters