/
inProperty.js
92 lines (78 loc) · 1.91 KB
/
inProperty.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
const {
isUndefined, isNull, isNaNStrict,
isBoolean, isNumber, isInteger, isString,
isFunction, isObject, isArray, isDate, isRegExp,
isException,
} = require('../type/isType.js');
const {
_replaceAll,
} = require('../string/_replaceAll.js');
const {
isObjectParameter,
} = require('../object/isObjectParameter.js');
const {
_getPropertyBase,
} = require('../object/object_common.js');
/**
* _inProperty
*/
const _inProperty = (object, propertyPathArray, hasOwn = true) => {
if (!isObject(object)) {
return false;
}
if (isString(propertyPathArray)) {
propertyPathArray = _replaceAll(propertyPathArray, ' ', '').split(',');
}
for (let i = 0; i < propertyPathArray.length; i += 1) {
// if ((propertyPathArray[i] === '')
// || (isUndefined(propertyPathArray[i]))) {
// continue;
// }
if (!isString(propertyPathArray[i])) {
throw new TypeError(
'_inProperty args(propertyArray) element is not string',
);
}
const result = _getPropertyBase(object, propertyPathArray[i], hasOwn);
if (result.in === false) {
return false;
}
}
return true;
};
/**
* inProperty
*/
const inProperty = (object, propertyPathArray, hasOwn = true) => {
if (isObjectParameter(object, 'object, propertyPathArray', 'hasOwn')) {
({ object, propertyPathArray, hasOwn = true } = object);
}
if (!isObject(object)) {
throw new TypeError(
'inProperty args(fromObject) is not object',
);
}
if (!isString(propertyPathArray)) {
if (!isArray(propertyPathArray)) {
throw new TypeError(
'inProperty args(propertyPathArray) is not [array|string]',
);
}
}
if (!isBoolean(hasOwn)) {
throw new TypeError(
'inProperty args(hasOwn) is not boolean',
);
}
return _inProperty(
object,
propertyPathArray,
hasOwn,
);
};
const inProp = inProperty;
module.exports = {
_inProperty,
inProperty,
inProp,
};