UNPKG

@httpx/assert

Version:
1 lines 4.27 kB
{"version":3,"sources":["../src/object.guards.ts"],"names":["isPlainObject","v","proto"],"mappings":"aA+FO,IAAMA,CAAAA,CAGXC,CAAAA,EAGyB,CACzB,GAAIA,CAAAA,GAAM,IAAA,EAAQ,OAAOA,CAAAA,EAAM,QAAA,CAC7B,OAAO,MAAA,CAGT,IAAMC,CAAAA,CAAQ,MAAA,CAAO,cAAA,CAAeD,CAAC,CAAA,CACrC,OACEC,CAAAA,GAAU,IAAA,EACVA,CAAAA,GAAU,MAAA,CAAO,SAAA,EAEjB,MAAA,CAAO,cAAA,CAAeA,CAAK,CAAA,GAAM,IAErC","file":"object.guards.cjs","sourcesContent":["import type {\n BasePlainObject,\n DefaultBasePlainObject,\n} from './object.internal.types';\nimport type { PlainObject } from './object.types';\n\n/**\n * Check if a value is a plain object\n *\n * An object is plain if it's created by either {}, new Object(), or Object.create(null).\n *\n * @example\n * ```typescript\n * import { isPlainObject } from '@httpx/plain-object';\n *\n * // ✅👇 True\n *\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(Math); // ❌ Static built-in classes\n * isPlainObject(Promise.resolve({})); // ❌\n * isPlainObject(Object.create({})); // ❌\n * ```\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 *\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 */\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"]}