Skip to content
This repository has been archived by the owner on Dec 30, 2023. It is now read-only.

strong-roots-capital/as-array

Repository files navigation

as-array Build status npm version codecov

Promote non-array values to an array

  • dead-simple
  • strongly-typed
  • promotes objects as expected
  • no dependencies

Install

npm install @strong-roots-capital/as-array

Use

import asArray from '@strong-roots-capital/as-array'

console.log(asArray(null))
//=>[]

console.log(asArray(undefined))
//=>[]

console.log(asArray([]))
//=>[]

console.log(asArray('horse'))
//=>[ 'horse' ]

console.log(asArray(1))
//=>[ 1 ]

console.log(asArray([1, 2, 3]))
//=>[ 1, 2, 3 ]

console.log(asArray({foo: 'bar'}))
//=>[ { foo: 'bar' } ]

console.log(asArray([{foo: 'bar'}, {loo: 'fah'}]))
//=>[ { foo: 'bar' }, { loo: 'fah' } ]

Related