summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2012-06-29 23:14:59 +0200
committerTrygve Laugstøl <trygvis@inamo.no>2012-06-29 23:14:59 +0200
commit7a244dd2af1375af9197188aa84f13bc215b4065 (patch)
treeef58ac0aac597b00a6b1adb7139b320ffee07a00
parentb883e87b1e5222388e6562296009a49126d3af3c (diff)
downloadcollection_json.js-7a244dd2af1375af9197188aa84f13bc215b4065.tar.gz
collection_json.js-7a244dd2af1375af9197188aa84f13bc215b4065.tar.bz2
collection_json.js-7a244dd2af1375af9197188aa84f13bc215b4065.tar.xz
collection_json.js-7a244dd2af1375af9197188aa84f13bc215b4065.zip
o Remembering that all objects should start with a single 'collection'.
-rw-r--r--.gitignore1
-rw-r--r--index.js21
-rw-r--r--test/collection_json.test.js2
3 files changed, 14 insertions, 10 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..3c3629e
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+node_modules
diff --git a/index.js b/index.js
index 5705fec..583d5ae 100644
--- a/index.js
+++ b/index.js
@@ -4,15 +4,18 @@ var _ = require('underscore');
* Takes any object and make it look like a collection+json object with utility methods.
*/
function fromObject(root) {
- root = root || {};
- var collection = root.collection || {};
- collection.items = collection.items || {};
- collection.links = collection.links || {};
- collection.queries = collection.queries || {};
- collection.templates = collection.templates || {};
+ root = _.isObject(root) ? root : {};
+ root.collection = _.isObject(root.collection) ? root.collection : {};
+ var c = root.collection;
+ c.version = _.isString(c.version) ? c.version : "1.0";
+ c.items = _.isArray(c.items) ? c.items : [];
+ c.links = _.isArray(c.links) ? c.links : [];
+ c.queries = _.isArray(c.queries) ? c.queries : [];
+ c.templates = _.isArray(c.templates) ? c.templates : [];
- collection.mapItemData = function(f) {
- return _.map(collection.items, function(item) {
+ // TODO: make un-enumerable
+ root.mapItemData = function(f) {
+ return _.map(this.collection.items, function(item) {
// console.log("item", item);
var map = _.reduce(item.data, function(map, field) {
// console.log("field", field);
@@ -24,7 +27,7 @@ function fromObject(root) {
});
};
- return collection;
+ return root;
}
module.exports.fromObject = fromObject;
diff --git a/test/collection_json.test.js b/test/collection_json.test.js
index b74d99b..24f69e4 100644
--- a/test/collection_json.test.js
+++ b/test/collection_json.test.js
@@ -22,7 +22,7 @@ var collection1 = {collection: {
describe('fromObject', function() {
it('Has basic fields', function() {
- var collection = collection_json.fromObject({});
+ var collection = collection_json.fromObject({}).collection;
assert.deepEqual([], collection.items);
assert.deepEqual([], collection.links);
assert.deepEqual([], collection.queries);