Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 89 additions & 12 deletions flow.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,55 @@
* Сделано задание на звездочку
* Реализованы методы mapLimit и filterLimit
*/
exports.isStar = true;
exports.isStar = false;

/**
* Последовательное выполнение операций
* @param {Function[]} operations – функции для выполнения
* @param {Function} callback
*/
exports.serial = function (operations, callback) {
console.info(operations, callback);

if (!operations || operations.length === 0) {
callback(null, []);

return;
}

var nextCallback = function (error, data) {
if (error || operations.length === 0) {
callback(error, data);
} else {
operations.shift()(data, nextCallback);
}
};

operations.shift()(nextCallback);
};

var executeConcurrent = function (operations, callback) {
var errorFound = false;
var operationQueue = [];

if (operations.length === 0) {
callback(null, []);
}

operations.forEach (function (func, index) {
if (!errorFound) {
func(function (error, data) {
if (error) {
errorFound = true;
callback(error, data);
} else {
operationQueue.push(data);
}
if (index === operations.length - 1) {
callback(null, operationQueue);
}
});
}
});
};

/**
Expand All @@ -22,7 +62,20 @@ exports.serial = function (operations, callback) {
* @param {Function} callback
*/
exports.map = function (items, operation, callback) {
console.info(items, operation, callback);

if (items.length === 0) {
callback(null, []);

return;
}

var operations = items.map(function (i) {
return function (cb) {
operation(i, cb);
};
});

executeConcurrent(operations, callback);
};

/**
Expand All @@ -32,15 +85,36 @@ exports.map = function (items, operation, callback) {
* @param {Function} callback
*/
exports.filter = function (items, operation, callback) {
console.info(items, operation, callback);

var func = function (error, data) {
if (error) {
callback(error);
} else {
callback(null, items.filter(function (item, index) {
return data[index];
}));
}
};

exports.map(items, operation, func);
};

/**
* Асинхронизация функций
* @param {Function} func – функция, которой суждено стать асинхронной
* @returns {Function}
*/
exports.makeAsync = function (func) {
console.info(func);
return function () {
setTimeout(function (args) {
var callback = args.pop();
try {
callback(null, func.apply(null, args));
} catch (error) {
callback(error);
}
}, 0, [].slice.call(arguments));
};
};

/**
Expand All @@ -51,10 +125,11 @@ exports.makeAsync = function (func) {
* @param {Function} operation – функция для обработки элементов
* @param {Function} callback
*/
exports.mapLimit = function (items, limit, operation, callback) {
callback(new Error('Функция mapLimit не реализована'));
};

/*
exports.mapLimit = function (items, limit, operation, callback) {
callback(new Error('Функция mapLimit не реализована'));
};
*/
/**
* Параллельная фильтрация элементов с ограничением
* @star
Expand All @@ -63,6 +138,8 @@ exports.mapLimit = function (items, limit, operation, callback) {
* @param {Function} operation – функция для обработки элементов
* @param {Function} callback
*/
exports.filterLimit = function (items, limit, operation, callback) {
callback(new Error('Функция filterLimit не реализована'));
};
/*
exports.filterLimit = function (items, limit, operation, callback) {
callback(new Error('Функция filterLimit не реализована'));
};
*/