match-requires
v2.0.1
Published
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.
Downloads
864
Readme
match-requires
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 :heart: 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
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:
- requires-regex: Regular expression for matching javascript require statements. | homepage
- to-regex-range: Pass two numbers, get a regex-compatible source string for matching ranges. Validated against more than… more | homepage
- year-range-regex: Generates a regular expression for validating a range of years. | homepage
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.