From 31afcecdd849529bc5d6ae0719313f0f37e7cebc Mon Sep 17 00:00:00 2001 From: amankumarsingh77 Date: Sat, 2 May 2026 15:36:13 +0530 Subject: feat(util): add pure keycodes and safeJs template helper --- src/util/js-template.ts | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 src/util/js-template.ts (limited to 'src/util/js-template.ts') 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 +): string => { + let out = strings[0] ?? ""; + for (let i = 0; i < values.length; i++) { + out += JSON.stringify(values[i]); + out += strings[i + 1] ?? ""; + } + return out; +}; -- cgit v1.2.3