summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2012-07-14 00:41:25 +0200
committerTrygve Laugstøl <trygvis@inamo.no>2012-07-14 00:41:25 +0200
commit3f310615b388d428c68bbf3b773505c868e90db6 (patch)
tree4d41a94836d5aaf5c4c93b293092a1501a28ab36
parent63214f5e9e69c640701e6d92dc4a9bee9ec91cba (diff)
downloadcollection_json.js-3f310615b388d428c68bbf3b773505c868e90db6.tar.gz
collection_json.js-3f310615b388d428c68bbf3b773505c868e90db6.tar.bz2
collection_json.js-3f310615b388d428c68bbf3b773505c868e90db6.tar.xz
collection_json.js-3f310615b388d428c68bbf3b773505c868e90db6.zip
o More tests.
-rw-r--r--index.js76
-rw-r--r--test/collection_json.test.js36
2 files changed, 85 insertions, 27 deletions
diff --git a/index.js b/index.js
index 8e2709a..07078cc 100644
--- a/index.js
+++ b/index.js
@@ -1,33 +1,62 @@
var _ = require('underscore');
+function toObject() {
+ return _.reduce(this.data, function(map, field) {
+ map[field.name] = field.value;
+ return map;
+ }, {});
+};
+
+function fixTemplate(t) {
+ t.toObject = toObject;
+ t.data = _.isArray(t.data) ? t.data : [];
+}
+
/**
- * Takes any object and make it look like a collection+json object with utility methods.
+ * Takes any object and make it look like a collection+json collection object.
*/
function fromObject(root) {
- root = _.isObject(root) ? root : {};
- root.collection = _.isObject(root.collection) ? root.collection : {};
- var c = root.collection;
- c.version = _.isString(c.version) ? c.version : "1.0";
+// console.log('in ', root);
+
+ root.isCollection = function() {
+ return typeof this.collection == 'object';
+ }
+ root.isTemplate = function() {
+ return typeof this.template == 'object';
+ }
- c.items = _.isArray(c.items) ? c.items : [];
- _.each(c.items, function(item) {
- item.links = _.isArray(item.links) ? item.links : [];
- item.toObject = function() {
- return _.reduce(item.data, function(map, field) {
- map[field.name] = field.value;
- return map;
- }, {});
+ if(root.isTemplate()) {
+ fixTemplate(root.template);
+ }
+ else {
+ root = _.isObject(root) ? root : {collection: {}};
+ if(!_.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 : [];
+ _.each(c.items, function(item) {
+ item.links = _.isArray(item.links) ? item.links : [];
+ item.toObject = toObject;
+ });
+
+ c.links = _.isArray(c.links) ? c.links : [];
- c.links = _.isArray(c.links) ? c.links : [];
+ c.queries = _.isArray(c.queries) ? c.queries : [];
+ _.each(c.queries, function(query) {
+ query.data = _.isArray(query.data) ? query.data : [];
+ });
- c.queries = _.isArray(c.queries) ? c.queries : [];
- _.each(c.queries, function(query) {
- query.data = _.isArray(query.data) ? query.data : [];
- });
+ if(_.isObject(c.template)) {
+ fixTemplate(c.template);
+ }
+ else {
+ delete c.template;
+ }
+ }
- c.template = _.isObject(c.template) ? c.template : undefined;
// c.templates = _.isArray(c.templates) ? c.templates : [];
// TODO: make un-enumerable
@@ -41,13 +70,8 @@ function fromObject(root) {
return rel === link.rel;
});
}
- root.isCollection = function() {
- return typeof this.collection == 'object';
- }
- root.isTemplate = function() {
- return typeof this.template == 'object';
- }
+// console.log('out', root);
return root;
}
module.exports.fromObject = fromObject;
diff --git a/test/collection_json.test.js b/test/collection_json.test.js
index 0cd003e..e1c4297 100644
--- a/test/collection_json.test.js
+++ b/test/collection_json.test.js
@@ -22,8 +22,17 @@ function collection1() {
}});
}
+function template1() {
+ return collection_json.fromObject({template: {
+ data: [
+ { name: 'field-1', value: 'value-1', zap: 'zoot' },
+ { name: 'field-2' }
+ ]
+ }});
+}
+
describe('fromObject', function() {
- it('Has basic fields', function() {
+ it('Has basic fields on a collection', function() {
var collection = collection_json.fromObject({}).collection;
assert.deepEqual(collection.items, []);
assert.deepEqual(collection.links, []);
@@ -31,6 +40,15 @@ describe('fromObject', function() {
assert.equal(collection.template, undefined);
});
+ // Assert what a template object should look like
+ it('Has basic fields on a template', function() {
+ var template = collection_json.fromObject({template: {}}).template;
+ assert.equal(template.items, undefined);
+ assert.equal(template.links, undefined);
+ assert.equal(template.queries, undefined);
+ assert.equal(typeof template.data, 'object');
+ });
+
it('Can map data field in items', function() {
assert.deepEqual(collection1().mapItems(identity),
[{"field-1": "1", "field-2": "2"}
@@ -38,6 +56,22 @@ describe('fromObject', function() {
function f(map) { return parseInt(map["field-1"]) + parseInt(map["field-2"]); }
assert.deepEqual(collection1().mapItems(f), [3, 7]);
});
+ /*
+ */
+
+ it('Knows the difference between silly objects, collections and templates', function() {
+ var silly = collection_json.fromObject({});
+ var collection = collection_json.fromObject(collection1());
+ var template = collection_json.fromObject(template1());
+
+ assert.equal(template.isCollection(), false);
+ assert.equal(template.isTemplate(), true);
+
+ assert.equal(silly.isCollection(), true);
+ assert.equal(silly.isTemplate(), false);
+ assert.equal(collection.isCollection(), true);
+ assert.equal(collection.isTemplate(), false);
+ });
});
describe('collection object', function() {