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

isogrammify

v1.1.0

Published

Substitute a function's parameters by the letters of any given word (isogram)

Downloads

7,148

Readme

isogrammify

npm version Build Status Test Coverage Code Climate

Have you ever wanted to turn this

!function(e,t,n,o,a,d,r,l,c,i,s,u,x){…

…into this?

!function(t,r,o,u,b,l,e,m,a,k,i,n,g){…

Well, that is exactly what isogrammify does. You pass a function and a word, and iosgrammify renames the variables for you, such that the renamed parameters form that word.

Usage in script

isogrammify takes three parameters,

  • program (String|Function) The JS program to transform. Note that this must be a complete syntactically valid script. You cannot pass a simple anonymous function. That is a syntax error. You can pass a named function, because it forms a valid program by itself.
  • target (String) The string that the variables should be replaced by. Must be an isogram, that is a word without duplicate letters
  • raw (Boolean, optional) true to return an AST instead of a string. Defaults to false.

Examples (scripting)

var isogrammify = require('isogrammify');

var f = '!function(test){}()';
isogrammify(f, 'x');
//>     '!function(x){}()'

var f = '!function(x,y,z){}()';
isogrammify(f, 'Yay');
//>     '!function(Y,a,y){}()'

var f = function (x,y,z){};
isogrammify(f, 'Yay');
//> UnexpectedTokenError, since the function alone is not a valid program

var f = function f(x,y,z){};
isogrammify(f, 'abc');
//>     function f(a,b,c){}

Usage from command line

There is also a command-line interface, where isogrammify takes three arguments

  • inputFile The filename of the JS program to transform. The file must contain a valid JavaScript program as descibed above.
  • isogram The string that the variables should be replaced by. Must be an isogram, that is a word without duplicate letters
  • outputFile (optional) A filename to write the output file to. Can be the same as inputFile. If omitted, the output is written to the console.

Examples (CLI)

$ npm install isogrammify
…

$ isogrammify foo.js HelLo
!function(H,e,l,L,o){…

$ isogrammify foo.js HelLo bar.js
Wrote 4242 bytes to "bar.js".

Does it accept unicode characters?

Oh yes, it does!

This tool was originally created as a part of minislides, where I did this:

var f = '!function(e,a,t,c,n,o,s,r,i,l,d,u,f,y,k,m){…';
isogrammify(f, 'ツminïslĩdeṣ_FTWǃ');
//>     '!function(ツ,m,i,n,ï,s,l,ĩ,d,e,ṣ,_,F,T,W,ǃ){…'

But later, I decided against using non-ASCII characters, since they take up more than one byte per letter.

Note that ǃ is a valid identifier, it is not an exclamation mark. Please use Mathias Bynens’s variable name validator to find valid characters, or browse the complete list.

Why can’t I simply search & replace instead?

Because the function body may contain identifiers with conflicting names. Even if you make sure that only the given variable is renamed, you may run into trouble because of inner functions with overlapping variable scopes.

For example, if you want to rename this function’s parameters to T,e,s,t:

!function (foo, bar, baz, qux) { function t(e, foo) {e(); foo(); bar();} t(); a();}()

…you may destroy the inner bar() call when you try to rename bar to e. That is because the inner function establishes a new scope for e.

!function (T, e, s, t) { function t(e, foo) {e(); foo(); e();} t(); a();}()
// broken!                                               ^

So you’ll need to rename that e to something else first, and so on.

!function (T, e, s, t) { function t(something, foo) {something(); foo(); e();} t(); a();}()

License: Apache 2.0