2.0.0 • Published 4 years ago

funcs-js v2.0.0

Weekly downloads
839
License
Apache-2.0
Repository
github
Last release
4 years ago

funcs-js

NPM Version CI Coverage Status Known Vulnerabilities Inline docs License Total Downloads

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

See contributing guide

Release History

DateVersionDescription
2020-05-12v2.0.0Migrate to github actions, upgrade minimal node version and remove bower
2019-02-08v1.0.18Maintenance
2017-04-28v1.0.1Offical release
2017-04-28v0.0.6Docs
2017-04-28v0.0.5Rename package as funcs-js
2017-04-27v0.0.1Initial release

License

Developed by Sagie Gur-Ari and licensed under the Apache 2 open source license.

2.0.0

4 years ago

1.0.18

5 years ago

1.0.17

6 years ago

1.0.16

6 years ago

1.0.15

6 years ago

1.0.14

6 years ago

1.0.13

7 years ago

1.0.12

7 years ago

1.0.11

7 years ago

1.0.10

7 years ago

1.0.9

7 years ago

1.0.8

7 years ago

1.0.7

7 years ago

1.0.6

7 years ago

1.0.5

7 years ago

1.0.4

7 years ago

1.0.3

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago

0.0.6

7 years ago

0.0.5

7 years ago

0.0.4

7 years ago

0.0.3

7 years ago