isBlank function
Checks whether a text is missing or holds only whitespace: a required field left empty.
ts
import { isBlank } from 'utils';Signature
ts
export function isBlank(input: string | null | undefined): booleanParameters
| Name | Description |
|---|---|
input | A string, null or undefined. |
Returns
true for null, undefined, '' or whitespace only.
Example
ts
isBlank(' \n'); // true
isBlank(' a '); // falseTests
Line coverage: 100 %.
- ✅ checks null as true
- ✅ checks undefined as true
- ✅ checks "" as true
- ✅ checks " \n\t" as true
- ✅ checks " a " as false
- ✅ checks "0" as false
Source
src/string/is-blank.ts (on GitHub)
Source code
ts
/**
* Checks whether a text is missing or holds only whitespace: a required field left empty.
*
* @param input - A string, `null` or `undefined`.
* @returns `true` for `null`, `undefined`, `''` or whitespace only.
* @example
* isBlank(' \n'); // true
* isBlank(' a '); // false
*/
export function isBlank(input: string | null | undefined): boolean {
return input === null || input === undefined || input.trim() === '';
}Test file
ts
import { isBlank } from './is-blank';
describe(isBlank, () => {
it.for([
[null, true],
[undefined, true],
['', true],
[' \n\t', true],
[' a ', false],
['0', false],
] as const)('checks %j as %s', ([input, expected]) => {
expect(isBlank(input)).toBe(expected);
});
});Keywords: is, blank, strings, function