https://github.com/sospedra/len
Safely access arrays' length property 🍥
https://github.com/sospedra/len
array length micro util
Last synced: 7 days ago
JSON representation
Safely access arrays' length property 🍥
- Host: GitHub
- URL: https://github.com/sospedra/len
- Owner: sospedra
- License: mit
- Created: 2018-01-17T14:35:23.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2018-01-18T07:48:19.000Z (over 8 years ago)
- Last Synced: 2026-07-13T02:53:36.619Z (7 days ago)
- Topics: array, length, micro, util
- Language: JavaScript
- Homepage:
- Size: 43 KB
- Stars: 5
- Watchers: 0
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
`len` 🍥
========
[](https://travis-ci.org/sospedra/len)
[]()
[](https://standardjs.com)
Safely access arrays' length property
### Install
```bash
yarn add @sospedra/len
```
*\* vendors/scope are a good idea, let's embrace it!*
### Usage
`len` will return the array target length or `0` 👀
Meaning that **non-array targets always return `0`**.
```js
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*
```js
if (len(array)) array.map(myCallback)
```
Wonderful 😍