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 🙏

© 2025 – Pkg Stats / Ryan Hefner

script-transform

v0.4.0

Published

Securely transform script code

Readme

script-transform

NPM License Coding style

Installation

Install this package using NPM:

npm install script-transform

What is it about?

script-transform is a tiny package offering two simple yet powerful instruments to transform code of an ECMAScript without having to create a syntax tree: match() and replace().

script-transform takes care of the following aspects:

  • Tokenization: scripts are internally tokenized in such a way that match() and replace() will only match structures of code but neither strings nor comments. This kind of an interface has proven to be very powerful in many use cases.

  • Normalization: the user is guaranteed a normalized form of a script. Unexpected and non-trivial special cases (two examples being Unicode variable names and whitespace outside the range of [\x00-\xFF]) can be neglected so that variable names match /[\w\\]+/ and whitespace matches /\s+/.

  • Security: the module takes all necessesary precautions such that enforced transformations can not be circumvented even if tokenization is inaccurate.

API

new Script(code: string|Script)

Creates a new transformable ECMAScript from code code.

script.replace(pattern: RegExp|string, replacement: Function|string, all = false)

Replaces a pattern in a script in-place. Accepts the same arguments as the str.replace() method. Replaces matches of either a regular expression or a string by either a string or the result of a callback.

By default, replace() will only match segments of code but neither string literals nor templates nor comments. As an example, script.replace(/\bwhile\s*\(/) is guaranteed to never match anything else but a while control structure. If all is set to true, anything, including strings, will be matched.

If pattern is a string or a non-global regular expression, only the first match in the script will be replaced.

Returns this.

script.match(pattern: RegExp|string, callback?: Function, all = false)

Searches a script for a pattern. If a function is given as a callback, it will be called for each match with the match parameters as arguments. Otherwise, the match parameters will be returned as an array.

Unless all is set to true, match() will only match segments of code as has been explained for the replace() function.

If pattern is a string or a non-global regular expression, match() terminates after the first match.

Returns a match if no callback is given or undefined otherwise.

script.toString()

Returns the transformed code as a string.

License

MIT © 2016 (see full text)