0.0.2 • Published 5 years ago

if-any v0.0.2

Weekly downloads
-
License
MIT
Repository
-
Last release
5 years ago

if-any

Run a function if any item in an array is truthy

Installation

npm install --save if-any

Usage

import ifAny from 'if-any'

const array = [false, '', 0, true, 'hello', 100]

ifAny(array, truthyIndexes => console.log(truthyIndexes))
//=> [ 3, 4, 5 ]

Examples

If there is a truthy item in the array, if-any will return the return value of the callback

const array = [false, true, true, true, 'truthy']

const result = ifAny(array, truthyIndexes => {
  return truthyIndexes
    .map(index => `Truthy value: "${array[index]}", at index: ${index}`)
    .join('\n')
})

console.log(result)
//=> Truthy value: "true", at index: 1
//=> Truthy value: "true", at index: 2
//=> Truthy value: "true", at index: 3
//=> Truthy value: "truthy", at index: 4

If there are no truthy items in the array, the callback won't get called and if-any will return false

const callback = () => console.log(`will I run?`)

const result = ifAny([false, null], callback)

console.log(result)
//=> false

See more examples

License

This project is licensed under the MIT License

0.0.2

5 years ago

0.0.1

5 years ago