From ed9d1cff95a4e1c8154f89cb426e9411904f5944 Mon Sep 17 00:00:00 2001 From: uzlopak Date: Thu, 19 Oct 2023 22:33:25 +0200 Subject: [PATCH] add entry for isPlainObject --- README.md | 41 ++++++++++++++++++++++++++++++ tests/unit/all.js | 63 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 104 insertions(+) diff --git a/README.md b/README.md index 2bd1095..c8c3d14 100644 --- a/README.md +++ b/README.md @@ -163,6 +163,7 @@ For more information, see [Configuring the ESLint Plugin](configuring.md) 1. [_.has](#_has) 1. [_.get](#_get) 1. [_.invert](#_invert) +1. [_.isPlainObject](#_isplainobject) 1. [_.keys](#_keys) 1. [_.mapKeys](#_mapKeys) 1. [_.omit](#_omit) @@ -2264,6 +2265,46 @@ Checks if value is an integer. **[⬆ back to top](#quick-links)** +### _.isPlainObject + +Checks if value is a plain object, that is, an object created by the Object constructor or one with a [[Prototype]] of null. + + ```js + var object = { 'a': 1, 'b': 2, 'c': 1 }; + + // Underscore/Lodash + var result = _.isPlainObject(object); + console.log(result) + // output: true + + function isPlainObject(value) { + 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) + ); + + var result = invert(object); + console.log(result) + // output: true + } + ``` + +#### Browser Support for `Object.getPrototypeOf()` + +![Chrome][chrome-image] | ![Edge][edge-image] | ![Firefox][firefox-image] | ![IE][ie-image] | ![Opera][opera-image] | ![Safari][safari-image] +:-: | :-: | :-: | :-: | :-: | :-: | + 5.0 ✔ | 12.0 ✔ | 3.5 ✔ | ✖ | 12.1 ✔ | 5.0 ✔ | + +**[⬆ back to top](#quick-links)** + ### _.isNaN Checks if a value is NaN. diff --git a/tests/unit/all.js b/tests/unit/all.js index 0239b8d..08d79fd 100644 --- a/tests/unit/all.js +++ b/tests/unit/all.js @@ -299,6 +299,69 @@ describe('code snippet example', () => { ) }) }) + describe('isPlainObject', () => { + function isPlainObject(value) { + 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) + ); + } + + function Foo() { + this.a = 1; + } + + it('_.isPlainObject(NaN)', () => { + assert.equal( + _.isPlainObject(NaN), + isPlainObject(NaN) + ) + }) + it('_.isPlainObject([1, 2, 3])', () => { + assert.equal( + _.isPlainObject([1, 2, 3]), + isPlainObject([1, 2, 3]) + ) + }) + it('_.isPlainObject(null)', () => { + assert.equal( + _.isPlainObject(null), + isPlainObject(null) + ) + }) + it("_.isPlainObject({ 'x': 0, 'y': 0 })", () => { + assert.equal( + _.isPlainObject({ 'x': 0, 'y': 0 }), + isPlainObject({ 'x': 0, 'y': 0 }) + ) + }) + it("_.isPlainObject(Object.create(null))", () => { + assert.equal( + _.isPlainObject(Object.create(null)), + isPlainObject(Object.create(null)) + ) + }) + it("_.isPlainObject(Object.create(new Foo()))", () => { + assert.equal( + _.isPlainObject(Object.create(new Foo())), + isPlainObject(Object.create(new Foo())) + ) + }) + it("_.isPlainObject(Object.create(new Date()))", () => { + assert.equal( + _.isPlainObject(Object.create(new Date())), + isPlainObject(Object.create(new Date())) + ) + }) + }) describe('get', () => { const get = (obj, path, defaultValue) => { const travel = regexp =>