UNPKG

2.07 kBJavaScriptView Raw
1function isValidNode(obj) {
2 var ELEM_TYPE = 1;
3 var FRAG_TYPE = 11;
4 return typeof HTMLElement === 'object'
5 ? obj instanceof HTMLElement || obj instanceof DocumentFragment
6 : obj &&
7 typeof obj === 'object' &&
8 obj !== null &&
9 (obj.nodeType === ELEM_TYPE || obj.nodeType === FRAG_TYPE) &&
10 typeof obj.nodeName === 'string';
11}
12export function isClassOrId(str) {
13 return str.length > 1 && (str[0] === '.' || str[0] === '#');
14}
15export function isDocFrag(el) {
16 return el.nodeType === 11;
17}
18export function checkValidContainer(container) {
19 if (typeof container !== 'string' && !isValidNode(container)) {
20 throw new Error('Given container is not a DOM element neither a selector string.');
21 }
22}
23export function getValidNode(selectors) {
24 var domElement = typeof selectors === 'string'
25 ? document.querySelector(selectors)
26 : selectors;
27 if (typeof selectors === 'string' && domElement === null) {
28 throw new Error("Cannot render into unknown element `" + selectors + "`");
29 }
30 return domElement;
31}
32export function getSelectors(namespace) {
33 var res = '';
34 for (var i = namespace.length - 1; i >= 0; i--) {
35 if (namespace[i].type !== 'selector') {
36 break;
37 }
38 res = namespace[i].scope + ' ' + res;
39 }
40 return res.trim();
41}
42export function isEqualNamespace(a, b) {
43 if (!Array.isArray(a) || !Array.isArray(b) || a.length !== b.length) {
44 return false;
45 }
46 for (var i = 0; i < a.length; i++) {
47 if (a[i].type !== b[i].type || a[i].scope !== b[i].scope) {
48 return false;
49 }
50 }
51 return true;
52}
53export function makeInsert(map) {
54 return function (type, elm, value) {
55 if (map.has(type)) {
56 var innerMap = map.get(type);
57 innerMap.set(elm, value);
58 }
59 else {
60 var innerMap = new Map();
61 innerMap.set(elm, value);
62 map.set(type, innerMap);
63 }
64 };
65}
66//# sourceMappingURL=utils.js.map
\No newline at end of file