isEmpty

isEmpty helps normalize emptiness checks across mixed data shapes.

It treats null, undefined, empty strings (including whitespace-only), empty arrays, empty maps/sets, and plain objects with no own keys as empty.

When to use

  • Validating form payloads before submit.
  • Guarding API parameters that can be optional or polymorphic.
  • Creating reusable predicates for filtering or conditional rendering.

Behavior notes

  • Whitespace-only strings are considered empty.
  • Arrays, maps, and sets are evaluated by length/size.
  • Only plain objects are considered for empty object checks.
  • Numbers, booleans, dates, regex values, and class instances are not considered empty.

Examples

isEmpty('   ');
// true
isEmpty({ status: 'active' });
// false

Usage

import { isEmpty } from '@branditdev/utils';

const blankField = '   ';
const tags = ['docs'];
const payload = {};

export const isEmptyDemo = {
  blankFieldIsEmpty: isEmpty(blankField),
  tagsAreEmpty: isEmpty(tags),
  payloadIsEmpty: isEmpty(payload),
};

API

isEmpty(value): boolean

Defined in: validators/isEmpty/isEmpty.ts:21

Checks whether a value is empty.

Empty values include null, undefined, empty strings (after trimming), empty arrays, empty maps, empty sets, and plain objects with no own keys.

Parameters

NameTypeOptionalDefaultDescription
valueunknownno-The value to evaluate.

Returns

boolean

true when the value is considered empty, otherwise false.

Source

export function isEmpty(value: unknown): boolean {
  if (value == null) return true;

  if (typeof value === 'string') {
    return value.trim().length === 0;
  }

  if (Array.isArray(value)) {
    return value.length === 0;
  }

  if (value instanceof Map || value instanceof Set) {
    return value.size === 0;
  }

  if (isPlainObject(value)) {
    return Object.keys(value).length === 0;
  }

  return false;
}

function isPlainObject(value: unknown): value is Record<string, unknown> {
  if (typeof value !== 'object' || value === null) {
    return false;
  }

  const prototype = Object.getPrototypeOf(value) as never;
  return prototype === Object.prototype || prototype === null;
}