-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
29 lines (22 loc) · 1.18 KB
/
utils.js
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
export const getConstructor = obj => !!obj.constructor && obj.constructor.name;
export const select = type => Array.from(document.querySelectorAll(`[data-${type}]`));
export const classList = arr => `'${arr.join(' ').trim()}'`;
export const traverseFragment = (fragment, cb) => { Array.from(fragment.childNodes).forEach(cb); };
const events = Object.keys(window).filter(key => key.startsWith('on')).map(key => key.slice(2).toLowerCase());
const bindings = [
...events, 'list', 'component', 'class', 'press', 'edit', 'if', 'state', 'textinput'
];
export const parseDataBinding = attr => {
if (attr.startsWith('data-')) {
const type = attr.split('data-')[1];
if (events.includes(type)) return 'event';
else if (bindings.includes(type)) return type;
} else return 'attr';
};
export const unwrapAccessor = accessor => {
return getConstructor(accessor) === 'StateManager' ? accessor.is() : getConstructor(accessor) === 'Is' ? accessor.evaluate() : null;
};
export const extractStates = accessor => {
return getConstructor(accessor) === 'StateManager' ? [accessor] : getConstructor(accessor) === 'Is' ? accessor.states : [];
}
export const stringIsNum = str => isNaN(+str) === false;