Security News
Input Validation Vulnerabilities Dominate MITRE's 2024 CWE Top 25 List
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
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.
Works with node.js (tested) or in the browser (not tested)
npm install toSrc
Params
1
or true
.In node.js
require("toSrc")(obj, depth);
In the browser
Just call toSrc(obj, depth);
var toSrc = require("toSrc");
// Primitives
///////////////////////////////////////
toSrc(1); // = '1'
toSrc(true); // = 'true'
toSrc("1"); // = '"1"'
// 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(<the time of creation in ms>)'
// Functions
///////////////////////////////////////
toSrc(function() {
var test = "hello";
}); /* = 'function () {
var test = "hello";
}' */
// 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 the test/test.js
toSrc(Math.PI); // = 'Math.PI' instead of 3.14...
toSrc(new Date()) // = 'new Date(<time of creation in ms>)'
new RegExp()
will NOT be dynamic anymore. toSrc(new RegExp(someString))
will return '/<value of someString>/'
instead of `'new RegExp(someString)'Feel free to modify the code to meet your needs.
FAQs
Turns every JavaScript object or primitive into valid source code.
The npm package toSrc receives a total of 0 weekly downloads. As such, toSrc popularity was classified as not popular.
We found that toSrc demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.
Research
Security News
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.