-
Notifications
You must be signed in to change notification settings - Fork 0
/
DOMingo.js
86 lines (66 loc) · 2.54 KB
/
DOMingo.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
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
function nodeReduce(nodes, node) {
return [...(node.childNodes || [])]
.reduce(nodeReduce, nodes)
.concat([...(node.attributes || [])])
.concat(node.nodeValue ? node : []);
}
var sanitize = (str) => str.replace(/[$^?.*+\\[\]{}()]/gm, match => `\\${match}`);
var bindFinder = (openDelim, closeDelim) => new RegExp(`(?:${sanitize(openDelim)}\\s?)(\\S+)(?:\\s?${sanitize(closeDelim)})`, 'g');
var hasBindingsWithDelem = (openDelim = '{{', closeDelim = '}}') => node =>
node.nodeValue.match(bindFinder(openDelim, closeDelim));
function interleave (arr1, arr2) {
return arr1.reduce((interleaved, item, i) => {
let arr2Item = arr2[i];
interleaved.push(item);
if (arr2Item !== undefined) {
interleaved.push(arr2Item);
}
return interleaved;
}, []);
}
function dominate (openDelem = '{{', closeDelem = '}}') {
return node => {
let re = bindFinder(openDelem, closeDelem),
text = node.nodeValue,
// splits string at delimiters.
parts = text.split(re);
//odd indicies: will always be data paths
node[symbols.dataPaths] = parts.filter((part, i) => i % 2);
//even indicies: will always be static values
node[symbols.staticValues] = parts.filter((part, i) => !(i % 2));
return node;
};
}
var deepPath = (obj, path) => path.split(/[.\/]/)
.reduce((currObj, nextProp) => Object(currObj)[nextProp], obj);
const symbols = {
dataPaths: Symbol('dataPaths'),
staticValues: Symbol('staticValues')
};
function DOMingo(root, {
delimiters = ['{{', '}}']
} = {}) {
const hasBindings = hasBindingsWithDelem(...delimiters),
nodesWithBindings = [...root.childNodes]
//collects flat list of all nodes
.reduce(nodeReduce, [])
//removes nodes that don't have bindings
.filter(hasBindings)
//adds symbol properties to nodes for quick rendering
.map(dominate(...delimiters));
//the render function.
return function (data) {
nodesWithBindings.map(node => {
const currText = node.nodeValue,
dataValues = node[symbols.dataPaths]
.map(dataPath => deepPath(data, dataPath))
.map(val => val == null ? '' : val),
newText = interleave(node[symbols.staticValues], dataValues)
.join('');
if (newText !== currText) {
node.nodeValue = newText;
}
});
};
}
export { symbols };export default DOMingo;