Skip to content

Commit

Permalink
Add strict version (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
fregante authored Oct 27, 2024
1 parent b21271c commit 3c1ee88
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 6 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@ node_modules
npm-debug.log
index.js
index.d.ts
strict.js
strict.d.ts
*.map
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
"license": "MIT",
"author": "Federico Brigante <[email protected]> (https://fregante.com)",
"type": "module",
"exports": "./index.js",
"exports": {
".": "./index.js",
"./strict.js": "./strict.js"
},
"main": "./index.js",
"types": "./index.d.ts",
"files": [
Expand Down
18 changes: 14 additions & 4 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@

> Lightweight `querySelector`/`All` wrapper that outputs an Array
Version 7+ only supports browsers with [iterable `NodeList`s](https://developer.mozilla.org/en-US/docs/Web/API/NodeList/entries). If you need IE support, stick to [`select-dom@6`](https://github.com/fregante/select-dom/tree/v6.0.4) or lower.

## Install

```bash
npm install select-dom
```

```js
// This module is only offered as a ES Module
import {$, $$, lastElement, elementExists} from 'select-dom';

// And a stricter version
import {$, $optional} from 'select-dom/strict.js';
```

## API
Expand All @@ -26,7 +26,6 @@ import {$, $$, lastElement, elementExists} from 'select-dom';
### `$(selector[, baseElement = document])`

Maps to `baseElement.querySelector(selector)`, except it returns `undefined` if it's not found

```js
$('.foo a[href=bar]');
// => <Element>
Expand Down Expand Up @@ -73,6 +72,17 @@ $$('.foo', [baseElement1, baseElement2]);
// This is similar to jQuery([baseElement1, baseElement2]).find('.foo')
```

## /strict.js

The strict export will throw an error if the element is not found, instead of returning `undefined`. This is also reflected in the types, which are non-nullable:

```ts
import {$, $optional} from 'select-dom/strict.js';

const must: HTMLAnchorElement = $('.foo a[href=bar]'); //
const optional: HTMLAnchorElement | undefined = $optional('.foo a[href=bar]');
```

## Related

- [delegate-it](https://github.com/fregante/delegate-it) - DOM event delegation, in <1KB.
Expand Down
1 change: 1 addition & 0 deletions strict.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {$ as $optional, expectElement as $} from './index.js';
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"extends": "@sindresorhus/tsconfig",
"files": [
"index.ts"
"index.ts",
"strict.ts"
]
}

0 comments on commit 3c1ee88

Please sign in to comment.