From e4751178540a3c6e64598b93977481ec599704d2 Mon Sep 17 00:00:00 2001 From: Alex Early Date: Sun, 8 Jul 2018 16:58:36 -0700 Subject: ES6-ify codebase (#1553) * cancelable foreach * cancelable waterfall * cancellable auto * fix lint * fix tests * cancelable whilst/until/during/forever * fix waterfall test. It WILL get there * docs * use rest params instead of slice * clean up internals * remove property func * clarify uses of createTester * happy path async funtions in asyncify * stop using arguments * DLL to class * moar arrows * fix merge issues * remove forOwn * moar arrows * fix merge mistake * even more arrows, what can stop him * mo more fn.apply(null,...) * remove more spurious uses of apply * update lint config * just when you thought there couldn't possibly be more arrows * use eslint:recommended * even less uses or aguments * get rid of prototype cuteness * fix concat tests * fix more tests --- lib/internal/DoublyLinkedList.js | 139 ++++++++++++++++++++------------------- lib/internal/applyEach.js | 6 +- lib/internal/consoleFunc.js | 26 +++----- lib/internal/createTester.js | 26 ++++---- lib/internal/doLimit.js | 4 +- lib/internal/doParallel.js | 4 +- lib/internal/doParallelLimit.js | 4 +- lib/internal/eachOfLimit.js | 4 +- lib/internal/filter.js | 28 ++++---- lib/internal/findGetResult.js | 3 - lib/internal/forOwn.js | 8 --- lib/internal/getIterator.js | 4 +- lib/internal/identity.js | 3 - lib/internal/initialParams.js | 5 +- lib/internal/iterator.js | 2 +- lib/internal/map.js | 6 +- lib/internal/notId.js | 3 - lib/internal/once.js | 4 +- lib/internal/onlyOnce.js | 4 +- lib/internal/parallel.js | 13 ++-- lib/internal/property.js | 5 -- lib/internal/queue.js | 36 +++++----- lib/internal/reject.js | 4 +- lib/internal/setImmediate.js | 9 +-- lib/internal/slice.js | 9 --- lib/internal/withoutIndex.js | 4 +- lib/internal/wrapAsync.js | 4 +- 27 files changed, 147 insertions(+), 220 deletions(-) delete mode 100644 lib/internal/findGetResult.js delete mode 100644 lib/internal/forOwn.js delete mode 100644 lib/internal/identity.js delete mode 100644 lib/internal/notId.js delete mode 100644 lib/internal/property.js delete mode 100644 lib/internal/slice.js (limited to 'lib/internal') diff --git a/lib/internal/DoublyLinkedList.js b/lib/internal/DoublyLinkedList.js index 86c5cfd..259ac0d 100644 --- a/lib/internal/DoublyLinkedList.js +++ b/lib/internal/DoublyLinkedList.js @@ -2,86 +2,89 @@ // used for queues. This implementation assumes that the node provided by the user can be modified // to adjust the next and last properties. We implement only the minimal functionality // for queue support. -export default function DLL() { - this.head = this.tail = null; - this.length = 0; -} - -function setInitial(dll, node) { - dll.length = 1; - dll.head = dll.tail = node; -} +export default class DLL { + constructor() { + this.head = this.tail = null; + this.length = 0; + } -DLL.prototype.removeLink = function(node) { - if (node.prev) node.prev.next = node.next; - else this.head = node.next - if (node.next) node.next.prev = node.prev; - else this.tail = node.prev; + removeLink(node) { + if (node.prev) node.prev.next = node.next; + else this.head = node.next + if (node.next) node.next.prev = node.prev; + else this.tail = node.prev; - node.prev = node.next = null; - this.length -= 1; - return node; -} + node.prev = node.next = null; + this.length -= 1; + return node; + } -DLL.prototype.empty = function () { - while(this.head) this.shift(); - return this; -}; + empty () { + while(this.head) this.shift(); + return this; + } -DLL.prototype.insertAfter = function(node, newNode) { - newNode.prev = node; - newNode.next = node.next; - if (node.next) node.next.prev = newNode; - else this.tail = newNode; - node.next = newNode; - this.length += 1; -} + insertAfter(node, newNode) { + newNode.prev = node; + newNode.next = node.next; + if (node.next) node.next.prev = newNode; + else this.tail = newNode; + node.next = newNode; + this.length += 1; + } -DLL.prototype.insertBefore = function(node, newNode) { - newNode.prev = node.prev; - newNode.next = node; - if (node.prev) node.prev.next = newNode; - else this.head = newNode; - node.prev = newNode; - this.length += 1; -} + insertBefore(node, newNode) { + newNode.prev = node.prev; + newNode.next = node; + if (node.prev) node.prev.next = newNode; + else this.head = newNode; + node.prev = newNode; + this.length += 1; + } -DLL.prototype.unshift = function(node) { - if (this.head) this.insertBefore(this.head, node); - else setInitial(this, node); -}; + unshift(node) { + if (this.head) this.insertBefore(this.head, node); + else setInitial(this, node); + } -DLL.prototype.push = function(node) { - if (this.tail) this.insertAfter(this.tail, node); - else setInitial(this, node); -}; + push(node) { + if (this.tail) this.insertAfter(this.tail, node); + else setInitial(this, node); + } -DLL.prototype.shift = function() { - return this.head && this.removeLink(this.head); -}; + shift() { + return this.head && this.removeLink(this.head); + } -DLL.prototype.pop = function() { - return this.tail && this.removeLink(this.tail); -}; + pop() { + return this.tail && this.removeLink(this.tail); + } -DLL.prototype.toArray = function () { - var arr = Array(this.length); - var curr = this.head; - for(var idx = 0; idx < this.length; idx++) { - arr[idx] = curr.data; - curr = curr.next; + toArray () { + var arr = Array(this.length); + var curr = this.head; + for(var idx = 0; idx < this.length; idx++) { + arr[idx] = curr.data; + curr = curr.next; + } + return arr; } - return arr; -} -DLL.prototype.remove = function (testFn) { - var curr = this.head; - while(!!curr) { - var next = curr.next; - if (testFn(curr)) { - this.removeLink(curr); + remove (testFn) { + var curr = this.head; + while(curr) { + var next = curr.next; + if (testFn(curr)) { + this.removeLink(curr); + } + curr = next; } - curr = next; + return this; } - return this; } + +function setInitial(dll, node) { + dll.length = 1; + dll.head = dll.tail = node; +} + diff --git a/lib/internal/applyEach.js b/lib/internal/applyEach.js index c0ed402..b029184 100644 --- a/lib/internal/applyEach.js +++ b/lib/internal/applyEach.js @@ -1,13 +1,11 @@ -import slice from './slice'; import initialParams from './initialParams'; import wrapAsync from './wrapAsync'; export default function applyEach(eachfn) { - return function(fns/*, ...args*/) { - var args = slice(arguments, 1); + return function(fns, ...args) { var go = initialParams(function(args, callback) { var that = this; - return eachfn(fns, function (fn, cb) { + return eachfn(fns, (fn, cb) => { wrapAsync(fn).apply(that, args.concat(cb)); }, callback); }); diff --git a/lib/internal/consoleFunc.js b/lib/internal/consoleFunc.js index c977647..d2c1cb1 100644 --- a/lib/internal/consoleFunc.js +++ b/lib/internal/consoleFunc.js @@ -1,23 +1,15 @@ -import slice from './slice'; import wrapAsync from './wrapAsync'; export default function consoleFunc(name) { - return function (fn/*, ...args*/) { - var args = slice(arguments, 1); - args.push(function (err/*, ...args*/) { - var args = slice(arguments, 1); - if (typeof console === 'object') { - if (err) { - if (console.error) { - console.error(err); - } - } else if (console[name]) { - args.forEach(function (x) { - console[name](x); - }); + return (fn, ...args) => wrapAsync(fn)(...args, (err, ...resultArgs) => { + if (typeof console === 'object') { + if (err) { + if (console.error) { + console.error(err); } + } else if (console[name]) { + resultArgs.forEach(x => console[name](x)); } - }) - wrapAsync(fn).apply(null, args); - }; + } + }) } diff --git a/lib/internal/createTester.js b/lib/internal/createTester.js index 8786833..4a5d39f 100644 --- a/lib/internal/createTester.js +++ b/lib/internal/createTester.js @@ -2,28 +2,24 @@ import noop from './noop'; import breakLoop from './breakLoop'; export default function _createTester(check, getResult) { - return function(eachfn, arr, iteratee, cb) { + return (eachfn, arr, iteratee, cb) => { cb = cb || noop; var testPassed = false; var testResult; - eachfn(arr, function(value, _, callback) { - iteratee(value, function(err, result) { - if (err) { - callback(err); - } else if (check(result) && !testResult) { + eachfn(arr, (value, _, callback) => { + iteratee(value, (err, result) => { + if (err) return callback(err) + + if (check(result) && !testResult) { testPassed = true; testResult = getResult(true, value); - callback(null, breakLoop); - } else { - callback(); + return callback(null, breakLoop); } + callback(); }); - }, function(err) { - if (err) { - cb(err); - } else { - cb(null, testPassed ? testResult : getResult(false)); - } + }, err => { + if (err) return cb(err); + cb(null, testPassed ? testResult : getResult(false)); }); }; } diff --git a/lib/internal/doLimit.js b/lib/internal/doLimit.js index 7d2f111..70fb495 100644 --- a/lib/internal/doLimit.js +++ b/lib/internal/doLimit.js @@ -1,5 +1,3 @@ export default function doLimit(fn, limit) { - return function (iterable, iteratee, callback) { - return fn(iterable, limit, iteratee, callback); - }; + return (iterable, iteratee, cb) => fn(iterable, limit, iteratee, cb) } diff --git a/lib/internal/doParallel.js b/lib/internal/doParallel.js index deed912..2b76b68 100644 --- a/lib/internal/doParallel.js +++ b/lib/internal/doParallel.js @@ -2,7 +2,5 @@ import eachOf from '../eachOf'; import wrapAsync from './wrapAsync'; export default function doParallel(fn) { - return function (obj, iteratee, callback) { - return fn(eachOf, obj, wrapAsync(iteratee), callback); - }; + return (obj, iteratee, cb) => fn(eachOf, obj, wrapAsync(iteratee), cb); } diff --git a/lib/internal/doParallelLimit.js b/lib/internal/doParallelLimit.js index 6d0f607..5e2703a 100644 --- a/lib/internal/doParallelLimit.js +++ b/lib/internal/doParallelLimit.js @@ -2,7 +2,5 @@ import eachOfLimit from './eachOfLimit'; import wrapAsync from './wrapAsync'; export default function doParallelLimit(fn) { - return function (obj, limit, iteratee, callback) { - return fn(eachOfLimit(limit), obj, wrapAsync(iteratee), callback); - }; + return (obj, limit, iteratee, cb) => fn(eachOfLimit(limit), obj, wrapAsync(iteratee), cb); } diff --git a/lib/internal/eachOfLimit.js b/lib/internal/eachOfLimit.js index 2928366..1d710dc 100644 --- a/lib/internal/eachOfLimit.js +++ b/lib/internal/eachOfLimit.js @@ -6,8 +6,8 @@ import onlyOnce from './onlyOnce'; import breakLoop from './breakLoop'; -export default function _eachOfLimit(limit) { - return function (obj, iteratee, callback) { +export default (limit) => { + return (obj, iteratee, callback) => { callback = once(callback || noop); if (limit <= 0) { throw new RangeError('concurrency limit cannot be less than 1') diff --git a/lib/internal/filter.js b/lib/internal/filter.js index 1923264..4c9422b 100644 --- a/lib/internal/filter.js +++ b/lib/internal/filter.js @@ -1,17 +1,16 @@ import isArrayLike from './isArrayLike'; -import property from './property'; import noop from './noop'; import wrapAsync from './wrapAsync'; function filterArray(eachfn, arr, iteratee, callback) { var truthValues = new Array(arr.length); - eachfn(arr, function (x, index, callback) { - iteratee(x, function (err, v) { + eachfn(arr, (x, index, callback) => { + iteratee(x, (err, v) => { truthValues[index] = !!v; callback(err); }); - }, function (err) { + }, err => { if (err) return callback(err); var results = []; for (var i = 0; i < arr.length; i++) { @@ -23,29 +22,26 @@ function filterArray(eachfn, arr, iteratee, callback) { function filterGeneric(eachfn, coll, iteratee, callback) { var results = []; - eachfn(coll, function (x, index, callback) { - iteratee(x, function (err, v) { + eachfn(coll, (x, index, callback) => { + iteratee(x, (err, v) => { if (err) { callback(err); } else { if (v) { - results.push({index: index, value: x}); + results.push({index, value: x}); } callback(); } }); - }, function (err) { - if (err) { - callback(err); - } else { - callback(null, results.sort(function (a, b) { - return a.index - b.index; - }).map(property('value'))); - } + }, err => { + if (err) return callback(err); + callback(null, results + .sort((a, b) => a.index - b.index) + .map(v => v.value)); }); } export default function _filter(eachfn, coll, iteratee, callback) { var filter = isArrayLike(coll) ? filterArray : filterGeneric; - filter(eachfn, coll, wrapAsync(iteratee), callback || noop); + return filter(eachfn, coll, wrapAsync(iteratee), callback || noop); } diff --git a/lib/internal/findGetResult.js b/lib/internal/findGetResult.js deleted file mode 100644 index 7345df6..0000000 --- a/lib/internal/findGetResult.js +++ /dev/null @@ -1,3 +0,0 @@ -export default function _findGetResult(v, x) { - return x; -} diff --git a/lib/internal/forOwn.js b/lib/internal/forOwn.js deleted file mode 100644 index ea16519..0000000 --- a/lib/internal/forOwn.js +++ /dev/null @@ -1,8 +0,0 @@ -export default function forOwn(object, callback) { - if (!object) { - return; - } - Object.keys(object).forEach(function (key) { - callback(object[key], key); - }); -} diff --git a/lib/internal/getIterator.js b/lib/internal/getIterator.js index 87860f4..99a5718 100644 --- a/lib/internal/getIterator.js +++ b/lib/internal/getIterator.js @@ -1,5 +1,3 @@ -var iteratorSymbol = typeof Symbol === 'function' && Symbol.iterator; - export default function (coll) { - return iteratorSymbol && coll[iteratorSymbol] && coll[iteratorSymbol](); + return coll[Symbol.iterator] && coll[Symbol.iterator](); } diff --git a/lib/internal/identity.js b/lib/internal/identity.js deleted file mode 100644 index 7e6669d..0000000 --- a/lib/internal/identity.js +++ /dev/null @@ -1,3 +0,0 @@ -export default function identity(value) { - return value; -} diff --git a/lib/internal/initialParams.js b/lib/internal/initialParams.js index 2079703..4014785 100644 --- a/lib/internal/initialParams.js +++ b/lib/internal/initialParams.js @@ -1,8 +1,5 @@ -import slice from './slice'; - export default function (fn) { - return function (/*...args, callback*/) { - var args = slice(arguments); + return function (...args/*, callback*/) { var callback = args.pop(); fn.call(this, args, callback); }; diff --git a/lib/internal/iterator.js b/lib/internal/iterator.js index 6555187..b0c4644 100644 --- a/lib/internal/iterator.js +++ b/lib/internal/iterator.js @@ -26,7 +26,7 @@ function createObjectIterator(obj) { var len = okeys.length; return function next() { var key = okeys[++i]; - return i < len ? {value: obj[key], key: key} : null; + return i < len ? {value: obj[key], key} : null; }; } diff --git a/lib/internal/map.js b/lib/internal/map.js index c7f64dd..b99d8ee 100644 --- a/lib/internal/map.js +++ b/lib/internal/map.js @@ -8,13 +8,13 @@ export default function _asyncMap(eachfn, arr, iteratee, callback) { var counter = 0; var _iteratee = wrapAsync(iteratee); - eachfn(arr, function (value, _, callback) { + return eachfn(arr, (value, _, callback) => { var index = counter++; - _iteratee(value, function (err, v) { + _iteratee(value, (err, v) => { results[index] = v; callback(err); }); - }, function (err) { + }, err => { callback(err, results); }); } diff --git a/lib/internal/notId.js b/lib/internal/notId.js deleted file mode 100644 index 65a676e..0000000 --- a/lib/internal/notId.js +++ /dev/null @@ -1,3 +0,0 @@ -export default function notId(v) { - return !v; -} diff --git a/lib/internal/once.js b/lib/internal/once.js index f601185..1293d5e 100644 --- a/lib/internal/once.js +++ b/lib/internal/once.js @@ -1,8 +1,8 @@ export default function once(fn) { - return function () { + return function (...args) { if (fn === null) return; var callFn = fn; fn = null; - callFn.apply(this, arguments); + callFn.apply(this, args); }; } diff --git a/lib/internal/onlyOnce.js b/lib/internal/onlyOnce.js index bb1979a..07664a4 100644 --- a/lib/internal/onlyOnce.js +++ b/lib/internal/onlyOnce.js @@ -1,8 +1,8 @@ export default function onlyOnce(fn) { - return function() { + return function (...args) { if (fn === null) throw new Error("Callback was already called."); var callFn = fn; fn = null; - callFn.apply(this, arguments); + callFn.apply(this, args); }; } diff --git a/lib/internal/parallel.js b/lib/internal/parallel.js index bbfdb42..89e181e 100644 --- a/lib/internal/parallel.js +++ b/lib/internal/parallel.js @@ -1,21 +1,18 @@ import isArrayLike from './isArrayLike'; import noop from './noop'; -import slice from './slice'; import wrapAsync from './wrapAsync'; export default function _parallel(eachfn, tasks, callback) { callback = callback || noop; var results = isArrayLike(tasks) ? [] : {}; - eachfn(tasks, function (task, key, callback) { - wrapAsync(task)(function (err, result) { - if (arguments.length > 2) { - result = slice(arguments, 1); + eachfn(tasks, (task, key, callback) => { + wrapAsync(task)((err, ...result) => { + if (result.length < 2) { + result = result[0]; } results[key] = result; callback(err); }); - }, function (err) { - callback(err, results); - }); + }, err => callback(err, results)); } diff --git a/lib/internal/property.js b/lib/internal/property.js deleted file mode 100644 index 567b3c3..0000000 --- a/lib/internal/property.js +++ /dev/null @@ -1,5 +0,0 @@ -export default function property(property) { - return function (object) { - return object[property]; - } -} diff --git a/lib/internal/queue.js b/lib/internal/queue.js index a421d2e..a811080 100644 --- a/lib/internal/queue.js +++ b/lib/internal/queue.js @@ -27,9 +27,7 @@ export default function queue(worker, concurrency, payload) { } if (data.length === 0 && q.idle()) { // call drain immediately if there are no tasks - return setImmediate(function() { - q.drain(); - }); + return setImmediate(() => q.drain()); } for (var i = 0, l = data.length; i < l; i++) { @@ -47,7 +45,7 @@ export default function queue(worker, concurrency, payload) { if (!processingScheduled) { processingScheduled = true; - setImmediate(function() { + setImmediate(() => { processingScheduled = false; q.process(); }); @@ -55,7 +53,7 @@ export default function queue(worker, concurrency, payload) { } function _next(tasks) { - return function(err){ + return function (err, ...args) { numRunning -= 1; for (var i = 0, l = tasks.length; i < l; i++) { @@ -68,7 +66,7 @@ export default function queue(worker, concurrency, payload) { workersList.splice(index, 1); } - task.callback.apply(task, arguments); + task.callback(err, ...args); if (err != null) { q.error(err, task.data); @@ -89,8 +87,8 @@ export default function queue(worker, concurrency, payload) { var isProcessing = false; var q = { _tasks: new DLL(), - concurrency: concurrency, - payload: payload, + concurrency, + payload, saturated: noop, unsaturated:noop, buffer: concurrency / 4, @@ -99,20 +97,20 @@ export default function queue(worker, concurrency, payload) { error: noop, started: false, paused: false, - push: function (data, callback) { + push (data, callback) { _insert(data, false, callback); }, - kill: function () { + kill () { q.drain = noop; q._tasks.empty(); }, - unshift: function (data, callback) { + unshift (data, callback) { _insert(data, true, callback); }, - remove: function (testFn) { + remove (testFn) { q._tasks.remove(testFn); }, - process: function () { + process () { // Avoid trying to start too many processing operations. This can occur // when callbacks resolve synchronously (#1267). if (isProcessing) { @@ -145,22 +143,22 @@ export default function queue(worker, concurrency, payload) { } isProcessing = false; }, - length: function () { + length () { return q._tasks.length; }, - running: function () { + running () { return numRunning; }, - workersList: function () { + workersList () { return workersList; }, - idle: function() { + idle() { return q._tasks.length + numRunning === 0; }, - pause: function () { + pause () { q.paused = true; }, - resume: function () { + resume () { if (q.paused === false) { return; } q.paused = false; setImmediate(q.process); diff --git a/lib/internal/reject.js b/lib/internal/reject.js index e082328..1522632 100644 --- a/lib/internal/reject.js +++ b/lib/internal/reject.js @@ -1,8 +1,8 @@ import filter from './filter'; export default function reject(eachfn, arr, iteratee, callback) { - filter(eachfn, arr, function(value, cb) { - iteratee(value, function(err, v) { + return filter(eachfn, arr, (value, cb) => { + iteratee(value, (err, v) => { cb(err, !v); }); }, callback); diff --git a/lib/internal/setImmediate.js b/lib/internal/setImmediate.js index 95191f9..e14e52e 100644 --- a/lib/internal/setImmediate.js +++ b/lib/internal/setImmediate.js @@ -1,7 +1,5 @@ 'use strict'; -import slice from './slice'; - export var hasSetImmediate = typeof setImmediate === 'function' && setImmediate; export var hasNextTick = typeof process === 'object' && typeof process.nextTick === 'function'; @@ -10,12 +8,7 @@ export function fallback(fn) { } export function wrap(defer) { - return function (fn/*, ...args*/) { - var args = slice(arguments, 1); - defer(function () { - fn.apply(null, args); - }); - }; + return (fn, ...args) => defer(() => fn(...args)); } var _defer; diff --git a/lib/internal/slice.js b/lib/internal/slice.js deleted file mode 100644 index cd2e5b3..0000000 --- a/lib/internal/slice.js +++ /dev/null @@ -1,9 +0,0 @@ -export default function slice(arrayLike, start) { - start = start|0; - var newLen = Math.max(arrayLike.length - start, 0); - var newArr = Array(newLen); - for(var idx = 0; idx < newLen; idx++) { - newArr[idx] = arrayLike[start + idx]; - } - return newArr; -} diff --git a/lib/internal/withoutIndex.js b/lib/internal/withoutIndex.js index 2d70dd4..4de0f31 100644 --- a/lib/internal/withoutIndex.js +++ b/lib/internal/withoutIndex.js @@ -1,5 +1,3 @@ export default function _withoutIndex(iteratee) { - return function (value, index, callback) { - return iteratee(value, callback); - }; + return (value, index, callback) => iteratee(value, callback); } diff --git a/lib/internal/wrapAsync.js b/lib/internal/wrapAsync.js index c4d1ea8..2b1490c 100644 --- a/lib/internal/wrapAsync.js +++ b/lib/internal/wrapAsync.js @@ -1,9 +1,7 @@ import asyncify from '../asyncify'; -var supportsSymbol = typeof Symbol === 'function'; - function isAsync(fn) { - return supportsSymbol && fn[Symbol.toStringTag] === 'AsyncFunction'; + return fn[Symbol.toStringTag] === 'AsyncFunction'; } function wrapAsync(asyncFn) { -- cgit v1.2.1