select-dom
Advanced tools
Comparing version 4.1.3 to 4.2.0-0
46
index.js
'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)); | ||
} | ||
@@ -48,4 +50,4 @@ | ||
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) { | ||
@@ -55,2 +57,3 @@ all = Array.apply(null, current); | ||
} | ||
for (ii = 0; ii < current.length; ii++) { | ||
@@ -62,2 +65,3 @@ if (all.indexOf(current[ii]) < 0) { | ||
} | ||
return all; | ||
@@ -64,0 +68,0 @@ }; |
{ | ||
"name": "select-dom", | ||
"version": "4.1.3", | ||
"version": "4.2.0-0", | ||
"description": "Extra lightweight DOM selector helper", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -22,33 +22,34 @@ # select-dom [![gzipped size][badge-gzip]](#no-link) [![Travis build status][badge-travis]][link-travis] [![npm version][badge-version]][link-npm] [![npm downloads][badge-downloads]][link-npm] | ||
var select = require('select-dom') | ||
``` | ||
/** | ||
* select() | ||
*/ | ||
### `select()` | ||
```js | ||
select('.foo a[href=bar]') | ||
// => <Element> | ||
select('.foo a[href=bar]', parentElement) | ||
select('.foo a[href=bar]', baseElement) | ||
// => <Element> | ||
``` | ||
/** | ||
* select.exists() | ||
*/ | ||
### `select.exists()` | ||
```js | ||
select.exists('.foo a[href=bar]') | ||
// => true/false | ||
select.exists('.foo a[href=bar]', parentElement) | ||
select.exists('.foo a[href=bar]', baseElement) | ||
// => true/false | ||
``` | ||
### `select.all()` | ||
/** | ||
* select.all() | ||
*/ | ||
```js | ||
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>] | ||
@@ -59,24 +60,24 @@ ``` | ||
### `select(selector[, parent = document])` | ||
**Note:** if a falsy value is passed as `baseElement`, you'll always get an empty result (bd578b9) | ||
Maps to `parent.querySelector(selector)` | ||
### `select(selector[, baseElement = document])` | ||
### `select.exists(selector[, parent = document])` | ||
Maps to `baseElement.querySelector(selector)` | ||
### `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 *undefined*, an element, an array of elements, or NodeList | ||
* baseElements can be an element, an array of elements, or NodeList | ||
Essentially now you can do something like: | ||
This lets you search through an existing list of elements, like: | ||
```js | ||
const parents = select.all('.parents'); | ||
if(checkSomething(parents)) { | ||
select.all('.foo a[href=bar]', parents); | ||
} | ||
const baseElements = select.all('.baseElements').filter(Math.random); | ||
select.all('.foo a[href=bar]', baseElements); | ||
``` |
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
6077
5
97
82
2