Network Powering Makers

    is-pojo

    1.0.2 • Public • Published

    is-pojo Build Status NPM version

    Installation

    npm install is-pojo

    Usage

    var isPojo = require("is-pojo");
    isPojo({}) // true
     
    // anything besides an absolutely plain object will return false.
     
    // here are examples from the tests:
     
    function Foo () {}
    function Bar () {}
    Bar.prototype.constructor = Object;
     
    isPojo(function () {}) // false
    isPojo([]) // false
    isPojo(new Date()) // false
    isPojo(true); // false
    isPojo("abc"); // false
    isPojo(123); // false
    isPojo(new RegExp()); // false
    isPojo(null); // false
    isPojo(undefined); // false
    isPojo(Object.create({})); // false
    isPojo(new Foo()); // false
    isPojo(new Bar()); // false
    isPojo({constructor: Foo})// true

    Why?

    The other module on npm that I found to do this checks an object's constructor property against Object. This approach is error-prone since there's nothing special about the constructor property; you can set it willy-nilly (see example above).

    In contrast, an object's prototype can be set just once, at instantiation time. As such it's safe to check an object's prototype (obtained with Object.getPrototypeOf) against Object.prototype.

    Install

    npm i is-pojo

    DownloadsWeekly Downloads

    56

    Version

    1.0.2

    License

    MIT

    Last publish

    Collaborators

    • nickbottomley