UNPKG

@qntm-code/utils

Version:

A collection of useful utility functions with associated TypeScript types. All functions have been unit tested.

25 lines (24 loc) 766 B
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getAncestors = void 0; const isNullOrUndefined_js_1 = require("../type-predicates/isNullOrUndefined.js"); /** * Gets all the elements that a given element is nested within */ function getAncestors(element) { return _getAncestors(element); } exports.getAncestors = getAncestors; /** * Internal function for building the array and navigating up the tree */ function _getAncestors(element, result = []) { const parent = element.parentNode; if (!(0, isNullOrUndefined_js_1.isNullOrUndefined)(parent) && parent.nodeType === parent.ELEMENT_NODE) { result.push(parent); return _getAncestors(parent, result); } else { return result; } }