diff --git a/package-lock.json b/package-lock.json index 98f324048..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": { @@ -28,7 +27,7 @@ "glob": "^10.2.4", "jest": "^29.0.0", "lolex": "^6.0.0", - "prettier": "3.0.3", + "prettier": "3.1.0", "semantic-release-plugin-update-version-in-files": "^1.0.0", "string-to-arraybuffer": "^1.0.2", "ts-jest": "^29.0.0", @@ -5793,9 +5792,9 @@ } }, "node_modules/prettier": { - "version": "3.0.3", - "resolved": "https://registry.npmjs.org/prettier/-/prettier-3.0.3.tgz", - "integrity": "sha512-L/4pUDMxcNa8R/EthV08Zt42WBO4h1rarVtK0K+QJG0X187OLo7l699jWw0GKuwzkPQ//jMFA/8Xm6Fh3J/DAg==", + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/prettier/-/prettier-3.1.0.tgz", + "integrity": "sha512-TQLvXjq5IAibjh8EpBIkNKxO749UEWABoiIZehEPiY4GNpVdhaFKqSTu+QrlU6D2dPAfubRmtJTi4K4YkQ5eXw==", "dev": true, "bin": { "prettier": "bin/prettier.cjs" diff --git a/package.json b/package.json index f95dea173..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": { @@ -41,7 +40,7 @@ "glob": "^10.2.4", "jest": "^29.0.0", "lolex": "^6.0.0", - "prettier": "3.0.3", + "prettier": "3.1.0", "semantic-release-plugin-update-version-in-files": "^1.0.0", "string-to-arraybuffer": "^1.0.2", "ts-jest": "^29.0.0", 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); + }); +});