Utils.Enums Class
Provides functions for enumerables (object/arrays)
Methods
each
-
list -
fn -
[scope]
Allows to iterate over any enumerable with a function taking up to three arguments
Parameters:
-
listString | Array | Array-like | ObjectThe enumerable
-
fnFunctionThe iteration function
-
[scope]optionalOptional context to execute the given function in
Returns:
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]
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:
-
listString | Array | Array-like | ObjectThe enumerable
-
fnFunctionThe iteration function
-
[scope]optionalOptional context to execute the function in
Returns:
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]
Allows to filter any enumerable with a function
Parameters:
-
listString | Array | Array-like | ObjectThe enumerable
-
fnFunctionThe iteration function
-
[scope]optionalOptional context to execute the function in
Returns:
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:
-
listString | Array | Array-like | ObjectThe enumerable
-
fnFunctionThe predicate function
-
[scope]optionalOptional 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:
-
listString | Array | Array-like | ObjectThe enumerable
-
fnFunctionThe predicate function
-
[scope]optionalOptional 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
Returns the index of a given item inside a enumerable from the left
Parameters:
-
listString | Array | Array-like | ObjectThe enumerable
-
itemThe item to compare against
Returns:
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
Returns the index of a given item inside a enumerable from the right
Parameters:
-
listString | Array | Array-like | ObjectThe enumerable
-
itemThe item to compare against
Returns:
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]
Allows to map a function over a enumerable
Parameters:
-
listString | Array | Array-like | ObjectThe enumerable
-
fnFunctionThe iteration function
-
[scope]optionalOptional context to execute the given function in
Returns:
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:
-
listString | Array | Array-like | ObjectThe enumerable
-
fnFunctionThe iteration function
-
seedStarting value
-
[scope]optionalOptional 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:
-
listString | Array | Array-like | ObjectThe enumerable
-
fnFunctionThe iteration function
-
seedStarting value
-
[scope]optionalOptional 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]
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:
-
listString | Array | Array-like | ObjectThe enumerable
-
fnFunctionThe iteration function
-
[scope]optionalOptional scope to execute the function in
Returns:
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
