Skip to content

Commit

Permalink
Merge pull request #384 from Uzlopak/isPlainObject
Browse files Browse the repository at this point in the history
add entry for isPlainObject
  • Loading branch information
stevemao committed Oct 19, 2023
2 parents d8d098d + ed9d1cf commit 57bfcef
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 0 deletions.
41 changes: 41 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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.
Expand Down
63 changes: 63 additions & 0 deletions tests/unit/all.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 =>
Expand Down

0 comments on commit 57bfcef

Please sign in to comment.