Skip to content

Commit

Permalink
feat: implement isEmptyObject guard
Browse files Browse the repository at this point in the history
  • Loading branch information
abxlfazl authored and ASafaeirad committed Mar 5, 2024
1 parent bb55823 commit 15a0503
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/guards/isEmptyObject.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { isPlainObject } from './isPlainObject';

/**
* Check whether the given value is an empty object or not
*
* @param x any value
* @returns {boolean} true if the value is an empty object
*
* @example
*
* isEmptyObject({}) // true
* isEmptyObject([]) // false
* isEmptyObject(10) // false
* isEmptyObject(true) // false
* isEmptyObject(null) // false
* isEmptyObject("bar") // false
* isEmptyObject({a: 1}) // false
* isEmptyObject(() => {}) // false
* isEmptyObject(new Map()) // false
* isEmptyObject(new Set()) // false
* isEmptyObject([1, "foo"]) // false
* isEmptyObject(new Set([1, true])) // false
* isEmptyObject(new Map([["a", 1]])) // false
*/

export function isEmptyObject(x: unknown): x is boolean {
if (!isPlainObject(x)) return false;

return Object.keys(x).length === 0;
}

0 comments on commit 15a0503

Please sign in to comment.