Skip to content

Commit

Permalink
fix(checks): fix #77, update & minor optimization isPlainObject()
Browse files Browse the repository at this point in the history
- add/update tests
- add AUTHORS.md
  • Loading branch information
postspectacular committed Mar 12, 2019
1 parent cf30cd4 commit 47ac88a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 10 deletions.
8 changes: 8 additions & 0 deletions packages/checks/AUTHORS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
## Maintainer

- Karsten Schmidt (@postspectacular)

## Contributors

- Gavin Cannizzaro (@gavinpc-mindgrub)
- Jay Zawrotny (@andrew8er)
9 changes: 5 additions & 4 deletions packages/checks/src/is-plain-object.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const OBJP = Object.getPrototypeOf({});
const OBJP = Object.getPrototypeOf;

/**
* Similar to `isObject()`, but also checks if prototype is that of
Expand All @@ -7,9 +7,10 @@ const OBJP = Object.getPrototypeOf({});
* @param x
*/
export const isPlainObject = (x: any): x is object => {
let proto;
let p;
return (
Object.prototype.toString.call(x) === "[object Object]" &&
((proto = Object.getPrototypeOf(x)), proto === null || proto === OBJP)
x != null &&
typeof x === "object" &&
((p = OBJP(x)) === null || OBJP(p) === null)
);
};
18 changes: 12 additions & 6 deletions packages/checks/test/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as assert from "assert";

import * as vm from "vm";
import { existsAndNotNull } from "../src/exists-not-null";
import { implementsFunction } from "../src/implements-function";
import { isArray } from "../src/is-array";
Expand All @@ -12,8 +12,7 @@ import { isSymbol } from "../src/is-symbol";
import { isTransferable } from "../src/is-transferable";
import { isTypedArray } from "../src/is-typedarray";

describe("checks", function () {

describe("checks", function() {
it("existsAndNotNull", () => {
assert.ok(existsAndNotNull([]), "empty array");
assert.ok(existsAndNotNull(new Uint8Array(1)), "typedarray");
Expand Down Expand Up @@ -66,7 +65,7 @@ describe("checks", function () {
});

it("isObject", () => {
function Foo() { };
function Foo() {}
assert.ok(isObject([]), "empty array");
assert.ok(isObject(new Uint8Array(1)), "typedarray");
assert.ok(isObject({}), "obj");
Expand All @@ -79,17 +78,25 @@ describe("checks", function () {
});

it("isPlainObject", () => {
function Foo() { };
const ctxClass = vm.runInNewContext("class A {}; new A();");
const ctxObj = vm.runInNewContext("({})");

function Foo() {}

assert.ok(isPlainObject({}), "obj");
assert.ok(isPlainObject(Object.create(null)), "obj");
assert.ok(isPlainObject(new Object()), "obj ctor");
assert.ok(!isPlainObject(Foo), "fn");
assert.ok(!isPlainObject((function*() {})()), "generator");
assert.ok(!isPlainObject(new Foo()), "class");
assert.ok(!isPlainObject([]), "empty array");
assert.ok(!isPlainObject(new Uint8Array(1)), "typedarray");
assert.ok(!isPlainObject("[]"), "string");
assert.ok(!isPlainObject(0), "zero");
assert.ok(!isPlainObject(null), "null");
assert.ok(!isPlainObject(undefined), "null");
assert.ok(isPlainObject(ctxObj), "vm ctx obj");
assert.ok(!isPlainObject(ctxClass), "vm ctx class");
});

it("isString", () => {
Expand Down Expand Up @@ -142,5 +149,4 @@ describe("checks", function () {
assert.ok(!isTransferable(null), "null");
assert.ok(!isTransferable(undefined), "undefined");
});

});

0 comments on commit 47ac88a

Please sign in to comment.