Use standard array functions on both arrays and non-array types.
If I should maintain this repo, please ⭐️
DM me on Twitter if you have questions or suggestions.
yarn add @bconnorwhite/for-any
npm install @bconnorwhite/for-any
pnpm add @bconnorwhite/for-any
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);
// output: [2, 8, 18, 32]
let itemResult = mapAny(item, callback;
console.log(itemResult);
// output: 10
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);
// output: ["exuberant", "destruction", "present"]
let item1Result = filterAny(item1, callback);
console.log(item1Result);
// output: undefined
let item2Result = filterAny(item2, callback);
console.log(item2Result);
// output: "testing #2"
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);
// output: 10
let itemResult = reduceAny(item, callback);
console.log(itemResult);
// output: 5
import { forEachAny } from '@bconnorwhite/for-any';
let array = ['a', 'b', 'c'];
let item = 'd';
let callback = (element)=>console.log(element);
forEachAny(array, callback);
// output: "a"
// output: "b"
// output: "c"
forEachAny(item, callback);
// output: "d"
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);
// output: 12
let item1Result = findAny(item1, callback);
console.log(item1Result);
// output: 15
let item2Result = findAny(item2, callback);
console.log(item2Result);
// output: undefined
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);
// output: "This forms a sentence."
let itemResult = reduceAny(item, callback);
console.log(itemResult);
// output: "Nice."
import { asArray } from '@bconnorwhite/for-any';
let array = [5, 12, 8, 130, 44];
let item = 15;
let arrayResult = asArray(array);
console.log(arrayResult);
// output: [5, 12, 8, 130, 44]
let itemResult = asArray(item);
console.log(itemResult);
// output: [15]
- as-typed-array: Make any value an array
- autorepo: Autorepo abstracts away your dev dependencies, providing a single command to run all of your scripts.