aboutsummaryrefslogtreecommitdiff
path: root/src/util
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
parent64cb4c87978434f478227fa2ea97277ae50269aa (diff)
feat(util): add pure keycodes and safeJs template helper
Diffstat (limited to 'src/util')
-rw-r--r--src/util/js-template.ts20
-rw-r--r--src/util/keycodes.ts12
2 files changed, 32 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;
+};
diff --git a/src/util/keycodes.ts b/src/util/keycodes.ts
new file mode 100644
index 0000000..8669d1f
--- /dev/null
+++ b/src/util/keycodes.ts
@@ -0,0 +1,12 @@
+const VIRTUAL_KEY_CODES: Readonly<Record<string, number>> = {
+ Enter: 13, Tab: 9, Backspace: 8, Escape: 27, Delete: 46, " ": 32,
+ ArrowLeft: 37, ArrowUp: 38, ArrowRight: 39, ArrowDown: 40,
+ Home: 36, End: 35, PageUp: 33, PageDown: 34,
+};
+
+export const SPECIAL_KEYS: ReadonlyArray<string> = Object.keys(VIRTUAL_KEY_CODES);
+
+export const virtualKeyCode = (key: string): number =>
+ VIRTUAL_KEY_CODES[key] ?? (key.length === 1 ? key.charCodeAt(0) : 0);
+
+export const keyCode = (key: string): string => key;