From c487b4f6dcbfa35f60c575b5049321bf4be06534 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Evandro=20Leopoldino=20Gon=C3=A7alves?= Date: Sun, 18 Apr 2021 12:13:49 +0200 Subject: [PATCH 1/2] implement cancel method to throttle --- packages/function-throttle/index.js | 32 +++++++++++++++++++++-------- test/function-throttle/index.js | 15 ++++++++++++++ 2 files changed, 39 insertions(+), 8 deletions(-) diff --git a/packages/function-throttle/index.js b/packages/function-throttle/index.js index bdb49b110..8cb5f256b 100644 --- a/packages/function-throttle/index.js +++ b/packages/function-throttle/index.js @@ -1,35 +1,51 @@ module.exports = throttle; function throttle(fn, interval, options) { - var wait = false; + var timeoutId = null; var leading = (options && options.leading); var trailing = (options && options.trailing); + if (leading == null) { leading = true; // default } + if (trailing == null) { trailing = !leading; //default } + if (leading == true) { trailing = false; // forced because there should be invocation per call } - return function() { - var callNow = leading && !wait; + var cancel = function() { + if (timeoutId) { + clearTimeout(timeoutId); + timeoutId = null; + } + }; + + var throttleWrapper = function() { + var callNow = leading && !timeoutId; var context = this; var args = arguments; - if (!wait) { - wait = true; - setTimeout(function() { - wait = false; + + if (!timeoutId) { + timeoutId = setTimeout(function() { + timeoutId = null; + if (trailing) { return fn.apply(context, args); } }, interval); } + if (callNow) { callNow = false; - return fn.apply(this, arguments); + return fn.apply(context, args); } }; + + throttleWrapper.cancel = cancel; + + return throttleWrapper; } diff --git a/test/function-throttle/index.js b/test/function-throttle/index.js index 052754eeb..ca0b8473c 100644 --- a/test/function-throttle/index.js +++ b/test/function-throttle/index.js @@ -392,3 +392,18 @@ test('invokes repeatedly when wait is falsey', function(t) { }, 40); }, 40); }); + +test('cancel delayed function', function(t) { + t.plan(1); + + var callCounter = 0; + var fn = throttle(function() { + callCounter++; + }, 200); + + fn.cancel(); + + setTimeout(function() { + t.equal(callCounter, 0); + }, 400); +}); From 2ec62a09eb455d19c4c55d42c0844138f9d5534d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Evandro=20Leopoldino=20Gon=C3=A7alves?= Date: Thu, 6 May 2021 18:57:32 +0200 Subject: [PATCH 2/2] flat array using iterative solution --- packages/array-flatten/index.js | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/packages/array-flatten/index.js b/packages/array-flatten/index.js index 7610b4f69..78f053369 100644 --- a/packages/array-flatten/index.js +++ b/packages/array-flatten/index.js @@ -6,20 +6,21 @@ module.exports = flatten; */ function flattenHelper(arr, depth) { + var stack = arr.slice(); var result = []; - var len = arr.length; - for (var i = 0; i < len; i++) { - var elem = arr[i]; + while (stack.length) { + var item = stack.pop(); - if (Array.isArray(elem) && depth > 0) { - result.push.apply(result, flattenHelper(elem, depth - 1)); + if (Array.isArray(item) && depth > 0) { + stack.push.apply(stack, item); + depth--; } else { - result.push(elem); + result.push(item); } } - return result; + return result.reverse(); } function flatten(arr, depth) { @@ -31,7 +32,5 @@ function flatten(arr, depth) { throw new Error('depth expects a number'); } - var optionDepth = typeof depth === 'number' ? depth : Infinity; - - return flattenHelper(arr, optionDepth); + return flattenHelper(arr, typeof depth === 'number' ? depth : Infinity); }