funcs-js
v2.0.0
Published
Function wrappers for enhanced behavior.
Downloads
1,101
Maintainers
Readme
funcs-js
Function wrappers for enhanced behavior.
Overview
The funcs-js provides different utility functions which enable to wrap functions and modify their behavior.
This library requires no external dependencies, and it is supported for both browser and node.js environments.
Usage
In order to use the library in browser, you first must add the relevant dependency:
<script type="text/javascript" src="funcs.js"></script>
When using an AMD loader (such as RequireJS) or CommonJS type loader, the funcs object is not automatically defined on the window scope.
For node.js, simply require the module as follows:
var funcs = require('funcs-js');
'funcs.once(fn, [options]) ⇒ function'
Ensures the provided function is invoked only once. This is the same as calling funcs.maxTimes(fn, 1) This function output can be chained with other funcs apis.
Example
const onlyOnceCallback = funcs.once(callback);
//can also chain multiple modifications (chained functions do not require original function as argument)
const asyncOnceCallback = funcs.once(callback).async();
'funcs.maxTimes(fn, times, [options]) ⇒ function'
Wraps the provided function and ensures it is invoked no more than the provided amount. This function output can be chained with other funcs apis.
Example
const onlyOnceCallback = funcs.maxTimes(callback, 1);
//can also chain multiple modifications (chained functions do not require original function as argument)
const delayedMaxTimesCallback = funcs.maxTimes(callback, 5).delay(500);
'funcs.async(fn, [options]) ⇒ function'
Ensures the function is invoked only in the next cycle. This is the same as calling funcs.delay(fn, 0) This function output can be chained with other funcs apis.
Example
const asyncCallback = funcs.async(callback);
//can also chain multiple modifications (chained functions do not require original function as argument)
const asyncOnceCallback = funcs.async(callback).once();
'funcs.delay(fn, [delay], [options]) ⇒ function'
Trigger the actual function only after the provided delay. This function output can be chained with other funcs apis.
Example
const delayedCallback = funcs.delay(callback, 500);
//can also chain multiple modifications (chained functions do not require original function as argument)
const delayedMaxTimesCallback = funcs.delay(callback, 500).maxTimes(5);
'funcs.isFunction([fn]) ⇒ Boolean'
Returns true if the provided argument is a function.
Example
const isFn = funcs.isFunction(myFunction);
funcs.isFunction(function () {}); //true
funcs.isFunction(); //false
funcs.isFunction(5); //false
funcs.isFunction(true); //false
'funcs.noop() ⇒ undefined'
Empty function.
'funcs.ensure([fn]) ⇒ function'
Ensures a return function. If a function is provided, it will be returned, otherwise a noop function will be returned.
Example
const handler = funcs.ensure(maybeHandler);
Installation
Run npm install in your project as follows:
npm install --save funcs-js
Or if you are using bower, you can install it as follows:
bower install funcs-js --save
API Documentation
See full docs at: API Docs
Contributing
Release History
| Date | Version | Description | | ----------- | ------- | ----------- | | 2020-05-12 | v2.0.0 | Migrate to github actions, upgrade minimal node version and remove bower | | 2019-02-08 | v1.0.18 | Maintenance | | 2017-04-28 | v1.0.1 | Offical release | | 2017-04-28 | v0.0.6 | Docs | | 2017-04-28 | v0.0.5 | Rename package as funcs-js | | 2017-04-27 | v0.0.1 | Initial release |
License
Developed by Sagie Gur-Ari and licensed under the Apache 2 open source license.