Skip to content

Commit 2554219

Browse files
authored
fix: integrate isPlainObject (#651)
1 parent e5d3577 commit 2554219

File tree

5 files changed

+49
-3
lines changed

5 files changed

+49
-3
lines changed

Diff for: package-lock.json

-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: package.json

-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
"@octokit/endpoint": "^9.0.0",
2626
"@octokit/request-error": "^5.0.0",
2727
"@octokit/types": "^12.0.0",
28-
"is-plain-object": "^5.0.0",
2928
"universal-user-agent": "^6.0.0"
3029
},
3130
"devDependencies": {

Diff for: src/fetch-wrapper.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { isPlainObject } from "is-plain-object";
1+
import { isPlainObject } from "./is-plain-object";
22
import { RequestError } from "@octokit/request-error";
33
import type { EndpointInterface } from "@octokit/types";
44

Diff for: src/is-plain-object.ts

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
export function isPlainObject(value: unknown): value is Object {
2+
if (typeof value !== "object" || value === null) return false;
3+
4+
if (Object.prototype.toString.call(value) !== "[object Object]") return false;
5+
6+
const proto = Object.getPrototypeOf(value);
7+
if (proto === null) return true;
8+
9+
const Ctor =
10+
Object.prototype.hasOwnProperty.call(proto, "constructor") &&
11+
proto.constructor;
12+
return (
13+
typeof Ctor === "function" &&
14+
Ctor instanceof Ctor &&
15+
Function.prototype.call(Ctor) === Function.prototype.call(value)
16+
);
17+
}

Diff for: test/is-plain-object.test.ts

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { isPlainObject } from "../src/is-plain-object";
2+
3+
describe("isPlainObject", () => {
4+
function Foo() {
5+
// @ts-ignore
6+
this.a = 1;
7+
}
8+
9+
it("isPlainObject(NaN)", () => {
10+
expect(isPlainObject(NaN)).toBe(false);
11+
});
12+
it("isPlainObject([1, 2, 3])", () => {
13+
expect(isPlainObject([1, 2, 3])).toBe(false);
14+
});
15+
it("isPlainObject(null)", () => {
16+
expect(isPlainObject(null)).toBe(false);
17+
});
18+
it("isPlainObject({ 'x': 0, 'y': 0 })", () => {
19+
expect(isPlainObject({ x: 0, y: 0 })).toBe(true);
20+
});
21+
it("isPlainObject(Object.create(null))", () => {
22+
expect(isPlainObject(Object.create(null))).toBe(true);
23+
});
24+
it("isPlainObject(Object.create(new Foo()))", () => {
25+
// @ts-ignore
26+
expect(isPlainObject(Object.create(new Foo()))).toBe(false);
27+
});
28+
it("isPlainObject(Object.create(new Date()))", () => {
29+
expect(isPlainObject(Object.create(new Date()))).toBe(false);
30+
});
31+
});

0 commit comments

Comments
 (0)