Skip to content

Commit

Permalink
v2.0.0-alpha.1 (#24)
Browse files Browse the repository at this point in the history
  * **Breaking change**: Requires Node 10.x
  * migrate toolchain: TypeScript, prettier, eslint, jest, rollup
  * export TypeScript types and esm export
  * add support for `Map`, `Set`, negative zero, sparse arrays
  * serialize numeric object keys as numbers (e.g. `{'1':2}` becomes `{1:2}`)
  • Loading branch information
marcello3d authored Mar 31, 2020
1 parent 242b092 commit 42d4cda
Show file tree
Hide file tree
Showing 16 changed files with 6,482 additions and 248 deletions.
16 changes: 16 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"extends": [
"plugin:@typescript-eslint/recommended",
"prettier",
"prettier/@typescript-eslint"
],
"rules": {
"@typescript-eslint/no-explicit-any": 0,
"@typescript-eslint/no-inferrable-types": 0,
"@typescript-eslint/no-use-before-define": 0,
"@typescript-eslint/no-parameter-properties": 0,
"@typescript-eslint/explicit-function-return-type": 0,
"no-var": ["error"],
"prefer-const": ["error"]
}
}
25 changes: 25 additions & 0 deletions .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: Node CI

on: [push]

jobs:
build:
runs-on: ubuntu-latest

strategy:
matrix:
node-version: [10.x, 12.x]

steps:
- uses: actions/checkout@v1
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- run: npm ci
- run: npm run lint
- run: npm run build
- run: npm run test-ci
- run: npx codecov
env:
CODECOV_TOKEN: ${{secrets.CODECOV_TOKEN}}
22 changes: 22 additions & 0 deletions .github/workflows/npmpublish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: Publish to NPM

on:
release:
types: [created]

jobs:
publish-npm:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- uses: actions/setup-node@v1
with:
node-version: 12
registry-url: https://registry.npmjs.org/
- run: npm ci
- run: npm run lint
- run: npm run test-ci
- run: npm run build
- run: npm publish --access public
env:
NODE_AUTH_TOKEN: ${{secrets.NPM_AUTH_TOKEN}}
26 changes: 25 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,25 @@
node_modules
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# production
/dist

# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local
.rpt2_cache
.idea

npm-debug.log*
yarn-debug.log*
yarn-error.log*
5 changes: 5 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"trailingComma": "all",
"singleQuote": true,
"arrowParens": "always"
}
8 changes: 0 additions & 8 deletions .travis.yml

This file was deleted.

7 changes: 7 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# 2.0.0-alpha.1 (2020-03-30)
* **Breaking change**: Requires Node 10.x
* migrate toolchain: TypeScript, prettier, eslint, jest, rollup
* export TypeScript types and esm export
* add support for `Map`, `Set`, negative zero, sparse arrays
* serialize numeric object keys as numbers (e.g. `{'1':2}` becomes `{1:2}`)

# 1.0.0 (2015-09-03)

* added changelog
Expand Down
126 changes: 67 additions & 59 deletions Readme.md
Original file line number Diff line number Diff line change
@@ -1,57 +1,67 @@
node-tosource
=============
# 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
------------
## 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
------------
## Installation

```
npm install tosource
```

Examples
--------
## Examples

The following code:

```js
var toSource = require('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)
}
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")]
))
new Date('Wed, 09 Aug 1995 00:00:00 GMT'),
]),
);
```

Output:

```js
```
[ 4,
5,
6,
"hello",
{ "1":4,
{ 1:4,
a:2,
b:3,
"if":5,
Expand All @@ -62,41 +72,39 @@ Output:
"undefined":undefined,
"null":null,
foo:function (bar) {
console.log("woo! a is "+a)
console.log("and bar is "+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

See [test.js][1] for more examples.

Supported Types
---------------
* Numbers
* Strings
* Array literals
* object literals
* function
* RegExp literals
* Dates
* true
* false
* undefined
* null
* NaN
* Infinity

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:1}`

License
-------
toSource is open source software under the [zlib license][2].

[1]: https://github.com/marcello3d/node-tosource/blob/master/test.js
[1]: https://github.com/marcello3d/node-tosource/blob/master/src/tosource.test.ts
[2]: https://github.com/marcello3d/node-tosource/blob/master/LICENSE
Loading

0 comments on commit 42d4cda

Please sign in to comment.