Categories

Tags

Meta

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:

1

2

3

//config.js

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

And it’s processed like this:

JavaScript

1

2

3

4

5

6

// build.js

vartaggedReplace=require('tagged-replace');

varcontents=get_the_contents();

varresult=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.