D
Deno

help

Detecting invalid JS strings

Aabi2/24/2023
Is there any built-in way to "detect" invalid strings? Here's an example:
// this is an invalid unicode code point
const bad = "\udc11"

// but i can console.log it:
console.log(bad)
// prints: �

// and i can use it in other strings:
const foo = bad + "-" + bad

// but when i try to evaluate it in the repl:
> badAndPrefixed
Unterminated string literal Unknown exception
// this is an invalid unicode code point
const bad = "\udc11"

// but i can console.log it:
console.log(bad)
// prints: �

// and i can use it in other strings:
const foo = bad + "-" + bad

// but when i try to evaluate it in the repl:
> badAndPrefixed
Unterminated string literal Unknown exception
Some questions: 1. Can I somehow detect "bad" Unicode strings? 2. Why can I console.log it, and what does it do? 3. What happens in the Deno REPL that makes it throw an error?
ABTAndreu Botella (they/them)2/24/2023
There's a stage 3 proposal to detect non-valid-UTF-8 (or to use the term they're going with in the proposal, "non-well-formed") strings: https://github.com/tc39/proposal-is-usv-string
GitHub
GitHub - tc39/proposal-is-usv-string: a proposal for a method to de...
a proposal for a method to determine if a String is welll-formed Unicode - GitHub - tc39/proposal-is-usv-string: a proposal for a method to determine if a String is welll-formed Unicode
ABTAndreu Botella (they/them)2/24/2023
for now you can test it with !/\p{Surrogate}/u.test(str) 2. console.log() and the REPL have different printing implementations, with console.log() being written in JS and the REPL mainly in Rust. Rust's string type is UTF-8-based, and it doesn't support invalid UTF-16. Looks like what console.log() is essentially doing is encoding the JS string into UTF-8 using the "lossy" encoding which turns invalid UTF-16 code points into a replacement character (U+FFFD, �) I think there's a bug open for the REPL output to do that what the REPL currently does is use a lossless conversion that can fail when given invalid UTF-16, as in this case

Looking for more? Join the community!

Recommended Posts
Error connecting to MongoDB AtlasHello, I'm trying to connect to MongoDB Atlas via `mongoose` (`npm:mongoose`) but encountered the fointeract with deno Extension at runtimeIs there any way to interact with an extension at code execution? I need to pass a pointer from my rAny good way to modify remote url file and debug it?For example, I'm using `import server "https://deno.land/x/ultra@v2.1.7/server.ts"` I want to modifyCache related headers not included in deno deploy for static file servingI have a hello world level static site using `serveDir`. When I run the site locally I see that WeakError with relativ import path after "deno install"Is it possible that imports in deno.json are ignored after "deno install"? ```bash deno run ./my-Run examples on WindowsAnyone know how to run the `Piping to files` example on Windows? https://deno.land/manual@v1.30.3/exPython alternative to ctypes, can't figure how to convert a codeHello, I am trying to convert a Python ctypes code to Deno using FFI, but can't know why its not woSuggestions for minimal JSX rendering?Any suggestions for rendering jsx to a string (or ideally a stream), in the most minimal manner fromDeno.run Strange IssueI use `Deno.run` + stdout/signals/etc a lot, but this behavior is beyond my current debugging abilitProperly continuously read data from a ReaderWhat's the proper way to continuously read from a `Deno.Reader`? I'm trying to parse incoming HTTP2npm:redis – everything returns `string`?Every command I try to use from `npm:redis` seems to have inferred return type `string`. Look at theMy magic middlewares typeIf anyone wants a typescript challange here is one: I am trying to create a tuple type which containRead one byte at a time from ReadableStreamWhat would be the most obvious way to read a single byte from a ReadableStream at a time?My deno.lock file exceeds 4000 lines of JSON nowIs this common for application projects? Is there any nice way to clean it up?