The isEmpty
function checks if the input string is empty or contains only whitespace characters. It returns true
if the input string is empty or consists of only whitespace characters, and false
otherwise.
The function can be imported using ES6 syntax from the "multiform-validator" package:
import { isEmpty } from 'multiform-validator';
Alternatively, you can import the function using CommonJS syntax with require
(Node.js):
const { isEmpty } = require('multiform-validator');
The function takes one parameter:
value
(string) - The input string to check if it is empty or contains only whitespace characters.// Example 1 - Empty or whitespace strings
const result1 = isEmpty(''); // true
console.log(result1);
const result2 = isEmpty(' '); // true
console.log(result2);
// Example 2 - Non-empty strings
const result3 = isEmpty('Hello'); // false
console.log(result3);
const result4 = isEmpty(' Hello '); // false
console.log(result4);
// Example 3 - Non-string inputs (will throw an error)
const result5 = isEmpty(null);
console.log(result5);
const result6 = isEmpty(undefined);
console.log(result6);
The function expects the input value to be passed as a string. If the input is not a string, the function throws a TypeError. It removes any leading and trailing whitespace from the input string using the. trim()
method and checks if the resulting string is empty (i.e., has a length of 0). If the input string consists of only whitespace characters, the function also returns. true
as it considers such strings to be empty.