A lightweight utility that adds an .isBlank() method to native JavaScript types, inspired by Ruby on Rails' Object#blank?.
npm install is-blank-jsA 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.
``bash`
npm install is-blank-js
First, require the package to extend the prototypes:
`javascript`
require('is-blank-js');
Then, you can use the isBlank() method on your objects:
A string is blank if it's empty or contains only whitespace.
`javascript`
"".isBlank() // => true
" ".isBlank() // => true
"hello".isBlank() // => false
An array is blank if it's empty.
`javascript`
[].isBlank() // => true
[1, 2].isBlank() // => false
An object is blank if it's a plain object with no keys.
`javascript`
({}).isBlank() // => true
({ a: 1 }).isBlank() // => false
A boolean is blank if it's false.
`javascript`
false.isBlank() // => true
true.isBlank() // => false
A number is never blank.
`javascript`
(0).isBlank() // => false
(1).isBlank() // => false
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.
To run the tests:
`bash``
node test.js