Рефакторинг

This commit is contained in:
Book Pauk
2022-11-09 16:47:36 +07:00
parent b64c5de5a3
commit 18da23530b

View File

@@ -1,9 +1,6 @@
class ObjectNavigator { class ObjectNavigator {
constructor(raw = null) { constructor(raw = null) {
if (Array.isArray(raw)) this.raw = raw;
this.raw = raw;
else
this.raw = [raw];
} }
makeSelector(selector) { makeSelector(selector) {
@@ -39,34 +36,30 @@ class ObjectNavigator {
let raw = this.raw; let raw = this.raw;
for (const s of selector) { for (const s of selector) {
if (s.name) { if (s.name) {
raw = raw[0];
if (typeof(raw) === 'object' && !Array.isArray(raw)) if (typeof(raw) === 'object' && !Array.isArray(raw))
raw = raw[s.name]; raw = raw[s.name];
else else
raw = null; raw = null;
}
if (raw !== null && !s.last) { if (raw !== null && !s.last) {
if (Array.isArray(raw)) if (Array.isArray(raw))
raw = raw[s.index]; raw = raw[s.index];
else if (s.index > 0) else if (s.index > 0)
raw = null; raw = null;
}
} else {
raw = raw[s.index];
} }
if (raw === undefined || raw === null) { if (raw === undefined || raw === null) {
raw = null; raw = null;
break; break;
} }
if (!Array.isArray(raw))
raw = [raw];
} }
if (raw === null) if (raw === null)
return null; return null;
raw = (Array.isArray(raw) ? raw : [raw]);
const result = []; const result = [];
for (const r of raw) for (const r of raw)
result.push(new ObjectNavigator(r)); result.push(new ObjectNavigator(r));
@@ -84,7 +77,7 @@ class ObjectNavigator {
} }
get value() { get value() {
return (this.raw.length ? this.raw[0] : null); return this.raw;
} }
text(selector = '') { text(selector = '') {