Skip to content

Commit

Permalink
fix(checks): add prototype check for isPlainObject(), add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Apr 4, 2018
1 parent 9c91dc3 commit bffc443
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
10 changes: 9 additions & 1 deletion packages/checks/src/is-plain-object.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
/**
* Similar to `isObject()`, but also checks if prototype is that of
* `Object` (or `null`).
*
* @param x
*/
export function isPlainObject(x: any): x is Object {
return Object.prototype.toString.call(x) === "[object Object]";
let proto;
return Object.prototype.toString.call(x) === "[object Object]" &&
(proto = Object.getPrototypeOf(x), proto === null || proto === Object.getPrototypeOf({}));
}
18 changes: 18 additions & 0 deletions packages/checks/test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { isTransferable } from "../src/is-transferable";
import { isTypedArray } from "../src/is-typedarray";

describe("checks", function () {

it("existsAndNotNull", () => {
assert(existsAndNotNull([]), "empty array");
assert(existsAndNotNull(new Uint8Array(1)), "typedarray");
Expand All @@ -23,6 +24,7 @@ describe("checks", function () {
assert(!existsAndNotNull(null), "null");
assert(!existsAndNotNull(undefined), "null");
});

it("isArray", () => {
assert(isArray([]), "empty array");
assert(!isArray(new Uint8Array(1)), "typedarray");
Expand All @@ -32,6 +34,7 @@ describe("checks", function () {
assert(!isArray(null), "null");
assert(!isArray(undefined), "null");
});

it("isTypedArray", () => {
assert(isTypedArray(new Uint8Array(1)), "u8");
assert(isTypedArray(new Uint8ClampedArray(1)), "u8c");
Expand All @@ -49,6 +52,7 @@ describe("checks", function () {
assert(!isTypedArray(null), "null");
assert(!isTypedArray(undefined), "null");
});

it("isArrayLike", () => {
assert(isArrayLike([]), "empty array");
assert(isArrayLike(new Uint8Array(1)), "typedarray");
Expand All @@ -59,25 +63,34 @@ describe("checks", function () {
assert(!isArrayLike(null), "null");
assert(!isArrayLike(undefined), "null");
});

it("isObject", () => {
function Foo() { };
assert(isObject([]), "empty array");
assert(isObject(new Uint8Array(1)), "typedarray");
assert(isObject({}), "obj");
assert(isObject(new Foo()), "class");
assert(!isObject(Foo), "fn");
assert(!isObject("[]"), "string");
assert(!isObject(0), "zero");
assert(!isObject(null), "null");
assert(!isObject(undefined), "null");
});

it("isPlainObject", () => {
function Foo() { };
assert(isPlainObject({}), "obj");
assert(isPlainObject(new Object()), "obj ctor");
assert(!isPlainObject(Foo), "fn");
assert(!isPlainObject(new Foo()), "class");
assert(!isPlainObject([]), "empty array");
assert(!isPlainObject(new Uint8Array(1)), "typedarray");
assert(!isPlainObject("[]"), "string");
assert(!isPlainObject(0), "zero");
assert(!isPlainObject(null), "null");
assert(!isPlainObject(undefined), "null");
});

it("isString", () => {
assert(isString(""), "empty string");
assert(isString("a"), "empty string");
Expand All @@ -88,6 +101,7 @@ describe("checks", function () {
assert(!isString(null), "null");
assert(!isString(undefined), "null");
});

it("isFunction", () => {
assert(isFunction((_) => null), "fn");
assert(isFunction(Uint8Array), "ctor");
Expand All @@ -100,6 +114,7 @@ describe("checks", function () {
assert(!isFunction(null), "null");
assert(!isFunction(undefined), "undefined");
});

it("implementsFunction", () => {
assert(implementsFunction({ a: () => true }, "a"), "obj");
assert(implementsFunction([], Symbol.iterator), "arr iterator");
Expand All @@ -108,13 +123,15 @@ describe("checks", function () {
assert(!implementsFunction(null, Symbol.iterator), "null");
assert(!implementsFunction(undefined, Symbol.iterator), "undefined");
});

it("isSymbol", () => {
assert(isSymbol(Symbol.iterator), "iterator");
assert(!isSymbol("iterator"), "string");
assert(!isFunction(0), "zero");
assert(!isFunction(null), "null");
assert(!isFunction(undefined), "undefined");
});

it("isTransferable", () => {
assert(isTransferable(new ArrayBuffer(4)), "arraybuffer");
assert(!isTransferable(new Uint8Array(4)), "typedarray");
Expand All @@ -124,4 +141,5 @@ describe("checks", function () {
assert(!isTransferable(null), "null");
assert(!isTransferable(undefined), "undefined");
});

});

0 comments on commit bffc443

Please sign in to comment.