
Research
/Security News
9 Malicious NuGet Packages Deliver Time-Delayed Destructive Payloads
Socket researchers discovered nine malicious NuGet packages that use time-delayed payloads to crash applications and corrupt industrial control systems.
@randajan/function-parser
Advanced tools
A lightweight library for converting functions to strings and reconstructing functions from strings in JavaScript.
Effortlessly convert between functions and strings with fnToStr and strToFn. Serialize functions into minimal arrow notation or transform string code into callable functions. Perfect for dynamic JavaScript environments where function manipulation is key.
npm install @randajan/function-parser
or
yarn add @randajan/function-parser
fnToStr(fn)Converts a given JavaScript function to a minimized arrow function string representation.
Parameters:
fn (Function): The JavaScript function to convert.Returns:
Example Usages:
fnToStr(() => "test"); // "()=>'test'"
fnToStr(function foo() { return "bar"; }); // "()=>'bar'"
fnToStr(function foobar(bar) { return { foo: bar }; }); // "bar=>({ foo: bar })"
fnToStr(function bar(foo, bar) { console.log(foo, bar); return foo + bar; }); // "(foo, bar)=>{console.log(foo, bar); return foo + bar;}"
Errors:
"Stringify function - not a function" if the input is not a valid JavaScript function.strToFn(str)Converts a given common or arrow function string representation back to an executable JavaScript function.
Parameters:
str (String): The arrow function in string form to convert back to a JavaScript function.Returns:
Example Usages:
strToFn("()=>'test'")(); // 'test'
strToFn("()=>'bar'")(); // 'bar'
strToFn("bar=>({foo:bar})")("example"); // { foo: "example" }
strToFn("(foo, bar)=>{ console.log(foo, bar); return foo + bar }")(2, 3); // Logs 2, 3 and returns 5
Errors:
"Stringify function - not a string" if the input is not a valid string."Parsing arguments - malformatted" if the arguments list in str is malformed."Parsing common function - arguments are missing left bracket '('" if the left bracket ( is missing in a traditional function argument list."Parsing common function - arguments are missing right bracket ')'" if the right bracket ) is missing in a traditional function argument list."Parsing common function - body missing brackets '{...}'" if the function body is missing {} in a traditional function."Parsing arrow function - missing the arrow '=>' if the arrow (=>) is missing in the arrow function syntax."Parsing arrow function - body missing right bracket '}'" if the body of the arrow function is missing the closing bracket }.anyToFn(any)Converts a given value (any) into a function that, when called, returns the original value.
Parameters:
any (Any): The value to be wrapped in a function. Supported types include string, number, boolean, bigint, object, and Date.Returns:
any value.Behavior:
any is a valid value, anyToFn will convert it into a function returning any when executed.any is a string that doesn’t start with a single quote ('), anyToFn interprets it as a function string and attempts to parse it as a function using strToFn.anyToFn throws a descriptive error.Example Usages:
anyToFn("'Hello'")(); // Returns "Hello"
anyToFn(42)(); // Returns 42
anyToFn(true)(); // Returns true
anyToFn(new Date("2024-01-01"))(); // Returns a Date object representing 2024-01-01
anyToFn({ foo: "bar" })(); // Returns { foo: "bar" }
anyToFn("(a, b) => a + b")(3, 4); // Returns 7
anyToFn(/[a-zA-Z]+/g).test("ABC"); // Returns true
Errors:
"Creating function - symbol is not supported": Raised if any is of type symbol."Creating function - object replication failed": Raised if any is an object that cannot be stringified (e.g., contains circular references)."Creating function - unknown input": Raised if any is of an unsupported or unrecognized type.Happy hacking
MIT © randajan
FAQs
A lightweight library for converting functions to strings and reconstructing functions from strings in JavaScript.
The npm package @randajan/function-parser receives a total of 0 weekly downloads. As such, @randajan/function-parser popularity was classified as not popular.
We found that @randajan/function-parser demonstrated a healthy version release cadence and project activity because the last version was released less than 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.

Research
/Security News
Socket researchers discovered nine malicious NuGet packages that use time-delayed payloads to crash applications and corrupt industrial control systems.

Security News
Socket CTO Ahmad Nassri discusses why supply chain attacks now target developer machines and what AI means for the future of enterprise security.

Security News
Learn the essential steps every developer should take to stay secure on npm and reduce exposure to supply chain attacks.