A node package appears: tagged-replace

I’ve recently become acquainted with node.js and it’s been a delight so far. It’s taken some time to get used to a new package manager and build tools and it’s been some time since I last touched ECMAScript (we all know it as Javascript though, right?). In an attempt to get truly familiar with it I’ve ventured to create a module and publish it to the npm.

The package’s name is tagged-replace and it can be downloaded from npm or from github. It’s a simple function that processes text input and replaces the space between tag markers with a string. The inspiration from this comes from our current build script for a web app that needs to change the server when deployed to testing or production. We used another plugin, made for Grunt that can do the same using regular expressions. It’s useful, but dealing with multiple similar regular expressions becomes repetitive and bug prone really quickly.

The module uses regular expressions itself, but hides this from the user who only needs to provide an object with keys and values corresponding to the tags and contents to replace. The tags are also fully configurable, so they can be modified to process different languages. The defaults allow tags that use the multiline comment syntax to allow inline replacements in javascript. It leaves the tags in the resulting text, and it will be configurable in a future version.

A tag looks like this:

//config.js

var server = /*server*/ 'this will be replaced' /*/server*/

And it’s processed like this:

// build.js
var taggedReplace = require('tagged-replace');

var contents = get_the_contents();

var result = taggedReplace(contents, { server: 'new server' });

For more info on the configurations available, check out the project site on github. If you want/need more functionality, create an issue with the details. A pull request will make the change more likely to happen.

:>

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.