@httpx/plain-object
Version:
Fast and lightweight utility functions to check if a value is a plain object.
1 lines • 5.24 kB
Source Map (JSON)
{"version":3,"sources":["../src/is-plain-object.ts","../src/assert-plain-object.ts","../src/is-static-built-in-class.ts"],"names":["isPlainObject","v","proto","assertPlainObject","msgOrErrorFactory","isStaticBuiltInClass"],"mappings":"aAqDO,IAAMA,CAGXC,CAAAA,CAAAA,EAGyB,CACzB,GAAIA,CAAM,GAAA,IAAA,EAAQ,OAAOA,CAAAA,EAAM,QAC7B,CAAA,OAAO,MAGT,CAAA,IAAMC,CAAQ,CAAA,MAAA,CAAO,cAAeD,CAAAA,CAAC,CACrC,CAAA,OACEC,CAAU,GAAA,IAAA,EACVA,CAAU,GAAA,MAAA,CAAO,SAEjB,EAAA,MAAA,CAAO,cAAeA,CAAAA,CAAK,CAAM,GAAA,IAErC,EC5BO,SAASC,CAGdF,CAAAA,CAAAA,CACAG,CAGsB,CAAA,CACtB,GAAI,CAACJ,CAAsBC,CAAAA,CAAC,CAC1B,CAAA,MACEG,CAAsB,GAAA,SAAA,EACtB,OAAOA,CAAAA,EAAsB,QAEvB,CAAA,IAAI,SAAUA,CAAAA,CAAAA,EAAqB,mBAAmB,CAAA,CAExDA,CAAkB,EAE5B,CC1DO,IAAMC,CAAwBJ,CAAAA,CAAAA,EAC5BA,CAAM,GAAA,IAAA,EAAQA,CAAM,GAAA,IAAA,EAAQA,CAAM,GAAA","file":"index.cjs","sourcesContent":["import type { BasePlainObject, DefaultBasePlainObject } from './internal.types';\nimport type { PlainObject } from './plain-object.types';\n\n/**\n * Check if a value is a plain object\n *\n * A plain object is a basic JavaScript object, such as {}, { data: [] }, new Object() or Object.create(null).\n *\n * @example\n * ```typescript\n * import { isPlainObject } from '@httpx/plain-object';\n *\n * // ✅👇 True\n *\n * isPlainObject({ }); // ✅\n * isPlainObject({ key: 'value' }); // ✅\n * isPlainObject({ key: new Date() }); // ✅\n * isPlainObject(new Object()); // ✅\n * isPlainObject(Object.create(null)); // ✅\n * isPlainObject({ nested: { key: true} }); // ✅\n * isPlainObject(new Proxy({}, {})); // ✅\n * isPlainObject({ [Symbol('tag')]: 'A' }); // ✅\n *\n * // ✅👇 (node context, workers, ...)\n * const runInNewContext = await import('node:vm').then(\n * (mod) => mod.runInNewContext\n * );\n * isPlainObject(runInNewContext('({})')); // ✅\n *\n * // ❌👇 False\n *\n * class Test { };\n * isPlainObject(new Test()) // ❌\n * isPlainObject(10); // ❌\n * isPlainObject(null); // ❌\n * isPlainObject('hello'); // ❌\n * isPlainObject([]); // ❌\n * isPlainObject(new Date()); // ❌\n * isPlainObject(new Uint8Array([1])); // ❌\n * isPlainObject(Buffer.from('ABC')); // ❌\n * isPlainObject(Promise.resolve({})); // ❌\n * isPlainObject(Object.create({})); // ❌\n * isPlainObject(new (class Cls {})); // ❌\n * isPlainObject(globalThis); // ❌\n *\n * // ✅👇 Note that static built-in classes are treated as plain objects\n * // check for `isStaticBuiltInClass` to exclude if needed\n *\n * isPlainObject(Math); // ✅\n * isPlainObject(JSON); // ✅\n * isPlainObject(Atomics); // ✅\n * ```\n */\nexport const isPlainObject = <\n TValue extends BasePlainObject = DefaultBasePlainObject,\n>(\n v: unknown\n): v is TValue extends DefaultBasePlainObject\n ? BasePlainObject\n : PlainObject<TValue> => {\n if (v === null || typeof v !== 'object') {\n return false;\n }\n\n const proto = Object.getPrototypeOf(v) as typeof Object.prototype | null;\n return (\n proto === null ||\n proto === Object.prototype ||\n // Required to support node:vm.runInNewContext({})\n Object.getPrototypeOf(proto) === null\n );\n};\n","import type {\n BasePlainObject,\n DefaultBasePlainObject,\n MsgOrErrorFactory,\n} from './internal.types';\nimport { isPlainObject } from './is-plain-object';\nimport type { PlainObject } from './plain-object.types';\n\n/**\n * Assert a value is a plain object\n *\n * @example\n * ```typescript\n * import { assertPlainObject } from '@httpx/plain-object';\n * import type { PlainObject } from '@httpx/plain-object';\n *\n * function fn(value: unknown) {\n *\n * // 👇 Throws `new TypeError('Not a plain object')` if not a plain object\n * assertPlainObject(value);\n *\n * // 👇 Throws `new TypeError('Custom message')` if not a plain object\n * assertPlainObject(value, 'Custom message');\n *\n * // 👇 Throws custom error if not a plain object\n * assertPlainObject(value, () => {\n * throw new HttpBadRequest('Custom message');\n * });\n *\n * return value;\n * }\n *\n * try {\n * const value = fn({ key: 'value' });\n * // ✅ Value is known to be PlainObject<unknown>\n * assertType<PlainObject>(value);\n * } catch (error) {\n * console.error(error);\n * }\n * ```\n *\n * @throws TypeError\n */\nexport function assertPlainObject<\n TValue extends BasePlainObject = DefaultBasePlainObject,\n>(\n v: unknown,\n msgOrErrorFactory?: MsgOrErrorFactory\n): asserts v is TValue extends DefaultBasePlainObject\n ? BasePlainObject\n : PlainObject<TValue> {\n if (!isPlainObject<TValue>(v)) {\n if (\n msgOrErrorFactory === undefined ||\n typeof msgOrErrorFactory === 'string'\n ) {\n throw new TypeError(msgOrErrorFactory ?? `Not a PlainObject`);\n }\n throw msgOrErrorFactory();\n }\n}\n","export type StaticBuiltInClass = Math | JSON | Atomics;\n\nexport const isStaticBuiltInClass = (v: unknown): v is StaticBuiltInClass => {\n return v === Math || v === JSON || v === Atomics;\n};\n"]}