41663c19b00a6efe81ccca254dad7e34bb94ccaf567954d687b55f9c15d32c73fa1ef563fb37ac3a6126bd6154ac95068767eb96ef204346a36806d2271182 4.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. anymatch [![Build Status](https://travis-ci.org/es128/anymatch.svg?branch=master)](https://travis-ci.org/es128/anymatch) [![Coverage Status](https://img.shields.io/coveralls/es128/anymatch.svg?branch=master)](https://coveralls.io/r/es128/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. [![NPM](https://nodei.co/npm/anymatch.png?downloads=true&downloadRank=true&stars=true)](https://nodei.co/npm/anymatch/)
  8. [![NPM](https://nodei.co/npm-dl/anymatch.png?height=3&months=9)](https://nodei.co/npm-dl/anymatch/)
  9. Usage
  10. -----
  11. ```sh
  12. npm install anymatch --save
  13. ```
  14. #### anymatch (matchers, testString, [returnIndex], [startIndex], [endIndex])
  15. * __matchers__: (_Array|String|RegExp|Function_)
  16. String to be directly matched, string with glob patterns, regular expression
  17. test, function that takes the testString as an argument and returns a truthy
  18. value if it should be matched, or an array of any number and mix of these types.
  19. * __testString__: (_String|Array_) The string to test against the matchers. If
  20. passed as an array, the first element of the array will be used as the
  21. `testString` for non-function matchers, while the entire array will be applied
  22. as the arguments for function matchers.
  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. * __startIndex, endIndex__: (_Integer [optional]_) Can be used to define a
  27. subset out of the array of provided matchers to test against. Can be useful
  28. with bound matcher functions (see below). When used with `returnIndex = true`
  29. preserves original indexing. Behaves the same as `Array.prototype.slice` (i.e.
  30. includes array members up to, but not including endIndex).
  31. ```js
  32. var anymatch = require('anymatch');
  33. var matchers = [
  34. 'path/to/file.js',
  35. 'path/anyjs/**/*.js',
  36. /foo\.js$/,
  37. function (string) {
  38. return string.indexOf('bar') !== -1 && string.length > 10
  39. }
  40. ];
  41. anymatch(matchers, 'path/to/file.js'); // true
  42. anymatch(matchers, 'path/anyjs/baz.js'); // true
  43. anymatch(matchers, 'path/to/foo.js'); // true
  44. anymatch(matchers, 'path/to/bar.js'); // true
  45. anymatch(matchers, 'bar.js'); // false
  46. // returnIndex = true
  47. anymatch(matchers, 'foo.js', true); // 2
  48. anymatch(matchers, 'path/anyjs/foo.js', true); // 1
  49. // skip matchers
  50. anymatch(matchers, 'path/to/file.js', false, 1); // false
  51. anymatch(matchers, 'path/anyjs/foo.js', true, 2, 3); // 2
  52. anymatch(matchers, 'path/to/bar.js', true, 0, 3); // -1
  53. // using globs to match directories and their children
  54. anymatch('node_modules', 'node_modules'); // true
  55. anymatch('node_modules', 'node_modules/somelib/index.js'); // false
  56. anymatch('node_modules/**', 'node_modules/somelib/index.js'); // true
  57. anymatch('node_modules/**', '/absolute/path/to/node_modules/somelib/index.js'); // false
  58. anymatch('**/node_modules/**', '/absolute/path/to/node_modules/somelib/index.js'); // true
  59. ```
  60. #### anymatch (matchers)
  61. You can also pass in only your matcher(s) to get a curried function that has
  62. already been bound to the provided matching criteria. This can be used as an
  63. `Array.prototype.filter` callback.
  64. ```js
  65. var matcher = anymatch(matchers);
  66. matcher('path/to/file.js'); // true
  67. matcher('path/anyjs/baz.js', true); // 1
  68. matcher('path/anyjs/baz.js', true, 2); // -1
  69. ['foo.js', 'bar.js'].filter(matcher); // ['foo.js']
  70. ```
  71. Change Log
  72. ----------
  73. [See release notes page on GitHub](https://github.com/es128/anymatch/releases)
  74. NOTE: As of v1.2.0, anymatch uses [micromatch](https://github.com/jonschlinkert/micromatch)
  75. for glob pattern matching. The glob matching behavior should be functionally
  76. equivalent to the commonly used [minimatch](https://github.com/isaacs/minimatch)
  77. library (aside from some fixed bugs and greater performance), so a major
  78. version bump wasn't merited. Issues with glob pattern matching should be
  79. reported directly to the [micromatch issue tracker](https://github.com/jonschlinkert/micromatch/issues).
  80. License
  81. -------
  82. [ISC](https://raw.github.com/es128/anymatch/master/LICENSE)