From f1dfb09de98c62304e036947114aefa9fbaf6d87 Mon Sep 17 00:00:00 2001 From: Rumiantsev Oleksii Date: Wed, 12 Jun 2019 12:22:06 +0300 Subject: [PATCH] Drop `ArrayChain` Refs: https://github.com/metarhia/metasync/issues/420 --- .metadocrc | 1 - README.md | 47 ------------------ lib/chain.js | 132 -------------------------------------------------- metasync.js | 1 - test/chain.js | 114 ------------------------------------------- 5 files changed, 295 deletions(-) delete mode 100644 lib/chain.js delete mode 100644 test/chain.js diff --git a/.metadocrc b/.metadocrc index 4bcfcdef..27245777 100644 --- a/.metadocrc +++ b/.metadocrc @@ -7,7 +7,6 @@ "lib/adapters.js", "lib/array.js", "lib/async-iterator.js", - "lib/chain.js", "lib/collector.js", "lib/composition.js", "lib/control.js", diff --git a/README.md b/README.md index 5d4c8b15..e05baf57 100644 --- a/README.md +++ b/README.md @@ -366,52 +366,6 @@ Create an AsyncIterator instance #### AsyncIterator.prototype.enumerate() -### for(array) - -- `array`: [``][array] start mutations from this data - -_Returns:_ [``][arraychain] - -Create ArrayChain instance - -### class ArrayChain - -#### ArrayChain.prototype.constructor(array) - -#### ArrayChain.prototype.execute(err) - -#### ArrayChain.prototype.fetch(fn) - -#### ArrayChain.prototype.map(fn) - -#### ArrayChain.prototype.filter(fn) - -#### ArrayChain.prototype.reduce(fn) - -#### ArrayChain.prototype.each(fn) - -#### ArrayChain.prototype.series(fn) - -#### ArrayChain.prototype.find(fn) - -#### ArrayChain.prototype.concat(...args) - -#### ArrayChain.prototype.slice(...args) - -#### ArrayChain.prototype.includes(...args) - -#### ArrayChain.prototype.reverse(...args) - -#### ArrayChain.prototype.sort(...args) - -#### ArrayChain.prototype.shift(...args) - -#### ArrayChain.prototype.unshift(...args) - -#### ArrayChain.prototype.push(...args) - -#### ArrayChain.prototype.pop(...args) - ### collect(expected) - `expected`: [``][number]|[``][string] @@ -915,7 +869,6 @@ Set timeout for asynchronous function execution [asynciterable]: https://tc39.github.io/ecma262/#sec-asynciterable-interface [asynciterator]: #class-asynciterator -[arraychain]: #class-arraychain [collector]: #class-collector [queue]: #class-queue [object]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object diff --git a/lib/chain.js b/lib/chain.js deleted file mode 100644 index 8df942e2..00000000 --- a/lib/chain.js +++ /dev/null @@ -1,132 +0,0 @@ -'use strict'; - -const { map, filter, reduce, each, series, find } = require('./array'); - -const async = op => { - switch (op) { - case 'map': - return map; - case 'filter': - return filter; - case 'reduce': - return reduce; - case 'each': - return each; - case 'series': - return series; - case 'find': - return find; - } - return (items, fn, callback) => { - callback(null, items.slice()); - }; -}; - -function ArrayChain(array) { - this.array = array; - this.chain = []; -} - -ArrayChain.prototype.execute = function(err) { - const item = this.chain.shift() || {}; - - if (!item.op) { - if (err) throw err; - else return null; - } - - const next = (err, data) => { - this.array = data; - this.execute(err); - }; - - if (item.op === 'fetch') { - return item.fn(err, this.array, next); - } - - if (err) { - this.execute(err); - return null; - } - - if (item.isSync) { - this.array = item.fn(); - this.execute(null); - } else { - const op = async(item.op); - op(this.array, item.fn, next); - } - - return null; -}; - -ArrayChain.prototype.fetch = function(fn) { - this.chain.push({ op: 'fetch', fn }); - this.execute(); - return this; -}; - -ArrayChain.prototype.map = function(fn) { - this.chain.push({ op: 'map', fn }); - return this; -}; - -ArrayChain.prototype.filter = function(fn) { - this.chain.push({ op: 'filter', fn }); - return this; -}; - -ArrayChain.prototype.reduce = function(fn) { - this.chain.push({ op: 'reduce', fn }); - return this; -}; - -ArrayChain.prototype.each = function(fn) { - this.chain.push({ op: 'each', fn }); - return this; -}; - -ArrayChain.prototype.series = function(fn) { - this.chain.push({ op: 'series', fn }); - return this; -}; - -ArrayChain.prototype.find = function(fn) { - this.chain.push({ op: 'find', fn }); - return this; -}; - -const syncDelegates = { - returns: { - opNames: ['concat', 'slice', 'includes'], - handler(op, ...args) { - return this.array[op](...args); - }, - }, - modify: { - opNames: ['reverse', 'sort', 'shift', 'unshift', 'push', 'pop'], - handler(op, ...args) { - this.array[op](...args); - return this.array; - }, - }, -}; - -for (const delegateType in syncDelegates) { - const { opNames, handler } = syncDelegates[delegateType]; - for (const op of opNames) { - ArrayChain.prototype[op] = function(...args) { - const fn = handler.bind(this, op, ...args); - this.chain.push({ op, fn, isSync: true }); - return this; - }; - } -} - -// Create ArrayChain instance -// array - , start mutations from this data -// -// Returns: -const forArrayChain = array => new ArrayChain(array); - -module.exports = { for: forArrayChain, ArrayChain }; diff --git a/metasync.js b/metasync.js index a51f620a..ab26165a 100644 --- a/metasync.js +++ b/metasync.js @@ -7,7 +7,6 @@ const submodules = [ 'composition', // Unified abstraction 'adapters', // Adapters to convert different async contracts 'array', // Array utilities - 'chain', // Process arrays sync and async array in chain 'collector', // DataCollector and KeyCollector 'control', // Control flow utilities 'do', // Simple chain/do diff --git a/test/chain.js b/test/chain.js deleted file mode 100644 index f6f7a16c..00000000 --- a/test/chain.js +++ /dev/null @@ -1,114 +0,0 @@ -'use strict'; - -const metasync = require('..'); -const metatests = require('metatests'); - -metatests.test('for.map', test => { - const data = [1, 2, 3, 4]; - const expected = [2, 4, 6, 8]; - const fn = (item, callback) => - process.nextTick(() => { - callback(null, item * 2); - }); - metasync - .for(data) - .map(fn) - .fetch((error, result) => { - test.error(error); - test.strictSame(result, expected); - test.end(); - }); -}); - -metatests.test('for chain sync', test => { - metasync - .for([1, 2, 3, 4]) - .filter((item, cb) => cb(null, item % 2 === 0)) - .map((item, cb) => cb(null, item * 2)) - .reduce((a, b, cb) => cb(null, a + b)) - .fetch((error, result) => { - test.error(error); - test.strictSame(result, 12); // 2 * 2 + 4 * 2 - test.end(); - }); -}); - -metatests.test('for chain async', test => { - metasync - .for([1, 2, 3, 4]) - .filter((item, cb) => process.nextTick(cb, null, item % 2 === 0)) - .map((item, cb) => process.nextTick(cb, null, item * 2)) - .reduce((a, b, cb) => process.nextTick(cb, null, a + b)) - .fetch((error, result) => { - test.error(error); - test.strictSame(result, 12); // 2 * 2 + 4 * 2 - test.end(); - }); -}); - -metatests.test('for chain error', test => { - metasync - .for([1, 2, 3, 4]) - .filter((item, cb) => cb(null, item % 2 === 0)) - .map((item, cb) => cb(new Error('Something happens'))) - .reduce((a, b, cb) => cb(null, a + b)) - .fetch((error, result) => { - test.strictSame(error instanceof Error, true); - test.strictSame(result, undefined); - test.end(); - }); -}); - -metatests.test('for chain after fetch', test => { - metasync - .for([1, 2, 3, 4]) - .map((item, cb) => cb(null, item * item)) - .filter((item, cb) => cb(null, item > 5)) - .fetch((error, result, resume) => { - test.error(error); - test.strictSame(result, [9, 16]); - resume(null, result); - }) - .filter((item, cb) => { - cb(null, item > 10); - }) - .map((item, cb) => { - cb(null, --item); - }) - .fetch((error, result) => { - test.error(error); - test.strictSame(result, [15]); - test.end(); - }); -}); - -metatests.test('for chain all methods', test => { - metasync - .for([1, 2, 3, 4]) - .concat([8, 6, 7]) - .slice(1) - .sort() - .reverse() - .shift() - .unshift(10) - .pop() - .push(11) - .fetch((error, result, resume) => { - test.error(error); - const expected = [10, 7, 6, 4, 3, 11]; - test.strictSame(result, expected); - resume(null, result); - }) - .includes(6) - .fetch((error, result, resume) => { - test.error(error); - test.strictSame(result, true); - resume(null, [6, 8]); - }) - .includes(7) - .fetch((error, result) => { - test.error(error); - test.strictSame(result, false); - test.end(); - }); -});