|
|
|
|
|
by crdrost
604 days ago
|
|
That advice is 90% because developers are lazy. Like we'll write const csv = rows.map(cols => cols.join(','))
.join('\n')
because we are too lazy to write the more correct, const esc = cell => `"${String(cell).replace(/"/g, '""')}"`
const csv = rows.map(cols => cols.map(esc).join(','))
.join('\n')
(And perhaps something slightly more efficient but slower that only quotes each cell when it needs to be escaped.)I caught myself doing it the other day, Go has a JSON library and here I was too lazy to define a struct, w.WriteHeader(500)
fmt.Fprintf(w, `{"error": %q}`, err.Error())
Is %q a JSON-compatible format? I have no idea without reading some source code! Almost certainly it won't \u-encode weird characters. That might be OK, I think the only stuff you really have to escape in JSON strings is newlines, backslashes, and double quotes? And %q probably handles those. Maybe it breaks on ASCII control characters...But yeah, we are meant to always use a library because we have deadlines and we are willing to compromise a whole lot of quality to deliver on them. |
|
Specifically json and unjson I make globally available in all my projects. If I used csv more often than once in a decade, I’d have csvesc(s) too.
Sometimes you read some stdlib reference and wonder what they were thinking with things like System.out.println and without one-line one-arg readtext(), tojson(), fetch() and so on. It’s like a kitchen with all appliances still in boxes and all utensils in a tight vacuum cover. Everything is there, but preparation friction makes it absolutely unusable.