source: node_modules/ramda-adjunct/es/isPlainObj.js

main
Last change on this file was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago

Initial commit

  • Property mode set to 100644
File size: 1.5 KB
Line 
1import { pipe, type, identical, both, equals, toString, pathSatisfies, curryN } from 'ramda';
2import isNull from './isNull';
3import isObjLike from './isObjLike';
4import isFunction from './isFunction';
5var isObject = pipe(type, identical('Object'));
6var isObjectConstructor = pipe(toString, equals(toString(Object)));
7var 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 */
37var 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});
47export default isPlainObj;
Note: See TracBrowser for help on using the repository browser.