1 | var staticHGKeyRE = require('./staticHGKeyRE')
|
2 |
|
3 | var helperGetHGSKeys = require('./helperGetHGSKeys')
|
4 | var hasOwnProp = require('./hasOwnProp')
|
5 | var isUndefined = require('./isUndefined')
|
6 | var eqNull = require('./eqNull')
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | function get (obj, property, defaultValue) {
|
16 | if (eqNull(obj)) {
|
17 | return defaultValue
|
18 | }
|
19 | var result = getValueByPath(obj, property)
|
20 | return isUndefined(result) ? defaultValue : result
|
21 | }
|
22 |
|
23 | function getDeepProps (obj, key) {
|
24 | var matchs = key ? key.match(staticHGKeyRE) : ''
|
25 | return matchs ? (matchs[1] ? (obj[matchs[1]] ? obj[matchs[1]][matchs[2]] : undefined) : obj[matchs[2]]) : obj[key]
|
26 | }
|
27 |
|
28 | function getValueByPath (obj, property) {
|
29 | if (obj) {
|
30 | var rest, props, len
|
31 | var index = 0
|
32 | if (obj[property] || hasOwnProp(obj, property)) {
|
33 | return obj[property]
|
34 | } else {
|
35 | props = helperGetHGSKeys(property)
|
36 | len = props.length
|
37 | if (len) {
|
38 | for (rest = obj; index < len; index++) {
|
39 | rest = getDeepProps(rest, props[index])
|
40 | if (eqNull(rest)) {
|
41 | if (index === len - 1) {
|
42 | return rest
|
43 | }
|
44 | return
|
45 | }
|
46 | }
|
47 | }
|
48 | return rest
|
49 | }
|
50 | }
|
51 | }
|
52 |
|
53 | module.exports = get
|