-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathindex.js
More file actions
100 lines (90 loc) · 2.61 KB
/
index.js
File metadata and controls
100 lines (90 loc) · 2.61 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
//@ts-nocheck
import {
ELEMENT,
Node,
Comment,
DocumentType,
Text,
Fragment,
Element,
Component,
fromJSON,
props,
} from '../dom/ish.js';
import {
COMPONENT,
DOTS,
KEY,
HOLE,
update,
} from './update.js';
import parser from '../parser/index.js';
import resolve from '../json/resolve.js';
import { assign } from '../utils.js';
const textParser = parser({
Comment,
DocumentType,
Text,
Fragment,
Element,
Component,
update,
});
const { stringify, parse } = JSON;
const isNode = node => node instanceof Node;
const get = node => node.props === props ? (node.props = props) : node.props;
export default (jsx, jsxs = jsx) => {
const twm = new WeakMap;
const cache = (template, values) => {
const parsed = textParser(template, values, true);
parsed[0] = parse(stringify(parsed[0]));
twm.set(template, parsed);
return parsed;
};
const getProps = (node) => {
const { children } = node;
if (children.length) get(node).children = children.map(getValue);
return get(node);
};
const getValue = node => {
if (isNode(node)) {
return node.type === ELEMENT ?
getInvoke(node)(node.name, getProps(node)) :
node.toString()
;
}
return node;
};
const getInvoke = ({ children }) => (jsx === jsxs || children.every(isNode)) ? jsx : jsxs;
return (template, ...values) => {
const [json, updates] = twm.get(template) || cache(template, values);
// TODO: this could be mapped once so that every consecutive call
// will simply loop over values and updates[length](values[length])
// before returning the list or arguments to pass to jsx or jsxs
// this way it'd be way faster on repeated invokes, if needed/desired
const root = fromJSON(json);
let length = values.length, args, prev, node;
while (length--) {
const [path, type] = updates[length];
const value = values[length];
if (prev !== path) {
node = resolve(root, path);
prev = path;
args = [node.name, getProps(node)];
}
if (type === COMPONENT) {
args[0] = value;
const { children } = node.parent;
children[children.indexOf(node)] = getInvoke(node)(...args);
}
else if (type === KEY) args.push(value);
else if (type === DOTS) assign(get(node), value);
else if (type === HOLE) {
const { children } = node.parent;
children[children.indexOf(node)] = value;
}
else get(node)[type] = value;
}
return getValue(root.children[0]);
};
};