Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

tealight

jlmakes24kMIT0.3.6TypeScript support: included

DOM queries that always return an array

html, dom, element, utility, query, selector

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:

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:

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.

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


Installation

Browser

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

<script src="https://unpkg.com/tealight"></script>

This will create the global variable tealight.

Module

$ npm install tealight

CommonJS

const tealight = require("tealight");

ES2015

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:

<div id="jar">
    <div class="chocolate-chip cookie"></div>
    <div class="peanut-butter cookie"></div>
    <div class="short-bread cookie"></div>
</div>

tealight(target: string): Array<HTMLElement>

string targets will be used as CSS selectors.

tealight("#jar");
// => [ <div#jar> ]
tealight(".cookie");
// => [ <div.chocolate-chip.cookie>, <div.peanut-butter.cookie>, <div.short-bread.cookie> ]

tealight(target: HTMLElement): Array<HTMLElement>

HTMLElement targets will simply be wrapped in an Array

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

tealight(node);
// => [ <div#jar> ]

tealight(target: HTMLCollection) : Array<HTMLElement>

HTMLCollection arguments will be converted to Array.

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

tealight(nodeList);
// => [ <div.chocolate-chip.cookie>, <div.peanut-butter.cookie>, <div.short-bread.cookie> ]

tealight(target: Array<any>): Array<HTMLElement>

Array targets will be filtered, leaving only HTMLElement

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

tealight(array);
// => [ <div#jar> ]



Copyright 2018 Fisssion LLC.
Open source under the MIT License.

changelog

Change Log

[0.3.6] - 2018-08-11

Fixed

  • Consolidate overloaded declarations.

0.3.5 - 2018-08-11

Fixed

  • Update type declaration for optional second argument.

0.3.4 - 2018-08-10

Changed

  • Include type declaration file in published files.

Fixed

  • Typo in readme

0.3.3 - 2018-08-10

Fixed

  • Typo in type declaration file path.

0.3.2 - 2018-08-10

Added

  • Add module function type declaration.

0.3.1 - 2018-07-25

Changed

  • Rename default function export.

0.3.0 - 2018-01-12

Changed

  • Refactor ES module distribution with exteral dependencies.
  • Add usage instructions to readme.

0.2.3 - 2018-01-11

Changed

  • Upgraded is-dom-node dependency.
  • Upgraded is-dom-node-list dependency.

Fixed

  • Upgrades removed duplicate is-dom-node code.

0.2.2 - 2018-01-11

  • Upgrade is-dom-node-list dependency.

0.2.1 - 2018-01-05

Changed

  • Updated distribution license format.

0.2.0 - 2018-01-04

Added

  • Tealight now supports a second context argument to scope DOM selectors.

Changed

  • The getNodes utility has been renamed to index, and is now exported by default.
  • Minified distribution now uses a condensed license format.

Removed

  • raf and mathSign polyfills were removed.
  • The isNode and isNodeList named exports were removed.
  • The deepAssign, each, getNode and isObject utilities were removed.

0.1.2 - 2017-12-04

  • Updated distribution license format.

0.1.1 - 2017-11-24

Changed

  • Added library version and size badges to README.md.
  • Added repository, keywords and author email to package.json

0.1.0 - 2017-11-24

Hello world!