diff options
Diffstat (limited to 'lib/utils')
-rw-r--r-- | lib/utils/path.js | 12 | ||||
-rw-r--r-- | lib/utils/promise.js | 26 |
2 files changed, 35 insertions, 3 deletions
diff --git a/lib/utils/path.js b/lib/utils/path.js index dc97d5d..2d722a5 100644 --- a/lib/utils/path.js +++ b/lib/utils/path.js @@ -1,6 +1,11 @@ var _ = require('lodash'); var path = require('path'); +// Normalize a filename +function normalizePath(filename) { + return path.normalize(filename); +} + // Return true if file path is inside a folder function isInRoot(root, filename) { filename = path.normalize(filename); @@ -17,10 +22,10 @@ function resolveInRoot(root) { .slice(1) .reduce(function(current, p) { // Handle path relative to book root ("/README.md") - if (p[0] == "/" || p[0] == "\\") return p.slice(1); + if (p[0] == '/' || p[0] == '\\') return p.slice(1); return current? path.join(current, p) : path.normalize(p); - }, "") + }, '') .value(); result = path.resolve(root, input); @@ -36,5 +41,6 @@ function resolveInRoot(root) { module.exports = { isInRoot: isInRoot, - resolveInRoot: resolveInRoot + resolveInRoot: resolveInRoot, + normalize: normalizePath }; diff --git a/lib/utils/promise.js b/lib/utils/promise.js new file mode 100644 index 0000000..c25b349 --- /dev/null +++ b/lib/utils/promise.js @@ -0,0 +1,26 @@ +var Q = require('q'); +var _ = require('lodash'); + +// Reduce an array to a promise +function reduce(arr, iter, base) { + return _.reduce(arr, function(prev, elem, i) { + return prev.then(function(val) { + return iter(val, elem, i); + }); + }, Q(base)); +} + +// Transform an array +function serie(arr, iter, base) { + return reduce(arr, function(before, item, i) { + return Q(iter(item, i)) + .then(function(r) { + before.push(r); + return before; + }); + }, []); +} + +module.exports = Q; +module.exports.reduce = reduce; +module.exports.serie = serie; |