is-blank-js
v1.0.0
Published
A lightweight utility that adds an .isBlank() method to native JavaScript types, inspired by Ruby on Rails' Object#blank?.
Maintainers
Readme
is-blank-js
A JavaScript utility to check for blank values, inspired by Ruby on Rails' blank? method.
This library extends the prototypes of String, Array, Object, and Boolean to include an isBlank() method.
Installation
npm install is-blank-jsUsage
First, require the package to extend the prototypes:
require('is-blank-js');Then, you can use the isBlank() method on your objects:
Strings
A string is blank if it's empty or contains only whitespace.
"".isBlank() // => true
" ".isBlank() // => true
"hello".isBlank() // => falseArrays
An array is blank if it's empty.
[].isBlank() // => true
[1, 2].isBlank() // => falseObjects
An object is blank if it's a plain object with no keys.
({}).isBlank() // => true
({ a: 1 }).isBlank() // => falseBooleans
A boolean is blank if it's false.
false.isBlank() // => true
true.isBlank() // => falseNumbers
A number is never blank.
(0).isBlank() // => false
(1).isBlank() // => falseComparison to Rails' blank?
This library aims to replicate the behavior of Rails' blank? method for common JavaScript types.
| Value in JavaScript | isBlank() |
| ------------------- | ----------- |
| "" | true |
| " " | true |
| [] | true |
| {} | true |
| false | true |
| true | false |
| 0 | false |
| null | (error) |
| undefined | (error) |
Note: This library does not handle null or undefined because they don't have prototypes to extend. Calling isBlank() on null or undefined will result in a TypeError.
Development
To run the tests:
node test.js