-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
123 lines (121 loc) · 3.38 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
function investigate(key, filter, object) {
const c1 = filter[key];
const c2 = object[key];
if (!c2) return false;
if (c1 instanceof Object) {
for (const key in c1) {
if (!investigate(key, c1, c2)) {
return false;
}
}
return true;
} else {
if (c1 !== c2) return false;
return true;
}
}
module.exports = {
travel: function travel(object, ...args) {
let item = args[0];
let originalItem = item;
if (item instanceof Object && object instanceof Array) {
// assuming that 'object' is an array in this context
item = object.findIndex((i) => {
for (const key in item) {
if (!investigate(key, item, i)) return false;
}
return true;
});
if (item === -1) item = -2;
}
if (object instanceof Array && item === -1) {
const nextArgs = args.filter((i) => i !== item);
for (const [i, v] of object.entries()) {
const res = travel(object, i, ...nextArgs);
if (res !== "") {
return res;
}
}
return "";
} else {
if (object && !(item instanceof Array) && object.hasOwnProperty(item)) {
const curatedArgs = args.filter((i) => i !== originalItem);
if (curatedArgs.length > 0) {
return travel(object[item], ...curatedArgs);
} else {
return object[item];
}
} else {
if (item instanceof Array) {
let retData = {};
for (const key of item) {
if (key instanceof Object) {
retData[Object.keys(key)[0]] = travel(
object,
...Object.values(key)[0]
);
} else {
retData[key] = object[key];
}
}
return retData;
}
return "";
}
}
},
travelOr: function travel(alternate, object, ...args) {
let item = args[0];
let originalItem = item;
if (item instanceof Object && object instanceof Array) {
// assuming that 'object' is an array in this context
item = object.findIndex((i) => {
for (const key in item) {
if (!investigate(key, item, i)) return false;
}
return true;
});
if (item === -1) item = -2;
}
if (object instanceof Array && item === -1) {
const nextArgs = args.filter((i) => i !== item);
for (const [i, v] of object.entries()) {
const res = travel(alternate, object, i, ...nextArgs);
if (res !== alternate) {
return res;
}
}
return alternate;
} else {
if (
object &&
!(item instanceof Array) &&
Object.prototype.hasOwnProperty.call(object, item)
) {
const curatedArgs = args.filter((i) => i !== originalItem);
if (curatedArgs.length > 0) {
return travel(alternate, object[item], ...curatedArgs);
} else {
return object[item];
}
} else {
if (item instanceof Array) {
let retData = {};
for (const key of item) {
if (key instanceof Object) {
retData[Object.keys(key)[0]] = travel(
alternate,
object,
...Object.values(key)[0]
);
} else {
retData[key] = object[key];
}
}
return retData;
}
return alternate;
}
}
},
};