Skip to content

Commit

Permalink
fix: integrate is-plain-object (#457)
Browse files Browse the repository at this point in the history
  • Loading branch information
Uzlopak authored Nov 23, 2023
1 parent cc5343e commit 4ec784d
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 3 deletions.
2 changes: 1 addition & 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
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
},
"dependencies": {
"@octokit/types": "^12.0.0",
"is-plain-object": "^5.0.0",
"universal-user-agent": "^6.0.0"
},
"release": {
Expand Down
17 changes: 17 additions & 0 deletions src/util/is-plain-object.ts
Original file line number Diff line number Diff line change
@@ -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)
);
}
2 changes: 1 addition & 1 deletion src/util/merge-deep.ts
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
31 changes: 31 additions & 0 deletions test/is-plaint-object.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});

0 comments on commit 4ec784d

Please sign in to comment.