Module index

Source

Array§

Source§

allElements<I extends any[]>(
    data: I,
    condition: Predicate<ElementType<I>>,
): boolean

Returns true if all elements of the data array pass the condition.

example
allElements([2, 3, 4], isGt(1));    // true
allElements([2, 3, 4], isGt(3)); // false
Source§

allElements<I extends any[]>(
    condition: Predicate<ElementType<I>>,
): (data: I) => boolean

example
allElements(isGt(1))([2, 3, 4]);    // true
Source§

aperture<I extends any[]>(data: I, size: number): I[]

Returns an array of arrays of length size with consecutive elements from the original array. Returns an empty array if size is shorter than the array, or is not a positive integer.

example
aperture([2, 4, 6, 8, 10], 3);     // [[2, 4, 6], [4, 6, 8], [6, 8, 10]]
Source§

aperture<I extends any[]>(size: number): (data: I) => I[]

example
aperture(3)([2, 4, 6, 8, 10]);     // [[2, 4, 6], [4, 6, 8], [6, 8, 10]]
Source§

append<I extends string | any[]>(data: I, value: I): I

Returns a data the value array appended. To append an element (rather than another array) to an array instead, use appendElement.

example
append([2, 3, 4], [10, 11]);            // [2, 3, 4, 10, 11]
append("ostrich", "weasel"); // "ostrichweasel"
Source§

append<I extends string | any[]>(value: I): (data: I) => I

example
append([10, 11])([2, 3, 4]);            // [2, 3, 4, 10, 11]
append<string>("weasel")("ostrich") // "ostrichweasel"
Source§

appendElement<I extends any[]>(data: I, value: ElementType<I>): I

Appends the element to the end of the data array and returns a copy. For strings, or to append an array to another array, use append.

example
appendElement([2, 3, 4], 10);            // [2, 3, 4, 10]
Source§

appendElement<I extends any[]>(value: ElementType<I>): (data: I) => I

example
appendElement(10)([2, 3, 4]);            // [2, 3, 4, 10]
Source§

chunk<I extends string | any[]>(data: I, size: number): I[]

Breaks data into an array of subarrays or substrings, each of length size. If size does not divide evenly into the length of data, the last subarray will contain fewer than size elements. Returns an empty array if size is invalid.

example
chunk([2, 3, 4, 5, 6, 7], 2);     // [[2, 3], [4, 5], [6, 7]]
chunk([2, 3, 4, 5, 6], 3); // [[2, 3, 4], [5, 6]]
chunk("ostrich", 3); // ["ost", "ric", "h"]
Source§

chunk<I extends string | any[]>(size: number): (data: I) => I[]

example
chunk(2)([2, 3, 4, 5, 6, 7]);     // [[2, 3], [4, 5], [6, 7]]
Source§

contains(data: string | any[], value: any): boolean

Indicates whether or not the data array contains the element value, or the data string contains the substring value.

example
contains([2, 3, 4], 4);           // true
contains([2, 3, 4], 10); // false
contains("ostrich", "rich"); // true
contains("ostrich", "cat"); // false
Source§

contains(value: any): (data: string | any[]) => boolean

example
contains(4)([2, 3, 4]);           // true
Source§

count<I extends any[]>(data: I, value: ElementType<I>): number

Count the number of elements (strictly) equal to value in the array data.

example
count([1, 2, 3, 1, 2], 1); // 2
Source§

count<I extends any[]>(value: ElementType<I>): (data: I) => number

example
count(1)([1, 2, 3, 1, 2]); // 2
Source§

countWith<I extends any[]>(
    data: I,
    condition: Predicate<ElementType<I>>,
): number

Counts the number of elements in the data array that pass the condition predicate.

example
countWith([2, 10, -2, -6], isNegative);      // 2
Source§

countWith<I extends any[]>(
    condition: Predicate<ElementType<I>>,
): (data: I) => number

example
countWith(isNegative)([2, 10, -2, -6]);      // 2
Source§

createArrayOf<E>(length: number, value: E): E[]

Creates a new array with length elements, each filled with value.

Creates an empty array for invalid length (non-integer or negative).

example
createArrayOf(5, 4);         // [4, 4, 4, 4, 4]
Source§

createArrayWith<E>(length: number, transform: (x: number) => E): E[]

Creates a new array with length elements. Element values are determined by a transform of the element index.

Creates an empty array for invalid length (non-integer or negative).

example
createArrayWith(5, i => i ** 2);         // [0, 1, 4, 9, 16]
Source§

elementAt<I extends any[]>(data: I, index: number): undefined | ElementType<I>

Returns the element of data at index, or undefined if index is out of bounds or otherwise invalid.

example
elementAt([2, 4, 6], 2);      // 6
elementAt([2, 4, 6], -2); // 4
elementAt([2, 4, 6], 5); // undefined
Source§

elementAt<I extends any[]>(
    index: number,
): (data: I) => undefined | ElementType<I>

example
elementAt(2)([2, 4, 6]);      // 6
Source§

elementPasses<I extends any[]>(
    data: I,
    index: number,
    condition: Predicate<ElementType<I>>,
): boolean

Indicates whether the element of array data at index passes the condition. If index is invalid or out of bounds, returns false.

example
elementPasses([-1, 0, 1], 2, x => x > 0);     // true
elementPasses([-1, 0, 1], 0, x => x > 0); // false
elementPasses([-1, 0, 1], 5, x => x > 0); // false (since index is out of bounds)
Source§

elementPasses<I extends any[]>(
    index: number,
    condition: Predicate<ElementType<I>>,
): (data: I) => boolean

example
elementPasses(2, x => x > 0)([-1, 0, 1]);      // true
Source§

filter<I extends any[]>(data: I, condition: Predicate<ElementType<I>>): I

Similar to Array.prototype.filter, but with the difference that the condition function is not called with an index as the second parameter. This prevents unexpected behavior when the transform function has a second optional parameter that respresents something other than an index. Use filterWithIndex instead if you need the index.

example
filter([4, -2, 0, 2, -4], isPositive);               // [4, 2]
Source§

filter<I extends any[]>(condition: Predicate<ElementType<I>>): (data: I) => I

example
filter(isPositive)([4, -2, 0, 2, -4]);      // [4, 2]
Source§

filterWithIndex<I extends any[]>(
    data: I,
    condition: IndexedPredicate<ElementType<I>>,
): I

Similar to filter (which is in turn similar to Array.prototype.map), but the condition function requires an index as the second parameter. Use filter instead if you do not need the index.

example
filterWithIndex([4, -2, 0, 2, -4], (_, i) => isEven(i));      // [4, 0, -4]
Source§

filterWithIndex<I extends any[]>(
    condition: IndexedPredicate<ElementType<I>>,
): (data: I) => I

example
filterWithIndex((_, i) => isEven(i))([4, -2, 0, 2, -4]);      // [4, 0, -4]
Source§

findIndex<I extends any[]>(
    data: I,
    condition: Predicate<ElementType<I>>,
): number

Returns the index of the first element of data that passes condition, or returns NaN if no eleement passes.

remarks

There are two reason that NaN is used as the return value when so element is found instead of -1, as is used in JavaSCript's built-in Array.prototype.finsIndex. One is that NaN is more consistent with the conventions used elsewhere in this library. The other is that the functions in this library routinely recongize an index of -1 as the last element of an array, so surprising behavior could occur if -1 were used to epresent an ostensibly non-existent index.

example
findIndex([2, 4, -2, 10], isNegative);          // 2
findIndex([2, 4, 12, 10], isNegative); // NaN
Source§

findIndex<I extends any[]>(
    condition: Predicate<ElementType<I>>,
): (data: I) => number

example
findIndex(isNegative)([2, 4, -2, 10]);          // 2
Source§

findLastIndex<I extends any[]>(
    data: I,
    condition: Predicate<ElementType<I>>,
): number

Returns the index of the last element of data that passes condition, or returns NaN if no eleement passes.

remarks

There are two reason that NaN is used as the return value when so element is found instead of -1, as is used in JavaSCript's built-in Array.prototype.finsIndex. One is that NaN is more consistent with the conventions used elsewhere in this library. The other is that the functions in this library routinely recongize an index of -1 as the last element of an array, so surprising behavior could occur if -1 were used to epresent an ostensibly non-existent index.

example
findLastIndex([2, -4, -2, 10], isNegative);         // 2
findLastIndex([2, 4, 12, 10], isNegative); // NaN
Source§

findLastIndex<I extends any[]>(
    condition: Predicate<ElementType<I>>,
): (data: I) => number

example
findLastIndex(isNegative)([2, -4, -2, 10]);          // 2
Source§

findLastOr<I extends any[]>(
    data: I,
    condition: Predicate<ElementType<I>>,
    fallback: unknown,
): unknown

Returns the last element in the data array that passes the condition, or returns the fallback value if no element meets the condition.

example
findLastOr([2, -10, -2, 6], isNegative, NaN)).toBe(-2);
findLastOr([2, 10, 4, 6], isNegative, NaN)).toBe(NaN);
Source§

findLastOr<I extends any[]>(
    condition: Predicate<ElementType<I>>,
    fallback: unknown,
): (data: I) => unknown

example
findLastOr(x => x < 0, NaN)([2, -10, -2, 6]).toBe(-2);
Source§

findOr<I extends any[]>(
    data: I,
    condition: Predicate<ElementType<I>>,
    fallback: unknown,
): unknown

Returns the first element in the data array that passes the condition, or returns the fallback value if no element meets the condition.

example
findOr([2, 10, -2, -6], isNegative, NaN);      // -2
findOr([2, 10, 4, 6], isNegative, NaN); // NaN
Source§

findOr<I extends any[]>(
    condition: Predicate<ElementType<I>>,
    fallback: unknown,
): (data: I) => unknown

example
findOr(isNegative, NaN)([2, 10, -2, -6]);      // -2
Source§

first<I extends any[]>(data: I): undefined | ElementType<I>

Returns the first element of an array, or undefined if the array is empty.

example
first([3, 4, 6]);    // 3
Source§

flatten<I extends any[]>(data: I): ElementType<I>

Flatten an array one level.

example
flatten([[3, 4], [5]]);      // [3, 4, 5]
flatten([[[1, 2, 3]], [[4, 5], 6], [7, 8, [9]]]);
// [[1, 2, 3], [4, 5], 6, 7, 8, [9]]
Source§

insert<I extends string | any[]>(
    data: I,
    index: number,
    content: I extends any[] ? ElementType<I<I>> : string,
): I

Inserts the content element into the data array, or the content substring into the data string at the index position. If index is equal to the length of data, then content will be appended to the end. Returns a shallow copy of data if the index is invalid.

example
insert([2, 3, 4], 1, 10);        // [2, 10, 3, 4]
insert([2, 3, 4], -1, 10); // [2, 3, 10, 4]
insert("ostrich", 1, "aa"); // "oaastrich"
insert("ostrich", 7, "aa"); // "ostrichaa"
Source§

insert<I extends string | any[]>(
    index: number,
    content: I extends any[] ? ElementType<I<I>> : string,
): (data: I) => I

example
 * insert(1, 10)(2, 3, 4]);       // [2, 10, 3, 4]
Source§

insertArray<I extends any[]>(data: I, index: number, content: I): I

Inserts the content array into the data array at the index position. If index is equal to the length of data, then content will be appended to the end. Returns a shallow copy of data if the index is invalid.

example
insertArray([2, 3, 4], 1, [10, 11]);       // [2, 10, 11, 3, 4];
insertArray([2, 3, 4], -1, [10, 11]); // [2, 3, 10, 11, 4];
insertArray([2, 3, 4], 3, [10, 11]); // [2, 3, 4, 10, 11];
Source§

insertArray<I extends any[]>(index: number, content: I): (data: I) => I

example
insertArray(1, [10, 11])([2, 3, 4]);       // [2, 10, 11, 3, 4];
Source§

isEmpty(data: string | any[]): boolean

Indicates whether a string or array has length zero.

example
isEmpty([]);             // true
isEmpty("ostrich"); // false
Source§

isNotEmpty(data: string | any[]): boolean

Indicates whether a string or array has length of at least one.

example
isNotEmpty("ostrich");      // true
isNotEmpty([]); // false
Source§

join(data: string[], separator: string): string

Concatenates the strings in the data array, inserting the separator string between each.

example
join(['a', 'b', 'c'], '_');     // 'a_b_c'
Source§

join(separator: string): (data: string[]) => string

example
join('')(['a', 'b', 'c']);     // 'abc'
Source§

last<I extends any[]>(data: I): undefined | ElementType<I>

Returns the last element of an array, or undefined if the array is empty.

example
last([3, 4, 6]);    // 6
last([]); // undefined
Source§

length(data: string | any[]): number

Returns the length of a string or array

example
length([2, 3, 4]);      // 3
length("ostrich"); // 7
Source§

lengthPasses(data: string | any[], condition: Predicate<number>): boolean

Indicates whether the length of the data string or array passes the condition.

example
lengthPasses([2, 3, 4], isLt(5));     // true
lengthPasses("ostrich", isLt(5)); // false
Source§

lengthPasses<I extends string | any[]>(
    condition: Predicate<number>,
): (data: string | any[]) => boolean

example
lengthPasses(isLt(5))([2, 3, 4]);     // true
Source§

map<I extends any[], O extends any[] = I>(
    data: I,
    transform: (x: ElementType<I>) => O[number],
): O

Similar in concept Array.prototype.map, but with the distinction that it does not automatically call transform with a second argument for the index. This prevents unexpected behavior when the transform function has a second optional parameter that respresents something other than an index (e.g., parseInt). Use mapWithIndex instead if you need the index.

example
map([2, 3, 4], x => x * 2);        [4, 6, 8];
Source§

map<I extends any[], O extends any[] = I>(
    transform: (x: ElementType<I>) => O[number],
): (data: I) => O

example
map(x => x * 2)([4, 6, 8]);    [4, 6, 8];
Source§

mapWithIndex<I extends any[], O extends any[] = I>(
    data: I,
    transform: (x: ElementType<I>, i: number) => O[number],
): O

Similar to map (which is in turn similar to Array.prototype.map), but the transform function requires an index as the second parameter. Use map instead if you do not need the index.

example
mapWithIndex([2, 3, 4], (x, i) => x + i);        [2, 4, 6];
Source§

mapWithIndex<I extends any[], O extends any[] = I>(
    transform: (x: ElementType<I>, i: number) => O[number],
): (data: I) => O

example
mapWithIndex((x, i) => x + i)([2, 3, 4]);        [2, 4, 6];
Source§

maximum(data: number[]): number

Returns the maximum value of the array, or NaN if the array is empty.

example
maximum([3, -1, 7, -2]);       // 7
Source§

maximumWith<I extends any[]>(
    data: I,
    transform: (x: ElementType<I>) => number,
): number

Applies a transform to each element of the array, then returns the maximum of the transformed array. Returns NaN if the array is empty.

example
maximumWith([1, 4, -6, -2], Math.abs);        // 6
Source§

maximumWith<I extends any[]>(
    transform: (x: ElementType<I>) => number,
): (data: I) => number

example
maximumWith(Math.abs, [1, 4, -6, -2]);        // 6
Source§

mean(data: number[]): number

Returns the mean of an array of numbers, or NaN if the array is empty.

example
mean([3, 5, 10]);          // 6
Source§

meanWith<I extends any[]>(
    data: I,
    transform: Fn<ElementType<I>, number>,
): number

Applies the transform function to each element of an array, then returns the mean of the result. Returns NaN if the array is empty.

example
meanWith([1, 3, -8], Math.abs);       // 4
Source§

meanWith<I extends any[]>(
    transform: Fn<ElementType<I>, number>,
): (data: I) => number

example
meanWith(Math.abs)([1, 3, -8]);       // 4
Source§

minimum(data: number[]): number

Returns the minimum value of the array, or NaN if the array is empty.

example
minimum([3, -1, -7, 2]);       // -7
Source§

minimumWith<I extends any[]>(
    data: I,
    transform: (x: ElementType<I>) => number,
): number

Applies a transform to each element of the array, then returns the minimum of the transformed array. Returns NaN if the array is empty.

example
minimumWith([1, 4, -6, -2], Math.abs);        // 1
Source§

minimumWith<I extends any[]>(
    transform: (x: ElementType<I>) => number,
): (data: I) => number

example
minimumWith(Math.abs, [1, 4, -6, -2]);        // 1
Source§

noElements<I extends any[]>(
    data: I,
    condition: Predicate<ElementType<I>>,
): boolean

Returns true if no elements of the data array pass the condition.

example
noElements([2, 3, 4], isGt(5));    // true
noElements([2, 3, 4], isGt(3)); // false
Source§

noElements<I extends any[]>(
    condition: Predicate<ElementType<I>>,
): (data: I) => boolean

example
noElements(isGt(5))([2, 3, 4]);    // true
Source§

notAllElements<I extends any[]>(
    data: I,
    condition: Predicate<ElementType<I>>,
): boolean

Returns true if one or more elements of the data array fails to pass the condition.

example
notAllElements([2, 3, 4], isGt(3));    // true
notAllElements([2, 3, 4], isGt(1)); // false
Source§

notAllElements<I extends any[]>(
    condition: Predicate<ElementType<I>>,
): (data: I) => boolean

example
notAllElements(isGt(1))([2, 3, 4]);    // true
Source§

prepend<I extends string | any[]>(data: I, value: I): I

Returns a data the value array prepended. To prepend an element (rather than another array) to an array instead, use prependElement.

example
prepend([2, 3, 4], [10, 11]);            // [10, 11, 2, 3, 4]
prepend("ostrich", "weasel"); // "weaselostrich"
Source§

prepend<I extends string | any[]>(value: I): (data: I) => I

example
prepend([10, 11])([2, 3, 4]);            // [10, 11, 2, 3, 4]
prepend<string>("weasel")("ostrich") // "weaselostrich"
Source§

prependElement<I extends any[]>(data: I, value: ElementType<I>): I

Prepends the element to the end of the data array and returns a copy. For strings, or to prepend an array to another array, use prepend.

example
prependElement([2, 3, 4], 10);            // [10, 2, 3, 4]
Source§

prependElement<I extends any[]>(value: ElementType<I>): (data: I) => I

example
prependElement(10)([2, 3, 4]);            // [10, 2, 3, 4]
Source§

product(data: number[]): number

Returns the product of an array of numbers, or if the array is empty, 1.

example
product([3, 5, 7]);          // 105
Source§

reduce<I extends any[], O = ElementType<I>>(
    data: I,
    accumulator: Accumulator<I, O>,
    initial: O,
): O

A data-first counterpart to Array.prototype.reduce, but with more permissive typing than the TS library uses out of the box, allowing the result to have a different type from the data elements themselves.

remarks

The generic type implementation may be needed if accumulator has overloads.

example
reduce<number>([3, 5, 6], add, 0);                 // 14
reduce(
["cat", "ferret", "ostrich"],
(acc: number, el: string) => el.length + acc,
0
); // 16
Source§

reduce<I extends any[], O = ElementType<I>>(
    accumulator: Accumulator<I, O>,
    initial: O,
): (data: I) => O

example
reduce(add, 0)([3, 5, 6]);                         // 14
Source§

reduceRight<I extends any[], O = ElementType<I>>(
    data: I,
    accumulator: Accumulator<I, O>,
    initial: O,
): O

A data-frst counterpart to Array.prototype.reduceRight, but with more permissive typing than the TS library uses out of the box, allowing the result to have a different type from the data elements themselves.

example
reduceRight(
[3, 5, 6],
(acc: number[], el: number) => acc = [...acc, el + last(acc)],
[0]
); // [0, 6, 11, 14]
Source§

reduceRight<I extends any[], O = ElementType<I>>(
    accumulator: Accumulator<I, O>,
    initial: O,
): (data: I) => O

example
reduceRight(
(acc: number[], el: number) => [...acc, el + last(acc)!],
[0]
)([3, 5, 6]); // [0, 6, 11, 14]
Source§

reverse(data: any[]): any[]

Returns a copy of the array in reversed order.

example
reverse([1, 2, 3]);      // [3, 2, 1]
Source§

setElement<I extends any[]>(data: I, index: number, value: ElementType<I>): I

Returns a copy of the array data with the element at index set to value. A negative index will count backward from the end of the array. If index is out of bounds or not an integer, returns an unaltered copy of data.

example
setElement([2, 3, 4], 0, 10);        // [10, 3, 4]
setElement([2, 3, 4], -1, 10); // [2, 3, 10]
Source§

setElement<I extends any[]>(
    index: number,
    value: ElementType<I>,
): (data: I) => I

example
setElement(0, 10)([2, 3, 4]);        // [10, 3, 4]
Source§

slice<I extends string | any[]>(data: I, from: number, to: number): I

Returns a shallow copy of the portion of the data array or string beginning at index from and ending with, but not including, the index to. Negative index values count backward from the end of the arrry. Returns an empty array if from or to is not an integer, or if from is out of the array bounds.

example
slice([2, 3, 4, 5, 6], 2, 4);            // [4, 5]
slice([2, 3, 4, 5, 6], 2, -1); // [4, 5]
slice("ostrich", 2, 4); // "tr"
slice("ostrich", 2, -1); // "tr"
slice([2, 3, 4, 5, 6], -4, 10); // [3, 4, 5, 6], going past end of array is OK)
slice([2, 3, 4, 5, 6], -10, 4); // [], starting at -10 is non-sensical
Source§

slice<I extends string | any[]>(from: number, to: number): (data: I) => I

example
slice(2, 4)([2, 3, 4, 5, 6]);            // [4, 5]
Source§

sliceFrom<I extends string | any[]>(data: I, from: number): I

Returns a shallow copy of the data array or string starting from index from. A negative index counts backwards from the end of the array. Returns an empty array if from is not an integer or is out of bounds.

example
sliceFrom([2, 3, 4], 2);        // [4]
sliceFrom([2, 3, 4], -2); // [3, 4]
sliceFrom("ostrich", 2); // "trich"
sliceFrom("ostrich", -2); // "ch"
sliceFrom([2, 3, 4], 6); // []
Source§

sliceFrom<I extends string | any[]>(from: number): (data: I) => I

example
sliceFrom(2)([2, 3, 4]);        // [4]
Source§

sliceTo<I extends string | any[]>(data: I, to: number): I

Return a shallow copy of the data array or string up to, but not including, the index 'to. Returns an empty array if to` is not an integer.

example
sliceTo([2, 3, 4, 5], 2);     // [2, 3]
sliceTo([2, 3, 4, 5], -1); // [2, 3, 4]
sliceTo([2, 3, 4, 5], 6); // [2, 3, 4, 5]
sliceTo("ostrich", 2); // "os"
sliceTo("ostrich", -2); // "ostri"
sliceTo("ostrich", 10); // "ostrich"
Source§

sliceTo<I extends string | any[]>(to: number): (data: I) => I

example
sliceTo(2)([2, 3, 4, 5]);     // [2, 3]
Source§

someElements<I extends any[]>(
    data: I,
    condition: Predicate<ElementType<I>>,
): boolean

Returns true if one or more elements of the data array pass the condition. Returns false for an empty array.

example
someElements([2, 3, 4], isGt(3));    // true
someElements([2, 3, 4], isGt(5)); // false
Source§

someElements<I extends any[]>(
    condition: Predicate<ElementType<I>>,
): (data: I) => boolean

example
someElements(isGt(3))([2, 3, 4]);    // true
Source§

sortAlphabetical(data: string[]): string[]

Sorts an array of strings alphabetically, ignoring punctuation. and placing uppercase letters first.

example
sortAplhabetical(["a", "a c", "ab", "Ab"]);      // ["a", "Ab", "ab", "a c"]
Source§

sortNumeric(data: number[]): number[]

Sorts an array of numbers in ascending order, wiith NaNs at the end.

example
sortNumeric([30, 6, 1, NaN, 200, 5]);      // [1, 5, 6, 30, 200, NaN]
Source§

sum(data: number[]): number

Returns the sum of an array of numbers, or sero if the array is empty.

example
sum([3, 5, 7]);          // 15
Source§

sumWith<I extends any[]>(data: I, transform: Fn<ElementType<I>, number>): number

Applies the transform function to each element in data, then returns the sum of the transformed array.

example
sumWith([1, 2, 3], pow(2));     // 14
Source§

sumWith<I extends any[]>(
    transform: Fn<ElementType<I>, number>,
): (data: I) => number

example
sumWith(pow(2))([1, 2, 3]);     // 14

Logic§

Source§

all<T>(data: T, conditions: Predicate<T>[]): boolean

Returns true if and only if all conditions are true for the data.

example
all(2, [isGt(0), isLt(4), Number.isInteger]);      // true
all(2.5, [isGt(0), isLt(4), Number.isInteger]); // false
Source§

all<T>(conditions: Predicate<T>[]): (data: T) => boolean

example
all([isGt(0), isLt(4), Number.isInteger])(2);      // true
Source§

asIs<T>(value: T): T

Returns its argument unchanged (similar to identity in some other libraries)

example
asIs(42);    // 42
Source§

equals<T>(data: T, value: T): boolean

Indicates whether the data is strictly equal to (===) value.

example
equals(5, 5);       // true
equals(5, "5"); // false
Source§

equals<T>(value: T): (data: T) => boolean

example
equals(5)(5);       // true
Source§

equalsOneOf(data: any, values: any[]): boolean

Returns true if data is equal to one of the values, false otherwise.

example
equalsOneOf(1, [1, 2, 3]);    // true
equalsOneOf(4, [1, 2, 3]); // false
Source§

equalsOneOf(values: any[]): (data: any) => boolean

example
equalsOneOf([1, 2, 3])(1);    // true
Source§

equalsWith<I>(data: I, value: I, transform: (x: I) => any): boolean

Tells whether data and value are strictly equal after the transform function is applied to each.

example
equalsWith(4, -4, Math.abs);      // true
Source§

equalsWith<I>(value: I, transform: (x: I) => any): (data: I) => boolean

remarks

The generic type specification is needed so TS doesn't infer the type as a specific constant.

example
equalsWith<number>(-4, Math.abs)(4);     // true
Source§

isFunction(data: any): data is Function

Returns true if the data is a function, false otherwise.

This is implemented using typeof data === "function".

Source§

isInstanceOf(data: any, className: Object): boolean

Tells whether data is an instance of the class className.

example
class A {}
const a = new A();

isInstanceOf(a, A); // true
isInstanceOf([1, 2, 3], Array); // true
isInstanceOf({}, A); // false
Source§

isInstanceOf(className: Object): (data: any) => boolean

example
class A {}
const a = new A();

isInstanceOf(A)(a); // true
Source§

isNotNullish(data: any): boolean

Returns false if data is null or undefined, otherwise true.

example
isNotNullish(null);      // true
isNotNullish(undefined); // true
isNotNullish(0); // false
Source§

isNullish(data: any): boolean

Tells whether data is null or undefined.

example
isNullish(null);      // true
isNullish(undefined); // true
isNullish(0); // false
Source§

isNumber(data: any): data is number

Returns true if data is a number false otherwise.

example
isNumber(42;            // true
isNumber("42"); // false
Source§

isString(data: any): data is string

Returns true if data is a string, false otherwise.

example
isString("ostrich");     // true
isString(3); // false
Source§

none<T>(data: T, conditions: Predicate<T>[]): boolean

Returns true if and only if none of the conditions are true for the data.

example
none(2.5, [isLt(0), isGt(4), Number.isInteger]);      // true
none(2, [isGt(0), isLt(4), Number.isInteger]); // false
Source§

none<T>(conditions: Predicate<T>[]): (data: T) => boolean

example
none([isLt(0), isGt(4), Number.isInteger])(2.5);      // true
Source§

notAll<T>(data: T, conditions: Predicate<T>[]): boolean

Returns true if at least one of the conditions is not true for the data.

example
notAll(2.5, [isGt(0), isLt(4), Number.isInteger]);      // true
notAll(2, [isGt(0), isLt(4), Number.isInteger]); // false
Source§

notAll<T>(conditions: Predicate<T>[]): (data: T) => boolean

example
notAll([isGt(0), isLt(4), Number.isInteger])(2.5);      // true
Source§

notEqual<T>(data: T, value: T): boolean

Indicates if the data is not strictly equal to value.

You will likely need to use a type annotation if the types of the data and value are different.

example
notEqual(5, 7);                // true
notEqual("a", "b"); // false
notEqual<unknown>(5, "5") // true
Source§

notEqual<T>(value: T): (data: T) => boolean

example
notEqual(7)(5);         true
§

nullishTo<T>(data: undefined | null | T, value: T): T

Returns value if data is null or undefined, otherwise returns data unchanged.

example
nullishTo(null, 5);        // 5
nullishTo(undefined, 5); // 5
nullishTo(10, 5); // 10
§

nullishTo<T>(value: T): (data: undefined | null | T) => T

Returns value if data is null or undefined, otherwise returns data unchanged.

example
nullishTo(5)(null);        // 5
nullishTo(5)(undefined); // 5
nullishTo(5)(10); // 10
§

nullTo<T>(data: null | T, value: T): T

Returns value if data is null, otherwise returns data unchanged.

example
nullTo(null, 5);     // 5
nullTo(10, 5); // 10
§

nullTo<T>(value: T): (data: null | T) => T

example
nullTo(5)(null);      // 5
nullTo(5)(10); // 10
Source§

some<T>(data: T, conditions: Predicate<T>[]): boolean

Returns true if at least one of the conditions is true for the data.

example
some(2, [isLt(0), isGt(4), Number.isInteger]);      // true
some(2.5, [isLt(0), isGt(4), Number.isInteger]); // false
Source§

some<T>(conditions: Predicate<T>[]): (data: T) => boolean

example
some([isLt(0), isGt(4), Number.isInteger])(2);      // true
§

undefinedTo<T>(data: undefined | T, value: T): T

Returns value if data is undefined, otherwise returns data unchanged.

example
undefinedTo(undefined, 5);   // 5
undefinedTo(10, 5); // 10
§

undefinedTo<T>(value: T): (data: undefined | T) => T

example
undefinedTo(5)(undefined);    // 5
undefinedTo(5)(10); // 10

Math§

Source§

add(data: number, x: number): number

Adds two numbers

example
add(3, 5);     // 8
Source§

add(x: number): (data: number) => number

example
add(3)(5);     // 8
Source§

clamp(data: number, min: number, max: number): number

Returns min if data is less than min, max if data is greater than max. If data is already between min and max, then the value of data is returned unchanged.

example
clamp(15, 0, 100);        // 15
clamp(-5, 0, 100); // 0
clamp(140, 0, 100); // 100
Source§

clamp(min: number, max: number): (data: number) => number

example
clamp(0, 100)(-5);        // 0
Source§

divideBy(data: number, x: number): number

Divide data by x.

example
divideBy(6, 3);     // 2
Source§

divideBy(x: number): (data: number) => number

example
divideBy(3)(6);     // 2
Source§

divideInto(data: number, x: number): number

Divide data into x, i.e., divide x by data.

example
divideInto(6, 3);     // 0.5
Source§

divideInto(x: number): (data: number) => number

example
divideInto(3)(6);     // 0.5
Source§

interpolate(data: number, x1: number, x2: number): number

Returns the value a fraction data between the numbers x1 and x2. data may be outside the range [0, 1], in which case the result will be outside the range [x1, x2].

example
interpolate(0.1, 100, 200);    // 110
interpolate(-0.1, 100, 200); // 90
Source§

interpolate(x1: number, x2: number): (data: number) => number

example
interpolate(100, 200)(0.1);    // 110
Source§

isBetween(data: number, min: number, max: number): boolean

Indicates whether data is between the values of min and max (including the endpoints).

example
isBetween(10, 5, 20);      // true
isBetween(30, 5, 20); // false
Source§

isBetween(min: number, max: number): (data: number) => boolean

example
isBetween(5, 20)(10);      // true
Source§

isCloseTo(data: number, x: number, tolerance: number): boolean

Indicates whether data is within tolerance of x (in either direction)

example
isCloseTo(5.01, 5, 0.01);     // true
isCloseTo(4.99, 5, 0.1); // true
isCloseTo(5.01, 5, 0.001); // false
Source§

isCloseTo(x: number, tolerance: number): (data: number) => boolean

example
isCloseTo(5, 0.1)(5.01);      // true
Source§

isDivisibleBy(data: number, divisor: number): boolean

Indicates whether data is divislbe by divisor. If either data or divisor is not an integer, then a tolerance of 1e-15 will be allowed to allow for floating point errors.

example
isDivisibleBy(12, 3);             // true
isDivisibleBy(14, 3); // false
isDivisibleBy(12 + 1e-17, 3); // true (due to allowed tolerance of 1e-15)
Source§

isDivisibleBy(divisor: number): (data: number) => boolean

example
isDivisibleBy(12)(3);             // true
Source§

isEven(data: number): boolean

Indicates whether a number is even. Unlike isDivisibleBy(2), isEven doesn't not allow any tolerance for rounding errors in floating point numbers.

example
isEven(4);        // true
isEven(4.1); // false
Source§

isGt(data: number, value: number): boolean

Indicates whether data is greater than value.

example
isGt(5, 4);       // true
isGt(5, 5); // false
Source§

isGt(value: number): (data: number) => boolean

example
isGt(5)(4);       // true
Source§

isGte(data: number, value: number): boolean

Indicates whether data is greater than or equal to value.

example
isGte(5, 4);       // true
isGte(5, 5); // true
isGte(3, 5); // false
Source§

isGte(value: number): (data: number) => boolean

example
isGte(5)(4);       // true
Source§

isLt(data: number, value: number): boolean

Indicates whether data is less than value

example
isLt(4, 5);       // true
isLt(5, 5); // false
Source§

isLt(value: number): (data: number) => boolean

Indicates whether data is less than value

example
isLt(5)(4);       // true
Source§

isLte(data: number, value: number): boolean

Indicates whether data is less than or equal to value

example
isLte(4, 5);       // true
isLte(5, 5); // true
isLte(3, 2); // false
Source§

isLte(value: number): (data: number) => boolean

example
isLte(6)(5);       // true
Source§

isNegative(data: number): boolean

Indicates whether a number is negative.

example
isNegative(-2);    // true
isNegative(0); // false
Source§

isNonNegative(data: number): boolean

Indicates whether a number is non-negative, i.e., positive or zero.

example
isNonNegative(2);      // true
isNonNegative(0); // true
isNonNegative(-2); // false
Source§

isOdd(data: number): boolean

Indicates whether a number is an odd integer.

example
isEven(3);        // true
isEven(3.1); // false
Source§

isPositive(data: number): boolean

Indicates whether a number is positive.

example
isPositive(2);     // true
isPositive(0); // false
Source§

linearMap(data: number, x1: number, x2: number, y1: number, y2: number): number

Maps a value data from the range [x1, x2] to the range [y1, y2].

example
linearMap(11, 10, 20, 100, 200);    // 110
Source§

linearMap(
    x1: number,
    x2: number,
    y1: number,
    y2: number,
): (data: number) => number

example
linearMap(10, 20, 200, 100)(9);     // 210
Source§

mathModulo(data: number, modulus: number): number

Calculates data mod modulus using KKnuth's definition of modulus, which reflects the definition of mod used in mathematics. For a function that follows JavaScript's % operator instead, see modulo.

example
mathModulo(7, 6);         // 1
mathModulo(6)(-7); // 5
mathModulo(7, -6); // -5
mathModulo(-6, -7); // -1
Source§

mathModulo(modulus: number): (data: number) => number

example
mathModulo(7)(-6);       // -5
Source§

maximum(data: number[]): number

Returns the maximum value of the array, or NaN if the array is empty.

example
maximum([3, -1, 7, -2]);       // 7
Source§

maximumWith<I extends any[]>(
    data: I,
    transform: (x: ElementType<I>) => number,
): number

Applies a transform to each element of the array, then returns the maximum of the transformed array. Returns NaN if the array is empty.

example
maximumWith([1, 4, -6, -2], Math.abs);        // 6
Source§

maximumWith<I extends any[]>(
    transform: (x: ElementType<I>) => number,
): (data: I) => number

example
maximumWith(Math.abs, [1, 4, -6, -2]);        // 6
Source§

mean(data: number[]): number

Returns the mean of an array of numbers, or NaN if the array is empty.

example
mean([3, 5, 10]);          // 6
Source§

meanWith<I extends any[]>(
    data: I,
    transform: Fn<ElementType<I>, number>,
): number

Applies the transform function to each element of an array, then returns the mean of the result. Returns NaN if the array is empty.

example
meanWith([1, 3, -8], Math.abs);       // 4
Source§

meanWith<I extends any[]>(
    transform: Fn<ElementType<I>, number>,
): (data: I) => number

example
meanWith(Math.abs)([1, 3, -8]);       // 4
Source§

minimum(data: number[]): number

Returns the minimum value of the array, or NaN if the array is empty.

example
minimum([3, -1, -7, 2]);       // -7
Source§

minimumWith<I extends any[]>(
    data: I,
    transform: (x: ElementType<I>) => number,
): number

Applies a transform to each element of the array, then returns the minimum of the transformed array. Returns NaN if the array is empty.

example
minimumWith([1, 4, -6, -2], Math.abs);        // 1
Source§

minimumWith<I extends any[]>(
    transform: (x: ElementType<I>) => number,
): (data: I) => number

example
minimumWith(Math.abs, [1, 4, -6, -2]);        // 1
Source§

modulo(data: number, modulus: number): number

Calculates data mod modulus. modulo is implemented with the % operator, so it follows the JavaScript specification that the sign of a % b is the sign of a, and values are not required to be integers. See mathModulo for a function that follows Knuth's definition, which better reflects the use of mod in mathematics.

example
modulo(12, 10);         // 2
modulo(-12, 10); // -2
Source§

modulo(modulus: number): (data: number) => number

example
modulo(10)(12);         // 2
Source§

multiply(data: number, x: number): number

Multiplies two numbers

example
multiply(3, 5);     // 15
Source§

multiply(x: number): (data: number) => number

example
multiply(3)(5);     // 15
Source§

nthRoot(data: number, n: number): number

Calculates the nth root of data. n must be an integer and at least 2, or NaN will result.

example
nthRoot(8, 3);      // 2
nthRoot(-8, 3); // -2
nthRoot(-8, 4): // NaN
Source§

nthRoot(n: number): (data: number) => number

example
nthRoot(3)(8);      // 2
Source§

pow(data: number, exponent: number): number

Raises data to the power of exponent. This is a thin wrapper around the built-in Math.pow, so it has the same limitations, such as being unable to calculate non-integrer powers of negative bases. See nthRoot for a function that addresses part of these limitations.

example
pow(4, 3);    // 64
Source§

pow(exponent: number): (data: number) => number

example
pow(3)(4);    // 64
Source§

product(data: number[]): number

Returns the product of an array of numbers, or if the array is empty, 1.

example
product([3, 5, 7]);          // 105
Source§

round(data: number, places: number): number

Rounds a number to places digits after the decimal. If places is negative, rounding will be to -places digits before the decimal point, e.g., places of -2 will round to the nearest hundred. places must be an integer.

example
round(1469.278, 2);     // 1469.28
round(1469.278, 0); // 1469
round(1469.278, -2); // 1500
Source§

round(places: number): (data: number) => number

example
round(2)(1469.278);     // 1469.28
Source§

subtract(data: number, x: number): number

Subtract x from data.

example
subtract(5, 3);     // 2
Source§

subtract(x: number): (data: number) => number

example
subtract(3)(5);     // 2
Source§

subtractFrom(data: number, x: number): number

Subtract data from x.

example
subtract(5, 3);     // -2
Source§

subtractFrom(x: number): (data: number) => number

example
subtract(3)(5);     // -2
Source§

sum(data: number[]): number

Returns the sum of an array of numbers, or sero if the array is empty.

example
sum([3, 5, 7]);          // 15
Source§

sumWith<I extends any[]>(data: I, transform: Fn<ElementType<I>, number>): number

Applies the transform function to each element in data, then returns the sum of the transformed array.

example
sumWith([1, 2, 3], pow(2));     // 14
Source§

sumWith<I extends any[]>(
    transform: Fn<ElementType<I>, number>,
): (data: I) => number

example
sumWith(pow(2))([1, 2, 3]);     // 14
Source§

toHex(data: number): string

Converts a number to a string in hexadecimal form, or returns an empty string if the number is not an non-negative integer.

example
toHex(24);     // "18"
toHex(2.4); // ""

Object§

Source§

allProps<I extends POJO<any>>(
    data: I,
    condition: Predicate<ValueType<I>>,
): boolean

Returns true if all values of the data object pass the condition condition, otherwise false.

example
allProps({ x: 5, y: 10 }, isGt(0));    // true
allProps({ x: 5, y: 10 }, isGt(7)); // false
Source§

allProps<I extends POJO<any>>(
    condition: Predicate<ValueType<I>>,
): (data: I) => boolean

example
allProps(isGt(0))({ x: 5, y: 10 });    // true
Source§

hasKey<I extends POJO<any>>(data: I, key: string): boolean

Indicates whether the object data contains a given key. If a key is directly to undefined, the object is still considered to contain the key.

Implemented using hasOwnProperty.

example
hasKey({ x: 5, z: 10 }, "x");  // true
hasKey({ x: 5, z: 10 }, "y"); // false
hasKey({ x: 5, z: undefined }, "z"); // true
Source§

hasKey<I extends POJO<any>>(key: string): (data: I) => boolean

example
hasKey("x")({ x: 5, z: 10 });  // true
Source§

isKeyOf(data: string, obj: POJO<any>): boolean

Tells whether the plain object obj has its own key named data.

example
isKeyOf("a", { a: "weasel" });      // true
isKeyOf("a", { an: "ostrich" }); // false
Source§

isKeyOf(obj: POJO<any>): (data: string) => boolean

example
isKeyOf({ a: "weasel" })("a");      // true
Source§

mapProps<I extends POJO<any>, O extends POJO<any>>(
    data: I,
    transform: (x: ValueType<I>) => ValueType<O>,
): O

Applies a transform function to each property value in the object.

example
mapProps({ a: 1, b: 2, c: 3 }, x => x * 2);   // { a: 2, b: 4, c: 6 }
Source§

mapProps<I extends POJO<any>, O extends POJO<any>>(
    transform: (x: ValueType<I>) => ValueType<O>,
): (data: I) => O

example
mapProps(x => x * 2)({ a: 1, b: 2, c: 3 });   // { a: 2, b: 4, c: 6 }
Source§

noProps<I extends POJO<any>>(
    data: I,
    condition: Predicate<ValueType<I>>,
): boolean

Returns true if no value of the data object passes the condition condition, otherwise false.

example
noProps({ x: 5, y: 10 }, x => x > 15);    // true
noProps({ x: 5, y: 10 }, isGt(7)); // false
Source§

noProps<I extends POJO<any>>(
    condition: Predicate<ValueType<I>>,
): (data: I) => boolean

example
noProps(x => x > 15)({ x: 5, y: 10 });    // true
Source§

notAllProps<I extends POJO<any>>(
    data: I,
    condition: Predicate<ValueType<I>>,
): boolean

Returns true if at least one value of the data object fails the condition condition, otherwise false.

example
notAllProps({ x: 5, y: 10 }, isGt(7));    // true
notAllProps({ x: 5, y: 10 }, isGt(0)); // false
Source§

notAllProps<I extends POJO<any>>(
    condition: Predicate<ValueType<I>>,
): (data: I) => boolean

example
notAllProps(isGt(7))({ x: 5, y: 10 });    // true
§

omitKeys<I extends POJO<any>, O extends Partial<I>>(
    keys: string[],
): (data: I) => O

example
omitKeys(["a", "b"])({ a: 1, b: 2, c: 3 });   // { c: 3 }
§

pickKeys<I extends POJO<any>, O = Partial<I>>(data: I, keys: (keyof I)[]): O

Returns a new object consisting of only the portion of the data object with the specified keys.

example
pick({ a: 1, b: 2, c: 3 }, ["a", "b"]);   // { a: 1, b: 2 }
§

pickKeys<I extends POJO<any>, O = Partial<I>>(keys: string[]): (data: I) => O

Returns a new object consisting of only the portion of the data object with the specified keys.

example
pick(["a", "b"])({ a: 1, b: 2, c: 3 });   // { a: 1, b: 2 }
Source§

propOr<I extends POJO<any>>(
    data: I,
    key: string,
    fallback: ValueType<I>,
): ValueType<I>

Returns the value of an { x: 5 }ect property key if it exists, or the fallback value otherwise.

example
propOr({ x: 5, y: 10 }, "x", NaN);      // 5
propOr({ x: 5, y: 10 }, "z", NaN); // NaN
Source§

propOr<I extends POJO<any>>(
    key: string,
    fallback: ValueType<I>,
): (data: I) => ValueType<I>

example
propOr("x", NaN)({x: 5, y: 10});      // 5
Source§

propPasses<I extends POJO<any>>(
    data: I,
    key: string,
    condition: Predicate<ValueType<I>>,
): boolean

Returns true if an object property key exists and passes the condition, false otherwise.

example
propPasses({ x: 5 }, "x", x => x > 0);    // true
propPasses({ x: 5 }, "x", x => x < 0); // false
Source§

propPasses<I extends POJO<any>>(
    key: string,
    condition: Predicate<ValueType<I>>,
): (data: I) => boolean

example
propPasses("x", x => x > 0)({ x: 5 });    // true
Source§

someProps<I extends POJO<any>>(
    data: I,
    condition: Predicate<ValueType<I>>,
): boolean

Returns true if at least one value of the data object passes the condition condition, otherwise false.

example
someProps({ x: 5, y: 10 }, isGt(7));    // true
someProps({ x: 5, y: 10 }, isGt(15)); // false
Source§

someProps<I extends POJO<any>>(
    condition: Predicate<ValueType<I>>,
): (data: I) => boolean

example
someProps(isGt(7))({ x: 5, y: 10 });    // true

Other§

Source§

createArrayRange(start: number, end: number, spacing: SpacingOption): number[]

Creates an array of equally spaced numbers from start to end. You may control the spacing or number of points by setting any of the following:

  • n.intervals e.g., [1, 2, 3] would have two intervals--1 to 2 and 2 to 3 (minimum 1).
  • n.points to set the total number of points (minimum 2)
  • n.size to set the difference between successive points. If end - start does not divide evenly by n.space, then n.space will automatically be adjusted to allow even spacing.

If the value of n is not valid, then an empty array will be returned.

example

createArrayRange(2, 22, { points: 5 }); // [2, 7, 12, 17, 22] createArrayRange(2, 22, { intervals: 5 }); // [2, 6, 10, 14, 18, 22] createArrayRange(2, 22, { space: 4 }); // [2, 6, 10, 14, 18, 22] createArrayRange(2, 22, { space: 4.1 }); // [2, 6, 10, 14, 18, 22]


@category
Source§

isInstanceOf(data: any, className: Object): boolean

Tells whether data is an instance of the class className.

example
class A {}
const a = new A();

isInstanceOf(a, A); // true
isInstanceOf([1, 2, 3], Array); // true
isInstanceOf({}, A); // false
Source§

isInstanceOf(className: Object): (data: any) => boolean

example
class A {}
const a = new A();

isInstanceOf(A)(a); // true

String§

Source§

append<I extends string | any[]>(data: I, value: I): I

Returns a data the value array appended. To append an element (rather than another array) to an array instead, use appendElement.

example
append([2, 3, 4], [10, 11]);            // [2, 3, 4, 10, 11]
append("ostrich", "weasel"); // "ostrichweasel"
Source§

append<I extends string | any[]>(value: I): (data: I) => I

example
append([10, 11])([2, 3, 4]);            // [2, 3, 4, 10, 11]
append<string>("weasel")("ostrich") // "ostrichweasel"
Source§

charAt(data: string, index: number): string

Returns the element of data at index, or an empty string if index is out of bounds or otherwise invalid.

example
charAt("ostrich", 2);       // "t"
charAt("ostrich", -2); // "c"
charAt("ostrich", 10); // ""
Source§

charAt(index: number): (data: string) => string

example
charAt(2)("ostrich");       // "t"
Source§

contains(data: string | any[], value: any): boolean

Indicates whether or not the data array contains the element value, or the data string contains the substring value.

example
contains([2, 3, 4], 4);           // true
contains([2, 3, 4], 10); // false
contains("ostrich", "rich"); // true
contains("ostrich", "cat"); // false
Source§

contains(value: any): (data: string | any[]) => boolean

example
contains(4)([2, 3, 4]);           // true
Source§

everyMatch(data: string, regex: RegExp): string[]

Returns an array containing all matches to regex in the string data, or an empty array if there are no matches. The global flag will be added automatically if not already present on regex, since this function is designed to get all matches. (To return only the first match, use {@link: firstMatch} instead.)

example
everyMatch('weasels', /[aeiou]/);      // ['e', 'a', 'e']
everyMatch('weasels', /[xyz]/); // []
Source§

everyMatch(regex: RegExp): (data: string) => string[]

example
everyMatch(/[aeiou]/)('weasels');      // ['e', 'a', 'e']
Source§

firstMatch(data: string, regex: RegExp): string

Returns the first match to regex in data, or an empty string if no match exists. The global flag will be removed from regex automatically if present, since this function is designed to find only the first match. (To return all matches, use everyMatch instead.)

example
firstMatch('weasel', /[aeiou]/);     // 'e'
firstMatch('weasel', /[xyz]/); // ''
Source§

firstMatch(regex: RegExp): (data: string) => string

example
firstMatch(/[aeiou]/)('weasel');     // 'e'
Source§

insert<I extends string | any[]>(
    data: I,
    index: number,
    content: I extends any[] ? ElementType<I<I>> : string,
): I

Inserts the content element into the data array, or the content substring into the data string at the index position. If index is equal to the length of data, then content will be appended to the end. Returns a shallow copy of data if the index is invalid.

example
insert([2, 3, 4], 1, 10);        // [2, 10, 3, 4]
insert([2, 3, 4], -1, 10); // [2, 3, 10, 4]
insert("ostrich", 1, "aa"); // "oaastrich"
insert("ostrich", 7, "aa"); // "ostrichaa"
Source§

insert<I extends string | any[]>(
    index: number,
    content: I extends any[] ? ElementType<I<I>> : string,
): (data: I) => I

example
 * insert(1, 10)(2, 3, 4]);       // [2, 10, 3, 4]
Source§

isEmpty(data: string | any[]): boolean

Indicates whether a string or array has length zero.

example
isEmpty([]);             // true
isEmpty("ostrich"); // false
Source§

isNotEmpty(data: string | any[]): boolean

Indicates whether a string or array has length of at least one.

example
isNotEmpty("ostrich");      // true
isNotEmpty([]); // false
Source§

length(data: string | any[]): number

Returns the length of a string or array

example
length([2, 3, 4]);      // 3
length("ostrich"); // 7
Source§

lengthPasses(data: string | any[], condition: Predicate<number>): boolean

Indicates whether the length of the data string or array passes the condition.

example
lengthPasses([2, 3, 4], isLt(5));     // true
lengthPasses("ostrich", isLt(5)); // false
Source§

lengthPasses<I extends string | any[]>(
    condition: Predicate<number>,
): (data: string | any[]) => boolean

example
lengthPasses(isLt(5))([2, 3, 4]);     // true
Source§

lowerCase(data: string): string

Returns a copy of a string with all upper case letters changed to lower case.

example
lowerCase('SCUBA Diver');     // 'scuba diver'
Source§

matchesRegex(data: string, regex: RegExp): boolean

Returns true if the string data matches the regular expression regex, false otherwise.

example
matchesRegex('weasels', /easel/);     // true
matchesRegex('weasels', /^easel$/); // false
Source§

matchesRegex(regex: RegExp): (data: string) => boolean

example
matchesRegex(/easel/)('weasels');     // true
matchesRegex(/^easel$/)('weasels'); // false
Source§

padLeft(data: string | number, length: number, chars: string): string

If the string data is shorter than length characters, then adds copies of chars to the left of the string until the string length is reached. If chars is more than one character in length, the last copy will be truncated if necessary to reach the exact target length.

Returns the data unchanged if length is not an integer, or if chars is an empty string.

example
padLeft("ostrich", 10, ' ');    "   ostrich";
padLeft("ostrich", 5, ' '); "ostrich";
padLeft("cat", 10, '123'); "1231231cat";
Source§

padLeft(length: number, chars: string): (data: string | number) => string

example
padLeft(10, ' ')("ostrich");    "   ostrich";
Source§

padRight(data: string | number, length: number, chars: string): string

If the string data is shorter than length characters, then adds copies of chars to the right of the string until the string length is reached. If chars is more than one character in length, the last copy will be truncated if necessary to reach the exact target length.

Returns the data unchanged if length is not an integer, or if chars is an empty string.

example
padRight("ostrich", 10, ' ');    "ostrich   ";
padRight("ostrich", 5, ' '); "ostrich";
padRight("cat", 10, '123'); "cat1231231";
Source§

padRight(length: number, chars: string): (data: string | number) => string

example
padRight(10, ' ')("ostrich");    "ostrich   ";
Source§

prepend<I extends string | any[]>(data: I, value: I): I

Returns a data the value array prepended. To prepend an element (rather than another array) to an array instead, use prependElement.

example
prepend([2, 3, 4], [10, 11]);            // [10, 11, 2, 3, 4]
prepend("ostrich", "weasel"); // "weaselostrich"
Source§

prepend<I extends string | any[]>(value: I): (data: I) => I

example
prepend([10, 11])([2, 3, 4]);            // [10, 11, 2, 3, 4]
prepend<string>("weasel")("ostrich") // "weaselostrich"
Source§

replaceSubstrings(
    data: string,
    target: string | RegExp,
    replacement: string | ((s: string) => string),
): string

Replaces all instances of target in data with either a string replacement or a transformed string if target is a function. If target is aregular expression, the global flag will be added automatically if it isn't already there, since this function always replaces all innstances.

example
replaceAll('ostriches', "s", _);                    // "o_triche_*
replaceAll('ostriches', /[aeiou]/, '_'); // "_str_ch_s*
replaceAll('ostriches', /[aeiou]/, s => s.toUpperCase); // "OstrIchEs"
Source§

replaceSubstrings(
    target: string | RegExp,
    replacement: string | ((s: string) => string),
): (data: string) => string

example
replaceAll("s", _)("ostriches");                    // "o_triche_*
Source§

slice<I extends string | any[]>(data: I, from: number, to: number): I

Returns a shallow copy of the portion of the data array or string beginning at index from and ending with, but not including, the index to. Negative index values count backward from the end of the arrry. Returns an empty array if from or to is not an integer, or if from is out of the array bounds.

example
slice([2, 3, 4, 5, 6], 2, 4);            // [4, 5]
slice([2, 3, 4, 5, 6], 2, -1); // [4, 5]
slice("ostrich", 2, 4); // "tr"
slice("ostrich", 2, -1); // "tr"
slice([2, 3, 4, 5, 6], -4, 10); // [3, 4, 5, 6], going past end of array is OK)
slice([2, 3, 4, 5, 6], -10, 4); // [], starting at -10 is non-sensical
Source§

slice<I extends string | any[]>(from: number, to: number): (data: I) => I

example
slice(2, 4)([2, 3, 4, 5, 6]);            // [4, 5]
Source§

sliceFrom<I extends string | any[]>(data: I, from: number): I

Returns a shallow copy of the data array or string starting from index from. A negative index counts backwards from the end of the array. Returns an empty array if from is not an integer or is out of bounds.

example
sliceFrom([2, 3, 4], 2);        // [4]
sliceFrom([2, 3, 4], -2); // [3, 4]
sliceFrom("ostrich", 2); // "trich"
sliceFrom("ostrich", -2); // "ch"
sliceFrom([2, 3, 4], 6); // []
Source§

sliceFrom<I extends string | any[]>(from: number): (data: I) => I

example
sliceFrom(2)([2, 3, 4]);        // [4]
Source§

sliceTo<I extends string | any[]>(data: I, to: number): I

Return a shallow copy of the data array or string up to, but not including, the index 'to. Returns an empty array if to` is not an integer.

example
sliceTo([2, 3, 4, 5], 2);     // [2, 3]
sliceTo([2, 3, 4, 5], -1); // [2, 3, 4]
sliceTo([2, 3, 4, 5], 6); // [2, 3, 4, 5]
sliceTo("ostrich", 2); // "os"
sliceTo("ostrich", -2); // "ostri"
sliceTo("ostrich", 10); // "ostrich"
Source§

sliceTo<I extends string | any[]>(to: number): (data: I) => I

example
sliceTo(2)([2, 3, 4, 5]);     // [2, 3]
Source§

split(data: string, separator: string | RegExp): string[]

Splits the data string into an array of substrings based on the separator string or regular expression. This is a wrapper around the built-in String.prototype.split (but without the limit parameter), so it will exhibit the same behavior for capturing groups, empty strings, etc. See the MDN reference for details.

example
split("cat,dog", ",");                    // ['cat', 'dog']
split("cat,dog fox4pig", /[^a-z]+/i); // ['cat', 'dog', 'fox', 'pig']
Source§

split(separator: string | RegExp): (data: string) => string[]

example
split(",")("cat,dog");                    // ['cat', 'dog']
Source§

stripWhiteSpace(data: string): string

Removes all white space characters (as identified by the regular expression \s) from the string and returns the result.

example
stripWhiteSpace(" os trich  ");        // "ostrich"
stripWhiteSpace(`ostr
ich`); // "ostrich"
Source§

toHex(data: number): string

Converts a number to a string in hexadecimal form, or returns an empty string if the number is not an non-negative integer.

example
toHex(24);     // "18"
toHex(2.4); // ""
Source§

trim(data: string): string

A thin wrapper around the built-in String.prototype.trim to facilitate use in functional pipelines.

example
trim(" ostrich  ");             // "ostrich"
Source§

trimLeft(data: string): string

A thin wrapper around the built-in String.prototype.trimStart to facilitate use in functional pipelines.

example
trimLeft(" ostrich  ");             // "ostrich  "
Source§

trimRight(data: string): string

A thin wrapper around the built-in String.prototype.trimEnd to facilitate use in functional pipelines.

example
trimRight(" ostrich  ");             // " ostrich"
Source§

upperCase(data: string): string

A thin wrapper around the built-in String.prototype.toUpperCase to facilitate use in functional pipelines.

example
upperCase('SCUBA Diver');     // 'SCUBA DIVER'