62 lines
1.4 KiB
JavaScript
62 lines
1.4 KiB
JavaScript
function createSpaResponseShapes() {
|
|
function findNestedObject(value, predicate, visited = new Set()) {
|
|
if (!value || typeof value !== "object" || visited.has(value)) {
|
|
return null;
|
|
}
|
|
|
|
visited.add(value);
|
|
|
|
if (!Array.isArray(value) && predicate(value)) {
|
|
return value;
|
|
}
|
|
|
|
for (const child of Object.values(value)) {
|
|
const match = findNestedObject(child, predicate, visited);
|
|
|
|
if (match) {
|
|
return match;
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
function findAccountPlanId(response) {
|
|
const account = findNestedObject(response, (value) => (
|
|
Object.prototype.hasOwnProperty.call(value, "id")
|
|
&& value.id !== null
|
|
&& value.id !== undefined
|
|
&& value.id !== ""
|
|
));
|
|
return account ? account.id : "";
|
|
}
|
|
|
|
function extractAccountPlans(response) {
|
|
const plans = [];
|
|
const visited = new Set();
|
|
|
|
const visit = (value) => {
|
|
if (!value || typeof value !== "object" || visited.has(value)) {
|
|
return;
|
|
}
|
|
|
|
visited.add(value);
|
|
|
|
if (!Array.isArray(value) && (
|
|
Object.prototype.hasOwnProperty.call(value, "planType")
|
|
|| Object.prototype.hasOwnProperty.call(value, "subPlanNum")
|
|
)) {
|
|
plans.push(value);
|
|
return;
|
|
}
|
|
|
|
Object.values(value).forEach(visit);
|
|
};
|
|
|
|
visit(response);
|
|
return plans;
|
|
}
|
|
|
|
return { findNestedObject, findAccountPlanId, extractAccountPlans };
|
|
}
|