Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add entry for isPlainObject #384

Merged
merged 1 commit into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
Comment on lines +2294 to +2297
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These lines are not part of the isPlainObject() function.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please send a PR to fix anything

```

#### 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