summaryrefslogtreecommitdiffstats
path: root/lib/utils
diff options
context:
space:
mode:
Diffstat (limited to 'lib/utils')
-rw-r--r--lib/utils/error.js8
-rw-r--r--lib/utils/promise.js72
2 files changed, 68 insertions, 12 deletions
diff --git a/lib/utils/error.js b/lib/utils/error.js
index 27fa59d..d34c17e 100644
--- a/lib/utils/error.js
+++ b/lib/utils/error.js
@@ -32,6 +32,13 @@ var FileNotFoundError = TypedError({
filename: null
});
+// A file cannot be parsed
+var FileNotParsableError = TypedError({
+ type: 'file.not-parsable',
+ message: '"{filename}" file cannot be parsed',
+ filename: null
+});
+
// A file is outside the scope
var FileOutOfScopeError = TypedError({
type: 'file.out-of-scope',
@@ -92,6 +99,7 @@ module.exports = {
OutputError: OutputError,
RequireInstallError: RequireInstallError,
+ FileNotParsableError: FileNotParsableError,
FileNotFoundError: FileNotFoundError,
FileOutOfScopeError: FileOutOfScopeError,
diff --git a/lib/utils/promise.js b/lib/utils/promise.js
index d49cf27..b0cfb34 100644
--- a/lib/utils/promise.js
+++ b/lib/utils/promise.js
@@ -1,16 +1,43 @@
var Q = require('q');
-var _ = require('lodash');
+var Immutable = require('immutable');
-// Reduce an array to a promise
+/**
+ Reduce an array to a promise
+
+ @param {Array|List} arr
+ @param {Function(value, element, index)}
+ @return {Promise<Mixed>}
+*/
function reduce(arr, iter, base) {
- return _.reduce(arr, function(prev, elem, i) {
+ arr = Immutable.List(arr);
+
+ return arr.reduce(function(prev, elem, i) {
return prev.then(function(val) {
return iter(val, elem, i);
});
}, Q(base));
}
-// Transform an array
+/**
+ Iterate over an array using an async iter
+
+ @param {Array|List} arr
+ @param {Function(value, element, index)}
+ @return {Promise}
+*/
+function forEach(arr, iter) {
+ return reduce(arr, function(val, el) {
+ return iter(el);
+ });
+}
+
+/**
+ Transform an array
+
+ @param {Array|List} arr
+ @param {Function(value, element, index)}
+ @return {Promise}
+*/
function serie(arr, iter, base) {
return reduce(arr, function(before, item, i) {
return Q(iter(item, i))
@@ -21,9 +48,17 @@ function serie(arr, iter, base) {
}, []);
}
-// Iter over an array and return first result (not null)
+/**
+ Iter over an array and return first result (not null)
+
+ @param {Array|List} arr
+ @param {Function(element, index)}
+ @return {Promise<Mixed>}
+*/
function some(arr, iter) {
- return _.reduce(arr, function(prev, elem, i) {
+ arr = Immutable.List(arr);
+
+ return arr.reduce(function(prev, elem, i) {
return prev.then(function(val) {
if (val) return val;
@@ -32,7 +67,13 @@ function some(arr, iter) {
}, Q());
}
-// Map an array using an async (promised) iterator
+/**
+ Map an array using an async (promised) iterator
+
+ @param {Array|List} arr
+ @param {Function(element, index)}
+ @return {Promise<List>}
+*/
function map(arr, iter) {
return reduce(arr, function(prev, entry, i) {
return Q(iter(entry, i))
@@ -43,18 +84,25 @@ function map(arr, iter) {
}, []);
}
-// Wrap a fucntion in a promise
+/**
+ Wrap a fucntion in a promise
+
+ @param {Function} func
+ @return {Funciton}
+*/
function wrap(func) {
- return _.wrap(func, function(_func) {
- var args = Array.prototype.slice.call(arguments, 1);
+ return function() {
+ var args = Array.prototype.slice.call(arguments, 0);
+
return Q()
.then(function() {
- return _func.apply(null, args);
+ return func.apply(null, args);
});
- });
+ };
}
module.exports = Q;
+module.exports.forEach = forEach;
module.exports.reduce = reduce;
module.exports.map = map;
module.exports.serie = serie;