Better error handling, and handling of numeric config options

This commit is contained in:
Alan Grainger
2026-07-24 22:15:17 +12:00
parent a1e440c146
commit aa6183a95d
13 changed files with 345 additions and 51 deletions
+13
View File
@@ -12,6 +12,19 @@ export function escapeHtml (str: string): string {
.replace(/'/g, ''')
}
/**
* Serialise a value as JSON that is safe to embed inside an inline `<script>`
* block. `JSON.stringify` does not escape `<`, so a string value containing
* `</script>` would terminate the script element early and inject markup into
* the page.
*/
export function jsonForInlineScript (value: unknown): string {
return JSON.stringify(value)
.replace(/</g, '\\u003c')
.replace(/\u2028/g, '\\u2028')
.replace(/\u2029/g, '\\u2029')
}
/**
* Narrow an `unknown` to `string`, returning an empty string for any other
* type. Used at boundaries where the input shape isn't statically known