UNPKG

1.57 kBJavaScriptView Raw
1var staticHGKeyRE = require('./staticHGKeyRE')
2
3var helperGetHGSKeys = require('./helperGetHGSKeys')
4
5var hasOwnProp = require('./hasOwnProp')
6
7/**
8 * 检查键、路径是否是该对象的属性
9 *
10 * @param {Object/Array} data 对象
11 * @param {String/Function} property 键、路径
12 * @return {Boolean}
13 */
14function has (obj, property) {
15 if (obj) {
16 if (hasOwnProp(obj, property)) {
17 return true
18 } else {
19 var prop, arrIndex, objProp, matchs, rest, isHas
20 var props = helperGetHGSKeys(property)
21 var index = 0
22 var len = props.length
23 for (rest = obj; index < len; index++) {
24 isHas = false
25 prop = props[index]
26 matchs = prop ? prop.match(staticHGKeyRE) : ''
27 if (matchs) {
28 arrIndex = matchs[1]
29 objProp = matchs[2]
30 if (arrIndex) {
31 if (rest[arrIndex]) {
32 if (hasOwnProp(rest[arrIndex], objProp)) {
33 isHas = true
34 rest = rest[arrIndex][objProp]
35 }
36 }
37 } else {
38 if (hasOwnProp(rest, objProp)) {
39 isHas = true
40 rest = rest[objProp]
41 }
42 }
43 } else {
44 if (hasOwnProp(rest, prop)) {
45 isHas = true
46 rest = rest[prop]
47 }
48 }
49 if (isHas) {
50 if (index === len - 1) {
51 return true
52 }
53 } else {
54 break
55 }
56 }
57 }
58 }
59 return false
60}
61
62module.exports = has