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

runstring

v3.0.2

Published

Convert JS functions to runnable strings. With parameter serialization!

Downloads

15

Readme

runstring

CI Test Coverage Maintainability

Convert JS functions to runnable strings, with parameter serialization! (We also support TypeScript natively!)

This was made for Electron's executeJavaScript() method, so that the code does not need to be constructed as a string but can be passed as a function. runstring will convert that function and its parameters to an IIFE string.

The following parameter types are supported:

  • Literals null, undefined, true, false
  • Numbers
  • Strings
  • Functions (both standard notation and arrow notation)
  • Arrays
  • Objects

Strings will be escaped. Nesting of values (in objects and arrays) is supported without limit.

Usage

Basic Usage

Simply invoke the module with a function and its parameters to obtain the IIFE string.

const runstring = require('runstring')

const code = runstring(myFunction, arg1, arg2 /* , ... */)
// do something with `code`

Example 1

const runstring = require('runstring')

const code = runstring(function (a, b) {
  return a + b
}, 5, 7)

code would now store a string similar to this: ';(function (a, b) { return a + b })(5, 7);'. That string could be passed to Electron's executeJavaScript(), or the standard eval() (eval is evil, but if you have your reasons to use it, might as well do it right).

Example 2

Any parameter type is supported — numbers, strings, objects, arrays, and even other functions can all be passed to the module for stringification:

const runstring = require('runstring')

const code = runstring(function (predicate, action) {
  const elements = document.getElementsByClassName('item')
  for (let i = 0; i < elements.length; ++i) {
    if (predicate(elements[i])) {
      action(elements[i])
    }
  }
}, (e) => e.tagName.toLowerCase() === 'div', removeElement)

function removeElement (e) {
  e.parentNode.removeChild(e)
}

code would now store the following string:

';(function (predicate, action) {
  const elements = document.getElementsByClassName('item')
  for (let i = 0; i < elements.length; ++i) {
    if (predicate(elements[i])) {
      action(elements[i])
    }
  }
})((e) => e.tagName.toLowerCase() === 'div', function removeElement (e) {
  e.parentNode.removeChild(e)
});'