Skip to main content

Command Palette

Search for a command to run...

Today's npm package: strip-comments

Published
โ€ข1 min read
Today's npm package: strip-comments
P

I am developer from India currently based in Berlin, Germany.

I am very interested in Design, though I don't design ๐Ÿ˜‰ I take care of it while making my apps.

Photography, videography and having random conversation over beer are my activities of spare time. Timelapses and hyperlapses make me wonder all the time ๐Ÿ˜Š

Today's npm package is strip-comments

If you are working with a large codebase and want to apply a code mod, this package can be very useful.

Or before you ship your application to the production, you might wanna remove the comments from your codebase and this package can be very useful.

Leet's see how to use it. Consider following the index.js file from https://blog.pankaj.pro/todays-npm-package-jsdoc-to-markdown where we have discussed how to use JSDoc to generate Documentation in Markdown files.

// index.js

/**
 * Function to generate the Square of a Number
 *
 * @param {Number} x - The number to be squared
 * @return {Number} Squared value of passed Number
 */
const square = (x) => {
  return Math.pow(x, 2);
};

export default square;

For above file, we can use strip-comments as follows:

const fs = require('fs');
const path = require('path');
const strip = require('strip-comments');

const file = fs.readFileSync(
  path.join(process.cwd(), 'index.js')
);

const str = strip(file.toString());

console.log(str);
/*
const square = (x) => {
  return Math.pow(x, 2);
};

export default square;
*/

You can see it in action here: https://runkit.com/pankaj/strip-comments

Stay tuned for tomorrow's package of the Day.

More from this blog

P

Pankaj Patel - @heypankaj_

63 posts

Sharing my learnings and thoughts on Blogging, Personal Branding and Programming