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

pathrewrite

v1.0.4

Published

Rewrites paths using simple 'from => to' rules

Downloads

21

Readme

pathrewrite

Rewrite paths with simple 'replace' rules.

Getting Started

Install the module with: npm install pathrewrite

Then use the 'Rules' class and the 'go()' function to substitute paths:

var pathrewrite = require('pathrewrite');

var rules = new pathrewrite.Rules();
rules.add("lost", 'back');

var result = pathrewrite.go('/I/am/lost/', rules);

// result is '/I/am/back/'

You can also load multiple rules at once using the following syntax:

var rules = pathrewrite.Rules.loadMulti( [
    {
        FROM: 'a',
        TO: 'b'
    },
    {
        FROM: 'c',
        TO: 'd'
    }
] );

"Strict" rules

Both the Rules constructor and the loadMulti() method accept the optional "strict" parameter, of type boolean.
When strict is true an additional set of checks is enacted when calling the pathrewrite.go() method.
Currently the following are checked:

  • The resulting path must not contain the string ".."
  • The resulting path must not contain the character "%"
  • The resulting path must not contain the character "$"

Positional parameters

Positional-parameters substitutions can be requested right from the run() method, by passing to it an arbitrary number of arguments that will be substituted in the result.
"%n"-style placeholders must be put in the path to signal where the parameters should be inserted instead. The order of the parameters is strictly position-based, hence the first argument to the run() method after the 'rules' one will fill the placeholder named '%1' in the input path, as follows:

var rules = pathrewrite.Rules.loadMulti([]); // Empty set of rules...
var result = pathrewrite.go('a/%1/c', rules, 'b');

// result is 'a/b/c'

result = pathrewrite.go('a/%1/%2/', rules, 'b', 'c');

// result is 'a/b/c/'

The index of the parameters position is 1-based. A parameter can be substituted as many times as it appears in the input path, regardless of the position it appears in it:

result = pathrewrite.go('/%2/%1/%1/%1/%1/%1/%1/%1/%1/%1', rules, 'a', 'b');
        
// result is '/b/a/a/a/a/a/a/a/a/a'

Examples

Here are some other examples:

// Remove previously added rules:
rules.clear();
rules.add("your", 'my');

// 'count' will be '1':
console.log(rules.count());

result = pathrewrite.go('/this/is/your/file.txt', rules);

// result is '/this/is/my/file.txt'

And another one demo-ing some tolerance towards the inputs:

rules.clear();
rules.add("home", 'root');

result = pathrewrite.go('/home//user/file.txt', rules);

// result is '/root/user/file.txt'

Finally, this one throws an error:

var strict = true;
var rules  = new pathrewrite.Rules(strict);
rules.add('home', '..');

/* This 'go' method throws an error because of the ".." string
 * specified above, which gets caught by the "strict" checks. */
result = pathrewrite.go('home/root/file.txt', rules);

Contributing

In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using Grunt.

License

Copyright (c) 2014 Nicola Orritos
Licensed under the MIT license.