summaryrefslogtreecommitdiff
path: root/node_modules/collection_json/index.js
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2012-05-04 16:57:31 +0200
committerTrygve Laugstøl <trygvis@inamo.no>2012-05-04 16:57:31 +0200
commit3a7e65ca638bfcd615dcd24d4ee800596621be8b (patch)
tree76121f6812dbe0ae4867477828c570e330f561de /node_modules/collection_json/index.js
parenta55635dd4da622a82a091b13c533388e0d8a28cb (diff)
downloadbitraf-bot-3a7e65ca638bfcd615dcd24d4ee800596621be8b.tar.gz
bitraf-bot-3a7e65ca638bfcd615dcd24d4ee800596621be8b.tar.bz2
bitraf-bot-3a7e65ca638bfcd615dcd24d4ee800596621be8b.tar.xz
bitraf-bot-3a7e65ca638bfcd615dcd24d4ee800596621be8b.zip
o Adding a small module to process application/vnd.collection+json documents.
Diffstat (limited to 'node_modules/collection_json/index.js')
-rw-r--r--node_modules/collection_json/index.js40
1 files changed, 40 insertions, 0 deletions
diff --git a/node_modules/collection_json/index.js b/node_modules/collection_json/index.js
new file mode 100644
index 0000000..5705fec
--- /dev/null
+++ b/node_modules/collection_json/index.js
@@ -0,0 +1,40 @@
+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 || {};
+
+ collection.mapItemData = function(f) {
+ return _.map(collection.items, function(item) {
+// console.log("item", item);
+ var map = _.reduce(item.data, function(map, field) {
+// console.log("field", field);
+ map[field.name] = field.value;
+ return map;
+ }, {});
+// console.log("map", map);
+ return f(map);
+ });
+ };
+
+ return collection;
+}
+module.exports.fromObject = fromObject;
+
+function fromString(s) {
+ var object = {};
+ try {
+ object = JSON.parse(s);
+ }
+ catch(ex) {
+ }
+ return fromObject(object);
+}
+module.exports.fromString = fromString;