@stonecrop/aform 0.11.8 → 0.11.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,18 @@
1
+ /**
2
+ * Deserializes a stringified function expression into a typed callable.
3
+ *
4
+ * Throws if the string cannot be parsed as a function (SyntaxError) or if the
5
+ * resulting expression is not callable (TypeError), or if the expression references
6
+ * an undefined variable (ReferenceError). Callers are responsible for try/catch.
7
+ *
8
+ * @example
9
+ * ```ts
10
+ * const fn = deserializeFunction<(x: number) => number>('(x) => x * 2')
11
+ * fn(5) // 10
12
+ * ```
13
+ * @public
14
+ */
15
+ // eslint-disable-next-line @typescript-eslint/no-implied-eval, @typescript-eslint/no-unsafe-call
16
+ export function deserializeFunction<T extends (...args: any[]) => any>(source: string): T {
17
+ return Function(`"use strict"; return (${source})`)() as T
18
+ }