npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@randajan/function-parser

v0.0.4

Published

A lightweight library for converting functions to strings and reconstructing functions from strings in JavaScript.

Downloads

9

Readme

@randajan/function-parser

NPM JavaScript Style Guide

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.

Install

npm install @randajan/function-parser

or

yarn add @randajan/function-parser

Interfaces

fnToStr(fn)

Converts a given JavaScript function to a minimized arrow function string representation.

Parameters:

  • fn (Function): The JavaScript function to convert.

Returns:

  • (String): A minimized arrow function string representation of the input function.

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:

  • Throws "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:

  • (Function): The function created from the input string.

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:

  • Throws "Stringify function - not a string" if the input is not a valid string.
  • Throws "Parsing arguments - malformatted" if the arguments list in str is malformed.
  • Throws "Parsing common function - arguments are missing left bracket '('" if the left bracket ( is missing in a traditional function argument list.
  • Throws "Parsing common function - arguments are missing right bracket ')'" if the right bracket ) is missing in a traditional function argument list.
  • Throws "Parsing common function - body missing brackets '{...}'" if the function body is missing {} in a traditional function.
  • Throws "Parsing arrow function - missing the arrow '=>' if the arrow (=>) is missing in the arrow function syntax.
  • Throws "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:

  • (Function): A function that returns the original any value.

Behavior:

  • If any is a valid value, anyToFn will convert it into a function returning any when executed.
  • If 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.
  • For unsupported types or invalid inputs, 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

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

License

MIT © randajan