Socket
Socket
Sign inDemoInstall

is-plain

Package Overview
Dependencies
0
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    is-plain

Test if a value is a plain object


Version published
Weekly downloads
0
Maintainers
1
Install size
10.6 kB
Created
Weekly downloads
 

Changelog

Source

[1.1.0] - 2020-01-01

Changed

  • Update TypeScript definition: isPlain now acts as a type guard
  • Update logic to reduce size from 167 B to 140 B

Readme

Source

is-plain

NPM Release Build Status Code Coverage License

📖 Table of Contents

Tests if a value is a plain object. That is:

  • an object created using new Object()
  • or, an object without a prototype Object.create(null)
  • or, an object created using the literal notation {}

Installation

# using npm
npm install --save is-plain

# using yarn
yarn add is-plain

Usage

import isPlain from 'is-plain';

isPlain({ foo: 'bar' });
//=> true

isPlain(Object.create(null));
//=> true

isPlain(new Object());
//=> true

class Foo {}
isPlain(new Foo());
//=> false

isPlain([1, 2, 3]);
//=> false

isPlain(null);
//=> false

isPlain(Promise.resolve());
//=> false

Comparison with Other Libraries

There are already similar libraries available to test if a value is a plain object. Some of these libraries are very widely adopted. To name a few:

So, why need is-plain when other solutions already exist?

Small Package Size

is-plain is only 140 bytes minzipped.

The following is a size comparison between is-plain and other popular libraries ranked by their minified size:

Edge Cases Other Libraries Missed

While is-plain is very comparable to is-plain-obj in both size and logic (is-plain being smaller by 3 bytes), the size wasn't the the only motive. Most importantly, what I wanted to address is some of the edge cases the other libraries are missing.

The following are some of the cases that causes inconsistent, arguably incorrect, results when one of the other three libraries is used (note that in all of the following cases, is-plain considers these objects to be plain objects).

Case 1
// an object without a prototype
const value = Object.create(null);
is value a plain object?
is-plain (this package)true
is-plain-objtrue
⚠️is-plain-objectfalse
lodash.isplainobjecttrue
Case 2
// an object with an empty prototype
const value = Object.create({});
is value a plain object?
is-plain (this package)true
⚠️is-plain-objfalse
is-plain-objecttrue
⚠️lodash.isplainobjectfalse
Case 3
// an object with a prototype whose constructor is Object
const value = Object.create({ constructor: Object });
is value a plain object?
is-plain (this package)true
⚠️is-plain-objfalse
is-plain-objecttrue
lodash.isplainobjecttrue
Case 4
// an object literal with own property 'constructor'
const value = {
  constructor: Foo,
};
is value a plain object?
is-plain (this package)true
is-plain-objtrue
⚠️is-plain-objectfalse
lodash.isplainobjecttrue
Case 5
// an object literal with a '__proto__' property as an object
const value = {
  __proto__: {},
};
is value a plain object?
is-plain (this package)true
⚠️is-plain-objfalse
is-plain-objecttrue
⚠️lodash.isplainobjectfalse
Case 6
// an instance of a prototype whose constructor is Object
function ObjectConstructor() {}
ObjectConstructor.prototype.constructor = Object;
const value = new ObjectConstructor();
is value a plain object?
is-plain (this package)true
⚠️is-plain-objfalse
is-plain-objecttrue
lodash.isplainobjecttrue

License

MIT

FAQs

Last updated on 02 Jan 2020

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc