summaryrefslogtreecommitdiff
path: root/app/node_modules/okutil/index.js
diff options
context:
space:
mode:
authorSean Fridman <fridman@mail.sfsu.edu>2015-04-03 13:42:12 -0400
committerSean Fridman <fridman@mail.sfsu.edu>2015-04-06 15:27:52 -0400
commit0e968c54dbd30d0004009b623af901dcfb64cf3a (patch)
tree90654f042ccbca3ac766da09f2d6a72c9675a0e5 /app/node_modules/okutil/index.js
parent35bdb78a6701b919157b0dafa75ff39904197d49 (diff)
Add okutil
Diffstat (limited to 'app/node_modules/okutil/index.js')
-rw-r--r--app/node_modules/okutil/index.js40
1 files changed, 40 insertions, 0 deletions
diff --git a/app/node_modules/okutil/index.js b/app/node_modules/okutil/index.js
new file mode 100644
index 0000000..bef085c
--- /dev/null
+++ b/app/node_modules/okutil/index.js
@@ -0,0 +1,40 @@
+var isarray = require('lodash.isarray');
+var pluralize = require('pluralize');
+var Q = require('q');
+
+/**
+ * OKUtils!
+ * Cross cutting concerns n stuff
+ */
+module.exports = {
+
+ /**
+ * Takes a meta data query and an array of resource queries
+ * and returns a promise for an object merging all queried
+ * data, pluralizing keys where necessary.
+ *
+ * Lil bit convoluted, sorry.
+ */
+ fetchTemplateData: function fetchTemplateData(meta, queries, options) {
+ return Q.promise(function(resolve, reject) {
+ return Q.all(
+ [meta.get()].concat(queries.map(function(query) {
+ return query.get(options);
+ })))
+ .then(function(results) {
+ var metadata = results.shift();
+ var normalized = results.reduce(function(data, result, i) {
+ var type = queries[i].type;
+ if (isarray(result)) {
+ data[pluralize(type)] = result;
+ } else {
+ data[type] = result;
+ }
+ return data;
+ }, {meta: metadata});
+ resolve(normalized);
+ }, reject);
+ });
+ }
+
+};