From 5200a4ba8bb120a279376a13d45ab4f035b154a7 Mon Sep 17 00:00:00 2001 From: Tsvetkova Valeria Date: Wed, 30 Nov 2016 23:51:09 +0500 Subject: [PATCH 1/2] =?UTF-8?q?=D0=97=D0=B0=D0=B4=D0=B0=D1=87=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- flow.js | 101 +++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 89 insertions(+), 12 deletions(-) diff --git a/flow.js b/flow.js index d46e521..1eb6d8b 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 (item) { + return function (cb) { + operation(item, 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 не реализована')); + }; + */ From f23cd801183b94cf3addb348b06ae88ca36cc635 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A6=D0=B2=D0=B5=D1=82=D0=BA=D0=BE=D0=B2=D0=B0=20=D0=92?= =?UTF-8?q?=D0=B0=D0=BB=D0=B5=D1=80=D0=B8=D1=8F?= Date: Wed, 30 Nov 2016 23:59:48 +0500 Subject: [PATCH 2/2] Update flow.js --- flow.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/flow.js b/flow.js index 1eb6d8b..e46e8b5 100644 --- a/flow.js +++ b/flow.js @@ -69,9 +69,9 @@ exports.map = function (items, operation, callback) { return; } - var operations = items.map(function (item) { + var operations = items.map(function (i) { return function (cb) { - operation(item, cb); + operation(i, cb); }; });