-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
56 lines (46 loc) · 1.4 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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
const _ = exports;
_.symbol = Symbol('symbol');
_.flat = a => a.reduce((f, v) => Array.isArray(v) ? f.concat(_.flat(v)) : f.concat(v), []);
_.arrify = a => (Array.isArray(a) ? a : typeof a === undefined ? [] : [a]).filter(a => Boolean(typeof a !== undefined));
_.deDupe = a => Array.from(new Set(a));
const childTypes = 'string|number|function|boolean'.split('|');
_.isChild = it =>
childTypes.includes(typeof it)
|| Array.isArray(it)
|| (it && it[_.symbol])
// || (it && it.children)
// || (it && it.attributes)
|| (it && it.$$typeof);
_.getPropsAndChildren = args => {
let props;
let children = [];
if (!args || !args.length) {
//
} else if (_.isTTL(args)) {
props = null;
children = _.parseTTL(...args);
} else if (args.length === 1) {
if (_.isChild(args[0])) {
props = null;
if (Array.isArray(args[0])) {
children = args[0];
} else {
children = args;
}
} else {
props = args[0];
}
} else {
if (_.isChild(args[0])) {
props = null;
children = args;
} else {
props = args[0] || null;
children = args.slice(1);
}
}
return { props, children };
};
// TTL = Tagged Template Literals
_.isTTL = args => Array.isArray(args[0]) && 'raw' in args[0];
_.parseTTL = (str, ...keys) => str.reduce((children, child, i) => children.concat([child, keys[i] !== undefined && keys[i]].filter(Boolean)), []);