MIRROR: javascript for 馃悳's, a tiny runtime with big ambitions
1const assert = (cond, msg) => {
2 if (!cond) throw new Error(msg);
3};
4
5let captured;
6function tag(strings) {
7 captured = strings;
8 return strings;
9}
10
11const first = tag`a${1}b`;
12const sameTextDifferentSite = tag`a${2}b`;
13
14function sameSite(value) {
15 return tag`a${value}b`;
16}
17
18const sameSiteFirst = sameSite(1);
19const sameSiteSecond = sameSite(2);
20
21assert(typeof Array.isTemplateObject === "function", "Array.isTemplateObject should exist");
22assert(Array.isTemplateObject(first) === true, "tagged template strings array should be a template object");
23assert(Array.isTemplateObject(first.raw) === false, "raw array should not be a template object");
24assert(Array.isTemplateObject([]) === false, "ordinary array should not be a template object");
25assert(Array.isTemplateObject(Object.freeze(["a", "b"])) === false, "frozen ordinary array should not be a template object");
26assert(Array.isTemplateObject({ raw: ["a"] }) === false, "ordinary object should not be a template object");
27assert(Array.isTemplateObject("not an array") === false, "primitive should not be a template object");
28assert(Array.isTemplateObject() === false, "missing argument should return false");
29assert(first !== sameTextDifferentSite, "different template sites should not reuse the same object");
30assert(sameSiteFirst === sameSiteSecond, "same template site should reuse the same object");
31assert(captured === sameSiteSecond, "tag should receive cached template object");
32
33console.log("Array.isTemplateObject tests ok");