Skip to content
This repository was archived by the owner on Feb 20, 2019. It is now read-only.

Commit 739ac80

Browse files
Adam EuryKent C. Dodds
authored andcommitted
feat(isNullOrWhitespace): Add isNullOrWhitespace function
Closes #45
1 parent 9a881d0 commit 739ac80

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

src/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import getObjectSize from './get-object-size'
1010
import isArray from './is-array'
1111
import validateEmail from './validateEmail'
1212
import hex2rgb from './hex2rgb'
13+
import isNullOrWhitespace from './is-null-or-whitespace'
1314

1415
export {
1516
flatten,
@@ -24,4 +25,5 @@ export {
2425
isArray,
2526
validateEmail,
2627
hex2rgb,
28+
isNullOrWhitespace,
2729
}

src/is-null-or-whitespace.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
export default isNullOrWhitespace
2+
3+
/**
4+
* Original Source: http://stackoverflow.com/a/32800728
5+
*
6+
* This method checks whether a string passed as an
7+
* argument is undefined, null, empty, or whitespace.
8+
*
9+
* @param {String} str - The string to check
10+
* @return {Boolean} - True if str is undefined, null, empty, or whitespace else false
11+
*/
12+
function isNullOrWhitespace(str) {
13+
return !str || !str.trim()
14+
}

test/is-null-or-whitespace.test.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import test from 'ava'
2+
import {isNullOrWhitespace} from '../src'
3+
4+
test('check undefined', t => {
5+
const expected = true
6+
const actual = isNullOrWhitespace(undefined)
7+
t.deepEqual(actual, expected)
8+
})
9+
10+
test('check null', t => {
11+
const expected = true
12+
const actual = isNullOrWhitespace(null)
13+
t.deepEqual(actual, expected)
14+
})
15+
16+
test('check an empty string', t => {
17+
const expected = true
18+
const actual = isNullOrWhitespace('')
19+
t.deepEqual(actual, expected)
20+
})
21+
22+
test('check a string of whitespace', t => {
23+
const expected = true
24+
const actual = isNullOrWhitespace(' ')
25+
t.deepEqual(actual, expected)
26+
})
27+
28+
test('check a string with characters', t => {
29+
const expected = false
30+
const actual = isNullOrWhitespace('hello world')
31+
t.deepEqual(actual, expected)
32+
})
33+

0 commit comments

Comments
 (0)