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

@krausoft/comment-regexp-builder

v1.0.0

Published

Create custom comments RegExps. Leading and trailing space tolerant. With a content match.

Downloads

7

Readme

comment-regexp-builder

build Code Style: Google

RegExp wrapper for comment-like lines

  • custom tags, from strings ('/*', '*/', '//', ...)
  • leading and trailing space tolerant
  • opening, closing and section tags (ini files section, for example)
  • extract text from the tag

Examples

const crb = require('comment-regexp-builder');

const lineCommentTag = crb.createStartTag('//');
console.log(lineCommentTag.test('  // some comment '));
//=>true
console.log(lineCommentTag.innerText('  // some comment '));
//=>" some comment "

more line comment example

const crb = require('comment-regexp-builder');

const src = `
 //  my 2nd code

  // adds two numbers together
  //- simple and unconvenient
  const plus = (a, b) => {
    return a + b
  }
 module.exports = {
    // TODO: remove
    plus,
}
`;

const lineCommentTag = crb.createStartTag('//');
const lineComments = src.split('\n').filter(lineCommentTag.test);

console.log(lineComments);
//=> [
//   ' //  my 2nd code',
//   '  // adds two numbers together',
//   '  //- simple and unconvenient',
//   '    // TODO: remove'
// ]

console.log(lineComments.map(lineCommentTag.innerText));
//=> [
//   '  my 2nd code',
//   ' adds two numbers together',
//   '- simple and unconvenient',
//   ' TODO: remove'
// ]

ini file example

const crb = require('comment-regexp-builder');

const configText = `
; ini file

maxcount=4

 [common]

   ; in seconds
   timeout=10
; url to check
   url=http://something

[extras ] 
 
  maxtry=3

[user] 
name=John
  email=j@j

`;

const sectionTag = crb.createSectionTag('[', ']');
const sectionNames = configText
  .split('\n')
  .filter(sectionTag.test)
  .map(sectionTag.innerText);

console.log(sectionNames);
//=> [ 'common', 'extras ', 'user' ]

API

One can create three types of tags:

  • createStartTag(tagString)
  • createEndTag(tagString)
  • createSectionTag(leftPartString, rightPartString)

Each of tags has these methods:

  • regexp(): returns its internal RegExp object
  • test(line: string): a shorthand for its internal RegExp object test method
  • innerText(line: string): If tag is found at the line, returns the text that belongs to it

There is also useful constant tag object: matchAllTag, which meets the following:

  • matchAllTag.test(s) === true //for all strings s
  • matchAllTag.innerText(s) === s //for all strings s

Limitations

To recognize lines with tags, these criteria must be met:

  • Start-Tag must be the first non-white character at the line
  • End-Tag must be the last non-white character at the line
  • Section Tag pair can be surrounded only by white chars
const crb = require('comment-regexp-builder');

const startBlock = crb.createStartTag('/*');
const endBlock = crb.createEndTag('*/');

console.log(startBlock.test(' x /* some comment */  '));
//=>false
console.log(startBlock.innerText(' x /* some comment */  '));
//=>null
console.log(endBlock.test(' x /* some comment */  '));
//=>true
console.log(endBlock.innerText(' x /* some comment */  '));
//=>" x /* some comment "

console.log(startBlock.test('  /* some comment */ x '));
//=>true
console.log(startBlock.innerText('  /* some comment */ x '));
//=>" some comment */ x "
console.log(endBlock.test('  /* some comment */ x '));
//=>false
console.log(endBlock.innerText('  /* some comment */ x '));
//=>null

Because of that, this library cannot detects blocks inside the text:

const a = 10; //cannot detect this comment
const b = /*cannot detect this comment*/ true;