debounce-fn
Advanced tools
Comparing version 0.1.0 to 1.0.0
59
index.js
@@ -1,23 +0,46 @@ | ||
module.exports = debounce; | ||
'use strict'; | ||
const mimicFn = require('mimic-fn'); | ||
function debounce (fn, wait) { | ||
var timer; | ||
var args; | ||
module.exports = (fn, options) => { | ||
if (typeof fn !== 'function') { | ||
throw new TypeError(`Expected the first argument to be a function, got \`${typeof fn}\``); | ||
} | ||
if (arguments.length == 1) { | ||
wait = 250; | ||
} | ||
options = options || {}; | ||
return function () { | ||
if (timer != undefined) { | ||
clearTimeout(timer); | ||
timer = undefined; | ||
} | ||
let timeout; | ||
let result; | ||
args = arguments; | ||
const debounced = function () { | ||
const context = this; | ||
const args = arguments; | ||
timer = setTimeout(function () { | ||
fn.apply(undefined, args); | ||
}, wait); | ||
}; | ||
} | ||
const later = () => { | ||
timeout = null; | ||
if (!options.immediate) { | ||
result = fn.apply(context, args); | ||
} | ||
}; | ||
const callNow = options.immediate && !timeout; | ||
clearTimeout(timeout); | ||
timeout = setTimeout(later, options.wait || 0); | ||
if (callNow) { | ||
result = fn.apply(context, args); | ||
} | ||
return result; | ||
}; | ||
mimicFn(debounced, fn); | ||
debounced.cancel = () => { | ||
if (timeout) { | ||
clearTimeout(timeout); | ||
timeout = null; | ||
} | ||
}; | ||
return debounced; | ||
}; |
{ | ||
"name": "debounce-fn", | ||
"version": "0.1.0", | ||
"description": "Return a debounced version of the given function", | ||
"main": "index.js", | ||
"version": "1.0.0", | ||
"description": "Debounce a function", | ||
"license": "MIT", | ||
"repository": "sindresorhus/debounce-fn", | ||
"author": { | ||
"name": "Sindre Sorhus", | ||
"email": "sindresorhus@gmail.com", | ||
"url": "sindresorhus.com" | ||
}, | ||
"engines": { | ||
"node": ">=4" | ||
}, | ||
"scripts": { | ||
"test": "node test" | ||
"test": "xo && ava" | ||
}, | ||
"files": [ | ||
"index.js" | ||
], | ||
"keywords": [ | ||
"debounce", | ||
"debounced" | ||
"function", | ||
"debouncer", | ||
"fn", | ||
"func", | ||
"throttle", | ||
"delay", | ||
"invoked" | ||
], | ||
"repository": { | ||
"url": "git@github.com:azer/debounce-fn.git", | ||
"type": "git" | ||
"dependencies": { | ||
"mimic-fn": "^1.1.0" | ||
}, | ||
"author": "azer", | ||
"license": "BSD", | ||
"devDependencies": { | ||
"prova": "^1.14.0" | ||
"ava": "*", | ||
"delay": "^2.0.0", | ||
"xo": "*" | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Misc. License Issues
License(Experimental) A package's licensing information has fine-grained problems.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
3945
0
35
1
65
1
3
1
1
+ Addedmimic-fn@^1.1.0
+ Addedmimic-fn@1.2.0(transitive)