source: node_modules/ramda-adjunct/src/isPlainObj.js@ 65b6638

main
Last change on this file since 65b6638 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 {
2 pipe,
3 type,
4 identical,
5 both,
6 equals,
7 toString,
8 pathSatisfies,
9 curryN,
10} from 'ramda';
11
12import isNull from './isNull';
13import isObjLike from './isObjLike';
14import isFunction from './isFunction';
15
16const isObject = pipe(type, identical('Object'));
17const isObjectConstructor = pipe(toString, equals(toString(Object)));
18const 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 */
51const 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
65export default isPlainObj;
Note: See TracBrowser for help on using the repository browser.