Skip to content

sospedra/len

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

len 🍥

Build Status David JavaScript Style Guide

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 length or 0 👀

Meaning that non-array targets always return 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 😍

About

Safely access arrays' length property 🍥

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published