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!
- Host: GitHub
- URL: https://github.com/domiii/better-to-string
- Owner: Domiii
- Created: 2021-04-24T08:29:04.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2021-04-24T09:07:22.000Z (over 4 years ago)
- Last Synced: 2025-01-17T18:00:01.210Z (12 months ago)
- Language: JavaScript
- Size: 1.95 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
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.