remeda
Version:
A utility library for JavaScript and Typescript.
1 lines • 2.02 kB
Source Map (JSON)
{"version":3,"file":"isPlainObject.cjs","names":[],"sources":["../src/isPlainObject.ts"],"sourcesContent":["import type { NarrowedTo } from \"./internal/types/NarrowedTo\";\n\n/**\n * Checks if `data` is a \"plain\" object. A plain object is defined as an object with string keys and values of any type, including primitives, other objects, functions, classes, etc (aka struct/shape/record/simple). Technically, a plain object is one whose prototype is either `Object.prototype` or `null`, ensuring it does not inherit properties or methods from other object types.\n *\n * This function is narrower in scope than `isObjectType`, which accepts any entity considered an `\"object\"` by JavaScript's `typeof`.\n *\n * Note that Maps, Arrays, and Sets are not considered plain objects and would return `false`.\n *\n * @param data - The variable to check.\n * @returns The input type, narrowed to only plain objects.\n * @signature\n * R.isPlainObject(data)\n * @example\n * // true\n * R.isPlainObject({}) //=> true\n * R.isPlainObject({ a: 123 }) //=> true\n *\n * // false\n * R.isPlainObject([]) //=> false\n * R.isPlainObject(Promise.resolve(\"something\")) //=> false\n * R.isPlainObject(new Date()) //=> false\n * R.isPlainObject(new Error(\"error\")) //=> false\n * R.isPlainObject('somethingElse') //=> false\n * R.isPlainObject(null) //=> false\n * @category Guard\n */\nexport function isPlainObject<T>(\n data: Readonly<Record<PropertyKey, unknown>> | T,\n): data is NarrowedTo<T, Record<PropertyKey, unknown>> {\n if (typeof data !== \"object\" || data === null) {\n return false;\n }\n\n // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment -- This is a low-level check, we can't avoid it being typed as `any`.\n const proto = Object.getPrototypeOf(data);\n return proto === null || proto === Object.prototype;\n}\n"],"mappings":"AA2BA,SAAgB,EACd,EACqD,CACrD,GAAI,OAAO,GAAS,WAAY,EAC9B,MAAO,GAIT,IAAM,EAAQ,OAAO,eAAe,EAAK,CACzC,OAAO,IAAU,MAAQ,IAAU,OAAO"}