An open API service indexing awesome lists of open source software.

https://github.com/domiii/better-to-string

JavaScript traditionally does not do a good job converting arbitrary data to string. Here is the fix for that!
https://github.com/domiii/better-to-string

Last synced: 28 days ago
JSON representation

JavaScript traditionally does not do a good job converting arbitrary data to string. Here is the fix for that!

Awesome Lists containing this project

README

          

NOTE: I am using https://github.com/yahoo/serialize-javascript for now. It is not perfect, but gets much of the job done.

Converting objects to strings is a rather complex affair. Let's consider some of the default options:

# JSON.stringify()

JSON.stringify is a great tool for rudimentary string conversion, however it ignores a lot of important information:

| Issue | Example input | Example output |
| --- | --- | --- |
| function | `{ f() {} }` | `'{}'` |
| class information | | |
| `undefined` | | |
| `Date` | | |
| `Regex` | | |
| `Map` | | |
| `Set` | | |
| `Infinite` | | |
| `NaN` | | |
| [BigInt](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt) | | |

# util.inspect

-> Node only

# console.log(obj)

-> Only works well in browsers (for now). Does not allow us to easily add to a string/message.