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

extract-comments

v1.1.0

Published

Uses esprima to extract line and block comments from a string of JavaScript. Also optionally parses code context (the next line of code after a comment).

Downloads

195,245

Readme

extract-comments NPM version NPM monthly downloads NPM total downloads Linux Build Status

Uses esprima to extract line and block comments from a string of JavaScript. Also optionally parses code context (the next line of code after a comment).

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

Install

Install with npm:

$ npm install --save extract-comments

Usage

var extract = require('extract-comments');

// pass a string of JavaScript
extract(string);

Example

var str = '/**\n * this is\n *\n * a comment\n*/\n\n\nvar foo = "bar";\n';
var comments = extract(str);
console.log(comments);

[{
  type: 'block',
  raw: '/**\n * this is\n *\n * a comment\n*/',
  value: 'this is\na comment',
  loc: { start: { line: 1, column: 0 }, end: { line: 5, column: 33 } },
  code:
   { line: 7,
     loc: { start: { line: 7, column: 36 }, end: { line: 7, column: 52 } },
     value: 'var foo = "bar";' }

Extractors

By default, esprima is used for extracting comments. This can easily be changed by passing a function to options.extractor.

The easy way

Use a published module, such as:

Example:

extract(str, {extractor: require('babel-extract-comments')});

If you create a compatible extractor, feel free to do pr or create an issue to add it to the readme!

Roll your own

extract(str, {
  extractor: function(str) {
    // must return an array of tokens with:
    // - type: 'Block', 'CommentBlock', 'Line' or 'CommentLine'
    // - value: the comment inner string
    // - loc: with `start` and `end` line and column
    // example:
    return [
      { 
        type: 'Block',
        {start: { line: 1, column: 0 },
          end: { line: 5, column: 33 }},
        value: ' this is a comment string '
      }
    ];
  }
});

API

extract

Extract comments from the given string.

Params

  • string {String}
  • options {Object}: Pass first: true to return after the first comment is found.
  • tranformFn {Function}: (optional) Tranform function to modify each comment
  • returns {Array}: Returns an array of comment objects

Example

const extract = require('extract-comments');
console.log(extract(string, options));

.block

Extract block comments from the given string.

Params

  • string {String}
  • options {Object}: Pass first: true to return after the first comment is found.
  • returns {String}

Example

console.log(extract.block(string, options));

.line

Extract line comments from the given string.

Params

  • string {String}
  • options {Object}: Pass first: true to return after the first comment is found.
  • returns {String}

Example

console.log(extract.line(string, options));

.first

Extract the first comment from the given string.

Params

  • string {String}
  • options {Object}: Pass first: true to return after the first comment is found.
  • returns {String}

Example

console.log(extract.first(string, options));

Release history

v0.10.0

  • Parsing is now handled by esprima, so only JavaScript can be parsed. I'm working on parsers for other languages and will cross-link those here when they're pushed up.
  • Breaking change: since parsing is now done by esprima, on both the line and block comment objects, the loc.start.pos and loc.end.pos properties have been renamed to loc.start.column and loc.end.column.

v0.9.0

  • Breaking change: lines property was removed from Block comments, since this can easily be done by splitting value

About

Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.

Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:

$ npm install && npm test

(This project's readme.md is generated by verb, please don't edit the readme directly. Any changes to the readme must be made in the .verb.md readme template.)

To generate the readme, run the following command:

$ npm install -g verbose/verb#dev verb-generate-readme && verb

Related projects

You might also be interested in these projects:

Contributors

| Commits | Contributor | | --- | --- | | 93 | jonschlinkert | | 3 | cazzer | | 1 | architectcodes |

Author

Jon Schlinkert

License

Copyright © 2018, Jon Schlinkert. Released under the MIT License.


This file was generated by verb-generate-readme, v0.6.0, on February 12, 2018.