simmerjs
Version:
A pure Javascript reverse CSS selector engine which calculates a DOM element's unique CSS selector on the current page.
28 lines (26 loc) • 849 B
JavaScript
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = stackHierarchy;
function push(arr, val) {
arr.push(val);
return arr;
}
function tail(arr) {
return arr[arr.length - 1];
}
/**
* Retireve the element's ancestors up to the configured level.
* This is an internal function and is not to be used from the outside (nor can it, it is private)
* @param element (Object) The elemen't whose ancestry we want to retrieve
* @param depth (number) How deep to into the heirarchy to collect elements
*/
function stackHierarchy(element, depth) {
if (depth <= 0) {
throw new Error("Simmer: An invalid depth of " + depth + " has been specified");
}
return Array(depth - 1).fill().reduce(function (acc, val) {
return tail(acc).parent() ? push(acc, tail(acc).parent()) : acc;
}, [element]);
}
;