Skip to content

Commit

Permalink
Migrate to TypeScript (#339)
Browse files Browse the repository at this point in the history
* Migrate to TypeScript

* Add .codebeatignore

* Remove demo folder

* Update README

* Add tests

* Fix ESLint config

* Remove unused file
  • Loading branch information
scriptex committed Nov 28, 2022
1 parent 48755d9 commit 5066c0e
Show file tree
Hide file tree
Showing 20 changed files with 1,686 additions and 1,918 deletions.
28 changes: 28 additions & 0 deletions .codebeatsettings
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"TYPESCRIPT": {
"ABC": [
20,
40,
80,
120
],
"LOC": [
50,
80,
120,
160
],
"TOTAL_LOC": [
400,
500,
640,
900
],
"BLOCK_NESTING": [
6,
8,
10,
12
]
}
}
17 changes: 17 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"env": {
"node": true,
"browser": true
},
"extends": ["prettier", "plugin:@typescript-eslint/recommended"],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"project": "tsconfig-compile.json",
"sourceType": "module"
},
"plugins": ["@typescript-eslint"],
"ignorePatterns": ["*.js", "*.mjs", "dist/*"],
"rules": {
"@typescript-eslint/no-this-alias": "off"
}
}
13 changes: 4 additions & 9 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -1,21 +1,14 @@
# This workflow will do a clean install of node dependencies, build the source code and run tests across different versions of node
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions

name: Build

on:
push:
branches: [master]
pull_request:
branches: [master]
on: [push, pull_request]

jobs:
build:
runs-on: ubuntu-latest

strategy:
matrix:
node-version: [12.x, 14.x, 16.x]
node-version: [18.x]

steps:
- uses: actions/checkout@v3
Expand All @@ -24,4 +17,6 @@ jobs:
with:
node-version: ${{ matrix.node-version }}
- run: yarn
- run: yarn lint
- run: yarn test
- run: yarn build
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@ node_modules/
.Trashes
ehthumbs.db
Thumbs.db

dist
41 changes: 21 additions & 20 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ yarn-debug.log*
yarn-error.log*

# Dependency directory
node_modules/
node_modules

# Misc
.DS_Store
Expand All @@ -17,27 +17,28 @@ node_modules/
ehthumbs.db
Thumbs.db

# EditorConfig
# Config folders and files
.github
_config.yml
_.config.yml
.codebeatsettings
.editorconfig

# Git
.gitignore
.eslintrc
.gitattributes

# CI
.travis.yml

# lock files
yarn.lock

# Prettier
.prettierrc
.gitignore
.nvmrc
.prettierignore

# Config
.babelrc
.github
.prettierrc
.stylelintignore
.stylelintrc
.travis.yml
.whitesource
renovate.json
rollup.config.js
webpack.config.js
tsconfig.json
tsconfig-compile.json
tslint.json
yarn.lock

!dist
demo
rollup.config.mjs
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ node_js:
install:
- yarn
script:
- yarn lint
- yarn test
- yarn build
12 changes: 6 additions & 6 deletions .whitesource
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"checkRunSettings": {
"vulnerableCheckRunConclusionLevel": "success"
},
"issueSettings": {
"minSeverityLevel": "LOW"
}
"generalSettings": {
"shouldScanRepo": true
},
"checkRunSettings": {
"vulnerableCheckRunConclusionLevel": "success"
}
}
19 changes: 1 addition & 18 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
[![GitHub release](https://img.shields.io/github/release/three11/debounce.svg)](https://github.com/three11/debounce/releases/latest)
[![GitHub issues](https://img.shields.io/github/issues/three11/debounce.svg)](https://github.com/three11/debounce/issues)
[![GitHub last commit](https://img.shields.io/github/last-commit/three11/debounce.svg)](https://github.com/three11/debounce/commits/master)
[![Github file size](https://img.shields.io/github/size/three11/debounce/dist/debounce.min.js.svg)](https://github.com/three11/debounce/)
[![Build Status](https://travis-ci.org/three11/debounce.svg?branch=master)](https://travis-ci.org/three11/debounce)
[![npm](https://img.shields.io/npm/dt/@three11/debounce.svg)](https://www.npmjs.com/package/@three11/debounce)
[![npm](https://img.shields.io/npm/v/@three11/debounce.svg)](https://www.npmjs.com/package/@three11/debounce)
[![Analytics](https://ga-beacon.appspot.com/UA-83446952-1/github.com/three11/debounce/README.md)](https://github.com/three11/debounce/)

# Debounce

Debounce multiple function executions
> Debounce multiple function executions
## Install

Expand All @@ -23,22 +22,6 @@ or
yarn add @three11/debounce
```

or

Just download this repository and link the files located in dist folder:

```html
<script src="path-to-debounce/dist/debounce.min.js"></script>
```

or

Include it from Unpkg CDN

```html
<script src="//unpkg.com/@three11/debounce/dist/debounce.min.js"></script>
```

## Usage

First, import the module:
Expand Down
14 changes: 14 additions & 0 deletions _.config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
plugins:
- jekyll-relative-links
relative_links:
enabled: true
collections: true
include:
- CONTRIBUTING.md
- README.md
- LICENSE.md
- COPYING.md
- CODE_OF_CONDUCT.md
- CONTRIBUTING.md
- ISSUE_TEMPLATE.md
- PULL_REQUEST_TEMPLATE.md
2 changes: 1 addition & 1 deletion _config.yml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
theme: jekyll-theme-slate
theme: jekyll-theme-slate
1 change: 0 additions & 1 deletion dist/debounce.min.js

This file was deleted.

54 changes: 31 additions & 23 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
{
"name": "@three11/debounce",
"version": "1.0.0",
"version": "1.1.0",
"description": "Debounce multiple function executions",
"main": "dist/debounce.min.js",
"scripts": {
"build": "rollup -c"
},
"repository": {
"type": "git",
"url": "git+https://github.com/three11/debounce.git"
},
"keywords": [
"Debounce",
"Function",
"Wait",
"JavaScript",
"ES2017"
"Function delay",
"Function wait"
],
"homepage": "https://github.com/three11/debounce#readme",
"bugs": {
"url": "https://github.com/three11/debounce/issues"
},
"license": "GPL-3.0",
"authors": [
{
"name": "Three 11 Ltd",
Expand All @@ -34,17 +29,30 @@
"role": "Developer"
}
],
"license": "GPL-3.0",
"bugs": {
"url": "https://github.com/three11/debounce/issues"
"main": "dist/debounce.js",
"types": "dist/debounce.d.ts",
"repository": {
"type": "git",
"url": "git+https://github.com/three11/debounce.git"
},
"homepage": "https://github.com/three11/debounce#readme",
"scripts": {
"lint": "eslint 'src/**/*.ts'",
"test": "yarn build && node --experimental-specifier-resolution=node --loader ts-node/esm tests/index.mts",
"build": "rollup -c"
},
"dependencies": {},
"devDependencies": {
"@babel/cli": "7.19.3",
"@babel/core": "7.20.2",
"@babel/preset-env": "7.20.2",
"rollup": "2.79.1",
"rollup-plugin-babel": "4.4.0",
"rollup-plugin-terser": "7.0.2"
"@rollup/plugin-commonjs": "23.0.2",
"@rollup/plugin-typescript": "9.0.2",
"@types/node": "18.11.9",
"@types/tape": "4.13.2",
"@typescript-eslint/eslint-plugin": "5.44.0",
"@typescript-eslint/parser": "5.44.0",
"eslint": "8.28.0",
"eslint-config-prettier": "8.5.0",
"rollup": "3.4.0",
"tape": "5.6.1",
"ts-node": "10.9.1",
"typescript": "4.9.3"
}
}
19 changes: 0 additions & 19 deletions rollup.config.js

This file was deleted.

19 changes: 19 additions & 0 deletions rollup.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import commonjs from '@rollup/plugin-commonjs';
import typescript from '@rollup/plugin-typescript';

export default {
input: 'src/debounce.ts',
output: {
dir: 'dist',
name: 'debounce',
format: 'umd',
exports: 'named',
sourcemap: true
},
plugins: [
typescript({
tsconfig: './tsconfig-compile.json'
}),
commonjs()
]
};
28 changes: 0 additions & 28 deletions src/debounce.js

This file was deleted.

29 changes: 29 additions & 0 deletions src/debounce.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
export const debounce = function <T extends (...args: unknown[]) => void>(
fn: T,
delay = 15,
isImmediate = false
): <C>(this: C, ...rest: Parameters<T>) => void {
let timeout: ReturnType<typeof setTimeout> | null;

return function <C>(this: C, ...args: Parameters<T>) {
const context = this;

if (isImmediate && !timeout) {
fn.apply(context, args);
}

if (typeof timeout === 'number') {
clearTimeout(timeout);
}

timeout = setTimeout(() => {
timeout = null;

if (!isImmediate) {
fn.apply(context, args);
}
}, delay);
};
};

export default debounce;
Loading

0 comments on commit 5066c0e

Please sign in to comment.