Socket
Socket
Sign inDemoInstall

tealight

Package Overview
Dependencies
2
Maintainers
1
Versions
15
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    tealight

DOM queries that always return an array


Version published
Weekly downloads
7.4K
increased by11.37%
Maintainers
1
Install size
37.2 kB
Created
Weekly downloads
 

Changelog

Source

[0.3.6] - 2018-08-11

Fixed

  • Consolidate overloaded declarations.

Readme

Source

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.

Keywords

FAQs

Last updated on 11 Aug 2018

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc