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.

87 lines
3.9 KiB

2 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. Usage
  9. -----
  10. ```sh
  11. npm install anymatch
  12. ```
  13. #### anymatch(matchers, testString, [returnIndex], [options])
  14. * __matchers__: (_Array|String|RegExp|Function_)
  15. String to be directly matched, string with glob patterns, regular expression
  16. test, function that takes the testString as an argument and returns a truthy
  17. value if it should be matched, or an array of any number and mix of these types.
  18. * __testString__: (_String|Array_) The string to test against the matchers. If
  19. passed as an array, the first element of the array will be used as the
  20. `testString` for non-function matchers, while the entire array will be applied
  21. as the arguments for function matchers.
  22. * __options__: (_Object_ [optional]_) Any of the [picomatch](https://github.com/micromatch/picomatch#options) options.
  23. * __returnIndex__: (_Boolean [optional]_) If true, return the array index of
  24. the first matcher that that testString matched, or -1 if no match, instead of a
  25. boolean result.
  26. ```js
  27. const anymatch = require('anymatch');
  28. const matchers = [ 'path/to/file.js', 'path/anyjs/**/*.js', /foo.js$/, string => string.includes('bar') && string.length > 10 ] ;
  29. anymatch(matchers, 'path/to/file.js'); // true
  30. anymatch(matchers, 'path/anyjs/baz.js'); // true
  31. anymatch(matchers, 'path/to/foo.js'); // true
  32. anymatch(matchers, 'path/to/bar.js'); // true
  33. anymatch(matchers, 'bar.js'); // false
  34. // returnIndex = true
  35. anymatch(matchers, 'foo.js', {returnIndex: true}); // 2
  36. anymatch(matchers, 'path/anyjs/foo.js', {returnIndex: true}); // 1
  37. // any picomatc
  38. // using globs to match directories and their children
  39. anymatch('node_modules', 'node_modules'); // true
  40. anymatch('node_modules', 'node_modules/somelib/index.js'); // false
  41. anymatch('node_modules/**', 'node_modules/somelib/index.js'); // true
  42. anymatch('node_modules/**', '/absolute/path/to/node_modules/somelib/index.js'); // false
  43. anymatch('**/node_modules/**', '/absolute/path/to/node_modules/somelib/index.js'); // true
  44. const matcher = anymatch(matchers);
  45. ['foo.js', 'bar.js'].filter(matcher); // [ 'foo.js' ]
  46. anymatch master* ❯
  47. ```
  48. #### anymatch(matchers)
  49. You can also pass in only your matcher(s) to get a curried function that has
  50. already been bound to the provided matching criteria. This can be used as an
  51. `Array#filter` callback.
  52. ```js
  53. var matcher = anymatch(matchers);
  54. matcher('path/to/file.js'); // true
  55. matcher('path/anyjs/baz.js', true); // 1
  56. ['foo.js', 'bar.js'].filter(matcher); // ['foo.js']
  57. ```
  58. Changelog
  59. ----------
  60. [See release notes page on GitHub](https://github.com/micromatch/anymatch/releases)
  61. - **v3.0:** Removed `startIndex` and `endIndex` arguments. Node 8.x-only.
  62. - **v2.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).
  63. - **v1.2:** anymatch uses [micromatch](https://github.com/jonschlinkert/micromatch)
  64. for glob pattern matching. Issues with glob pattern matching should be
  65. reported directly to the [micromatch issue tracker](https://github.com/jonschlinkert/micromatch/issues).
  66. License
  67. -------
  68. [ISC](https://raw.github.com/micromatch/anymatch/master/LICENSE)