refactor: share plain object guard across config and utils

This commit is contained in:
Peter Steinberger
2026-02-19 14:10:58 +00:00
parent 397f243ded
commit ba538c98c7
4 changed files with 33 additions and 21 deletions

View File

@@ -0,0 +1,18 @@
import { describe, expect, it } from "vitest";
import { isPlainObject } from "./plain-object.js";
describe("isPlainObject", () => {
it("accepts plain objects", () => {
expect(isPlainObject({})).toBe(true);
expect(isPlainObject({ a: 1 })).toBe(true);
});
it("rejects non-plain values", () => {
expect(isPlainObject(null)).toBe(false);
expect(isPlainObject([])).toBe(false);
expect(isPlainObject(new Date())).toBe(false);
expect(isPlainObject(/re/)).toBe(false);
expect(isPlainObject("x")).toBe(false);
expect(isPlainObject(42)).toBe(false);
});
});

11
src/infra/plain-object.ts Normal file
View File

@@ -0,0 +1,11 @@
/**
* Strict plain-object guard (excludes arrays and host objects).
*/
export function isPlainObject(value: unknown): value is Record<string, unknown> {
return (
typeof value === "object" &&
value !== null &&
!Array.isArray(value) &&
Object.prototype.toString.call(value) === "[object Object]"
);
}