Page MenuHomePhorge

test_array_is_template_object.cjs
No OneTemporary

Size
1 KB
Referenced Files
None
Subscribers
None

test_array_is_template_object.cjs

const assert = (cond, msg) => {
if (!cond) throw new Error(msg);
};
let captured;
function tag(strings) {
captured = strings;
return strings;
}
const first = tag`a${1}b`;
const sameTextDifferentSite = tag`a${2}b`;
function sameSite(value) {
return tag`a${value}b`;
}
const sameSiteFirst = sameSite(1);
const sameSiteSecond = sameSite(2);
assert(typeof Array.isTemplateObject === "function", "Array.isTemplateObject should exist");
assert(Array.isTemplateObject(first) === true, "tagged template strings array should be a template object");
assert(Array.isTemplateObject(first.raw) === false, "raw array should not be a template object");
assert(Array.isTemplateObject([]) === false, "ordinary array should not be a template object");
assert(Array.isTemplateObject(Object.freeze(["a", "b"])) === false, "frozen ordinary array should not be a template object");
assert(Array.isTemplateObject({ raw: ["a"] }) === false, "ordinary object should not be a template object");
assert(Array.isTemplateObject("not an array") === false, "primitive should not be a template object");
assert(Array.isTemplateObject() === false, "missing argument should return false");
assert(first !== sameTextDifferentSite, "different template sites should not reuse the same object");
assert(sameSiteFirst === sameSiteSecond, "same template site should reuse the same object");
assert(captured === sameSiteSecond, "tag should receive cached template object");
console.log("Array.isTemplateObject tests ok");

File Metadata

Mime Type
application/javascript
Expires
Mon, May 4, 12:35 AM (2 d)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
545435
Default Alt Text
test_array_is_template_object.cjs (1 KB)

Event Timeline