API Docs for:
Show:

Utils.Funcs Class

Provides a whole bunch of utility functions

Methods

binary

(
  • fn
)
Function

Takes a function with a arity of N and transforms it into a arity of 2

Parameters:

  • fn Function

    The function to decorate

Returns:

Function:

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]
)
Function deprecated

Defined in development/js/utils/func.js:234

Deprecated: Use native Function.prototype.bind instead

Takes a function, a context and optional partial arguments and returns a function which is always executed in the given context

Parameters:

  • fn Function

    The function to decorate

  • scope Object

    The context of the function

  • [partials] optional

    Partial arguments to preset

Returns:

Function:

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*]
)
Function

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 optional

    One to N functions to compose together

Returns:

Function:

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
)
Function

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:

  • fn Function

    The function to curry

Returns:

Function:

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
)
Function

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:

  • fn Function

    The function to decorate

Returns:

Function:

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
)
Function

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:

  • predicate Function

    Predicate/guarding function

  • fn Function

    The normal function

Returns:

Function:

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
)
Function

Takes a function and returns a function which only executes if none of the incoming arguments is null or undefined. Otherwise returns null

Parameters:

  • fn Function

    The function to maybe execute

Returns:

Function:

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
)
Function

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:

  • fn Function

    The function to memoize

Returns:

Function:

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
)
Function deprecated

Defined in development/js/utils/func.js:202

Deprecated: Unneeded since ES6/ES2015, will be removed in future releases

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:

  • fn Function

    The function to decorate

Returns:

Function:

The decorated function

Example:

negate

(
  • fn
)
Function deprecated

Defined in development/js/utils/func.js:420

Deprecated: Use Utils.not instead

Takes a function and returns a function which itself returns the opposite boolean result of the original function

Parameters:

  • fn Function

    The function to negate

Returns:

Function:

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
)
Function

Takes a function with a arity of N and transforms it into a arity of zero

Parameters:

  • fn Function

    The function to decorate

Returns:

Function:

The decorated function

partial

(
  • fn
  • [partials]
)
Function

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:

  • fn Function

    The function to apply partially

  • [partials] optional

    Zero to N arguments to predefine

Returns:

Function:

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*]
)
Function

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 optional

    One to N functions to pipe together

Returns:

Function:

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
)
Function

Takes a function with a arity of N and transforms it into a arity of 4

Parameters:

  • fn Function

    The function to decorate

Returns:

Function:

The decorated function

Example:

SEE Utils.binary EXAMPLE

ternary

(
  • fn
)
Function

Takes a function with a arity of N and transforms it into a arity of 3

Parameters:

  • fn Function

    The function to decorate

Returns:

Function:

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
)
Function

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:

  • ms Number

    Milliseconds of throttling

  • fn Function

    The function to throttle

Returns:

Function:

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
)
Function

Takes a function with a arity of N and transforms it into a arity of one

Parameters:

  • fn Function

    The function to decorate

Returns:

Function:

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]