Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jhnns/tosrc
Turns every JavaScript object or primitive into valid source code that can be evaled again.
https://github.com/jhnns/tosrc
Last synced: about 2 months ago
JSON representation
Turns every JavaScript object or primitive into valid source code that can be evaled again.
- Host: GitHub
- URL: https://github.com/jhnns/tosrc
- Owner: jhnns
- License: mit
- Created: 2011-06-08T15:09:09.000Z (over 13 years ago)
- Default Branch: master
- Last Pushed: 2014-06-30T08:49:49.000Z (over 10 years ago)
- Last Synced: 2024-04-24T22:20:28.828Z (8 months ago)
- Language: JavaScript
- Homepage:
- Size: 618 KB
- Stars: 19
- Watchers: 4
- Forks: 3
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
**toSrc** [![Build Status](https://secure.travis-ci.org/jhnns/toSrc.png?branch=master)](http://travis-ci.org/jhnns/toSrc)
========**Turns every JavaScript object or primitive into valid source
code that can be evaled again.**You can use it to serialize classes, modules or other programming objects
and reuse them in an other environment such as a browser. JSON.stringify doesnt work with programming objects (that contain functions, dates, etc.) because they're no legal JSONs.
Installation
------------
`npm install toSrc`
Examples
-----```javascript
var toSrc = require("toSrc");
// Primitives
///////////////////////////////////////
toSrc(1); // = '1'
toSrc(true); // = 'true'
toSrc(undefined); // = 'undefined'
tOSrc(null); // = null
toSrc("1"); // = '"1"'
toSrc('1'); // = '"1"' toSrc always uses double-quotes// Constants
///////////////////////////////////////
toSrc(Math.PI); // = 'Math.PI'
toSrc(NaN); // = 'NaN'// RegExp
///////////////////////////////////////
toSrc(/myRegEx/gi); // = '/myRegEx/gi'
toSrc(new RegExp("myRegEx")); // = '/myRegEx/'// Date
///////////////////////////////////////
toSrc(new Date()); // = 'new Date()'// Functions
///////////////////////////////////////
function testFunc() {
var test = "hello";
}
toSrc(testFunc); // = 'function testFunc() {\n var test = "hello";\n}'
toSrc(String); // = 'String', native functions don't expose the source code// Arrays
///////////////////////////////////////
toSrc([1, 2, "3"]); // = '[1, 2, "3"]'
toSrc([1, 2, ["a", "b", "c"]]); // = '[1, 2, undefined]' because the depth
// is 1 by default
toSrc([1, 2, ["a", "b", "c"]], 2); // = '[1, 2, ["a", "b", "c"]]'// Objects
///////////////////////////////////////
toSrc({
regEx: /regex/gi,
anotherObj: {
test: "test"
}
});
// = '{"regEx": /regex/gi, "anotherObj": undefined}'
// anotherObj is undefined because the depth is 1 by default.
toSrc({
"regEx": /regex/gi,
"anotherObj": {
"test": "test"
}
}, 2);
// = '{"regEx": /regex/gi, "anotherObj": {"test": "test"}}'```
For more examples check out `test/test.js`
API
-----###toSrc(obj: *, depth: Number): String
- *obj*:
The object to stringify. Can also be a primitive like `1` or `true`.- *{Number=1} depth*:
The depth to go. All nested structures like objects or arrays deeper than this will be undefined. Defaults to 1, meaning that every object or array within `obj` will be undefined by default.
Usage
-----### In node.js
```javascript
var toSrc = require("toSrc");toSrc(obj, depth);
```### In the browser
Just call `toSrc(obj, depth);`
Notes
-----
* Circular references will be undefined. No error is thrown, but a warning is logged.
* All math constants are restored to their source representation, e.g.: `toSrc(Math.PI); // = 'Math.PI' instead of 3.14...`
* All dates are restored to their original time of creation, e.g.: `toSrc(new Date()) // = 'new Date(Feel free to modify the code to meet your needs.
## License
MIT