diff --git a/flow.js b/flow.js index d46e521..e46e8b5 100644 --- a/flow.js +++ b/flow.js @@ -4,7 +4,7 @@ * Сделано задание на звездочку * Реализованы методы mapLimit и filterLimit */ -exports.isStar = true; +exports.isStar = false; /** * Последовательное выполнение операций @@ -12,7 +12,47 @@ exports.isStar = true; * @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); + } + }); + } + }); }; /** @@ -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); }; /** @@ -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)); + }; }; /** @@ -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 @@ -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 не реализована')); + }; + */