Deep-check whether a value holds no real user input
A long survey form autosaves drafts. Before showing “You have an unsaved draft”, the app checks whether the user actually entered anything. A draft full of empty strings, whitespace, and empty sections does not count, but a 0 rating or an unchecked box (false) is a real answer. Implement isBlank(value), which returns true when value holds no real input.
isBlank(value) // => boolean
| Value | Blank when |
|---|---|
null, undefined |
always |
| string | it is empty or contains only whitespace (value.trim() === '') |
| number | it is NaN. Every other number, including 0, is not blank. |
| boolean, bigint, symbol, function | never |
| array | every element is blank. Holes count as blank, so [] and new Array(3) are blank. |
| plain object | every own enumerable string-keyed value is blank (symbol keys are ignored), so {} is blank |
Map |
every value is blank (keys are ignored) |
Set |
every member is blank |
Date |
it is an invalid date (getTime() is NaN) |
any other object (class instances, RegExp, typed arrays, …) |
never |
A plain object has the prototype Object.prototype or null.
Circular references: drafts can reference themselves. While checking a container, a reference back to a container that is still being checked counts as blank. Your function must always finish.
isBlank(' '); // => true
isBlank(0); // => false
isBlank(false); // => false
isBlank(['', null, [' ']]); // => true
isBlank({ name: '', address: { city: ' ' } }); // => true
isBlank({ rating: 0 }); // => false
isBlank(new Map([['note', '']])); // => true
isBlank(new Date('not a date')); // => true
isBlank(/x/); // => false
const draft = { title: '' };
draft.self = draft;
isBlank(draft); // => true