Skip to content

Commit 93516df

Browse files
committed
feat: add function to select a single element
sometimes you need to accept only a single element, selectOne allows you to do that
1 parent 54efaf1 commit 93516df

4 files changed

Lines changed: 73 additions & 17 deletions

File tree

README.md

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
·
2020
</p>
2121

22-
You are writing a JavaScript library and need to accept one or more HTML elements? But you want to leave your users a broad choice, how to select them? Then this is for you.
22+
You are writing a JavaScript library and need to receive one or more HTML elements? But you want to leave your users a broad choice, on how to select them? Then this might be for you.
2323

2424
## Table of Contents
2525

@@ -35,7 +35,7 @@ You are writing a JavaScript library and need to accept one or more HTML element
3535

3636
Universal Element Accept (uea) allows you to accept a range of different inputs as HTML elements.
3737

38-
It accepts, query selector strings, arrays, NodeLists and more (check out [usage](#usage)) and always returns an array with HTMLElements.
38+
It accepts query selector strings, arrays, NodeLists and more (check out [usage](#usage)) and always returns either an array with HTMLElements or a single element, depending on the function you use.
3939

4040
## Install
4141

@@ -56,14 +56,20 @@ npm install @compactjs/uea
5656
### As module:
5757

5858
```javascript
59-
import { accept } from '@compactjs/uea';
59+
import { select, selectOne } from '@compactjs/uea';
6060
```
6161

6262
### Example:
6363

6464
```javascript
65-
accept('.my-classes');
66-
accept(document.getElementById('my-id'));
65+
// select multiple elements
66+
select('.my-classes');
67+
select(document.getElementById('my-id'));
68+
select(document.forms);
69+
70+
// or only select a single element
71+
selectOne('.my-classes');
72+
selectOne(document.getElementById('my-id'));
6773
```
6874

6975
Check out the [demo](https://compactjs.github.io/uea/)
@@ -73,11 +79,18 @@ and the [examples file](https://github.com/CompactJS/uea/blob/main/example/index
7379

7480
```typescript
7581
/**
76-
* Allow a range of different ways to select HTML Elements
82+
* Use a range of different ways to select HTML elements.
83+
* Always returns an array of elements.
7784
*/
78-
function accept(
85+
export function select(
7986
input: string | HTMLElement | HTMLElement[] | HTMLCollection | NodeList
8087
): HTMLElement[];
88+
89+
/**
90+
* Use a range of different ways to select an HTMLElement.
91+
* Always returns a single HTMLElement.
92+
*/
93+
export function selectOne(selector: string | HTMLElement): HTMLElement | null;
8194
```
8295

8396
## Run tests

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
"test": "jest"
1717
},
1818
"files": [
19-
"dist"
19+
"dist",
20+
"package.json"
2021
],
2122
"repository": {
2223
"type": "git",

src/index.ts

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,42 @@ export type UniversalElementSelector =
55
| HTMLCollection
66
| NodeList;
77

8-
export const accept = (input: UniversalElementSelector): HTMLElement[] => {
8+
export type SingleElementSelector = string | HTMLElement;
9+
10+
/**
11+
* Allow a range of different ways to select HTML Elements.
12+
* Always returns a single element.
13+
*/
14+
export function selectOne(selector: SingleElementSelector): HTMLElement | null {
15+
if (typeof selector === 'string') {
16+
return document.querySelector<HTMLElement>(selector);
17+
} else if (selector instanceof HTMLElement) {
18+
return selector;
19+
}
20+
return null;
21+
}
22+
23+
/**
24+
* alias for backwards compatibility
25+
* @deprecated use select() instead
26+
*/
27+
export const accept = select;
28+
29+
/**
30+
* Allow a range of different ways to select HTML Elements.
31+
* Always returns an array of HTMLElements.
32+
*/
33+
export function select(input: UniversalElementSelector): HTMLElement[] {
934
if (typeof input === 'string')
1035
return Array.from(document.querySelectorAll(input));
1136
if (Array.isArray(input)) return input;
1237
if ('length' in input)
1338
return Array.from(input).reduce(nodeToElementArray, []);
1439
return [input];
15-
};
40+
}
1641

1742
// typeguard
18-
const nodeToElementArray = (
19-
array: HTMLElement[],
20-
node: Node
21-
): HTMLElement[] => {
43+
function nodeToElementArray(array: HTMLElement[], node: Node): HTMLElement[] {
2244
if (node.nodeType === Node.ELEMENT_NODE) array.push(node as HTMLElement);
2345
return array;
24-
};
46+
}

test/accept.test.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { accept } from '../src/index';
1+
import { accept, selectOne } from '../src/index';
22

3-
describe('accept', () => {
3+
describe('accepts multiple', () => {
44
it('returns a HTMLElement list for strings', () => {
55
document.body.innerHTML = `
66
<h1></h1>
@@ -40,3 +40,23 @@ describe('accept', () => {
4040
expect(acceptedElements[0]).toBeInstanceOf(HTMLFormElement);
4141
});
4242
});
43+
44+
describe('accepts single', () => {
45+
it('returns a HTMLElement for a string input', () => {
46+
document.body.innerHTML = `
47+
<h1></h1>
48+
<div class="a-class"></div>
49+
<span id="empty">empty</span>
50+
`;
51+
const element: HTMLElement = selectOne('h1');
52+
expect(element).toBeInstanceOf(HTMLHeadingElement);
53+
54+
const element2: HTMLElement = selectOne('.a-class');
55+
expect(element2).toBeInstanceOf(HTMLDivElement);
56+
});
57+
it('returns an HTMLElement with a HTMLElement as input', () => {
58+
const element = document.createElement('div');
59+
const acceptedElement = selectOne(element);
60+
expect(acceptedElement).toBeInstanceOf(HTMLDivElement);
61+
});
62+
});

0 commit comments

Comments
 (0)