A TypeScript utility library that provides a variety of functions for validating and inspecting objects. This library includes utilities for deep equality checks, verifying object properties like emptiness, sealed or frozen states, and more. With clear and descriptive function names, this library is ideal for developers looking to perform robust object validations in their TypeScript projects.
NPM:
npx jsr add @checker/object
PNPM:
pnpm dlx jsr add @checker/object
Deno:
deno add @checker/object
Yarn:
yarn dlx jsr add @checker/object
Bun:
bunx jsr add @checker/object
Checks if two objects are deeply equal.
check_is_deep_equal({ a: 1, b: 2 }, { a: 1, b: 2 }); // true
check_is_deep_equal({ a: 1, b: 2 }, { a: 2, b: 3 }); // false
check_is_not_deep_equal({ a: 1, b: 2 }, { a: 2, b: 3 }); // true
check_is_not_deep_equal({ a: 1, b: 2 }, { a: 1, b: 2 }); // false
Checks if a given object is an empty plain object.
check_is_empty_object({}); // true
check_is_empty_object(new Date()); // false
check_is_empty_object({ a: 1 }); // false
Checks if a given object is not an empty plain object.
check_is_not_empty_object(new Date()); // true
check_is_not_empty_object({ a: 1 }); // true
check_is_not_empty_object({}); // false
Checks if a given key exists in an object.
Note
This function does not type guard, if you need type guarding please use the
key in object
syntax.
check_key_exists_in_object({ a: 1, b: 2 }, "a"); // true, The key 'a' exists in the object
check_key_exists_in_object({ a: 1, b: 2 }, "c"); // false
type Obj = { a: 1; b: 2 } | { c: 3; d: 4 };
const obj = {} as Obj;
if ("c" in obj) {
obj; // obj type is { c: 3, d: 4 }
}
if (check_key_exists_in_object(obj, "c")) {
obj; // obj type is Obj
}
Checks if a given key does not exist in an object.
check_key_does_not_exist_in_object({ a: 1, b: 2 }, "c"); // true
check_key_does_not_exist_in_object({ a: 1, b: 2 }, "a"); // false
Checks if a given object is frozen.
check_is_frozen(Object.freeze({ a: 1 })); // true
check_is_frozen({ a: 1 }); // false
Checks if a given object is not frozen.
check_is_not_frozen({ a: 1 }); // true
check_is_not_frozen(Object.freeze({ a: 1 })); // false
Checks if a given value is an object.
check_is_object({ a: 1 }); // true
check_is_object(null); // false
check_is_object("string"); // false
Checks if a given value is not an object.
check_is_not_object("string"); // true
check_is_not_object({ a: 1 }); // false
Checks if a given object is sealed.
check_is_sealed(Object.seal({ a: 1 })); // true
check_is_sealed({ a: 1 }); // false
Checks if a given object is not sealed.
check_is_not_sealed({ a: 1 }); // true
check_is_not_sealed(Object.seal({ a: 1 })); // false
MIT