Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add TypeScript definition #4

Merged
merged 13 commits into from
Feb 9, 2019
Merged
26 changes: 13 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ var select = require('select-dom')
select('.foo a[href=bar]')
// => <Element>

select('.foo a[href=bar]', parentElement)
select('.foo a[href=bar]', baseElement)
// => <Element>
```

Expand All @@ -37,7 +37,7 @@ select('.foo a[href=bar]', parentElement)
select.exists('.foo a[href=bar]')
// => true/false

select.exists('.foo a[href=bar]', parentElement)
select.exists('.foo a[href=bar]', baseElement)
// => true/false
```

Expand All @@ -47,35 +47,35 @@ select.exists('.foo a[href=bar]', parentElement)
select.all('.foo a[href=bar]')
// => [<Element>, <Element>, <Element>]

select.all('.foo a[href=bar]', parentElement)
select.all('.foo a[href=bar]', baseElement)
// => [<Element>, <Element>, <Element>]

select.all('.foo a[href=bar]', [parentElement1, parentElement2])
select.all('.foo a[href=bar]', [baseElement1, baseElement2])
// => [<Element>, <Element>, <Element>]
```

## API

**Note:** if a falsy value is passed as `parent`, you'll always get an empty result (bd578b9)
**Note:** if a falsy value is passed as `baseElement`, you'll always get an empty result (bd578b9)

### `select(selector[, parent = document])`
### `select(selector[, baseElement = document])`

Maps to `parent.querySelector(selector)`
Maps to `baseElement.querySelector(selector)`

### `select.exists(selector[, parent = document])`
### `select.exists(selector[, baseElement = document])`

Tests the existence of one or more elements matching the selector.

### `select.all(selector[, parents = document])`
### `select.all(selector[, baseElements = document])`

Maps to `parents.querySelectorAll(selector)` plus:
Maps to `baseElements.querySelectorAll(selector)` plus:

* it always returns an array
* parents can be an element, an array of elements, or NodeList
* baseElements can be an element, an array of elements, or NodeList

This lets you search through an existing list of elements, like:

```js
const parents = select.all('.parents').filter(Math.random);
select.all('.foo a[href=bar]', parents);
const baseElements = select.all('.baseElements').filter(Math.random);
select.all('.foo a[href=bar]', baseElements);
```
47 changes: 47 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// https://github.com/Microsoft/TypeScript/blob/9d3707d671592d030386956c9ce39e539b8d0972/src/lib/dom.generated.d.ts#L10581

type BaseElement = Element | DocumentFragment;
type BaseElements = Element | Element[] | NodeList | DocumentFragment;

export interface SelectDom {
fregante marked this conversation as resolved.
Show resolved Hide resolved
<K extends keyof HTMLElementTagNameMap>(
selectors: K,
baseElement?: BaseElement
): HTMLElementTagNameMap[K] | null;
<K extends keyof SVGElementTagNameMap>(
selectors: K,
baseElement?: BaseElement
): SVGElementTagNameMap[K] | null;
<E extends Element = Element>(
selectors: string,
baseElement?: BaseElement
): E | null;
exists<K extends keyof HTMLElementTagNameMap>(
selectors: K,
baseElement?: BaseElement
): boolean;
exists<K extends keyof SVGElementTagNameMap>(
selectors: K,
baseElement?: BaseElement
): boolean;
exists<E extends Element = Element>(
selectors: string,
baseElement?: BaseElement
): boolean;
all<K extends keyof HTMLElementTagNameMap>(
selectors: K,
baseElements?: BaseElements
): HTMLElementTagNameMap[K][];
all<K extends keyof SVGElementTagNameMap>(
selectors: K,
baseElements?: BaseElements
): SVGElementTagNameMap[K][];
all<E extends Element = Element>(
selectors: string,
baseElements?: BaseElements
): E[];
}

declare const select: SelectDom;

export default select
42 changes: 21 additions & 21 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,54 +1,54 @@
'use strict';

/**
* @param {string} selector One or more CSS selectors separated by commas
* @param {Element} [parent] The element to look inside of
* @param {string} selectors One or more CSS selectors separated by commas
* @param {Element} [baseElement] The element to look inside of
* @return {?Element} The element found, if any
*/
function select(selector, parent) {
// Shortcut with specified-but-null parent
if (arguments.length === 2 && !parent) {
function select(selectors, baseElement) {
// Shortcut with specified-but-null baseElement
if (arguments.length === 2 && !baseElement) {
return null;
}

return (parent || document).querySelector(selector);
return (baseElement || document).querySelector(selectors);
}

/**
* @param {string} selector One or more CSS selectors separated by commas
* @param {Element} [parent] The element to look inside of
* @param {string} selectors One or more CSS selectors separated by commas
* @param {Element} [baseElement] The element to look inside of
* @return {boolean} Whether it's been found
*/
select.exists = function (selector, parent) {
select.exists = function (selectors, baseElement) {
if (arguments.length === 2) {
return Boolean(select(selector, parent));
return Boolean(select(selectors, baseElement));
}

return Boolean(select(selector));
return Boolean(select(selectors));
};

/**
* @param {string} selector One or more CSS selectors separated by commas
* @param {Element|Element[]} [parent] The element or list of elements to look inside of
* @param {string} selectors One or more CSS selectors separated by commas
* @param {Element|Element[]} [baseElements] The element or list of elements to look inside of
* @return {Element[]} An array of elements found
*/
select.all = function (selector, parent) {
// Shortcut with specified-but-null parent
if (arguments.length === 2 && !parent) {
select.all = function (selectors, baseElements) {
// Shortcut with specified-but-null baseElements
if (arguments.length === 2 && !baseElements) {
return [];
}

// Can be: select.all('selector') or select.all('selector', singleElementOrDocument)
if (!parent || typeof parent.querySelectorAll === 'function') {
return Array.apply(null, (parent || document).querySelectorAll(selector));
// Can be: select.all('selectors') or select.all('selectors', singleElementOrDocument)
if (!baseElements || typeof baseElements.querySelectorAll === 'function') {
return Array.apply(null, (baseElements || document).querySelectorAll(selectors));
}

var current;
var i;
var ii;
var all;
for (i = 0; i < parent.length; i++) {
current = parent[i].querySelectorAll(selector);
for (i = 0; i < baseElements.length; i++) {
current = baseElements[i].querySelectorAll(selectors);
if (!all) {
all = Array.apply(null, current);
continue;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "select-dom",
"version": "4.1.3",
"version": "4.2.0-3",
"description": "Extra lightweight DOM selector helper",
"main": "index.js",
"scripts": {
Expand Down