Skip to content

Commit

Permalink
Require Node.js 18 and modern browsers
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Nov 15, 2023
1 parent 2b78c0d commit 2872fb8
Show file tree
Hide file tree
Showing 16 changed files with 330 additions and 452 deletions.
12 changes: 12 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
root = true

[*]
indent_style = tab
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.yml]
indent_style = space
indent_size = 2
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text=auto eol=lf
5 changes: 2 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
components
build
node_modules
node_modules
yarn.lock
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
13 changes: 0 additions & 13 deletions CONTRIBUTORS

This file was deleted.

55 changes: 0 additions & 55 deletions History.md

This file was deleted.

22 changes: 6 additions & 16 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,21 +1,11 @@
MIT License

Copyright (c) 2012-2018 The Debounce Contributors. See CONTRIBUTORS.
Coprighht (c) Jeremy Ashkenas, Julian Gonggrijp, DocumentCloud, Investigative Reporters & Editors
Copyright (c) Ben Carpenter, Billy Moon, Josh Goldberg, Julian Gruber, Kristofer Selbekk, Matthew Mueller, Nathan Rajlich, Oleg Pudeyev, Stephen Mathieson, TJ Holowaychuk, suhaotian, ven
Copyright (c) Sindre Sorhus <[email protected]> (https://sindresorhus.com)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
11 changes: 0 additions & 11 deletions Makefile

This file was deleted.

69 changes: 0 additions & 69 deletions Readme.md

This file was deleted.

18 changes: 0 additions & 18 deletions component.json

This file was deleted.

22 changes: 22 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
type AnyFunction = (...arguments_: readonly any[]) => unknown;

/**
Returns a function, that, as long as it continues to be invoked, will not be triggered. The function will be called after it stops being called for N milliseconds.
If `immediate` is passed, trigger the function on the leading edge, instead of the trailing. The function also has a property 'clear' that is a function which will clear the timer to prevent previously scheduled executions.
*/
declare function debounce<F extends AnyFunction>(
function_: F,
wait?: number,
immediate?: boolean
): debounce.DebouncedFunction<F>;

declare namespace debounce {
type DebouncedFunction<F extends AnyFunction> = {
(...arguments_: Parameters<F>): ReturnType<F> | undefined;
clear(): void;
flush(): void;
};
}

export = debounce;
143 changes: 74 additions & 69 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,73 +1,78 @@
/**
* Returns a function, that, as long as it continues to be invoked, will not
* be triggered. The function will be called after it stops being called for
* N milliseconds. If `immediate` is passed, trigger the function on the
* leading edge, instead of the trailing. The function also has a property 'clear'
* that is a function which will clear the timer to prevent previously scheduled executions.
*
* @source underscore.js
* @see http://unscriptable.com/2009/03/20/debouncing-javascript-methods/
* @param {Function} func function to wrap
* @param {number} [wait=100] time to wait in ms (`100`)
* @param {boolean} [immediate=false] should execute at the beginning (`false`)
* @api public
*/
function debounce(func, wait, immediate){
var timeout, args, context, timestamp, result;
if (null == wait) wait = 100;

function later() {
var last = Date.now() - timestamp;

if (last < wait && last >= 0) {
timeout = setTimeout(later, wait - last);
} else {
timeout = null;
if (!immediate) {
var callContext = context, callArgs = args
context = args = null;
result = func.apply(callContext, callArgs);
}
}
};

var debounced = function(){
context = this;
args = arguments;
timestamp = Date.now();
var callNow = immediate && !timeout;
if (!timeout) timeout = setTimeout(later, wait);
if (callNow) {
var callContext = context, callArgs = args
context = args = null;
result = func.apply(callContext, callArgs);
}

return result;
};

debounced.clear = function() {
if (timeout) {
clearTimeout(timeout);
timeout = null;
}
};

debounced.flush = function() {
if (timeout) {
var callContext = context, callArgs = args;
context = args = null;
result = func.apply(callContext, callArgs);

clearTimeout(timeout);
timeout = null;
}
};

return debounced;
};
function debounce(function_, wait = 100, immediate) {
let storedContext;
let storedArguments;
let timeoutId;
let timestamp;
let result;

function later() {
const last = Date.now() - timestamp;

if (last < wait && last >= 0) {
timeoutId = setTimeout(later, wait - last);
} else {
timeoutId = undefined;

if (!immediate) {
const callContext = storedContext;
const callArguments = storedArguments;
storedContext = undefined;
storedArguments = undefined;
result = function_.apply(callContext, callArguments);
}
}
}

const debounced = function (...arguments_) {
storedContext = this; // eslint-disable-line unicorn/no-this-assignment
storedArguments = arguments_;
timestamp = Date.now();

const callNow = immediate && !timeoutId;

if (!timeoutId) {
timeoutId = setTimeout(later, wait);
}

if (callNow) {
const callContext = storedContext;
const callArguments = storedArguments;
storedContext = undefined;
storedArguments = undefined;
result = function_.apply(callContext, callArguments);
}

return result;
};

debounced.clear = () => {
if (!timeoutId) {
return;
}

clearTimeout(timeoutId);
timeoutId = undefined;
};

debounced.flush = () => {
if (!timeoutId) {
return;
}

const callContext = storedContext;
const callArguments = storedArguments;
storedContext = undefined;
storedArguments = undefined;
result = function_.apply(callContext, callArguments);

clearTimeout(timeoutId);
timeoutId = undefined;
};

return debounced;
}

// Adds compatibility for ES modules
debounce.debounce = debounce;
module.exports.debounce = debounce;

module.exports = debounce;
Loading

0 comments on commit 2872fb8

Please sign in to comment.