Skip to content

Commit 57bfcef

Browse files
authored
Merge pull request you-dont-need#384 from Uzlopak/isPlainObject
add entry for isPlainObject
2 parents d8d098d + ed9d1cf commit 57bfcef

2 files changed

Lines changed: 104 additions & 0 deletions

File tree

README.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ For more information, see [Configuring the ESLint Plugin](configuring.md)
163163
1. [_.has](#_has)
164164
1. [_.get](#_get)
165165
1. [_.invert](#_invert)
166+
1. [_.isPlainObject](#_isplainobject)
166167
1. [_.keys](#_keys)
167168
1. [_.mapKeys](#_mapKeys)
168169
1. [_.omit](#_omit)
@@ -2264,6 +2265,46 @@ Checks if value is an integer.
22642265

22652266
**[⬆ back to top](#quick-links)**
22662267

2268+
### _.isPlainObject
2269+
2270+
Checks if value is a plain object, that is, an object created by the Object constructor or one with a [[Prototype]] of null.
2271+
2272+
```js
2273+
var object = { 'a': 1, 'b': 2, 'c': 1 };
2274+
2275+
// Underscore/Lodash
2276+
var result = _.isPlainObject(object);
2277+
console.log(result)
2278+
// output: true
2279+
2280+
function isPlainObject(value) {
2281+
if (typeof value !== 'object' || value === null) return false
2282+
2283+
if (Object.prototype.toString.call(value) !== '[object Object]') return false
2284+
2285+
const proto = Object.getPrototypeOf(value);
2286+
if (proto === null) return true
2287+
2288+
const Ctor = Object.prototype.hasOwnProperty.call(proto, 'constructor') && proto.constructor;
2289+
return (
2290+
typeof Ctor === 'function' &&
2291+
Ctor instanceof Ctor && Function.prototype.call(Ctor) === Function.prototype.call(value)
2292+
);
2293+
2294+
var result = invert(object);
2295+
console.log(result)
2296+
// output: true
2297+
}
2298+
```
2299+
2300+
#### Browser Support for `Object.getPrototypeOf()`
2301+
2302+
![Chrome][chrome-image] | ![Edge][edge-image] | ![Firefox][firefox-image] | ![IE][ie-image] | ![Opera][opera-image] | ![Safari][safari-image]
2303+
:-: | :-: | :-: | :-: | :-: | :-: |
2304+
5.0 ✔ | 12.0 ✔ | 3.5 ✔ | ✖ | 12.1 ✔ | 5.0 ✔ |
2305+
2306+
**[⬆ back to top](#quick-links)**
2307+
22672308
### _.isNaN
22682309

22692310
Checks if a value is NaN.

tests/unit/all.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,69 @@ describe('code snippet example', () => {
299299
)
300300
})
301301
})
302+
describe('isPlainObject', () => {
303+
function isPlainObject(value) {
304+
if (typeof value !== 'object' || value === null) return false
305+
306+
if (Object.prototype.toString.call(value) !== '[object Object]') return false
307+
308+
const proto = Object.getPrototypeOf(value);
309+
if (proto === null) return true
310+
311+
const Ctor = Object.prototype.hasOwnProperty.call(proto, 'constructor') && proto.constructor;
312+
return (
313+
typeof Ctor === 'function' &&
314+
Ctor instanceof Ctor && Function.prototype.call(Ctor) === Function.prototype.call(value)
315+
);
316+
}
317+
318+
function Foo() {
319+
this.a = 1;
320+
}
321+
322+
it('_.isPlainObject(NaN)', () => {
323+
assert.equal(
324+
_.isPlainObject(NaN),
325+
isPlainObject(NaN)
326+
)
327+
})
328+
it('_.isPlainObject([1, 2, 3])', () => {
329+
assert.equal(
330+
_.isPlainObject([1, 2, 3]),
331+
isPlainObject([1, 2, 3])
332+
)
333+
})
334+
it('_.isPlainObject(null)', () => {
335+
assert.equal(
336+
_.isPlainObject(null),
337+
isPlainObject(null)
338+
)
339+
})
340+
it("_.isPlainObject({ 'x': 0, 'y': 0 })", () => {
341+
assert.equal(
342+
_.isPlainObject({ 'x': 0, 'y': 0 }),
343+
isPlainObject({ 'x': 0, 'y': 0 })
344+
)
345+
})
346+
it("_.isPlainObject(Object.create(null))", () => {
347+
assert.equal(
348+
_.isPlainObject(Object.create(null)),
349+
isPlainObject(Object.create(null))
350+
)
351+
})
352+
it("_.isPlainObject(Object.create(new Foo()))", () => {
353+
assert.equal(
354+
_.isPlainObject(Object.create(new Foo())),
355+
isPlainObject(Object.create(new Foo()))
356+
)
357+
})
358+
it("_.isPlainObject(Object.create(new Date()))", () => {
359+
assert.equal(
360+
_.isPlainObject(Object.create(new Date())),
361+
isPlainObject(Object.create(new Date()))
362+
)
363+
})
364+
})
302365
describe('get', () => {
303366
const get = (obj, path, defaultValue) => {
304367
const travel = regexp =>

0 commit comments

Comments
 (0)