Latest Threat Research:SANDWORM_MODE: Shai-Hulud-Style npm Worm Hijacks CI Workflows and Poisons AI Toolchains.Details
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 - npm Package Compare versions

Comparing version
1.0.2
to
2.0.0
+26
-28
index.js
/*!
* match-requires <https://github.com/jonschlinkert/match-requires>
*
* Copyright (c) 2014-2015, Jon Schlinkert.
* Licensed under the MIT License
* Copyright (c) 2014-2018, Jon Schlinkert.
* Released under the MIT License.
*/

@@ -10,35 +10,33 @@

var regex = require('requires-regex');
var re = require('requires-regex');
module.exports = function matchRequires(str, stripComments) {
if (stripComments === true) {
str = require('strip-comments')(str);
module.exports = function matchRequires(str, options = {}) {
if (typeof options === 'boolean' || typeof options === 'function') {
options = { stripComments: options };
}
if (typeof stripComments === 'function') {
str = stripComments(str);
if (options.stripComments === true) {
str = require('strip-comments')(str, options);
}
var lines = str.split('\n');
var len = lines.length;
var i = 0;
var res = [];
var re = regex();
var match;
if (typeof options.stripComments === 'function') {
str = options.stripComments(str);
}
while (len--) {
var line = lines[i++];
var match = re.exec(line);
if (match) {
res.push({
line: i,
col: match.index,
variable: match[1] || '',
module: match[2],
original: line
});
}
var matches = [];
var regex = re();
let match;
while ((match = regex.exec(str))) {
if (!match[4]) continue;
var tok = { string: match[0].trim(), variable: match[2] || '', name: match[4] };
Reflect.defineProperty(tok, 'match', {
enumerable: false,
value: match
});
matches.push(tok);
}
return res;
return matches;
};
The MIT License (MIT)
Copyright (c) 2014-2016, Jon Schlinkert.
Copyright (c) 2014-2018, Jon Schlinkert.

@@ -5,0 +5,0 @@ Permission is hereby granted, free of charge, to any person obtaining a copy

{
"name": "match-requires",
"description": "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.",
"version": "1.0.2",
"version": "2.0.0",
"homepage": "https://github.com/jonschlinkert/match-requires",

@@ -23,13 +23,11 @@ "author": "Jon Schlinkert (https://github.com/jonschlinkert)",

"dependencies": {
"requires-regex": "^0.3.3",
"strip-comments": "^0.4.3"
"requires-regex": "^1.0.2",
"strip-comments": "^1.0.1"
},
"devDependencies": {
"benchmarked": "^0.1.3",
"chalk": "^0.5.1",
"crequire": "^1.5.3",
"detective": "^4.0.0",
"gulp-format-md": "^0.1.4",
"mocha": "*",
"should": "*"
"crequire": "^1.8.1",
"detective": "^5.1.0",
"benchmarked": "^2.0.0",
"gulp-format-md": "^1.0.0",
"mocha": "^3.5.3"
},

@@ -65,12 +63,21 @@ "keywords": [

"verb": {
"toc": false,
"layout": "default",
"tasks": [
"readme"
],
"plugins": [
"gulp-format-md"
],
"related": {
"list": [
"requires-regex"
"requires-regex",
"to-regex-range",
"year-range-regex"
]
},
"plugins": [
"gulp-format-md"
],
"layout": "default"
"lint": {
"reflinks": true
}
}
}
+67
-70

@@ -1,17 +0,20 @@

# match-requires [![NPM version](https://img.shields.io/npm/v/match-requires.svg)](https://www.npmjs.com/package/match-requires) [![Build Status](https://img.shields.io/travis/jonschlinkert/match-requires.svg)](https://travis-ci.org/jonschlinkert/match-requires)
# match-requires [![NPM version](https://img.shields.io/npm/v/match-requires.svg?style=flat)](https://www.npmjs.com/package/match-requires) [![NPM monthly downloads](https://img.shields.io/npm/dm/match-requires.svg?style=flat)](https://npmjs.org/package/match-requires) [![NPM total downloads](https://img.shields.io/npm/dt/match-requires.svg?style=flat)](https://npmjs.org/package/match-requires) [![Linux Build Status](https://img.shields.io/travis/jonschlinkert/match-requires.svg?style=flat&label=Travis)](https://travis-ci.org/jonschlinkert/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.
- [Usage](#usage)
* [Code comments](#code-comments)
- [Benchmarks](#benchmarks)
- [License](#license)
Please consider following this project's author, [Jon Schlinkert](https://github.com/jonschlinkert), and consider starring the project to show your :heart: and support.
_(TOC generated by [verb](https://github.com/verbose/verb) using [markdown-toc](https://github.com/jonschlinkert/markdown-toc))_
## Install
Install with [npm](https://www.npmjs.com/):
```sh
$ npm install --save match-requires
```
## Usage
```js
var re = require('match-requires');
console.log(re('require(\'a-b-c\');\nvar fooBar = require(\'foo-bar\');'))
const matches = require('match-requires');
console.log(matches('require(\'a-b-c\');\nvar fooBar = require(\'foo-bar\');'));
```

@@ -22,104 +25,98 @@

```js
[ { line: 1,
variable: '',
module: 'a-b-c',
original: 'require(\'a-b-c\');' },
{ line: 2,
[ { string: 'require(\'a-b-c\');',
variable: '',
name: 'a-b-c' },
{ string: 'var fooBar = require(\'foo-bar\');',
variable: 'fooBar',
module: 'foo-bar',
original: 'var fooBar = require(\'foo-bar\');' } ]
name: 'foo-bar' } ]
```
### Code comments
### Ignore requires in code comments
To ignore require statements found in code comments, pass `true` as the second arg:
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**
```js
re('/* require(\'a-b-c\');*/\nvar fooBar = require(\'foo-bar\');');
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' } ]
```
Returns:
**_With_ comments stripped**
```js
[ { line: 2,
variable: 'fooBar',
module: 'foo-bar',
original: 'var fooBar = require(\'foo-bar\');' } ]
console.log(matches('/* require(\'a-b-c\');*/\nvar fooBar = require(\'foo-bar\');', true));
// [ { string: 'var fooBar = require(\'foo-bar\');',
// variable: 'fooBar',
// name: 'foo-bar' } ]
```
You may also pass a custom function for stripping code comments and/or quoted strings.
**_With_ custom function**
```js
var str = '/* require(\'a-b-c\');*/\nvar fooBar = require(\'foo-bar\');';
re(str, function(content) {
return require('my-own-comment-stripper')(content);
});
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' } ]
```
## Benchmarks
## About
See the [generated output](https://gist.github.com/jonschlinkert/ed359fe882a5973ee86e) that each lib produces for each benchmark.
<details>
<summary><strong>Contributing</strong></summary>
```bash
#1: after-return.js
crequire.js x 153,480 ops/sec ±0.71% (97 runs sampled)
detective.js x 55,015 ops/sec ±1.04% (91 runs sampled)
match-requires.js x 2,439,019 ops/sec ±0.77% (98 runs sampled)
Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new).
#2: basic.js
crequire.js x 391,131 ops/sec ±0.63% (96 runs sampled)
detective.js x 102,594 ops/sec ±0.84% (99 runs sampled)
match-requires.js x 2,674,151 ops/sec ±0.83% (94 runs sampled)
</details>
#3: do-while.js
crequire.js x 102,220 ops/sec ±0.93% (96 runs sampled)
detective.js x 41,635 ops/sec ±0.81% (97 runs sampled)
match-requires.js x 1,535,496 ops/sec ±0.59% (99 runs sampled)
<details>
<summary><strong>Running Tests</strong></summary>
#4: if-statement.js
crequire.js x 121,816 ops/sec ±0.82% (93 runs sampled)
detective.js x 43,148 ops/sec ±1.02% (96 runs sampled)
match-requires.js x 1,750,488 ops/sec ±0.72% (98 runs sampled)
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:
#5: in-method.js
crequire.js x 268,212 ops/sec ±0.64% (97 runs sampled)
detective.js x 55,454 ops/sec ±0.98% (96 runs sampled)
match-requires.js x 2,567,571 ops/sec ±0.77% (95 runs sampled)
#6: multiple.js
crequire.js x 70,591 ops/sec ±0.54% (96 runs sampled)
detective.js x 21,315 ops/sec ±0.96% (98 runs sampled)
match-requires.js x 893,286 ops/sec ±0.77% (96 runs sampled)
```sh
$ npm install && npm test
```
## Related projects
</details>
[requires-regex](https://www.npmjs.com/package/requires-regex): Regular expression for matching javascript require statements. | [homepage](https://github.com/jonschlinkert/requires-regex)
<details>
<summary><strong>Building docs</strong></summary>
## Running tests
_(This project's readme.md is generated by [verb](https://github.com/verbose/verb-generate-readme), please don't edit the readme directly. Any changes to the readme must be made in the [.verb.md](.verb.md) readme template.)_
Install dev dependencies:
To generate the readme, run the following command:
```sh
$ npm i -d && npm test
$ npm install -g verbose/verb#dev verb-generate-readme && verb
```
## Contributing
</details>
Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](https://github.com/jonschlinkert/match-requires/issues/new).
### Related projects
## Author
You might also be interested in these projects:
[requires-regex](https://www.npmjs.com/package/requires-regex): Regular expression for matching javascript require statements. | [homepage](https://github.com/jonschlinkert/requires-regex "Regular expression for matching javascript require statements.")
### Author
**Jon Schlinkert**
* [github/jonschlinkert](https://github.com/jonschlinkert)
* [twitter/jonschlinkert](http://twitter.com/jonschlinkert)
* [LinkedIn Profile](https://linkedin.com/in/jonschlinkert)
* [GitHub Profile](https://github.com/jonschlinkert)
* [Twitter Profile](https://twitter.com/jonschlinkert)
## License
### License
Copyright © 2016 [Jon Schlinkert](https://github.com/jonschlinkert)
Released under the MIT license.
Copyright © 2018, [Jon Schlinkert](https://github.com/jonschlinkert).
Released under the [MIT License](LICENSE).
***
_This file was generated by [verb](https://github.com/verbose/verb) on January 08, 2016._
_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.6.0, on April 15, 2018._