API Docs for:
Show:

Utils.Enums Class

Provides functions for enumerables (object/arrays)

Methods

each

(
  • list
  • fn
  • [scope]
)
Undefined

Allows to iterate over any enumerable with a function taking up to three arguments

Parameters:

  • list String | Array | Array-like | Object

    The enumerable

  • fn Function

    The iteration function

  • [scope] optional

    Optional context to execute the given function in

Returns:

Undefined:

This returns just nothing

Example:

import {each} from './utils/enums';

var colors = {red: '#f00', green: '#0f0'},
    result = '';

each(colors, function (colorValue, colorName, allColors) {
     result += colorName + ' has HEX of ' + colorValue + '<br>';
});

result;
// -> 'green has HEX of #0f0<br>red has HEX of #f00<br>'

every

(
  • list
  • fn
  • [scope]
)
Boolean

Evaluates each item in a enumerable against a predicate function. Returns false as soon as the first falsy evaluation is encountered. Does only return true if all items evaluate to true

Parameters:

  • list String | Array | Array-like | Object

    The enumerable

  • fn Function

    The iteration function

  • [scope] optional

    Optional context to execute the function in

Returns:

Boolean:

True if all evaluations are truthy, false otherwise

Example:

import {every} from './utils/enums';

var betweenTwoAndFive = function (n) {
    return n >= 2 && n <= 5;
};

var result = every([2, 3, 4], betweenTwoAndFive);

result;
// -> true

filter

(
  • list
  • fn
  • [scope]
)
Array

Allows to filter any enumerable with a function

Parameters:

  • list String | Array | Array-like | Object

    The enumerable

  • fn Function

    The iteration function

  • [scope] optional

    Optional context to execute the function in

Returns:

Array:

Filtered enumerable

Example:

import {filter} from './utils/enums';

var result = filter([1, 2, 3], function (n) {
    return n % 2 !== 0;
});

result;
// -> [1, 3]

find

(
  • list
  • fn
  • [scope]
)

Finds the first item in a enumerable from the left which evaluates truthy by applying a predicate to it

Parameters:

  • list String | Array | Array-like | Object

    The enumerable

  • fn Function

    The predicate function

  • [scope] optional

    Optional context to execute the predicate in

Returns:

:

First item from the left if available

Example:

import {find} from './utils/enums';

const startsWith = (x) => (y) => y[0] === x;

find(['a1', 'b1', 'a2', 'b2'], startsWith('b'));
// -> 'b1'

findLast

(
  • list
  • fn
  • [scope]
)

Finds the first item in a enumerable from the right which evaluates truthy by applying a predicate to it

Parameters:

  • list String | Array | Array-like | Object

    The enumerable

  • fn Function

    The predicate function

  • [scope] optional

    Optional context to execute the predicate in

Returns:

:

First item from the right if available

Example:

import {findLast} from './utils/enums';

const startsWith = (x) => (y) => y[0] === x;

findLast(['a1', 'b1', 'a2', 'b2'], startsWith('b'));
// -> 'b2'

indexOf

(
  • list
  • item
)
Number

Returns the index of a given item inside a enumerable from the left

Parameters:

  • list String | Array | Array-like | Object

    The enumerable

  • item

    The item to compare against

Returns:

Number:

Index or key, -1 if not in enumerable

Example:

import {indexOf} from './utils/enums';

var list = [1, 2, 3, 2, 1];

indexOf(list, 2);
// -> 1

lastIndexOf

(
  • list
  • item
)
Number

Returns the index of a given item inside a enumerable from the right

Parameters:

  • list String | Array | Array-like | Object

    The enumerable

  • item

    The item to compare against

Returns:

Number:

Index or key, -1 if not in enumerable

Example:

import {lastIndexOf} from './utils/enums';

var list = [1, 2, 3, 2, 1];

lastIndexOf(list, 2);
// -> 3

map

(
  • list
  • fn
  • [scope]
)
Array

Allows to map a function over a enumerable

Parameters:

  • list String | Array | Array-like | Object

    The enumerable

  • fn Function

    The iteration function

  • [scope] optional

    Optional context to execute the given function in

Returns:

Array:

The mapping

Example:

import {map} from './utils/enums';

var result = map([1, 2, 3], function (n) {
    return n * n;
});

result;
// -> [1, 4, 9]

reduce

(
  • list
  • fn
  • seed
  • [scope]
)

Allows to reduce or fold a enumerable into a single value

Parameters:

  • list String | Array | Array-like | Object

    The enumerable

  • fn Function

    The iteration function

  • seed

    Starting value

  • [scope] optional

    Optional context to execute the function in

Returns:

:

Depends on seed

Example:

import {reduce} from './utils/enums';

var add = function (a, b) { return a + b; };

var result = reduce(['hallo', ' ', 'welt'], add, '');

result;
// -> 'hallo welt'

reduceRight

(
  • list
  • fn
  • seed
  • [scope]
)

Allows to reduce or fold a enumerable into a single value from right

Parameters:

  • list String | Array | Array-like | Object

    The enumerable

  • fn Function

    The iteration function

  • seed

    Starting value

  • [scope] optional

    Optional context to execute the function in

Returns:

:

Depends on seed

Example:

import {reduceRight} from './utils/enums';

var add = function (a, b) { return a + b; };

var result = reduceRight(['hallo', ' ', 'welt'], div, '');

result;
// -> 'welt hallo'

some

(
  • list
  • fn
  • [scope]
)
Boolean

Evaluates each item in a enumerable against a predicate function until the first truthy evaluation is encountered, returns true if at least one item evaluates to true

Parameters:

  • list String | Array | Array-like | Object

    The enumerable

  • fn Function

    The iteration function

  • [scope] optional

    Optional scope to execute the function in

Returns:

Boolean:

True if one item evaluates to true, false otherwise

Example:

import {some} from './utils/enums';

var betweenTwoAndFive = function (n) {
    return n >= 2 && n <= 5;
};

var result = some([1, 3, 10], betweenTwoAndFive);

result;
// -> true