diff --git a/package-lock.json b/package-lock.json index 38221fb5..c18856ca 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,7 +10,6 @@ "license": "MIT", "dependencies": { "@octokit/types": "^12.0.0", - "is-plain-object": "^5.0.0", "universal-user-agent": "^6.0.0" }, "devDependencies": { @@ -4075,6 +4074,7 @@ "version": "5.0.0", "resolved": "https://registry.npmjs.org/is-plain-object/-/is-plain-object-5.0.0.tgz", "integrity": "sha512-VRSzKkbMm5jMDoKLbltAkFQ5Qr7VDiTFGXxYFXXowVj387GeGNOCsOH6Msy00SGZ3Fp84b1Naa1psqgcCIEP5Q==", + "dev": true, "engines": { "node": ">=0.10.0" } diff --git a/package.json b/package.json index 7db2b58c..e0e32a7a 100644 --- a/package.json +++ b/package.json @@ -35,7 +35,6 @@ }, "dependencies": { "@octokit/types": "^12.0.0", - "is-plain-object": "^5.0.0", "universal-user-agent": "^6.0.0" }, "release": { diff --git a/src/util/is-plain-object.ts b/src/util/is-plain-object.ts new file mode 100644 index 00000000..2addec5d --- /dev/null +++ b/src/util/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/src/util/merge-deep.ts b/src/util/merge-deep.ts index f5019c09..9b38cfc3 100644 --- a/src/util/merge-deep.ts +++ b/src/util/merge-deep.ts @@ -1,4 +1,4 @@ -import { isPlainObject } from "is-plain-object"; +import { isPlainObject } from "./is-plain-object"; export function mergeDeep(defaults: any, options: any): object { const result = Object.assign({}, defaults); diff --git a/test/is-plaint-object.test.ts b/test/is-plaint-object.test.ts new file mode 100644 index 00000000..44071449 --- /dev/null +++ b/test/is-plaint-object.test.ts @@ -0,0 +1,31 @@ +import { isPlainObject } from "../src/util/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); + }); +});