@sospedra/len
v1.0.1
Published
Safely access arrays' length property
Downloads
4
Readme
len
🍥
Safely access arrays' length property
Install
yarn add @sospedra/len
* vendors/scope are a good idea, let's embrace it!
Usage
len
will return the array target or 0
👀
Meaning that non-array targets always returns 0
.
import len from '@sospedra/len'
len([]) // 0
len([1, 2]) // 2
len(9) // 0
len('string') // 0
len(undefined) // 0
Why?
Most of the errorceptions, bugsnags, sentry, etc. looks like:
Cannot read property 'length' of undefined
And that makes me sad 🤷♀️
Also, by returning the length only of arrays we ensure that it's iterable.
Meaning that you can safely check for .map
, .reduce
, et altri
if (!len(array)) array.map(myCallback)
Wonderful 😍