-
Notifications
You must be signed in to change notification settings - Fork 72
Expand file tree
/
Copy pathget-key-value.js
More file actions
155 lines (144 loc) · 3.81 KB
/
get-key-value.js
File metadata and controls
155 lines (144 loc) · 3.81 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
'use strict';
var utils = require('./utils')
;
/**
* Make the get of a value with the key in the passed object
* @param fromObject
* @param fromKey
* @constructor
* @returns {*}
*/
function GetKeyValue(fromObject, fromKey) {
var regDot = /\./g
, regFinishArray = /.+(\[\])/g
, keys
, key
, result
, lastValue
;
keys = fromKey.split(regDot);
key = keys.splice(0, 1);
lastValue = fromKey.match(regFinishArray);
if(lastValue != null && lastValue[0] === fromKey){
fromKey = fromKey.slice(0,-2);
}else{
lastValue = null;
}
result = _getValue(fromObject, key[0], keys);
if (Array.isArray(result) && !lastValue) {
if (result.length) {
result = result.reduce(function (a, b) {
if (Array.isArray(a) && Array.isArray(b)) {
return a.concat(b);
} else if (Array.isArray(a)) {
a.push(b);
return a;
} else {
return [a, b];
}
});
}
if (!Array.isArray(result)) {
result = [result];
}
}
return result;
}
module.exports = GetKeyValue;
/**
* Get the value of key within passed object, considering if there is a array or object
* @param fromObject
* @param key
* @param keys
* @returns {*}
* @private
* @recursive
*/
function _getValue(fromObject, key, keys) {
var regArray = /(\[\]|\[(.*)\])$/g
, match
, arrayIndex
, isValueArray = false
, result
;
if (!fromObject) {
return;
}
match = regArray.exec(key);
if (match) {
key = key.replace(regArray, '');
isValueArray = (key !== '');
arrayIndex = match[2];
}
if (keys.length === 0) {
if (isValueArray) {
if (typeof arrayIndex === 'undefined' || fromObject[key] === undefined) {
result = fromObject[key];
} else {
result = fromObject[key][arrayIndex];
}
} else if (Array.isArray(fromObject)) {
if (key === '') {
if (typeof arrayIndex === 'undefined') {
result = fromObject;
} else {
result = fromObject[arrayIndex];
}
} else {
result = fromObject.map(function (item) {
return item[key];
})
}
} else {
result = fromObject[key];
}
} else {
if (isValueArray) {
if (Array.isArray(fromObject[key])) {
if (typeof arrayIndex === 'undefined') {
result = fromObject[key].map(function (item) {
return _getValue(item, keys[0], keys.slice(1));
});
} else {
result = _getValue(fromObject[key][arrayIndex], keys[0], keys.slice(1));
}
} else {
if (typeof arrayIndex === 'undefined') {
result = _getValue(fromObject[key], keys[0], keys.slice(1));
} else {
result = _getValue(fromObject[key][arrayIndex], keys[0], keys.slice(1));
}
}
} else if (Array.isArray(fromObject)) {
if (key === '') {
if (typeof arrayIndex === 'undefined') {
result = _getValue(fromObject, keys[0], keys.slice(1));
} else {
result = _getValue(fromObject[arrayIndex], keys[0], keys.slice(1));
}
} else {
result = fromObject.map(function (item) {
result = _getValue(item, keys[0], keys.slice(1));
})
}
if (typeof arrayIndex === 'undefined') {
result = fromObject.map(function (item) {
return _getValue(item, keys[0], keys.slice(1));
});
} else {
result = _getValue(fromObject[arrayIndex], keys[0], keys.slice(1));
}
} else {
result = _getValue(fromObject[key], keys[0], keys.slice(1));
}
}
if(Array.isArray(result)) {
result = result.filter(function (item) {
return (typeof item !== 'undefined' || utils._isEmptyObject(item));
});
if (utils._isEmptyArray(result)) {
result = undefined;
}
}
return result;
}