Utils.Funcs Class
Provides a whole bunch of utility functions
Item Index
Methods
binary
-
fn
Takes a function with a arity of N and transforms it into a arity of 2
Parameters:
-
fnFunctionThe function to decorate
Returns:
The decorated function
Example:
import {binary} from './utils/func';
var say = function (sentence, to) {
return sentence + ' ' + to;
}
say('Hi', 'Bob');
// -> 'Hi Bob'
var sayHi = binary(say)('Hi');
sayHi('Bob');
// -> 'Hi Bob'
bind
-
fn -
scope -
[partials]
Takes a function, a context and optional partial arguments and returns a function which is always executed in the given context
Parameters:
-
fnFunctionThe function to decorate
-
scopeObjectThe context of the function
-
[partials]optionalPartial arguments to preset
Returns:
The bound function
Example:
var person = {
name: 'John Doe',
sayHi: function () {
return 'Hi, my name is ' + this.name;
}
}
person.sayHi();
// -> 'Hi, my name is John Doe'
var personSaysHi = person.sayHi;
personSaysHi();
// -> Gobbledygokk
var boundSaysHi = Utils.bind(person.sayHi, person);
boundSaysHi();
// -> 'Hi, my name is John Doe'
compose
-
[fns*]
Takes one to N functions and returns a single function which executes all contained function in order. Please note that the given functions are executed from right-to-left. For the opposite behaviour just use the Utils.pipe function
Parameters:
-
[fns*]Function optionalOne to N functions to compose together
Returns:
Composition of functions
Example:
import {compose} from './utils/func';
var joinWithSpace = function (strArray) {
return strArray.join(' ');
}
var splitAtSpaces = function (str) {
return str.split(' ')
}
var firstUpperAll = function (strArray) {
return strArray.forEach(function (str) {
return str[0].toUpperCase() + str.slice(1);
});
}
var formatName = compose(
joinWithSpace,
firstUpperAll,
splitAtSpaces
);
formatName('john doe');
// -> 'John Doe'
curry
-
fn
Takes a function with a arity of two or more and returns a function, which takes the arguments one by one with subsequent invocations
Parameters:
-
fnFunctionThe function to curry
Returns:
The curried function
Example:
import {curry} from './utils/func';
var argsAsArray = function (a, b, c) {
return [a, b, c];
}
var argsAsArrayCurried = curry(argsAsArray);
argsAsArray(1);
// -> [1, undefined, undefined]
argsAsArrayCurried(1);
// -> function
argsAsArray(1)(2)(3);
// -> [1, 2, 3]
fluent
-
fn
Takes a function and returns a function which calls the given function in the current context and returns that context. This is useful to define fluent interfaces
Parameters:
-
fnFunctionThe function to decorate
Returns:
The fluent function
Example:
import {fluent} from './utils/func';
var person = {
name: 'John Doe',
formatName: function () {
this.name = this.name.toUpperCase();
}
}
person.formatName().name;
// -> Error, cannot read property name of undefined
var fluentPerson = {
name: 'John Doe',
formatName: fluent(function () {
this.name = this.name.toUpperCase();
})
}
fluentPerson.formatName().name;
// -> 'JOHN DOE'
given
-
predicate -
fn
Takes a predicate/guarding function and a normal function and returns a function, which tests incoming arguments with the predicate function and only executes the normal function, if the predicate returns true
Parameters:
-
predicateFunctionPredicate/guarding function
-
fnFunctionThe normal function
Returns:
A guarded function
Example:
import {given} from './utils/func';
import {isNumber} from './utils/type';
var squareNum = given(isNumber, (n) => n * n);
squareNum(2);
// -> 4
squareNum('abc');
// -> null
maybe
-
fn
Takes a function and returns a function which only executes if none of the incoming arguments is null or undefined. Otherwise returns null
Parameters:
-
fnFunctionThe function to maybe execute
Returns:
A function which is maybe executed
Example:
import {maybe} from './utils/func';
var maybeUpperCase = maybe((str) => str.toUpperCase());
maybeUpperCase(null);
// -> null, no Error!
maybeUpperCase('abc');
// -> 'ABC'
memoize
-
fn
Takes a function and returns a memoized version of it which immediatly returns a already calculated result if the incoming arguments have been used on any preceeding invocation
Parameters:
-
fnFunctionThe function to memoize
Returns:
A memoized function
Example:
import {memoize} from './utils/func';
var countInvocation = 0;
var doComplicatedStuff = function (initArg) {
countInvocation += 1;
// calculates complex stuff and returns it
}
var memoDoComplicatedStuff = memoize(doComplicatedStuff);
doComplicatedStuff('abc');
doComplicatedStuff('abc');
doComplicatedStuff('abc');
countInvocation;
// -> 3
countInvocation = 0;
memoDoComplicatedStuff('abc');
memoDoComplicatedStuff('abc');
memoDoComplicatedStuff('abc');
countInvocation;
// -> 1
multary
-
fn
Takes a function with a arity of N and transforms it into a variadic version, which takes a certain number of arguments (same as original) but collects the last and all following arguments into a array as last argument
Parameters:
-
fnFunctionThe function to decorate
Returns:
The decorated function
Example:
negate
-
fn
Takes a function and returns a function which itself returns the opposite boolean result of the original function
Parameters:
-
fnFunctionThe function to negate
Returns:
The negated function
Example:
import {not, negate} from './utils/func';
var truthy = function (x) {
return !!x;
}
var falsy = not(truthy); // or falsy = negate(truthy)
truthy(1);
// -> true
falsy(1);
// -> false
nullary
-
fn
Takes a function with a arity of N and transforms it into a arity of zero
Parameters:
-
fnFunctionThe function to decorate
Returns:
The decorated function
partial
-
fn -
[partials]
Takes a function and optional arguments and returns a partially applied function (meaning the already given arguments are predefined). Further calls return a new function until all needed arguments are given to execute the original function
Parameters:
-
fnFunctionThe function to apply partially
-
[partials]optionalZero to N arguments to predefine
Returns:
A partially applied function
Example:
import {partial} from './utils/func';
var argsAsArray = function (a, b, c) {
return [a, b, c];
}
var argsAsArrayPartial = partial(argsAsArray, undefined, 2, 3);
argsAsArray(1, 2, 3);
// -> [1, 2, 3]
argsAsArrayPartial(1, 5, 9);
// -> [1, 2, 3]
pipe
-
[fns*]
Takes one to N functions and returns a single function which executes all contained function in order. Please note that the given functions are executed from left-to-right. For the opposite behaviour just use the Utils.compose function
Parameters:
-
[fns*]Function optionalOne to N functions to pipe together
Returns:
Pipeline of functions
Example:
import {pipe} from './utils/func';
var joinWithSpace = function (strArray) {
return strArray.join(' ');
}
var firstUpperAll = function (strArray) {
return strArray.forEach(function (str) {
return str[0].toUpperCase() + str.slice(1);
});
}
var splitAtSpaces = function (str) {
return str.split(' ')
}
var formatName = pipe(
splitAtSpaces,
firstUpperAll,
joinWithSpace
);
formatName('john doe');
// -> 'John Doe'
quaternary
-
fn
Takes a function with a arity of N and transforms it into a arity of 4
Parameters:
-
fnFunctionThe function to decorate
Returns:
The decorated function
Example:
SEE Utils.binary EXAMPLE
ternary
-
fn
Takes a function with a arity of N and transforms it into a arity of 3
Parameters:
-
fnFunctionThe function to decorate
Returns:
The decorated function
Example:
import {ternary} from './utils/func';
var say = function (greet, sentence, to) {
var said = greet || '';
if (sentence) {
said += ' ' + sentence;
}
if (to) {
said += ' ' + to;
}
return said;
}
say('Hi', 'user', 'Bob');
// -> 'Hi user Bob'
var sayHi = ternary(say)('Hi');
sayHi('user', 'Bob'); // -> 'Hi user bob'
throttle
-
ms -
fn
Takes a function and returns a function which itself throttles the execution of the given function for the given amount of milliseconds after the last invocation of the returned function. This is super useful if you add continuous firing events (like resize) but want your event listener to only fire N milliseconds after the last invocation of the event
Parameters:
-
msNumberMilliseconds of throttling
-
fnFunctionThe function to throttle
Returns:
The throttled function
Example:
import {throttle} from './utils/func';
var onResize = throttle(250, function (event) {
// do something with the event
});
$(document.body).on('resize', onResize);
unary
-
fn
Takes a function with a arity of N and transforms it into a arity of one
Parameters:
-
fnFunctionThe function to decorate
Returns:
The decorated function
Example:
import {unary} from './utils/func';
var mapInt = function (nums) {
return nums.map(parseInt);
}
mapInt(['1', '2', '3', '4', '5']);
// -> [1, NaN, NaN, NaN, NaN]
var unaryMapInt = function (nums) {
return nums.map(unary(parseInt));
}
unaryMapInt(['1', '2', '3', '4', '5']);
// -> [1, 2, 3, 4, 5]
