An open API service indexing awesome lists of open source software.

https://github.com/jlmakes/tealight

DOM queries that always return an array.
https://github.com/jlmakes/tealight

dom-library query

Last synced: 23 days ago
JSON representation

DOM queries that always return an array.

Awesome Lists containing this project

README

        


Flaming tea light





Tealight

DOM queries that always return an array.


Build status
Coverage
Version
0.4KB min+gzip
MIT License



Browser compatibility matrix



## Introduction

Modern browsers enable us to perform DOM queries natively, e.g:

```js
let cookies = document.querySelectorAll(".cookie");
```

But we all want to loop over the returned elements. So in practice, we’ve got to do a little more work, particularly converting the resulting `NodeList` to an array, e.g:

```js
let cookies;
try {
let query = document.querySelectorAll(".cookie");
cookies = Array.prototype.slice.call(query);
} catch (err) {
console.error(err.message);
}

cookies.forEach(cookie => {
// ...
});
```

**Tealight** provides a familiar API to perform native queries, without the extra work.

```js
tealight(".cookie").forEach(cookie => {
// ...
});
```



## Installation

### Browser

A simple and fast way to get started is to include this script on your page:

```html

```

This will create the global variable `tealight`.

### Module

```bash
$ npm install tealight
```

#### CommonJS

```js
const tealight = require("tealight");
```

#### ES2015

```js
import tealight from "tealight";
```



## Usage

`tealight` accepts a single argument `target` and will **always return an array of 0 or more DOM nodes**.

For the examples below, we will query against this HTML fragment:

```html






```


### `tealight(target: string): Array`

`string` targets will be used as CSS selectors.

```js
tealight("#jar");
// => [

]
```

```js
tealight(".cookie");
// => [ , , ]
```


### `tealight(target: HTMLElement): Array`

`HTMLElement` targets will simply be wrapped in an `Array`

```js
const node = document.querySelector("#jar");

tealight(node);
// => [

]
```


### `tealight(target: HTMLCollection) : Array`

`HTMLCollection` arguments will be converted to `Array`.

```js
const nodeList = document.querySelectorAll(".cookie");

tealight(nodeList);
// => [ , , ]
```


### `tealight(target: Array): Array`

`Array` targets will be filtered, leaving only `HTMLElement`

```js
let node = document.querySelector("#jar");
let array = [node, null, ".cookie"];

tealight(array);
// => [

]
```



---

Copyright 2020 Julian Lloyd


Open source under the [MIT License](https://github.com/jlmakes/tealight/blob/master/LICENSE).