-
-
Notifications
You must be signed in to change notification settings - Fork 151
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Minor code refactoring and simplification
- Loading branch information
Showing
4 changed files
with
28 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// Turns a value into an array if it's not already an array | ||
export const arrayify = <T>(value: T | T[]): T[] => (Array.isArray(value) ? value : [value]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,21 @@ | ||
export type SelectAllSelectors = readonly (string | Element)[] | string | Element; | ||
import {arrayify} from './arrayify'; | ||
|
||
export type SelectAllSelectors = (string | Element)[] | string | Element; | ||
|
||
/** | ||
* Takes a selector (or array of selectors) and returns the matched nodes. | ||
* @param selector The selector or an Array of selectors. | ||
* @param doc | ||
* @returns {Array} Array of DOM-Nodes. | ||
*/ | ||
export const selectAll = (selector: SelectAllSelectors, doc: Document = document): Element[] => { | ||
const list = !Array.isArray(selector) ? [selector] : selector; | ||
let nodes: Element[] = []; | ||
|
||
for (let i = 0, l = list.length; i < l; i++) { | ||
const item = list[i]; | ||
|
||
if (typeof item === 'string') { | ||
/** | ||
* We can't use the spread operator here as with large amounts of elements | ||
* we'll get a "Maximum call stack size exceeded"-error. | ||
*/ | ||
nodes = nodes.concat(Array.from(doc.querySelectorAll(item))); | ||
} else if (item instanceof Element) { | ||
nodes.push(item); | ||
} | ||
} | ||
|
||
return nodes; | ||
}; | ||
export const selectAll = (selector: SelectAllSelectors, doc: Document = document): Element[] => | ||
arrayify(selector) | ||
.map(item => | ||
typeof item === 'string' | ||
? Array.from(doc.querySelectorAll(item)) | ||
: item instanceof Element | ||
? item | ||
: null | ||
) | ||
.flat() | ||
.filter(Boolean) as Element[]; |