• Stars
    star
    101
  • Rank 338,166 (Top 7 %)
  • Language
    TypeScript
  • License
    zlib License
  • Created over 13 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Converts JavaScript objects to source

node-tosource

Actions Status npm version codecov

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:

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

More Repositories

1

node-mongolian

[project inactive] Mongolian DeadBeef is an awesome Mongo DB driver for node.js
JavaScript
349
star
2

node-buffalo

[project inactive] Buffalo is a lightweight BSON library for Node.js
JavaScript
107
star
3

node-licensecheck

A quick way to see the licenses of node modules you depend on—recursively.
JavaScript
56
star
4

gulp-watchify

gulp + watchify
JavaScript
54
star
5

node-ractify

ractive.js + browserify
JavaScript
33
star
6

lascaux-sketch

Lascaux Sketch 2 is an open source web-based digital painting tool
TypeScript
23
star
7

trimerge

Three-way merge JSON structures
TypeScript
18
star
8

node-safestart

Safe start ensures all your node dependencies exist and are up to date
JavaScript
15
star
9

graphql-98

A visual GraphQL data browser. Like a SQL GUI for GraphQL!
TypeScript
13
star
10

firestarter

Fire Starter – Allegro Speedhack 2005
C++
12
star
11

falcor-shapes

object-based falcor pathset generator
JavaScript
11
star
12

jtablet

(Unsupported) JTablet provides a simple open-source (zlib license) interface for accessing tablet input in your Java applications.
Java
11
star
13

node-stylish

Stylus middleware for connect
JavaScript
9
star
14

polyfill.js

JavaScript polyfills
JavaScript
9
star
15

nithub

Nithub mashes up the npm and github apis to organize and rank npm packages.
JavaScript
8
star
16

trimerge-sync

Distributed data sync using trimerge
TypeScript
8
star
17

collabodux

Collabodux is an idea and experimental library for realtime collaboration on JSON structures. It is a client-oriented, declarative-functional approach to shared application state.
TypeScript
8
star
18

mode7ex

[2002] Mode 7 Ex extension for Multimedia Fusion
C++
5
star
19

node-waiter

A simple way to wait for multiple asynchronous calls to return in Node.js.
JavaScript
4
star
20

babel-preset-es2015-webpack2

Babel preset for all es2015 plugins except es2015-modules-commonjs (because webpack2 supports it already!).
JavaScript
4
star
21

node-taxman

Taxman caches values for you.
JavaScript
3
star
22

node-shimmy

JavaScript
2
star
23

node-localdeps

Symbolic links local dependencies to your project
JavaScript
2
star
24

node-handleify

browserify 2 + precompiled client-side handlebars
JavaScript
2
star
25

marcello3d.github.io

personal site
JavaScript
2
star
26

alt

JavaScript
2
star
27

draft-canvas

TypeScript
2
star
28

klikjs

Next.js-based Website that lets you play Classic Klik & Play games online
HTML
2
star
29

battle-of-the-braces-2012

Hackday project with @defunctzombie
JavaScript
1
star
30

headless-electron

Run node scripts in a headless electron environment
TypeScript
1
star
31

gulp-boilerplate.js

experimental gulpfile.js boilerplate for webapps
JavaScript
1
star
32

ractify-loader

Ractive component loader for webpack
JavaScript
1
star
33

io-ts-path

Generate type-safe paths from io-ts models.
TypeScript
1
star
34

react-electron-sample-app

JavaScript
1
star
35

casbah-mapper

Scala object to MongoDB mapper that builds atop Casbah
Scala
1
star
36

node-listenable

simple event emitter
JavaScript
1
star