vanillajs-browser-helpers
Version:
Collection of convenience code snippets (helpers) that aims to make it a little easier to work with vanilla JS in the browser
32 lines (31 loc) • 1.25 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var isString_1 = __importDefault(require("vanillajs-helpers/isString"));
/**
* Make a unique list of Elements from a list of selectors and and function to use that selector.
*
* @param selector - The selector(s) to use with the given `findElements` function
* @param findElements - The function to find elements
* @returns The unique list of found elements
*
* @example
*
* ```ts
* const byClassName = (selector) => document.getElementsByClassName(selector)
*
* // Using a single selector
* findUniqueNodeCollection('my-elements', byClassName);
*
* // Using multiple selectors
* findUniqueNodeCollection(['my-elements', 'sone-other-elements'], byClassName);
* ```
*/
function findUniqueNodeCollection(selector, findElements) {
var selectors = (0, isString_1.default)(selector) ? [selector] : selector;
var nodes = selectors.flatMap(function (s) { return Array.from(findElements(s)); });
return Array.from(new Set(nodes));
}
exports.default = findUniqueNodeCollection;