forked from observablehq/plot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplate.js
More file actions
25 lines (23 loc) · 678 Bytes
/
template.js
File metadata and controls
25 lines (23 loc) · 678 Bytes
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
export function template(strings, ...parts) {
let n = parts.length;
// If any of the interpolated parameters are strings rather than functions,
// bake them into the template to optimize performance during render.
for (let j = 0, copy = true; j < n; ++j) {
if (typeof parts[j] !== "function") {
if (copy) {
strings = strings.slice(); // copy before mutate
copy = false;
}
strings.splice(j, 2, strings[j] + parts[j] + strings[j + 1]);
parts.splice(j, 1);
--j, --n;
}
}
return (i) => {
let s = strings[0];
for (let j = 0; j < n; ++j) {
s += parts[j](i) + strings[j + 1];
}
return s;
};
}