-
-
Notifications
You must be signed in to change notification settings - Fork 126
Expand file tree
/
Copy pathcompile.js
More file actions
41 lines (36 loc) · 1.47 KB
/
compile.js
File metadata and controls
41 lines (36 loc) · 1.47 KB
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
import { parse } from './parse';
/**
* Can parse a compiled string, from a tagged template
* @param {String} value
* @param {Object} [props]
*/
export let compile = (str, defs, data) => {
return str.reduce((out, next, i) => {
let tail = defs[i];
// If this is a function we need to:
if (tail && tail.call) {
// 1. Call it with `data`
let res = tail(data);
// 2. Grab the className
let className = res && res.props && res.props.className;
// 3. If there's none, see if this is basically a
// previously styled className by checking the prefix
let end = className || (/^go/.test(res) && res);
if (end) {
// If the `end` is defined means it's a className
tail = '.' + end;
} else if (res && typeof res == 'object') {
// If `res` it's an object, we're either dealing with a vnode
// or an object returned from a function interpolation
tail = res.props ? '' : parse(res, '');
} else {
// Regular value returned. Can be falsy as well.
// Here we check if this is strictly a boolean with false value
// define it as `''` to be picked up as empty, otherwise use
// res value
tail = res;
}
}
return out + next + (tail == null || tail === false ? '' : tail);
}, '');
};