-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
331 lines (320 loc) · 8.4 KB
/
index.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
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
'use strict';
/**
* Get the value of the selected property if it exists.
* @param {Object} obj source object
* @param {String|Array} selector selector
* @param {Object|Array|String|Number|Boolean} defValue default value
* @returns {Object|Array|String|Number|Boolean|undefined}
* @example
* const selector = require("easy-object-selector");
* const select = selector.select;
* const obj = {
* a: {
* b: {
* c: "val1"
* },
* d: [
* {
* e: "val2"
* },
* {
* e: "val3"
* }
* ]
* }
* };
* select(obj, "a.b.c"); // => "val1"
* select(obj, ["a", "b", "c"]); // => "val1"
* select(obj, "a.b.x"); // => undefined
* select(obj, "a.b.x", "defValue"); // => "defValue"
* select(obj, "a.d.0.e"); // => "val2"
* select(obj, "a.d.1.e"); // => "val3"
* select(obj, "a.d.*.e"); // => ["val2", "val3"]
* select(obj, "a.d.first().e"); // => "val2"
* select(obj, "a.d.last().e"); // => "val3"
*/
exports.select = function (obj, selector, defValue) {
//console.log("selector: " + selector);
if (!obj && obj != 0) {
return defValue;
}
var tmpObj;
var tmpObj2;
var objType;
var objKeys;
var part;
var parts = Array.isArray(selector) ? selector : selector.split(".");
var n;
for (var i = 0; i < parts.length; i++) {
part = parts[i];
tmpObj = obj[part];
//console.log("tmpObj: ", tmpObj);
if (!tmpObj) {
objType = Object.prototype.toString.call(obj);
if (objType === "[object Array]") {
switch (part) {
// last
case "last()": {
tmpObj = obj[obj.length - 1];
break;
}
case "first()": {
tmpObj = obj[0];
break;
}
case "*": {
tmpObj = [];
parts = parts.slice(i + 1);
if (parts.length > 0) {
for (n = 0; n < obj.length; n++) {
tmpObj2 = exports.select(obj[n], parts, defValue);
if (tmpObj2 || tmpObj2 === 0) {
tmpObj.push(tmpObj2);
}
}
return tmpObj;
}
return obj;
}
}
} else if (objType === "[object Object]") {
switch (part) {
case "*": {
tmpObj = [];
parts = parts.slice(i + 1);
if (parts.length > 0) {
objKeys = Object.keys(obj);
for (n = 0; n < objKeys.length; n++) {
tmpObj2 = exports.select(obj[objKeys[n]], parts, defValue);
if (tmpObj2 || tmpObj2 === 0) {
tmpObj.push(tmpObj2);
}
}
return tmpObj.length > 0 ? tmpObj : defValue;
}
return obj;
}
}
}
}
obj = tmpObj;
//console.log(i, parts[i], o)
if (!obj && obj != 0) {
return defValue;
}
}
return obj;
};
/**
* Check if the selected property exists.
* @param {Object} obj source object
* @param {String|Array} selector selector
* @returns {Boolean}
* @example
* const selector = require("easy-object-selector");
* const has = selector.has;
* const obj = {
* a : {
* b : {
* c: "val1"
* }
* }
* };
* has(obj, "a.b.c"); // => true
* has(obj, ["a", "b", "c"]); // => true
* has(obj, "a.b.x"); // => false
*/
exports.has = function (obj, selector) {
var res = exports.select(obj, selector);
if (res && Array.isArray(res)) {
return res.length > 0;
}
return !!res;
};
/**
* Returns an array of keys.
* @param {Object} obj source object
* @param {String|Array} selector selector
* @param {Array} defKeys default keys array
* @returns {Array}
* @example
* const selector = require("easy-object-selector");
* const keys = selector.keys;
* const obj = {
* a : {
* b : {
* c: "val1",
* d: "val2",
* e: "val3",
* }
* }
* };
* keys(obj, "a.b"); // => ["c", "d", "e"]
* keys(obj, ["a", "b"]); // => ["c", "d", "e"]
* keys(obj, "a.b.x"); // => []
* keys(obj, "a.b.x", ["a", "b"]); // => ["a", "b"]
*/
exports.keys = function (obj, selector, defKeys) {
var res = exports.select(obj, selector);
if (!res) {
return defKeys || [];
}
return Object.keys(res);
};
/**
* Put the value in the object property.
* @param {Object} obj source object
* @param {String|Array} selector selector
* @param {Object|Array|String|Number|Boolean} value value
* @returns {Object}
* @example
* const selector = require("easy-object-selector");
* const put = selector.put;
* const o = {};
* put(o, "a.b.c", "val1") // => {a:{b:{c:"val1"}}}
* const o2 = {};
* put(o2, ["a", "b", "c"], "val1") // => {a:{b:{c:"val1"}}}
*/
exports.put = function (obj, selector, value) {
if (!obj) {
return;
}
var tmpObj = obj;
var part;
var parts = Array.isArray(selector) ? selector : selector.split(".");
for (var i = 0; i < parts.length; i++) {
part = parts[i];
if (i === parts.length - 1) {
obj[part] = value;
} else if (!obj[part]) {
obj[part] = {};
}
obj = obj[part];
}
return tmpObj;
};
//TODO: https://github.com/documentationjs/documentation/blob/068005eeba3093515aaa1536e4e43b0139f24b4a/__tests__/fixture/html/nested.input.js
/**
* @description Object wrapper
* @class
* @param {Object} obj object to wrap
*/
function ObjectWrapper(obj) {
this.obj = obj;
}
/**
* Get the value of the selected property if it exists.
* @param {String|Array} selector selector
* @param {Object|Array|String|Number|Boolean} defValue default value
* @returns {Object|Array|String|Number|Boolean|undefined}
* @example
* const selector = require("easy-object-selector");
* const obj = {
* a: {
* b: {
* c: "val1"
* },
* d: [
* {
* e: "val2"
* },
* {
* e: "val3"
* }
* ]
* }
* };
* const wrapper = selector.wrap(obj);
* wrapper.get("a.b.c"); // => "val1"
* wrapper.get(["a", "b", "c"]); // => "val1"
* wrapper.get("a.b.x"); // => undefined
* wrapper.get("a.b.x", "defValue"); // => "defValue"
* wrapper.get("a.d.0.e"); // => "val2"
* wrapper.get("a.d.1.e"); // => "val3"
* wrapper.get("a.d.*.e"); // => ["val2", "val3"]
* wrapper.get("a.d.first().e"); // => "val2"
* wrapper.get("a.d.last().e"); // => "val3"
*/
ObjectWrapper.prototype.get = function (selector, defValue) {
return exports.select(this.obj, selector, defValue);
};
/**
* Check if the selected property exists.
* @param {String|Array} selector selector
* @returns {Boolean}
* @example
* const selector = require("easy-object-selector");
* const obj = {
* a : {
* b : {
* c: "val1"
* }
* }
* };
* const wrapper = selector.wrap(obj);
* wrapper.has("a.b.c"); // => true
* wrapper.has(["a", "b", "c"]); // => true
* wrapper.has("a.b.x"); // => false
*/
ObjectWrapper.prototype.has = function (selector) {
return exports.has(this.obj, selector);
};
/**
* Returns an array of keys.
* @param {String|Array} selector selector
* @param {Array} defKeys default keys array
* @returns {Array}
* @example
* const selector = require("easy-object-selector");
* const obj = {
* a : {
* b : {
* c: "val1",
* d: "val2",
* e: "val3",
* }
* }
* };
* const wrapper = selector.wrap(obj);
* wrapper.keys("a.b"); // => ["c", "d", "e"]
* wrapper.keys(["a", "b"]); // => ["c", "d", "e"]
* wrapper.keys("a.b.x"); // => []
* wrapper.keys("a.b.x", ["a", "b"]); // => ["a", "b"]
*/
ObjectWrapper.prototype.keys = function (selector, defKeys) {
return exports.keys(this.obj, selector, defKeys);
};
/**
* Put the value in the object property.
* @param {String|Array} selector selector
* @param {Object|Array|String|Number|Boolean} value value
* @returns {Object}
* @example
* const selector = require("easy-object-selector");
* const o = {};
* const wrapper = selector.wrap(o);
* wrapper.put("a.b.c", "val1") // => {a:{b:{c:"val1"}}}
*/
ObjectWrapper.prototype.put = function (selector, value) {
return exports.put(this.obj, selector, value);
};
/**
* Creates a new {@link ObjectWrapper}
* @param {Object} obj object to wrap
* @returns {ObjectWrapper} wrapper
* @example
* const selector = require("easy-object-selector");
* const obj = {
* a: {
* b: {
* c: "val1"
* }
* }
* };
* const wrapper = selector.wrap(obj);
* wrapper.get("a.b.c"); // => "val1"
* wrapper.get(["a", "b", "c"]); // => "val1"
*/
exports.wrap = function (obj) {
return new ObjectWrapper(obj);
};