Skip to content

Commit

Permalink
fix: integrate isPlainObject (#651)
Browse files Browse the repository at this point in the history
  • Loading branch information
Uzlopak committed Nov 22, 2023
1 parent e5d3577 commit 2554219
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 3 deletions.
1 change: 0 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Expand Up @@ -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": {
Expand Down
2 changes: 1 addition & 1 deletion 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";

Expand Down
17 changes: 17 additions & 0 deletions 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)
);
}
31 changes: 31 additions & 0 deletions 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);
});
});

0 comments on commit 2554219

Please sign in to comment.