simmerjs
Version:
A pure Javascript reverse CSS selector engine which calculates a DOM element's unique CSS selector on the current page.
26 lines (24 loc) • 750 B
JavaScript
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
*/
export default 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(
(acc, val) => (tail(acc).parent() ? push(acc, tail(acc).parent()) : acc),
[element]
)
}