Skip to content

Commit

Permalink
Require Node.js 12 and move to ESM (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
lgrammel committed Jan 27, 2021
1 parent c4cf1b5 commit e07dc3f
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 51 deletions.
1 change: 0 additions & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ jobs:
node-version:
- 14
- 12
- 10
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
Expand Down
74 changes: 36 additions & 38 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,40 @@
declare namespace debounceFn {
interface Options {
/**
Time to wait until the `input` function is called.
export interface Options {
/**
Time to wait until the `input` function is called.
@default 0
*/
readonly wait?: number;
@default 0
*/
readonly wait?: number;

/**
Trigger the function on the leading edge of the `wait` interval.
/**
Trigger the function on the leading edge of the `wait` interval.
For example, this can be useful for preventing accidental double-clicks on a "submit" button from firing a second time.
For example, this can be useful for preventing accidental double-clicks on a "submit" button from firing a second time.
@default false
*/
readonly before?: boolean;
@default false
*/
readonly before?: boolean;

/**
Trigger the function on the trailing edge of the `wait` interval.
/**
Trigger the function on the trailing edge of the `wait` interval.
@default true
*/
readonly after?: boolean;
}
@default true
*/
readonly after?: boolean;
}

interface BeforeOptions extends Options {
readonly before: true;
}
export interface BeforeOptions extends Options {
readonly before: true;
}

interface NoBeforeNoAfterOptions extends Options {
readonly after: false;
readonly before?: false;
}
export interface NoBeforeNoAfterOptions extends Options {
readonly after: false;
readonly before?: false;
}

interface DebouncedFunction<ArgumentsType extends unknown[], ReturnType> {
(...arguments: ArgumentsType): ReturnType;
cancel(): void;
}
export interface DebouncedFunction<ArgumentsType extends unknown[], ReturnType> {
(...arguments: ArgumentsType): ReturnType;
cancel(): void;
}

/**
Expand All @@ -49,7 +47,7 @@ It comes with a `.cancel()` method to cancel any scheduled `input` function call
@example
```
import debounceFn = require('debounce-fn');
import debounceFn from 'debounce-fn';
window.onresize = debounceFn(() => {
// Do something on window resize
Expand All @@ -58,17 +56,17 @@ window.onresize = debounceFn(() => {
*/
declare function debounceFn<ArgumentsType extends unknown[], ReturnType>(
input: (...arguments: ArgumentsType) => ReturnType,
options: debounceFn.BeforeOptions
): debounceFn.DebouncedFunction<ArgumentsType, ReturnType>;
options: BeforeOptions
): DebouncedFunction<ArgumentsType, ReturnType>;

declare function debounceFn<ArgumentsType extends unknown[], ReturnType>(
input: (...arguments: ArgumentsType) => ReturnType,
options: debounceFn.NoBeforeNoAfterOptions
): debounceFn.DebouncedFunction<ArgumentsType, undefined>;
options: NoBeforeNoAfterOptions
): DebouncedFunction<ArgumentsType, undefined>;

declare function debounceFn<ArgumentsType extends unknown[], ReturnType>(
input: (...arguments: ArgumentsType) => ReturnType,
options?: debounceFn.Options
): debounceFn.DebouncedFunction<ArgumentsType, ReturnType | undefined>;
options?: Options
): DebouncedFunction<ArgumentsType, ReturnType | undefined>;

export = debounceFn;
export default debounceFn;
7 changes: 4 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';
const mimicFn = require('mimic-fn');
import mimicFn from 'mimic-fn';

module.exports = (inputFunction, options = {}) => {
const debounceFn = (inputFunction, options = {}) => {
if (typeof inputFunction !== 'function') {
throw new TypeError(`Expected the first argument to be a function, got \`${typeof inputFunction}\``);
}
Expand Down Expand Up @@ -52,3 +51,5 @@ module.exports = (inputFunction, options = {}) => {

return debouncedFunction;
};

export default debounceFn;
8 changes: 5 additions & 3 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import {expectType, expectError} from 'tsd';
import debounceFn = require('.');
import debounceFn, {DebouncedFunction, Options} from './index.js';

const stringToBoolean = (string: string) => true;

const options: debounceFn.Options = {};
const options: Options = {};
const debounced = debounceFn(stringToBoolean);
expectType<debounceFn.DebouncedFunction<[string], boolean | undefined>>(debounced);
expectType<DebouncedFunction<[string], boolean | undefined>>(debounced);
expectType<boolean | undefined>(debounced('foo'));
debounced.cancel();

Expand All @@ -20,5 +20,7 @@ expectError<boolean>(debounceFn(stringToBoolean, {after: true})('foo'));
expectType<boolean>(debounceFn(stringToBoolean, {before: true})('foo'));
expectType<boolean>(debounceFn(stringToBoolean, {before: true, after: true})('foo'));

// eslint-disable-next-line @typescript-eslint/no-confusing-void-expression
expectType<undefined>(debounceFn(stringToBoolean, {after: false})('foo'));
// eslint-disable-next-line @typescript-eslint/no-confusing-void-expression
expectError<boolean>(debounceFn(stringToBoolean, {after: false})('foo'));
10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
"email": "sindresorhus@gmail.com",
"url": "https://sindresorhus.com"
},
"type": "module",
"exports": "./index.js",
"engines": {
"node": ">=10"
"node": ">=12"
},
"scripts": {
"test": "xo && ava && tsd"
Expand All @@ -34,9 +36,9 @@
"mimic-fn": "^3.0.0"
},
"devDependencies": {
"ava": "^1.4.1",
"ava": "^3.15.0",
"delay": "^4.2.0",
"tsd": "^0.11.0",
"xo": "^0.26.1"
"tsd": "^0.14.0",
"xo": "^0.37.1"
}
}
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ $ npm install debounce-fn
## Usage

```js
const debounceFn = require('debounce-fn');
import debounceFn from 'debounce-fn';

window.onresize = debounceFn(() => {
// Do something on window resize
Expand Down
2 changes: 1 addition & 1 deletion test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import test from 'ava';
import delay from 'delay';
import debounceFn from '.';
import debounceFn from './index.js';

test('debounces a function', async t => {
let count = 0;
Expand Down

0 comments on commit e07dc3f

Please sign in to comment.