[d24f17c] | 1 | import {
|
---|
| 2 | pipe,
|
---|
| 3 | type,
|
---|
| 4 | identical,
|
---|
| 5 | both,
|
---|
| 6 | equals,
|
---|
| 7 | toString,
|
---|
| 8 | pathSatisfies,
|
---|
| 9 | curryN,
|
---|
| 10 | } from 'ramda';
|
---|
| 11 |
|
---|
| 12 | import isNull from './isNull';
|
---|
| 13 | import isObjLike from './isObjLike';
|
---|
| 14 | import isFunction from './isFunction';
|
---|
| 15 |
|
---|
| 16 | const isObject = pipe(type, identical('Object'));
|
---|
| 17 | const isObjectConstructor = pipe(toString, equals(toString(Object)));
|
---|
| 18 | const hasObjectConstructor = pathSatisfies(
|
---|
| 19 | both(isFunction, isObjectConstructor),
|
---|
| 20 | ['constructor']
|
---|
| 21 | );
|
---|
| 22 |
|
---|
| 23 | /* eslint-disable max-len */
|
---|
| 24 | /**
|
---|
| 25 | * Check to see if an object is a plain object (created using `{}`, `new Object()` or `Object.create(null)`).
|
---|
| 26 | *
|
---|
| 27 | * @func isPlainObj
|
---|
| 28 | * @aliases isPlainObject
|
---|
| 29 | * @memberOf RA
|
---|
| 30 | * @since {@link https://char0n.github.io/ramda-adjunct/0.5.0|v0.5.0}
|
---|
| 31 | * @category Type
|
---|
| 32 | * @sig * -> Boolean
|
---|
| 33 | * @param {*} val The value to test
|
---|
| 34 | * @return {boolean}
|
---|
| 35 | * @see {@link RA.isNotPlainObj|isNotPlainObj}, {@link RA.isObjLike|isObjLike}, {@link RA.isObj|isObj}
|
---|
| 36 | * @example
|
---|
| 37 | *
|
---|
| 38 | * class Bar {
|
---|
| 39 | * constructor() {
|
---|
| 40 | * this.prop = 'value';
|
---|
| 41 | * }
|
---|
| 42 | * }
|
---|
| 43 | *
|
---|
| 44 | * RA.isPlainObj(new Bar()); //=> false
|
---|
| 45 | * RA.isPlainObj({ prop: 'value' }); //=> true
|
---|
| 46 | * RA.isPlainObj(['a', 'b', 'c']); //=> false
|
---|
| 47 | * RA.isPlainObj(Object.create(null); //=> true
|
---|
| 48 | * RA.isPlainObj(new Object()); //=> true
|
---|
| 49 | */
|
---|
| 50 | /* eslint-enable max-len */
|
---|
| 51 | const isPlainObj = curryN(1, (val) => {
|
---|
| 52 | if (!isObjLike(val) || !isObject(val)) {
|
---|
| 53 | return false;
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | const proto = Object.getPrototypeOf(val);
|
---|
| 57 |
|
---|
| 58 | if (isNull(proto)) {
|
---|
| 59 | return true;
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | return hasObjectConstructor(proto);
|
---|
| 63 | });
|
---|
| 64 |
|
---|
| 65 | export default isPlainObj;
|
---|