select-dom

Lightweight querySelector/All/closest wrapper that returns an array and optionally throws if elements are not found

MIT 52 个版本
安装
npm install select-dom
yarn add select-dom
pnpm add select-dom
bun add select-dom
README

select-dom npm downloads

Lightweight querySelector/All/closest wrapper that returns an array and optionally throws if elements are not found

Install

npm install select-dom
import {
	$,
	$$,
	lastElement,
	elementExists,
	assertElementExists,
	$optional,
	$$optional,
	lastElementOptional,
	$closest,
	$closestOptional,
} from 'select-dom';

The package also includes an ESLint plugin entry point at select-dom/eslint-plugin; read documentation.

API

Note: if a falsy value is passed as baseElement, $, $$, lastElement throw ElementNotFoundError, while $optional, $$optional, lastElementOptional return undefined/[] (bd578b9)

$

$optional

$ maps to baseElement.querySelector(selector), except it throws ElementNotFoundError if no element is found. $optional returns undefined instead of throwing.

$('.foo a[href=bar]');
// => <Element>

$('.non-existent');
// throws ElementNotFoundError

$optional('.non-existent');
// => undefined

$$

$$optional

$$ maps to baseElements.querySelectorAll(selector) and always returns an array. baseElements can also be an array of elements to search within. Throws ElementNotFoundError if no elements are found. $$optional returns [] instead of throwing.

$$('.foo');
// => [<Element>, <Element>, <Element>]

$$('.foo', baseElement);
// => [<Element>, <Element>, <Element>]

$$('.foo', [baseElement1, baseElement2]);
// => [<Element>, <Element>, <Element>]

$$('.non-existent');
// throws ElementNotFoundError

$$optional('.non-existent');
// => []

lastElement

lastElementOptional

Like $/$optional, except they return the last matching element instead of the first.

lastElement('.foo');
// => <Element>

lastElement('.non-existent');
// throws ElementNotFoundError

lastElementOptional('.non-existent');
// => undefined

$closest

$closestOptional

Like element.closest(selector), except baseElement can be any Node including text nodes. $closest throws ElementNotFoundError if not found; $closestOptional returns undefined.

$closest('button', event.target);
// => <button>

$closest('button', button.firstChild); // text nodes are supported
// => <button>

$closest('.non-existent', element);
// throws ElementNotFoundError

$closestOptional('.non-existent', element);
// => undefined

elementExists

Tests the existence of one or more elements. Like $optional(), but returns a boolean.

elementExists('.foo a[href=bar]');
// => true/false

assertElementExists

Like elementExists(), but throws ElementNotFoundError instead of returning false.

assertElementExists('.foo a[href=bar]');
// => void (if element exists)
// => throws ElementNotFoundError (if element doesn't exist)

countElements

Counts the number of elements found. Shortcut for $$optional(selector).length.

countElements('a');
// => 3

ESLint

select-dom/eslint-plugin includes the select-dom/prefer rule, which autofixes .querySelector(), .querySelectorAll(), and .closest() calls to the matching select-dom helpers.

import selectDom from 'select-dom/eslint-plugin';

export default [
	{
		plugins: {
			'select-dom': selectDom,
		},
		rules: {
			'select-dom/prefer': 'error',
		},
	},
];

By default, the rule reports all supported calls. To keep readability exceptions like element.firstChild.querySelector(selector), enable allowReadabilityExceptions.

import selectDom from 'select-dom/eslint-plugin';

export default [
	{
		plugins: {
			'select-dom': selectDom,
		},
		rules: {
			'select-dom/prefer': ['error', {
				allowReadabilityExceptions: true,
			}],
		},
	},
];
  • delegate-it - DOM event delegation, in <1KB.
  • doma - Parse an HTML string into DocumentFragment or one Element, in a few bytes.
版本列表
10.1.0-1 2026-05-22
10.1.0-0 2026-05-22
10.1.0 2026-05-22
10.0.0-0 2026-04-24
10.0.0 2026-05-01
9.5.0 2025-11-18
9.4.0 2025-07-20
9.3.1 2025-03-31
9.3.0 2024-12-08
9.2.0 2024-11-04
9.1.1 2024-10-27
9.1.0 2024-10-27
9.0.1 2024-10-26
9.0.0 2023-10-20
8.0.0 2023-07-07
7.1.1 2021-04-07
7.1.0 2021-01-03
7.0.0-6 2020-12-30
7.0.0-5 2020-12-29
7.0.0-4 2020-12-29
7.0.0-3 2020-12-29
7.0.0-2 2020-12-27
7.0.0-1 2020-12-27
7.0.0-0 2020-12-27
7.0.0 2020-12-19
6.0.2 2020-05-01
6.0.1 2020-04-07
6.0.0 2019-12-15
5.1.0 2019-05-25
5.0.2 2019-05-10
5.0.1 2019-03-18
5.0.0-0 2019-03-13
5.0.0 2019-03-15
4.2.2 2019-03-13
4.2.1 2019-02-13
4.2.0-3 2019-02-06
4.2.0-2 2019-02-05
4.2.0-1 2019-02-04
4.2.0-0 2019-02-02
4.2.0 2019-02-09
4.1.3 2018-05-25
4.1.2 2018-05-25
4.1.1 2017-07-03
4.1.0 2017-06-07
4.0.0 2017-06-06
3.1.0 2016-06-03
3.0.2 2016-07-27
3.0.1 2016-06-03
2.0.0 2016-03-22
1.0.1 2016-06-03
0.0.1-security 2016-03-23
0.0.0 2013-09-14