-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.es6
43 lines (33 loc) · 1015 Bytes
/
index.es6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import { curry4 } from 'fj-curry';
import isDom from 'fd-isdom';
import cond from 'fj-cond';
import always from 'fj-always';
import and from 'fj-and';
import typeOf from 'fj-typeof';
let ELSE = always(true);
let isString = typeOf('string');
function toArray(arr) {
return Array.prototype.slice.call(arr);
}
function identity(arr) {
return arr;
}
function wrongType() {
throw new TypeError();
}
function queryAll(dom, selector) {
return dom.querySelectorAll(selector);
}
function queryOne(dom, selector) {
return dom.querySelector(selector);
}
function _select(queryFn, wrap, dom, selector) {
return cond([
[isString, () => wrap(queryFn(document, dom))],
[and(isDom(), () => !!selector), () => wrap(queryFn(dom, selector))],
[isDom(), () => curry4(_select)(queryFn)(wrap)(dom)],
[ELSE, wrongType]
])(dom);
}
export let select = (dom, selector) => _select(queryAll, toArray, dom, selector);
export let selectOne = (dom, selector) => _select(queryOne, identity, dom, selector);