You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

99 lines
4.5 KiB

4 years ago
  1. anymatch [![Build Status](https://travis-ci.org/micromatch/anymatch.svg?branch=master)](https://travis-ci.org/micromatch/anymatch) [![Coverage Status](https://img.shields.io/coveralls/micromatch/anymatch.svg?branch=master)](https://coveralls.io/r/micromatch/anymatch?branch=master)
  2. ======
  3. Javascript module to match a string against a regular expression, glob, string,
  4. or function that takes the string as an argument and returns a truthy or falsy
  5. value. The matcher can also be an array of any or all of these. Useful for
  6. allowing a very flexible user-defined config to define things like file paths.
  7. __Note: This module has Bash-parity, please be aware that Windows-style backslashes are not supported as separators. See https://github.com/micromatch/micromatch#backslashes for more information.__
  8. [![NPM](https://nodei.co/npm/anymatch.png?downloads=true&downloadRank=true&stars=true)](https://nodei.co/npm/anymatch/)
  9. [![NPM](https://nodei.co/npm-dl/anymatch.png?height=3&months=9)](https://nodei.co/npm-dl/anymatch/)
  10. Usage
  11. -----
  12. ```sh
  13. npm install anymatch --save
  14. ```
  15. #### anymatch (matchers, testString, [returnIndex], [startIndex], [endIndex])
  16. * __matchers__: (_Array|String|RegExp|Function_)
  17. String to be directly matched, string with glob patterns, regular expression
  18. test, function that takes the testString as an argument and returns a truthy
  19. value if it should be matched, or an array of any number and mix of these types.
  20. * __testString__: (_String|Array_) The string to test against the matchers. If
  21. passed as an array, the first element of the array will be used as the
  22. `testString` for non-function matchers, while the entire array will be applied
  23. as the arguments for function matchers.
  24. * __returnIndex__: (_Boolean [optional]_) If true, return the array index of
  25. the first matcher that that testString matched, or -1 if no match, instead of a
  26. boolean result.
  27. * __startIndex, endIndex__: (_Integer [optional]_) Can be used to define a
  28. subset out of the array of provided matchers to test against. Can be useful
  29. with bound matcher functions (see below). When used with `returnIndex = true`
  30. preserves original indexing. Behaves the same as `Array.prototype.slice` (i.e.
  31. includes array members up to, but not including endIndex).
  32. ```js
  33. var anymatch = require('anymatch');
  34. var matchers = [
  35. 'path/to/file.js',
  36. 'path/anyjs/**/*.js',
  37. /foo\.js$/,
  38. function (string) {
  39. return string.indexOf('bar') !== -1 && string.length > 10
  40. }
  41. ];
  42. anymatch(matchers, 'path/to/file.js'); // true
  43. anymatch(matchers, 'path/anyjs/baz.js'); // true
  44. anymatch(matchers, 'path/to/foo.js'); // true
  45. anymatch(matchers, 'path/to/bar.js'); // true
  46. anymatch(matchers, 'bar.js'); // false
  47. // returnIndex = true
  48. anymatch(matchers, 'foo.js', true); // 2
  49. anymatch(matchers, 'path/anyjs/foo.js', true); // 1
  50. // skip matchers
  51. anymatch(matchers, 'path/to/file.js', false, 1); // false
  52. anymatch(matchers, 'path/anyjs/foo.js', true, 2, 3); // 2
  53. anymatch(matchers, 'path/to/bar.js', true, 0, 3); // -1
  54. // using globs to match directories and their children
  55. anymatch('node_modules', 'node_modules'); // true
  56. anymatch('node_modules', 'node_modules/somelib/index.js'); // false
  57. anymatch('node_modules/**', 'node_modules/somelib/index.js'); // true
  58. anymatch('node_modules/**', '/absolute/path/to/node_modules/somelib/index.js'); // false
  59. anymatch('**/node_modules/**', '/absolute/path/to/node_modules/somelib/index.js'); // true
  60. ```
  61. #### anymatch (matchers)
  62. You can also pass in only your matcher(s) to get a curried function that has
  63. already been bound to the provided matching criteria. This can be used as an
  64. `Array.prototype.filter` callback.
  65. ```js
  66. var matcher = anymatch(matchers);
  67. matcher('path/to/file.js'); // true
  68. matcher('path/anyjs/baz.js', true); // 1
  69. matcher('path/anyjs/baz.js', true, 2); // -1
  70. ['foo.js', 'bar.js'].filter(matcher); // ['foo.js']
  71. ```
  72. Change Log
  73. ----------
  74. [See release notes page on GitHub](https://github.com/micromatch/anymatch/releases)
  75. NOTE: As of v2.0.0, [micromatch](https://github.com/jonschlinkert/micromatch) moves away from minimatch-parity and inline with Bash. This includes handling backslashes differently (see https://github.com/micromatch/micromatch#backslashes for more information).
  76. NOTE: As of v1.2.0, anymatch uses [micromatch](https://github.com/jonschlinkert/micromatch)
  77. for glob pattern matching. Issues with glob pattern matching should be
  78. reported directly to the [micromatch issue tracker](https://github.com/jonschlinkert/micromatch/issues).
  79. License
  80. -------
  81. [ISC](https://raw.github.com/micromatch/anymatch/master/LICENSE)