Skip to content

Instantly share code, notes, and snippets.

@nkpremices
Created September 28, 2022 06:57
Show Gist options
  • Save nkpremices/36b10845128e3134bc1a953274ba0a99 to your computer and use it in GitHub Desktop.
Save nkpremices/36b10845128e3134bc1a953274ba0a99 to your computer and use it in GitHub Desktop.
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