util

xduce. util

Source:

A series of utility functions that are used internally in Xduce. Most of them don't have a lot to do with transducers themselves, but since they were already available and potentially useful, they're provided here. The reduction-related functions are related to transducers, specifically to writing them, so they are also provided.

Namespaces

bmp
status

Methods

(static) complement(fn) → {function}

Source:

Creates a function that returns the opposite of the supplied predicate function.

The parameter lists of the input and output functions are exactly the same. The only difference is that the two functions will return opposite results. If a non-predicate function is passed into this function, the resulting function will still return a boolean that is the opposite of the truthiness of the original.

const even = x => x % 2 === 0;
const odd = xduce.util.complement(even);

console.log(even(2));      // -> true
console.log(odd(2));       // -> false
Parameters:
Name Type Description
fn function

A predicate function.

Returns:

A new function that takes the same arguments as the input function but returns the opposite result.

Type
function

(static) isArray(x) → {boolean}

Source:

Determines whether a value is an array.

This function merely delegates to Array.isArray. It is provided for consistency in calling style only.

Parameters:
Name Type Description
x *

The value being tested to see if it is an array.

Returns:

Either true if the test value is an array or false if it is not.

Type
boolean

(static) isFunction(x) → {boolean}

Source:

Determines whether a value is a function.

Parameters:
Name Type Description
x *

The value being tested to see if it is a function.

Returns:

Either true if the test value is a function or false if it is not.

Type
boolean

(static) isNumber(x) → {boolean}

Source:

Determines whether a value is a number.

This function will return true for any number literal or instance of Number except for Infinity or NaN. It will return false for strings that happen to also be numbers; the value must be an actual Number instance or number literal to return true.

Parameters:
Name Type Description
x *

The value being tested to see if it is a number.

Returns:

Either true if the test value is a finite number (not including string representations of numbers) or false if it is not.

Type
boolean

(static) isObject(x) → {boolean}

Source:

Determines whether a value is a plain object.

This function returns false if the value is any other sort of built-in object (such as an array or a string). It also returns false for any object that is created by a constructor that is not Object's constructor, meaning that "instances" of custom "classes" will return false. Therefore it's only going to return true for literal objects or those created with Object.create().

Parameters:
Name Type Description
x *

The value being tested to see if it is a plain object.

Returns:

Either true if the test value is a plain object or false if it is not.

Type
boolean

(static) isString(x) → {boolean}

Source:

Determines whether a value is a string.

Literal strings will return true, as will instances of the String object.

Parameters:
Name Type Description
x *

The value being tested to see if it is a string.

Returns:

Either true if the test value is a string or false if it is not.

Type
boolean

(static) range(startopt, end, stepopt) → {Array.<number>}

Source:

Creates an array of values between the specified ranges.

The actual range is [start, end), meaning that the start value is a part of the array, but the end value is not.

If only one parameter is supplied, it is taken to be end, and start is set to 0. If there is a third parameter, it defines the distance between each successive element of the array; if this is missing, it's set to 1 if start is less than end (an ascending range) or -1 if end is less than start (a descending range).

If the step is going in the wrong direction - it's negative while start is less than end, or vice versa - then the start value will be the only element in the returned array. This prevents the function from trying to generate infinite ranges.

const { range } = xduce.util;

console.log(range(5));         // -> [0, 1, 2, 3, 4]
console.log(range(1, 5));      // -> [1, 2, 3, 4]
console.log(range(0, 5, 2));   // -> [0, 2, 4]
console.log(range(5, 0, -1));  // -> [5, 4, 3, 2, 1]
console.log(range(5, 0));      // -> [5, 4, 3, 2, 1]
console.log(range(0, 5, -2));  // -> [0]
Parameters:
Name Type Attributes Default Description
start number <optional>
0

The starting point, inclusive, of the array. This value will be the first value of the array.

end number

The ending point, exclusive, of the array. This value will not be a part of the array; it will simply define the upper (or lower, if the array is descending) bound.

step number <optional>
1|-1

The amount that each element of the array increases over the previous element. If this is not present, it will default to 1 if end is greater than start, or -1 if start is greater than end.

Returns:

An array starting at start, with each element increasing by step until it reaches the last number before end.

Type
Array.<number>