The function namespace of Bijou.js, containing functions to work with functions themselves, such as debouncing, throttling, memoizing, currying, timing and much more!
Methods
# static exports.composeFunction(…functions) → {function}
Composes two functions together. Read more here: https://www.codementor.io/@michelre/use-function-composition-in-javascript-gkmxos5mj
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
functions |
function
|
<repeatable> |
The functions to be composed. |
The composed function.
Example
const add2 = (x) => x + 2;
const multiply2 = (x) => x * 2;
console.log(_$.composeFunction(add2, multiply2)(3)) // 8 - i.e 3 * 2 + 2
# static exports.curryFunction(fn, arityopt) → {function}
Returns the curried version of a function. Read more here: https://medium.com/@abitoprakash/implementing-a-curry-function-in-javascript-6a249dbcb1bb
Parameters:
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
fn |
function
|
The function to curry. |
||
arity |
Number
|
<optional> |
fn.length | The arity (number of params) of the function to curry. {...*} [args] Optional arguments to pass to the function being curried. |
The curried version of the function.
Example
let fn = (x, y, z, w) => x * y * z * w;
console.log(_$.curryFunction(fn, 4, 5)(4)(3)(2)); // 120 i.e. 5 * 4 * 3 * 2
# static exports.debounce(func, wait, immediateopt) → {function}
Debounces a function so that it only runs after it has not been called for a certain amount of time.
Parameters:
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
func |
function
|
The function to throttle. |
||
wait |
Number
|
The milliseconds to wait between executions. |
||
immediate |
Boolean
|
<optional> |
false | Whether or not to run immediately, or after a group of executions. |
The debounced function.
Example
window.addEventListener("keyup", _$.debounce(expensiveFunction, 100));//Run the function expensiveFunction at most every 100ms.
# static exports.fastestFunction(fns, iterationsopt) → {Number}
Returns the index of the fastest function in an array of functions.
Parameters:
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
fns |
Array.<function()>
|
The array of functions to execute. |
||
iterations |
Number
|
<optional> |
1000 | How many times to execute the functions. (More is more reliable but takes longer.) |
The index of the fastest function in the array.
Number
Example
_$.fastestFunction([_$.uuid, () => _$.syntaxHighlight("<h1>Hello world</h1>", "html")]);//0, the first function.
# static exports.isAsync(val) → {Boolean}
Returns if the given function is async or not.
Parameters:
| Name | Type | Description |
|---|---|---|
val |
function
|
The function to test. |
True if the function is async and false if not.
Boolean
Example
const asyncFn = async (x) => x ** 3; // It's a silly function, but a good example
console.log(_$.isAsync(asyncFn)); // true
# static exports.juxt(…fns) → {juxtCallback}
Runs a list of functions with a list of arguments.
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
fns |
function
|
<repeatable> |
The functions to call. |
The function to run with the args.
Example
//It returns an array of outputs, each item in the base array is the output of one function, and each item in that array is the output for each argument.
_$.juxt(
x => x + 1,
x => x - 1,
x => x * 10
)(1, 2, 3); // [[2, 3, 4], [0, 1, 2], [10, 20, 30]]
# static exports.limitArgs(fn, n) → {function}
Limits the arguments that a given function takes to only the 1st n arguments.
Parameters:
| Name | Type | Description |
|---|---|---|
fn |
function
|
The function to call. |
n |
Number
|
The number of arguments to accept. |
The new function that only takes the 1st n arguments.
Example
//Now console can only log one item. How utterly useless but explanatory at the same time!
console.log = _$.limitArgs(console.log, 1);
# static exports.memoize(fn) → {function}
Memoizes a function, basically caching the result of past operations so that if the exact same thing is called again it will return the same value instantly.
Parameters:
| Name | Type | Description |
|---|---|---|
fn |
function
|
The function to memoize. |
The memoized function.
Example
let uuid = _$.memoize(() => _$.uuid()); // uuid will always return the same uuid. (Note that _$.uuid is already very fast - it can generate up to 10 million values in 20 seconds.)
# static exports.runAsync(fn) → {Promise}
Runs a function asynchronously in a web worker.
Parameters:
| Name | Type | Description |
|---|---|---|
fn |
function
|
The function to run |
A promise that resolves into the return value of the function.
Promise
Example
_$.runAsync(() => "hello world").then(console.log); // "hello world"
# static exports.sleep(ms) → {Promise}
Returns a promise after a specified number of milliseconds.
Parameters:
| Name | Type | Description |
|---|---|---|
ms |
Number
|
The milliseconds to sleep. |
Promise
Example
(async () => {
while (true){
document.body.innerHTML = (await _$.getJSON("https://time.jsontest.com")).time
await _$.sleep(60000);//Wait one minute then loop.
}
})
# static exports.spread(fn) → {spreadCallback}
Uses an array of arguments to make a function based on the one inputted.
Parameters:
| Name | Type | Description |
|---|---|---|
fn |
function
|
The function to use |
Example
var say = _$.spread(function(who, what) {
return who + ' says ' + what;
});
say(["Fred", "hi"]);//"Fred says hi"
# static exports.throttle(func, options, wait) → {function}
Only runs the input function at MAX with the delay specified.
Parameters:
| Name | Type | Description |
|---|---|---|
func |
function
|
The function to run. |
options |
Object.<Boolean>
|
The options. |
wait |
Number
|
The number of milliseconds to wait. |
The throttled function
Example
const alert_function = _$.throttle(() => {alert("hello")}, 5000)
setInterval(alert_function, 1)
# static exports.timeFunction(fn, nameopt) → {Object}
Times the function passed.
Parameters:
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
fn |
function
|
The function to run and time. |
||
name |
String
|
<optional> |
_$ function timer | The name of the timer |
An object with "time" and "function" properties, time being time in milliseconds, and function being the original function passed.
Object
Example
// Times how long it took the user to enter their name.
_$.timeFunction(() => prompt("What's your name?"));