Skip to content

Commit 81ddb01

Browse files
committed
Minor code refactoring and simplification
1 parent 7f1b1eb commit 81ddb01

File tree

4 files changed

+28
-42
lines changed

4 files changed

+28
-42
lines changed

packages/vanilla/src/index.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -795,7 +795,7 @@ export default class SelectionArea extends EventTarget<SelectionEvents> {
795795

796796
/**
797797
* Cancel the current selection process, pass true to fire a stop event after cancel
798-
* @param keepEvent - If a stop event should be fired
798+
* @param keepEvent If a stop event should be fired
799799
*/
800800
cancel(keepEvent = false): void {
801801
this._onTapStop(null, !keepEvent);
@@ -823,8 +823,8 @@ export default class SelectionArea extends EventTarget<SelectionEvents> {
823823

824824
/**
825825
* Adds elements to the selection
826-
* @param query - CSS Query, can be an array of queries
827-
* @param quiet - If this should not trigger the move event
826+
* @param query CSS Query, can be an array of queries
827+
* @param quiet If this should not trigger the move event
828828
*/
829829
select(query: SelectAllSelectors, quiet = false): Element[] {
830830
const {changed, selected, stored} = this._selection;
@@ -853,8 +853,8 @@ export default class SelectionArea extends EventTarget<SelectionEvents> {
853853

854854
/**
855855
* Removes a particular element from the selection
856-
* @param query - CSS Query, can be an array of queries
857-
* @param quiet - If this should not trigger the move event
856+
* @param query CSS Query, can be an array of queries
857+
* @param quiet If this should not trigger the move event
858858
*/
859859
deselect(query: SelectAllSelectors, quiet = false) {
860860
const {selected, stored, changed} = this._selection;
+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// Turns a value into an array if it's not already an array
2+
export const arrayify = <T>(value: T | T[]): T[] => (Array.isArray(value) ? value : [value]);

packages/vanilla/src/utils/events.ts

+7-16
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,23 @@
11
/* eslint-disable @typescript-eslint/no-explicit-any */
2+
import {arrayify} from './arrayify';
3+
24
type Method = 'addEventListener' | 'removeEventListener';
35
type AnyFunction = (...arg: any) => any;
46

5-
export type EventBindingArgs = [
6-
(EventTarget | undefined) | (EventTarget | undefined)[],
7-
string | string[],
8-
AnyFunction,
9-
Record<string, unknown>?
10-
];
11-
127
const eventListener = (method: Method) => (
138
items: (EventTarget | undefined) | (EventTarget | undefined)[],
149
events: string | string[],
15-
fn: AnyFunction, options = {}
16-
): EventBindingArgs => {
10+
fn: AnyFunction,
11+
options = {}
12+
) => {
1713

1814
// Normalize array
1915
if (items instanceof HTMLCollection || items instanceof NodeList) {
2016
items = Array.from(items);
21-
} else if (!Array.isArray(items)) {
22-
items = [items];
2317
}
2418

25-
if (!Array.isArray(events)) {
26-
events = [events];
27-
}
19+
events = arrayify(events)
20+
items = arrayify(items);
2821

2922
for (const el of items) {
3023
if (el) {
@@ -33,8 +26,6 @@ const eventListener = (method: Method) => (
3326
}
3427
}
3528
}
36-
37-
return [items, events, fn, options];
3829
};
3930

4031
/**
+14-21
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,21 @@
1-
export type SelectAllSelectors = readonly (string | Element)[] | string | Element;
1+
import {arrayify} from './arrayify';
2+
3+
export type SelectAllSelectors = (string | Element)[] | string | Element;
24

35
/**
46
* Takes a selector (or array of selectors) and returns the matched nodes.
57
* @param selector The selector or an Array of selectors.
68
* @param doc
79
* @returns {Array} Array of DOM-Nodes.
810
*/
9-
export const selectAll = (selector: SelectAllSelectors, doc: Document = document): Element[] => {
10-
const list = !Array.isArray(selector) ? [selector] : selector;
11-
let nodes: Element[] = [];
12-
13-
for (let i = 0, l = list.length; i < l; i++) {
14-
const item = list[i];
15-
16-
if (typeof item === 'string') {
17-
/**
18-
* We can't use the spread operator here as with large amounts of elements
19-
* we'll get a "Maximum call stack size exceeded"-error.
20-
*/
21-
nodes = nodes.concat(Array.from(doc.querySelectorAll(item)));
22-
} else if (item instanceof Element) {
23-
nodes.push(item);
24-
}
25-
}
26-
27-
return nodes;
28-
};
11+
export const selectAll = (selector: SelectAllSelectors, doc: Document = document): Element[] =>
12+
arrayify(selector)
13+
.map(item =>
14+
typeof item === 'string'
15+
? Array.from(doc.querySelectorAll(item))
16+
: item instanceof Element
17+
? item
18+
: null
19+
)
20+
.flat()
21+
.filter(Boolean) as Element[];

0 commit comments

Comments
 (0)