Created
September 28, 2022 06:57
-
-
Save nkpremices/36b10845128e3134bc1a953274ba0a99 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const isFalsy = value => !value; | |
const isWhitespaceString = value => | |
typeof value === 'string' && /^\s*$/.test(value); | |
const isEmptyCollection = value => | |
(Array.isArray(value) || value === Object(value)) && | |
!Object.keys(value).length; | |
const isInvalidDate = value => | |
value instanceof Date && Number.isNaN(value.getTime()); | |
const isEmptySet = value => value instanceof Set && value.size === 0; | |
const isEmptyMap = value => value instanceof Map && value.size === 0; | |
const isBlank = value => { | |
if (isFalsy(value)) return true; | |
if (isWhitespaceString(value)) return true; | |
if (isEmptyCollection(value)) return true; | |
if (isInvalidDate(value)) return true; | |
if (isEmptySet(value)) return true; | |
if (isEmptyMap(value)) return true; | |
return false; | |
}; | |
isBlank(null); // true | |
isBlank(undefined); // true | |
isBlank(0); // true | |
isBlank(false); // true | |
isBlank(''); // true | |
isBlank(' \r\n '); // true | |
isBlank(NaN); // true | |
isBlank([]); // true | |
isBlank({}); // true | |
isBlank(new Date('hello')); // true | |
isBlank(new Set()); // true | |
isBlank(new Map()); // true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment