Use standard array functions on both arrays and non-array types.
Installation
yarn add @bconnorwhite/for-any
npm install @bconnorwhite/for-any
API
Standard Functions:
Extended Functions:
Utility Functions:
Standard Functions
mapAny
Usage:
import { mapAny } from '@bconnorwhite/for-any';
let array = [1, 4, 9, 16];
let item = 5;
let callback = (x)=>x*2;
let arrayResult = mapAny(array, callback);
console.log(arrayResult);
let itemResult = mapAny(item, callback;
console.log(itemResult);
Types:
function mapAny<T, V>(
any: (T | V[]),
callback: (currentValue: (T | V), index?: number, array?: (T | V)[]) => any,
thisArg?: any
): (any | any[]);
filterAny
Example usage:
import { filterAny } from '@bconnorwhite/for-any';
let array = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];
let item1 = 'test';
let item2 = 'testing #2';
let callback = (x)=>x.length > 6;
let arrayResult = filterAny(array, callback);
console.log(arrayResult);
let item1Result = filterAny(item1, callback);
console.log(item1Result);
let item2Result = filterAny(item2, callback);
console.log(item2Result);
Types:
function filterAny<T, V>(
any: (T | V[]),
callback: (element: (T | V), index?: number, array?: (T | V)[]) => boolean,
thisArg?: any
): (T | V[] | undefined);
reduceAny
Example usage:
import { reduceAny } from '@bconnorwhite/for-any';
let array = [1, 2, 3, 4];
let item = 5;
let callback = (accumulator, currentValue) => accumulator + currentValue;
let arrayResult = reduceAny(array, callback);
console.log(arrayResult);
let itemResult = reduceAny(item, callback);
console.log(itemResult);
Types:
function reduceAny<T, V>(
any: (T | V[]),
callback: (accumulator: any, currentValue: (T | V), index?: number, array?: (T | V)[]) => any,
initialValue?: any
): any;
forEachAny
Example usage:
import { forEachAny } from '@bconnorwhite/for-any';
let array = ['a', 'b', 'c'];
let item = 'd';
let callback = (element)=>console.log(element);
forEachAny(array, callback);
forEachAny(item, callback);
Types:
function forEachAny<T, V>(
any: (T | V[]),
callback: (element: (T | V), index?: number, array?: (T | V)[]) => void,
thisArg?: any
): void;
findAny
Example usage:
import { findAny } from '@bconnorwhite/for-any';
let array = [5, 12, 8, 130, 44];
let item1 = 15;
let item2 = 5;
let callback = (element) => element > 10;
let arrayResult = findAny(array, callback);
console.log(arrayResult);
let item1Result = findAny(item1, callback);
console.log(item1Result);
let item2Result = findAny(item2, callback);
console.log(item2Result);
Types:
function findAny<T, V>(
any: (T | V[]),
callback: (element: (T | V), index?: number, array?: (T | V)[]) => boolean,
thisArg?: any
): (T | V | undefined);
Extended Functions
stringReduceAny
Example usage:
import { stringReduceAny } from '@bconnorwhite/for-any';
let array = ["This", "forms", "a", "sentence"];
let item = "Nice";
let callback = (currentValue, index, array) => {
return currentValue + ((index === array.length-1) ? "." : " ");
}
let arrayResult = stringReduceAny(array, callback);
console.log(arrayResult);
let itemResult = reduceAny(item, callback);
console.log(itemResult);
Types:
function stringReduceAny<T, V>(
any: (T | V[]),
callback: (currentValue: (T | V), index?: number, array?: (T | V)[]) => string,
initialValue: string = ""
): string;
Utility Functions
asArray
Example usage:
import { asArray } from '@bconnorwhite/for-any';
let array = [5, 12, 8, 130, 44];
let item = 15;
let arrayResult = asArray(array);
console.log(arrayResult);
let itemResult = asArray(item);
console.log(itemResult);
Types:
function asArray<T>(any: (T | T[])): T[];
Dependencies
Dev Dependencies
License 
MIT