summaryrefslogtreecommitdiff
path: root/index.js
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 /index.js
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.
Diffstat (limited to 'index.js')
-rw-r--r--index.js76
1 files changed, 50 insertions, 26 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;