1.1.33 • Published 6 months ago

extra-function v1.1.33

Weekly downloads
5
License
MIT
Repository
github
Last release
6 months ago

A function is a set of statements that performs a task or calculates a value. 📦 Node.js, 🌐 Web, 📜 Files, 📰 JSDoc, 📘 Wiki.

Functions are one of the fundamental building blocks in JavaScript. It is similar to a procedure—a set of statements that performs a task or calculates a value (1). It can accept some parameters, and may define some local variables necessary for performing the desired operation. These parameters and local variables are contained in the scope of the function, and cannot be accessed from the outside. However, a function can access variables external to itself (as long as they are not overridden), such as global variables or variables in the scope this function is encapsulated in. A nested function can access free variables that are defined in outer scopes, even when it is being invoked away from where it is defined. It does this by capturing the free variables (by reference) into a closure (1). Closure, arrow functions, and more, are functional language features based on lambda calculus.

This package includes a number of methods to transform functions. This enables you to obtain a desired function by transforming the behavior of existing functions (without actually executing them). The result of a function can be manipulated with negate. In case a pure function is expensive, its results can cached with memoize. Parameters of a function can be manipulated with reverse, spread, unspread. reverse flips the order of parameters, spread spreads the first array parameter of a function, and unspread combines all parameters into the first parameter (array). If you want some functional behavior, compose, composeRight, curry, and curryRight can be used. composeRight is also known as pipe-forward operator or function chaining. If you are unfamiliar, Haskell is a great purely functional language, and there is great haskell beginner guide to learn from.

To control invocation time of a function, use delay. A function can be rate controlled with restrict, debounce, debounceEarly, throttle, throttleEarly. debounce and debounceEarly prevent the invocation of a function during hot periods (when there are too many calls), and can be used for example to issue AJAX request after user input has stopped (for certain delay time). throttle and throttleEarly can be used to limit the rate of invocation of a function, and can be used for example to minimize system usage when a user is constantly refreshing a webpage. Except restrict, all rate/time control methods can be flushed (flush()) to invoke the target function immediately, or cleared (clear()) to disable invocation of the target function.

In addition, is, isAsync, isGenerator, name, and length obtain metadata (about) information on a function. To attach a this to a function, use bind. A few generic functions are also included: NOOP, FALSE, TRUE, IDENTITY, COMPARE, ARGUMENTS.

This package is available in Node.js and Web formats. To use it on the web, simply use the extra_function global variable after loading with a <script> tag from the jsDelivr CDN.

Stability: Experimental.

const xfunction = require('extra-function');
// import * as xfunction from "extra-function";
// import * as xfunction from "https://unpkg.com/extra-function/index.mjs"; (deno)

var a = xfunction.composeRight(x => x*x, x => x+2);
a(10);
// → 102

var a = xfunction.curry((x, y) => x+y);
a(2)(3);
// → 7

var a = xfunction.unspread(Math.max);
a([2, 3, 1]);
// → 1.25

Index

PropertyDescription
NOOPDo nothing.
FALSEReturn false.
TRUEReturn false.
IDENTITYReturn the same (first) value.
COMPARECompare two values.
ARGUMENTSReturn the arguments passed as a array.
nameGet the name of a function.
lengthGet the number of parameters of a function.
bindBind this-object, and optional prefix arguments to a function.
callInvoke a function with specified this-object, and arguments provided individually.
applyInvoke a function with specified this-object, and arguments provided as an array.
isCheck if value is a function.
isAsyncCheck if value is an async function.
isGeneratorCheck if value is a generator function.
contextifyContextify a function by accepting the first parameter as this-object.
decontextifyDecontextify a function by accepting this-object as the first argument.
negateGenerate a result-negated version of a function.
memoizeGenerate result-cached version of a function.
reverseGenerate a parameter-reversed version of a function.
spreadGenerate a (first) parameter-spreaded version of a function.
unspreadGenerate a (first) parameter-collapsed version of a function.
attachAttach prefix arguments to leftmost parameters of a function.
attachRightAttach suffix arguments to rightmost parameters of a function.
composeCompose functions together, in applicative order.
composeRightCompose functions together, such that result is piped forward.
curryGenerate curried version of a function.
curryRightGenerate right-curried version of a function.
deferGenerate deferred version of a function, that executes after the current stack has cleared.
delayGenerate delayed version of a function.
restrictGenerate restricted-use version of a function.
restrictOnceRestrict a function to be used only once.
restrictBeforeRestrict a function to be used only upto a certain number of calls.
restrictAfterRestrict a function to be used only after a certain number of calls.
debounceGenerate debounced version of a function.
debounceEarlyGenerate leading-edge debounced version of a function.
throttleGenerate throttled version of a function.
throttleEarlyGenerate leading-edge throttled version of a function.

References

npm.io ORG DOI Coverage Status Test Coverage Maintainability

1.1.33

6 months ago

1.1.29

1 year ago

1.1.28

1 year ago

1.1.30

1 year ago

1.1.32

1 year ago

1.1.31

1 year ago

1.1.27

1 year ago

1.1.23

1 year ago

1.1.22

1 year ago

1.1.21

1 year ago

1.1.26

1 year ago

1.1.25

1 year ago

1.1.24

1 year ago

1.1.12

1 year ago

1.1.11

1 year ago

1.1.16

1 year ago

1.1.15

1 year ago

1.1.14

1 year ago

1.1.13

1 year ago

1.1.19

1 year ago

1.1.18

1 year ago

1.1.17

1 year ago

1.1.20

1 year ago

1.1.9

2 years ago

1.1.10

2 years ago

1.1.0

2 years ago

1.1.8

2 years ago

1.1.7

2 years ago

1.1.6

2 years ago

1.1.5

2 years ago

1.1.4

2 years ago

1.1.3

2 years ago

1.0.19

2 years ago

1.0.2

2 years ago

1.0.18

2 years ago

1.0.1

2 years ago

1.0.17

2 years ago

1.0.0

2 years ago

0.1.10

2 years ago

1.0.16

2 years ago

0.1.11

2 years ago

0.1.12

2 years ago

1.0.9

2 years ago

1.0.8

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.22

2 years ago

1.0.21

2 years ago

1.0.20

2 years ago

1.0.23

2 years ago

0.1.0

2 years ago

0.1.2

2 years ago

0.1.1

2 years ago

0.1.8

2 years ago

0.1.7

2 years ago

0.1.9

2 years ago

0.1.4

2 years ago

0.1.3

2 years ago

0.1.6

2 years ago

0.1.5

2 years ago

1.0.11

2 years ago

1.0.10

2 years ago

1.0.15

2 years ago

1.0.14

2 years ago

1.0.13

2 years ago

1.0.12

2 years ago

0.0.52

2 years ago

0.0.51

3 years ago

0.0.50

4 years ago

0.0.49

4 years ago

0.0.48

4 years ago

0.0.47

4 years ago

0.0.46

4 years ago

0.0.45

4 years ago

0.0.44

4 years ago

0.0.42

4 years ago

0.0.43

4 years ago

0.0.41

4 years ago

0.0.40

4 years ago

0.0.38

4 years ago

0.0.39

4 years ago

0.0.37

4 years ago

0.0.36

4 years ago

0.0.35

4 years ago

0.0.34

4 years ago

0.0.33

4 years ago

0.0.30

4 years ago

0.0.31

4 years ago

0.0.32

4 years ago

0.0.28

4 years ago

0.0.29

4 years ago

0.0.27

4 years ago

0.0.26

4 years ago

0.0.25

4 years ago

0.0.24

4 years ago

0.0.23

4 years ago

0.0.21

4 years ago

0.0.22

4 years ago

0.0.20

4 years ago

0.0.17

4 years ago

0.0.18

4 years ago

0.0.19

4 years ago

0.0.16

4 years ago

0.0.15

4 years ago

0.0.11

4 years ago

0.0.12

4 years ago

0.0.14

4 years ago

0.0.10

4 years ago

0.0.9

4 years ago

0.0.8

4 years ago

0.0.7

4 years ago

0.0.5

4 years ago

0.0.6

4 years ago

0.0.3

4 years ago

0.0.2

4 years ago

0.0.4

4 years ago

0.0.1

4 years ago