aboutsummaryrefslogtreecommitdiff
path: root/src/util/js-template.ts
diff options
context:
space:
mode:
authoramankumarsingh77 <amankumarsingh7702@gmal.com>2026-05-02 15:36:13 +0530
committeramankumarsingh77 <amankumarsingh7702@gmal.com>2026-05-02 15:36:13 +0530
commit31afcecdd849529bc5d6ae0719313f0f37e7cebc (patch)
treea7afbf01aa0ca68162c352a6137f4a164b0b8a4e /src/util/js-template.ts
parent64cb4c87978434f478227fa2ea97277ae50269aa (diff)
feat(util): add pure keycodes and safeJs template helper
Diffstat (limited to 'src/util/js-template.ts')
-rw-r--r--src/util/js-template.ts20
1 files changed, 20 insertions, 0 deletions
diff --git a/src/util/js-template.ts b/src/util/js-template.ts
new file mode 100644
index 0000000..e536b3d
--- /dev/null
+++ b/src/util/js-template.ts
@@ -0,0 +1,20 @@
+/**
+ * Build a JavaScript source string with safely-interpolated values.
+ * Every interpolated value is JSON.stringify'd, so strings, numbers,
+ * objects, and special characters all become valid JS literals.
+ *
+ * This is the ONLY supported way to build evaluation source in the
+ * codebase — never use raw template literals for JS that crosses the
+ * CDP boundary.
+ */
+export const safeJs = (
+ strings: TemplateStringsArray,
+ ...values: ReadonlyArray<unknown>
+): string => {
+ let out = strings[0] ?? "";
+ for (let i = 0; i < values.length; i++) {
+ out += JSON.stringify(values[i]);
+ out += strings[i + 1] ?? "";
+ }
+ return out;
+};