# tosource

> toSource converts JavaScript objects back to source

Latest version **2.0.0-alpha.3** (published 2021-07-31) · 0 weekly downloads

## Install

```sh
npm install tosource
pnpm add tosource
yarn add tosource
bun add tosource
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.0-alpha.3 |
| Published | 2021-07-31 |
| First published | 2011-04-24 |
| Weekly downloads | 0 |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=10 |
| Dependencies | 0 |
| Unpacked size | 17.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 105 |
| Author | Marcello Bastéa-Forte |
| Maintainers | marcello |
| Keywords | source, tosource, json, javascript object, object |

## Links

- npm: https://www.npmjs.com/package/tosource
- Repository: https://github.com/marcello3d/node-tosource
- Issues: https://github.com/marcello3d/node-tosource/issues
- npm.io page: https://npm.io/package/tosource

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 2.0.0-alpha.3 (latest) — 2021-07-31
- 2.0.0-alpha.2 — 2020-05-15
- 2.0.0-alpha.1 — 2020-03-31
- 1.0.0 — 2015-09-03
- 0.1.3 — 2014-10-08
- 0.1.2 — 2014-05-14
- 0.1.1 — 2011-04-24
- 0.1.0 — 2011-04-24

## 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

---
_Source: https://npm.io/package/tosource · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
