Comparing version 5.0.0 to 5.1.0
{ | ||
"name": "utilsac", | ||
"version": "5.0.0", | ||
"version": "5.1.0", | ||
"description": "tools mostly", | ||
@@ -5,0 +5,0 @@ "main": "files.js", |
@@ -17,3 +17,4 @@ # Utilities and random js files | ||
``` | ||
export { | ||
import { | ||
createDebouncedFunction, | ||
createThrottledFunction, | ||
@@ -27,3 +28,16 @@ createCustomRound, | ||
timePromise | ||
}; | ||
``` | ||
} from "path.../utility.js"; | ||
``` | ||
## About | ||
### License | ||
[CC0](license.txt) | ||
### Related | ||
* [fnk](https://github.com/seanohue/fnk) | ||
* jQuery | ||
* lodash | ||
* ramda |
export { | ||
createDebouncedFunction, | ||
createThrottledFunction, | ||
@@ -12,8 +13,32 @@ createCustomRound, | ||
const createDebouncedFunction = function (functionToDebounce, waitTime) { | ||
/* creates a function that is de-bounced, | ||
calling it, will eventually execute it, when you stop calling it | ||
useful for scroll events, resize, search etc | ||
the returned function always return undefined | ||
TODO is debounced the correct word ? | ||
*/ | ||
let timeOutId = 0; | ||
return function(...args) { | ||
if (timeOutId > 0) { | ||
clearTimeout(timeOutId); | ||
timeOutId = 0; | ||
} | ||
timeOutId = setTimeout(function () { | ||
functionToDebounce(...args); | ||
}, waitTime); | ||
}; | ||
}; | ||
const createThrottledFunction = function (functionToThrottle, minimumTimeSpace) { | ||
/* creates a function that is throttled, | ||
calling it once will execute it immediately | ||
calling it very often during a period less than minimumTimeSpace will only execute it once | ||
the returned function always return undefined | ||
an alternative implementation could use Date.now() , this means less performance | ||
but would work for throttling inside a single tick | ||
but would work for throttling inside a single long tick | ||
*/ | ||
@@ -25,7 +50,8 @@ let ready = true; | ||
return function(...args) { | ||
if (ready) { | ||
ready = false; | ||
functionToThrottle(...args); | ||
setTimeout(makeReady, minimumTimeSpace); | ||
if (!ready) { | ||
return; | ||
} | ||
ready = false; | ||
functionToThrottle(...args); | ||
setTimeout(makeReady, minimumTimeSpace); | ||
}; | ||
@@ -46,12 +72,10 @@ }; | ||
return function (anyNumber) { | ||
const rest = anyNumber % precision; | ||
const rest = anyNumber % precision; | ||
if (rest === 0) { | ||
return anyNumber; | ||
} else { | ||
if (rest > halfPrecision) { | ||
return anyNumber + (precision - rest); | ||
} else { | ||
return anyNumber - rest; | ||
} | ||
} | ||
if (rest > halfPrecision) { | ||
return anyNumber + (precision - rest); | ||
} | ||
return anyNumber - rest; | ||
}; | ||
@@ -58,0 +82,0 @@ }; |
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
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
15252
219
41