conditional-array
TypeScript icon, indicating that this package has built-in type declarations

1.0.2 • Public • Published

conditional-array

Helper function to create arrays with conditionally included elements

Installation

npm install conditional-array

Usage

conditionalArray(callback)

Provides a clean syntax for creating an array with conditionally included elements.

Example

conditionalArray(addIf => [
  addIf(true, 'foo'),
  addIf(true, 'bar'),
  addIf(false, 'baz'),
  addIf(word => word.includes('b'), 'qux'),
  addIf(sentence => sentence.length < 10, 'Some really really long sentence.', 'Too long!'),
  'always included'
])
// returns ['foo', 'bar', 'Too long!', 'always included']
conditionalArray(addIf => [
  { name: 'Alice', age: 26 },
  addIf(true, { name: 'Bob', age: 24 }),
  addIf(person => person.age >= 21, { name: 'Claire', age: 20 }),
])
// returns [{ name: 'Alice', age: 23 }, { name: 'Bob', age: 34 }]

Arguments

  • [callback(addIf)] User defined function that returns an array.

    • [addIf(condition, itemIfTrue, [itemIfFalse])] Returns an item or a placeholder value that is removed from the final array.

      • [condition] Either a boolean or predicate function. If it is a function it is invoked with itemIfTrue.

      • [itemIfTrue] The item returned if condition is or returns true.

      • [itemIfFalse] The item returned if condition is or returns false. Defaults to a placeholder value that is removed from the final array.

Returns

The array returned by the callback with the specified elements filtered out.

License

MIT

Acknowledgements

Thanks to stackoverflow user jcalz for helping with the typings

Readme

Keywords

Package Sidebar

Install

npm i conditional-array

Weekly Downloads

25

Version

1.0.2

License

MIT

Unpacked Size

4.61 kB

Total Files

5

Last publish

Collaborators

  • rinaldo646