@bconnorwhite/for-any
TypeScript icon, indicating that this package has built-in type declarations

2.0.0 • Public • Published

@bconnorwhite/for-any

NPM TypeScript

Use standard array functions on both arrays and non-array types.

If I should maintain this repo, please ⭐️ GitHub stars

DM me on Twitter if you have questions or suggestions. Twitter


Installation

yarn add @bconnorwhite/for-any
npm install @bconnorwhite/for-any
pnpm add @bconnorwhite/for-any

Usage

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);
// output: [2, 8, 18, 32]

let itemResult = mapAny(item, callback;
console.log(itemResult);
// output: 10

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);
// 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"

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);
// output: 10

let itemResult = reduceAny(item, callback);
console.log(itemResult);
// output: 5

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);
// output: "a"
// output: "b"
// output: "c"

forEachAny(item, callback);
// output: "d"

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);
// output: 12

let item1Result = findAny(item1, callback);
console.log(item1Result);
// output: 15

let item2Result = findAny(item2, callback);
console.log(item2Result);
// output: 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);
// output: "This forms a sentence."

let itemResult = reduceAny(item, callback);
console.log(itemResult);
// output: "Nice."

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);
// output: [5, 12, 8, 130, 44]

let itemResult = asArray(item);
console.log(itemResult);
// output: [15]

Dependenciesdependencies


Dev Dependencies

  • autorepo: Autorepo abstracts away your dev dependencies, providing a single command to run all of your scripts.

License license

MIT

Package Sidebar

Install

npm i @bconnorwhite/for-any

Weekly Downloads

1

Version

2.0.0

License

MIT

Unpacked Size

10.4 kB

Total Files

8

Last publish

Collaborators

  • bconnorwhite