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

cmtu

v1.0.0

Published

A Node.js utility library for easily removing, extracting, and manipulating comments in code strings with support for popular programming languages such as JavaScript, Python, and etc.

Downloads

20

Readme

cmtu - comment utils NPM version NPM monthly downloads NPM total downloads

cmtu - removing comments from code string has never been this easy.

Cmtu is a Node.js package that helps you easily remove, extract, and magic comments from code strings.

Built with TypeScript and has full type support.

Supports the most popular programming languages like Python, JavaScript, etc. out of the box.

Please consider following this project's author, Sina Bayandorian, and consider starring the project to show your :heart: and support.

Table of Contents

Install

Install with npm:

$ npm install --save cmtu

Usage

const cmtu = require('cmtu');

// * create and configure a cmtu object
const jsCmtu = cmtu(cmtu.Languages.JS.resolver);
const jsCode = `
/*
this is a multi-line JS comment
*/

const callout = 'this is not a comment';

// this is a single line js comment
`;

const codeWithNoComments = jsCmtu.strip(jsCode);

console.log(codeWithNoComments, codeWithNoComments.length);

see built-in languages for a list of all built-in languages

API

cmtu

returns a cmtu-object configured based on the args passed to it.

Params

  • resolver : Resolver

  • options : optional - Options

    • stringSensitive : boolean | undefined

      • if set to true will ignore strings that include a comment based on the provided string literals
    • stringLiterals : string[] | undefined

      defines the list of string literals - characters that a string start and end with

      • defaults to JavaScript string literals if stringSensitive is set to true
      • only used when stringSensitive is set to true
    • exclude : RegExp[] | undefined

      • an array of regexes
      • the comments that are matched by either one of these regexes are excluded from the output

Returns

Example

// initialize a pre-configured cmtu-object - jsCmtu
const jsCmtu = cmtu(cmtu.Languages.JS.resolver);
const jsCode = `
/*
this is a multi-line JS comment
*/

const callout = '// this is not a comment';

// this is a single line js comment
`;

const { strip, extract, magic } = jsCmtu;
// in this case the callout value will be included
// even though it's not an actual comment and is 
// a string because this instance the cmtu object
// exposes methods that are not string sensitive

// each of these methods is explained below
// at the cmtu-object section

const comments = jsCmtu.extract(jsCode);

console.log(comments);

cmtu.stringSensitive

returns a cmtu-object configured based on the args passed to it - the difference however is that methods exposed by this cmtu-object are stringSensitive meaning that in the rare cases where your strings might include comments themselves, these methods can understand the difference. Take a look at the example below:

Params

  • resolver : Resolver

  • options : optional - Omit<Options, 'stringSensitive'>

    • stringLiterals : string[] | undefined

      defines the list of string literals - characters that a string start and end with

      • defaults to JavaScript string literals if stringSensitive is set to true
      • only used when stringSensitive is set to true
    • exclude : RegExp[] | undefined

      • an array of regexes
      • the comments that are matched by either one of these regexes are excluded from the output

Returns

Example

// initialize a pre-configured cmtu-object - jsCmtu
const jsCmtu = cmtu.stringSensitive(cmtu.Languages.JS.resolver);
const jsCode = `
/*
this is a multi-line JS comment
*/

const callout = '// this is not a comment';

// this is a single line js comment
`;

const { strip, extract, magic } = jsCmtu;
// in this case the callout value won't be included
// as it's not an actual comment and is a comment
// inside of a srting

// each of these methods is explained below
// at the cmtu-object section

const codeWithNoComments = jsCmtu.strip(jsCode);

console.log(codeWithNoComments, codeWithNoComments.length);

Advanced Usage

you can customize cmtu to use it for languages that are not part of cmtu.Languages by default, for example:

// note that python is included in cmtu.Languages, and
// this is just an example to help you understand how
// to customize cmtu for your own use cases

const pyCode = `
# this is a comment in python
callout = "// this is not a comment in python"
#! python comment to be excluded
`;

// in order to customize the returned cmtu object
// we need to pass a proper resolver for python

// regex string to match python comments
const pyResolver = '#.*';
const pyStringLiterals = ["'", '"', "'''", '"""'];

const pyCmtu = cmtu.stringSensitive(
  pyResolver,
  {
    stringLiterals: pyStringLiterals,
    exclude: [/#!.*/] // optional
  }
);

const { strip, extract, magic } = pyCmtu;

console.log(extract(pyCode));
console.log(strip(pyCode));
console.log(magic(pyCode));

Interfaces

Resolver

type Resolver =
  | string
  | { block: string }
  | { inline: string }
  | { block: string; inline: string };

Built-In Languages

type LanguageName = 'JS' | 'CSS' | 'HTML' | 'CPP' | 'GO' | 'PYTHON' | 'PHP';

// support for php multi-line strings is lacking
// a good idea is to use a regex to exclude the
// comments that are within multi-line strings

Options

type Options = {
  stringSensitive?: boolean;
  stringLiterals?: string[];
  exclude?: RegExp[];
};

Cmtu Object

// 1- methods exposed by any configured cmtu object
// 2- return type of cmtu(...) and cmtu.stringSensitive(...)

{
  // returns a string with with its comments stripped
  strip: (code: string) => string;

  // returns an array of the stripped comments
  extract: (code: string) => string[];

  // returns a tuple [comment-stripped string, stripped comments]
  magic: (code: string) => [string, string[]];
}