@httpx/assert
Version:
Assertions and typeguards
1 lines • 3.62 kB
Source Map (JSON)
{"version":3,"sources":["../src/object.guards.ts"],"names":["isPlainObject","v","proto"],"mappings":"AAkEO,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.mjs","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 * 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 *\n * // ⚠️ Edge cases\n * //\n * // 👇 globalThis isn't properly portable across all JS environments\n * //\n *\n * isPlainObject(globalThis); // ✅ with Bun ❌ otherwise (browser, Nodejs, edge, cloudflare)\n *\n * // 👇 Static built-in classes aren't properly checked. This is a trade-off\n * // to maintain the best performance and size. If you need to check for these,\n * // use a custom type guard. But in most cases, you won't need to check for these\n * // as the probability of writing a code that receives these as plain objects is low.\n * // and probably indicates an issue in your code.\n *\n * isPlainObject(Math); // ⚠️✅ return true, but Math is not a plain object\n * isPlainObject(JSON); // ⚠️✅ return true, but JSON is not a plain object\n * isPlainObject(Atomics); // ⚠️✅ return true, but Atomics is not a plain object\n * isPlainObject(Reflect); // ⚠️✅ return true, but Reflect is not a plain object\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"]}