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