match-requires

2.0.1 • Public • Published

match-requires NPM version NPM monthly downloads NPM total downloads Linux Build Status

Match require statements in a string. Returns an array of matching require statements. Each match is an object with line number, variable name, and module name. Statements in code comments are ignored.

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

Install

Install with npm:

$ npm install --save match-requires

Usage

const matches = require('match-requires');
console.log(matches('require(\'a-b-c\');\nvar fooBar = require(\'foo-bar\');'));

Returns:

[ { string: 'require(\'a-b-c\');', 
    variable: '', 
    name: 'a-b-c' },
  { string: 'var fooBar = require(\'foo-bar\');',
    variable: 'fooBar',
    name: 'foo-bar' } ]

Ignore requires in code comments

To ignore require() statements found inside code comments, pass true as the second argument to strip comments before matching. Alternatively, you may pass a function as the second argument to use your own approach to stripping comments.

Without comments stripped

console.log(matches('/* require(\'a-b-c\');*/\nvar fooBar = require(\'foo-bar\');')); 
// [ { string: 'require(\'a-b-c\');', variable: '', name: 'a-b-c' },
//   { string: 'var fooBar = require(\'foo-bar\');',
//     variable: 'fooBar',
//     name: 'foo-bar' } ]

With comments stripped

console.log(matches('/* require(\'a-b-c\');*/\nvar fooBar = require(\'foo-bar\');', true));
// [ { string: 'var fooBar = require(\'foo-bar\');',
//     variable: 'fooBar',
//     name: 'foo-bar' } ]

With custom function

const str = '/* require(\'a-b-c\');*/\nvar fooBar = require(\'foo-bar\');';
const fn = require('some-comment-stripping-library');
console.log(matches(str, fn));
// [ { string: 'var fooBar = require(\'foo-bar\');',
//     variable: 'fooBar',
//     name: 'foo-bar' } ]

About

Contributing

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

Running Tests

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
Building docs

(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:

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 April 15, 2018.

Package Sidebar

Install

npm i match-requires

Weekly Downloads

417

Version

2.0.1

License

MIT

Unpacked Size

8.59 kB

Total Files

4

Last publish

Collaborators

  • doowb
  • jonschlinkert