Socket
Book a DemoInstallSign in
Socket

match-requires

Package Overview
Dependencies
Maintainers
2
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

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.

latest
Source
npmnpm
Version
2.0.1
Version published
Weekly downloads
166
219.23%
Maintainers
2
Weekly downloads
 
Created
Source

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

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

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.

Keywords

analyze

FAQs

Package last updated on 15 Apr 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts