2022-09-15 08:43:17 +00:00
|
|
|
const mb = function (path) {
|
|
|
|
return function (obj) {
|
2020-10-23 16:26:06 +00:00
|
|
|
return (
|
2022-09-15 08:43:17 +00:00
|
|
|
path.map(function (prop) {
|
2020-10-23 16:26:06 +00:00
|
|
|
obj = obj || {};
|
|
|
|
if (isNaN(parseInt(prop))) {
|
|
|
|
return (obj = obj[prop]);
|
|
|
|
} else {
|
|
|
|
return (obj = obj.objectAt(parseInt(prop)));
|
|
|
|
}
|
|
|
|
}) && obj
|
|
|
|
);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
let currentPage;
|
2022-09-15 08:43:17 +00:00
|
|
|
export const getCurrentPage = function () {
|
2020-10-23 16:26:06 +00:00
|
|
|
return currentPage;
|
|
|
|
};
|
2022-09-15 08:43:17 +00:00
|
|
|
export const setCurrentPage = function (page) {
|
2020-10-23 16:26:06 +00:00
|
|
|
currentPage = page;
|
|
|
|
return page;
|
|
|
|
};
|
2022-09-15 08:43:17 +00:00
|
|
|
export const find = function (path, page = currentPage) {
|
2020-10-23 16:26:06 +00:00
|
|
|
const parts = path.split('.');
|
|
|
|
const last = parts.pop();
|
|
|
|
let obj;
|
|
|
|
let parent = mb(parts)(page) || page;
|
|
|
|
if (typeof parent.objectAt === 'function') {
|
|
|
|
parent = parent.objectAt(0);
|
|
|
|
}
|
|
|
|
obj = parent[last];
|
|
|
|
if (typeof obj === 'undefined') {
|
|
|
|
throw new Error(`PageObject not found: The '${path}' object doesn't exist`);
|
|
|
|
}
|
|
|
|
if (typeof obj === 'function') {
|
|
|
|
obj = obj.bind(parent);
|
|
|
|
}
|
|
|
|
return obj;
|
|
|
|
};
|