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

strip-comments-strings

v1.2.0

Published

Strip or extract comments, strings or both from source code.

Downloads

799,404

Readme

Description

Strip or extract comments, strings or both from source code.

Install

npm install strip-comments-strings

Usage

const {parseString, stripStrings, stripComments, clearStrings} = require("strip-comments-strings");

or

import {parseString, stripStrings, stripComments, clearStrings} from "strip-comments-strings"

Examples

Clearing strings

📝 running-code.js ↴

let str = fs.readFileSync("./example.js", "utf-8");
let result = clearStrings(str);
console.log(result)

Before ↴ (example.js)

    let c1 = "Ho you \" How are \" the you?";
    let c2 = "dfgdfd dfgdfgd \" '*";
    let c3 = "Okay okay";

    let c4 = `sfdgfdf d d  dd 
    \` ${c2}
    le = "abd pdfdp \" ;lfgfdlgkd"`;
};

After ↴

    let c1 = "";
    let c2 = "";
    let c3 = "";

    let c4 = ``;
};

Stripping strings

📝 running-code.js ↴

let str = fs.readFileSync("./example.js", "utf-8");
let result = stripStrings(str);
console.log(result)

Before ↴ (example.js)

    let c1 = "Ho you \" How are \" the you?";
    let c2 = "dfgdfd dfgdfgd \" '*";
    let c3 = "Okay okay";

    let c4 = `sfdgfdf d d  dd 
    \` ${c2}
    le = "abd pdfdp \" ;lfgfdlgkd"`;
};

After ↴

    let c1 = ;
    let c2 = ;
    let c3 = ;

    let c4 = ;

};

Stripping comments

📝 running-code.js ↴

let str = fs.readFileSync("./example.js", "utf-8");
let result = stripComments(str);
console.log(result)

Before (example.js) ↴

//abcdefghij
const bb = `
    // Hi
    /** Copy **/
    const chalk004 = require("chalk-cjs");
    
    const chalk005 = require("chalk-cjs");
`
/**
 * @class SomeClass
 */

After ↴

const bb = `
    // Hi
    /** Copy **/
    const chalk004 = require("chalk-cjs");

    const chalk005 = require("chalk-cjs");
`

Replacing comments from source via callback

📝 running-code.js ↴

let str = fs.readFileSync(path.join(__dirname, "./example.js"), "utf-8");

const stripMyComments = (str) =>
{
    let counter = 0
    str = stripComments(str, function (info)
    {
        return `(${counter++})`
    });

    return str
}

console.log(str)

Before ↴

/** // **/
/** 1 **/
//aaaa
/** 2 **/
//bbbbb
//ccccc
///////////
/** 3 **/
/** 4 **/
/** 5 **/
""
aa
"attenti\"on"
bb
ccc
'warni\\'
'ng\''
ddd
eeee`info`
eeee
fffff
"silence"
fffff
gggggg
'noise'
gggggg
hhhhhhh`visibility`
hhhhhhh
"aaa"
"aaa"
"aaa"

After ↴

(9)
(8)
(7)(6)
(5)(4)(3)(2)
(1)
(0)
""
aa
"attenti\"on"
bb
ccc
'warni\\'
'ng\''
ddd
eeee`info`
eeee
fffff
"silence"
fffff
gggggg
'noise'
gggggg
hhhhhhh`visibility`
hhhhhhh
"aaa"
"aaa"
"aaa"

Replacing strings from source via callbacks

    let str = fs.readFileSync("./example.js", "utf-8");

str = stripStrings(str, function (info)
{
    return "!!"
}, {includeDelimiter: true});

Before (example.js) ↴

"What"
'Time'
`is it?`
!!
!!
!!

Strip comments and strings

// ...
str = stripComments(str);
str = stripStrings(str)
// ...

It is best to always start by removing comments then strings.


Parsing source

const found = parseString(str);
console.log( found );
{
  text: '',
  strings: [
    { content: '', index: 107, indexEnd: 107, item: [Object] },
    {
      content: 'attenti\\"on',
      index: 115,
      indexEnd: 126,
      item: [Object]
    },
    { content: 'warni\\\\', index: 139, indexEnd: 146, item: [Object] },
    { content: "ng\\'", index: 150, indexEnd: 154, item: [Object] },
    { content: 'info', index: 167, indexEnd: 171, item: [Object] },
    { content: 'silence', index: 188, indexEnd: 195, item: [Object] },
    { content: 'noise', index: 214, indexEnd: 219, item: [Object] },
    {
      content: 'visibility',
      index: 238,
      indexEnd: 248,
      item: [Object]
    },
    { content: 'aaa', index: 261, indexEnd: 264, item: [Object] },
    { content: 'aaa', index: 268, indexEnd: 271, item: [Object] },
    { content: 'aaa', index: 275, indexEnd: 278, item: [Object] }
  ],
  comments: [
    {
      type: 'commentBlock',
      content: '/** // **/',
      index: 0,
      indexEnd: 10
    },
    {
      type: 'commentBlock',
      content: '/** 1 **/',
      index: 12,
      indexEnd: 21
    },
    {
      type: 'commentLine',
      content: '//aaaa\r\n',
      index: 23,
      indexEnd: 31
    },
    {
      type: 'commentBlock',
      content: '/** 2 **/',
      index: 31,
      indexEnd: 40
    },
    {
      type: 'commentLine',
      content: '//bbbbb\r\n',
      index: 42,
      indexEnd: 51
    },
    {
      type: 'commentLine',
      content: '//ccccc\r\n',
      index: 51,
      indexEnd: 60
    },
    {
      type: 'commentLine',
      content: '///////////\r\n',
      index: 60,
      indexEnd: 73
    },
    {
      type: 'commentBlock',
      content: '/** 3 **/',
      index: 73,
      indexEnd: 82
    },
    {
      type: 'commentBlock',
      content: '/** 4 **/',
      index: 84,
      indexEnd: 93
    },
    {
      type: 'commentBlock',
      content: '/** 5 **/',
      index: 95,
      indexEnd: 104
    }
  ]
}