From 2554219c5bd805635b0fd3f96578c4c3e3ab988b Mon Sep 17 00:00:00 2001 From: Aras Abbasi Date: Wed, 22 Nov 2023 01:19:33 +0100 Subject: [PATCH] fix: integrate isPlainObject (#651) --- package-lock.json | 1 - package.json | 1 - src/fetch-wrapper.ts | 2 +- src/is-plain-object.ts | 17 +++++++++++++++++ test/is-plain-object.test.ts | 31 +++++++++++++++++++++++++++++++ 5 files changed, 49 insertions(+), 3 deletions(-) create mode 100644 src/is-plain-object.ts create mode 100644 test/is-plain-object.test.ts diff --git a/package-lock.json b/package-lock.json index f25ac4ca3..e5b22d227 100644 --- a/package-lock.json +++ b/package-lock.json @@ -12,7 +12,6 @@ "@octokit/endpoint": "^9.0.0", "@octokit/request-error": "^5.0.0", "@octokit/types": "^12.0.0", - "is-plain-object": "^5.0.0", "universal-user-agent": "^6.0.0" }, "devDependencies": { diff --git a/package.json b/package.json index 7b2872dc7..968114e90 100644 --- a/package.json +++ b/package.json @@ -25,7 +25,6 @@ "@octokit/endpoint": "^9.0.0", "@octokit/request-error": "^5.0.0", "@octokit/types": "^12.0.0", - "is-plain-object": "^5.0.0", "universal-user-agent": "^6.0.0" }, "devDependencies": { diff --git a/src/fetch-wrapper.ts b/src/fetch-wrapper.ts index 68bb78001..ef41903cf 100644 --- a/src/fetch-wrapper.ts +++ b/src/fetch-wrapper.ts @@ -1,4 +1,4 @@ -import { isPlainObject } from "is-plain-object"; +import { isPlainObject } from "./is-plain-object"; import { RequestError } from "@octokit/request-error"; import type { EndpointInterface } from "@octokit/types"; diff --git a/src/is-plain-object.ts b/src/is-plain-object.ts new file mode 100644 index 000000000..2addec5d5 --- /dev/null +++ b/src/is-plain-object.ts @@ -0,0 +1,17 @@ +export function isPlainObject(value: unknown): value is Object { + if (typeof value !== "object" || value === null) return false; + + if (Object.prototype.toString.call(value) !== "[object Object]") return false; + + const proto = Object.getPrototypeOf(value); + if (proto === null) return true; + + const Ctor = + Object.prototype.hasOwnProperty.call(proto, "constructor") && + proto.constructor; + return ( + typeof Ctor === "function" && + Ctor instanceof Ctor && + Function.prototype.call(Ctor) === Function.prototype.call(value) + ); +} diff --git a/test/is-plain-object.test.ts b/test/is-plain-object.test.ts new file mode 100644 index 000000000..962c915b2 --- /dev/null +++ b/test/is-plain-object.test.ts @@ -0,0 +1,31 @@ +import { isPlainObject } from "../src/is-plain-object"; + +describe("isPlainObject", () => { + function Foo() { + // @ts-ignore + this.a = 1; + } + + it("isPlainObject(NaN)", () => { + expect(isPlainObject(NaN)).toBe(false); + }); + it("isPlainObject([1, 2, 3])", () => { + expect(isPlainObject([1, 2, 3])).toBe(false); + }); + it("isPlainObject(null)", () => { + expect(isPlainObject(null)).toBe(false); + }); + it("isPlainObject({ 'x': 0, 'y': 0 })", () => { + expect(isPlainObject({ x: 0, y: 0 })).toBe(true); + }); + it("isPlainObject(Object.create(null))", () => { + expect(isPlainObject(Object.create(null))).toBe(true); + }); + it("isPlainObject(Object.create(new Foo()))", () => { + // @ts-ignore + expect(isPlainObject(Object.create(new Foo()))).toBe(false); + }); + it("isPlainObject(Object.create(new Date()))", () => { + expect(isPlainObject(Object.create(new Date()))).toBe(false); + }); +});