Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/marcello3d/node-tosource
Converts JavaScript objects to source
https://github.com/marcello3d/node-tosource
Last synced: 10 days ago
JSON representation
Converts JavaScript objects to source
- Host: GitHub
- URL: https://github.com/marcello3d/node-tosource
- Owner: marcello3d
- License: zlib
- Created: 2011-04-24T16:01:31.000Z (over 13 years ago)
- Default Branch: main
- Last Pushed: 2023-06-17T18:49:19.000Z (over 1 year ago)
- Last Synced: 2024-10-24T02:50:35.772Z (19 days ago)
- Language: TypeScript
- Homepage: https://github.com/marcello3d/node-tosource
- Size: 215 KB
- Stars: 101
- Watchers: 4
- Forks: 16
- Open Issues: 8
-
Metadata Files:
- Readme: Readme.md
- Changelog: HISTORY.md
- License: LICENSE
Awesome Lists containing this project
README
# node-tosource
[![Actions Status](https://github.com/marcello3d/node-tosource/workflows/Node%20CI/badge.svg)](https://github.com/marcello3d/node-tosource/actions)
[![npm version](https://badge.fury.io/js/tosource.svg)](https://badge.fury.io/js/tosource)
[![codecov](https://codecov.io/gh/marcello3d/node-tosource/branch/master/graph/badge.svg)](https://codecov.io/gh/marcello3d/node-tosource)toSource is a super simple function that converts JavaScript objects back to source code.
## Introduction
Motivation: JSON doesn't support serializing functions, dates, or regular expressions. I wanted
a quick and simple way to push trusted data structures with code from Node down to the browser.This should make it easier to share code and modules between the server and client.
## Installation
```
npm install tosource
```## Examples
The following code:
```js
import toSource from 'tosource';console.log(
toSource([
4,
5,
6,
'hello',
{
a: 2,
b: 3,
'1': 4,
if: 5,
yes: true,
no: false,
nan: NaN,
infinity: Infinity,
undefined: undefined,
null: null,
foo: function (bar) {
console.log('woo! a is ' + a);
console.log('and bar is ' + bar);
},
},
/we$/gi,
new Date('Wed, 09 Aug 1995 00:00:00 GMT'),
]),
);
```Output:
```
[ 4,
5,
6,
"hello",
{ 1:4,
a:2,
b:3,
"if":5,
yes:true,
no:false,
nan:NaN,
infinity:Infinity,
"undefined":undefined,
"null":null,
foo:function (bar) {
console.log('woo! a is ' + a);
console.log('and bar is ' + bar);
} },
/we$/gi,
new Date(807926400000) ]
```See [tosource.test.ts][1] for more examples.
## Supported Types
- numbers (including `NaN`, `Infinity`, and `-0`)
- strings
- Arrays (including sparse arrays)
- object literals
- function
- `RegExp` instances
- `Date` instances
- `Map`
- `Set`
- `true` / `false`
- `undefined`
- `null`## Notes
- Functions are serialized with `func.toString()`, no closure properties are serialized
- Multiple references to the same object become copies
- Circular references are encoded as `{$circularReference:true}`## License
toSource is open source software under the [zlib license][2].
[1]: https://github.com/marcello3d/node-tosource/blob/master/src/tosource.test.ts
[2]: https://github.com/marcello3d/node-tosource/blob/master/LICENSE