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.

README.md 33KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148
  1. # nanomatch [![NPM version](https://img.shields.io/npm/v/nanomatch.svg?style=flat)](https://www.npmjs.com/package/nanomatch) [![NPM monthly downloads](https://img.shields.io/npm/dm/nanomatch.svg?style=flat)](https://npmjs.org/package/nanomatch) [![NPM total downloads](https://img.shields.io/npm/dt/nanomatch.svg?style=flat)](https://npmjs.org/package/nanomatch) [![Linux Build Status](https://img.shields.io/travis/micromatch/nanomatch.svg?style=flat&label=Travis)](https://travis-ci.org/micromatch/nanomatch) [![Windows Build Status](https://img.shields.io/appveyor/ci/micromatch/nanomatch.svg?style=flat&label=AppVeyor)](https://ci.appveyor.com/project/micromatch/nanomatch)
  2. > Fast, minimal glob matcher for node.js. Similar to micromatch, minimatch and multimatch, but complete Bash 4.3 wildcard support only (no support for exglobs, posix brackets or braces)
  3. Please consider following this project's author, [Jon Schlinkert](https://github.com/jonschlinkert), and consider starring the project to show your :heart: and support.
  4. ## Table of Contents
  5. <details>
  6. <summary><strong>Details</strong></summary>
  7. - [Install](#install)
  8. - [What is nanomatch?](#what-is-nanomatch)
  9. - [Getting started](#getting-started)
  10. * [Installing nanomatch](#installing-nanomatch)
  11. * [Usage](#usage)
  12. - [Documentation](#documentation)
  13. * [Escaping](#escaping)
  14. - [API](#api)
  15. - [Options](#options)
  16. * [options.basename](#optionsbasename)
  17. * [options.bash](#optionsbash)
  18. * [options.cache](#optionscache)
  19. * [options.dot](#optionsdot)
  20. * [options.failglob](#optionsfailglob)
  21. * [options.ignore](#optionsignore)
  22. * [options.matchBase](#optionsmatchbase)
  23. * [options.nocase](#optionsnocase)
  24. * [options.nodupes](#optionsnodupes)
  25. * [options.noglobstar](#optionsnoglobstar)
  26. * [options.nonegate](#optionsnonegate)
  27. * [options.nonull](#optionsnonull)
  28. * [options.nullglob](#optionsnullglob)
  29. * [options.slash](#optionsslash)
  30. * [options.star](#optionsstar)
  31. * [options.snapdragon](#optionssnapdragon)
  32. * [options.sourcemap](#optionssourcemap)
  33. * [options.unescape](#optionsunescape)
  34. * [options.unixify](#optionsunixify)
  35. - [Features](#features)
  36. - [Bash expansion libs](#bash-expansion-libs)
  37. - [Benchmarks](#benchmarks)
  38. * [Running benchmarks](#running-benchmarks)
  39. * [Nanomatch vs. Minimatch vs. Multimatch](#nanomatch-vs-minimatch-vs-multimatch)
  40. - [About](#about)
  41. </details>
  42. ## Install
  43. Install with [npm](https://www.npmjs.com/):
  44. ```sh
  45. $ npm install --save nanomatch
  46. ```
  47. <details>
  48. <summary><strong>Release history</strong></summary>
  49. ## History
  50. ### key
  51. Changelog entries are classified using the following labels _(from [keep-a-changelog](https://github.com/olivierlacan/keep-a-changelog)_):
  52. * `added`: for new features
  53. * `changed`: for changes in existing functionality
  54. * `deprecated`: for once-stable features removed in upcoming releases
  55. * `removed`: for deprecated features removed in this release
  56. * `fixed`: for any bug fixes
  57. * `bumped`: updated dependencies, only minor or higher will be listed.
  58. ### [1.1.0](https://github.com/micromatch/nanomatch/compare/1.0.4...1.1.0) - 2017-04-11
  59. **Fixed**
  60. * adds support for unclosed quotes
  61. **Added**
  62. * adds support for `options.noglobstar`
  63. ### [1.0.4](https://github.com/micromatch/nanomatch/compare/1.0.3...1.0.4) - 2017-04-06
  64. Housekeeping updates. Adds documentation section about escaping, cleans up utils.
  65. ### [1.0.3](https://github.com/micromatch/nanomatch/compare/1.0.1...1.0.3) - 2017-04-06
  66. This release includes fixes for windows path edge cases and other improvements for stricter adherence to bash spec.
  67. **Fixed**
  68. * More windows path edge cases
  69. **Added**
  70. * Support for bash-like quoted strings for escaping sequences of characters, such as `foo/"**"/bar` where `**` should be matched literally and not evaluated as special characters.
  71. ### [1.0.1](https://github.com/micromatch/nanomatch/compare/1.0.0...1.0.1) - 2016-12-12
  72. **Added**
  73. * Support for windows path edge cases where backslashes are used in brackets or other unusual combinations.
  74. ### [1.0.0](https://github.com/micromatch/nanomatch/compare/0.1.0...1.0.0) - 2016-12-12
  75. Stable release.
  76. ### [0.1.0] - 2016-10-08
  77. First release.
  78. </details>
  79. ## What is nanomatch?
  80. Nanomatch is a fast and accurate glob matcher with full support for standard Bash glob features, including the following "metacharacters": `*`, `**`, `?` and `[...]`.
  81. **Learn more**
  82. * [Getting started](#getting-started): learn how to install and begin using nanomatch
  83. * [Features](#features): jump to info about supported patterns, and a glob matching reference
  84. * [API documentation](#api): jump to available options and methods
  85. * [Unit tests](test): visit unit tests. there is no better way to learn a code library than spending time the unit tests. Nanomatch has 36,000 unit tests - go become a glob matching ninja!
  86. <details>
  87. <summary><strong>How is this different?</strong></summary>
  88. **Speed and accuracy**
  89. Nanomatch uses [snapdragon](https://github.com/jonschlinkert/snapdragon) for parsing and compiling globs, which results in:
  90. * Granular control over the entire conversion process in a way that is easy to understand, reason about, and customize.
  91. * Faster matching, from a combination of optimized glob patterns and (optional) caching.
  92. * Much greater accuracy than minimatch. In fact, nanomatch passes _all of the spec tests_ from bash, including some that bash still fails. However, since there is no real specification for globs, if you encounter a pattern that yields unexpected match results [after researching previous issues](../../issues), [please let us know](../../issues/new).
  93. **Basic globbing only**
  94. Nanomatch supports [basic globbing only](#features), which is limited to `*`, `**`, `?` and regex-like brackets.
  95. If you need support for the other [bash "expansion" types](#bash-expansion-libs) (in addition to the wildcard matching provided by nanomatch), consider using [micromatch](https://github.com/micromatch/micromatch) instead. _(micromatch >=3.0.0 uses the nanomatch parser and compiler for basic glob matching)_
  96. </details>
  97. ## Getting started
  98. ### Installing nanomatch
  99. **Install with [yarn](https://yarnpkg.com/)**
  100. ```sh
  101. $ yarn add nanomatch
  102. ```
  103. **Install with [npm](https://npmjs.com)**
  104. ```sh
  105. $ npm install nanomatch
  106. ```
  107. ### Usage
  108. Add nanomatch to your project using node's `require()` system:
  109. ```js
  110. var nanomatch = require('nanomatch');
  111. // the main export is a function that takes an array of strings to match
  112. // and a string or array of patterns to use for matching
  113. nanomatch(list, patterns[, options]);
  114. ```
  115. **Params**
  116. * `list` **{String|Array}**: List of strings to perform matches against. This is often a list of file paths.
  117. * `patterns` **{String|Array}**: One or more [glob paterns](#features) to use for matching.
  118. * `options` **{Object}**: Any [supported options](#options) may be passed
  119. **Examples**
  120. ```js
  121. var nm = require('nanomatch');
  122. console.log(nm(['a', 'b/b', 'c/c/c'], '*'));
  123. //=> ['a']
  124. console.log(nm(['a', 'b/b', 'c/c/c'], '*/*'));
  125. //=> ['b/b']
  126. console.log(nm(['a', 'b/b', 'c/c/c'], '**'));
  127. //=> ['a', 'b/b', 'c/c/c']
  128. ```
  129. See the [API documentation](#api) for available methods and [options](https://github.com/einaros/options.js).
  130. ## Documentation
  131. ### Escaping
  132. _Backslashes and quotes_ can be used to escape characters, forcing nanomatch to regard those characters as a literal characters.
  133. **Backslashes**
  134. Use backslashes to escape single characters. For example, the following pattern would match `foo/*/bar` exactly:
  135. ```js
  136. 'foo/\*/bar'
  137. ```
  138. The following pattern would match `foo/` followed by a literal `*`, followed by zero or more of any characters besides `/`, followed by `/bar`.
  139. ```js
  140. 'foo/\**/bar'
  141. ```
  142. **Quoted strings**
  143. Use single or double quotes to escape sequences of characters. For example, the following patterns would match `foo/**/bar` exactly:
  144. ```js
  145. 'foo/"**"/bar'
  146. 'foo/\'**\'/bar'
  147. "foo/'**'/bar"
  148. ```
  149. **Matching literal quotes**
  150. If you need to match quotes literally, you can escape them as well. For example, the following will match `foo/"*"/bar`, `foo/"a"/bar`, `foo/"b"/bar`, or `foo/"c"/bar`:
  151. ```js
  152. 'foo/\\"*\\"/bar'
  153. ```
  154. And the following will match `foo/'*'/bar`, `foo/'a'/bar`, `foo/'b'/bar`, or `foo/'c'/bar`:
  155. ```js
  156. 'foo/\\\'*\\\'/bar'
  157. ```
  158. ## API
  159. ### [nanomatch](index.js#L40)
  160. The main function takes a list of strings and one or more glob patterns to use for matching.
  161. **Params**
  162. * `list` **{Array}**: A list of strings to match
  163. * `patterns` **{String|Array}**: One or more glob patterns to use for matching.
  164. * `options` **{Object}**: See available [options](#options) for changing how matches are performed
  165. * `returns` **{Array}**: Returns an array of matches
  166. **Example**
  167. ```js
  168. var nm = require('nanomatch');
  169. nm(list, patterns[, options]);
  170. console.log(nm(['a.js', 'a.txt'], ['*.js']));
  171. //=> [ 'a.js' ]
  172. ```
  173. ### [.match](index.js#L106)
  174. Similar to the main function, but `pattern` must be a string.
  175. **Params**
  176. * `list` **{Array}**: Array of strings to match
  177. * `pattern` **{String}**: Glob pattern to use for matching.
  178. * `options` **{Object}**: See available [options](#options) for changing how matches are performed
  179. * `returns` **{Array}**: Returns an array of matches
  180. **Example**
  181. ```js
  182. var nm = require('nanomatch');
  183. nm.match(list, pattern[, options]);
  184. console.log(nm.match(['a.a', 'a.aa', 'a.b', 'a.c'], '*.a'));
  185. //=> ['a.a', 'a.aa']
  186. ```
  187. ### [.isMatch](index.js#L167)
  188. Returns true if the specified `string` matches the given glob `pattern`.
  189. **Params**
  190. * `string` **{String}**: String to match
  191. * `pattern` **{String}**: Glob pattern to use for matching.
  192. * `options` **{Object}**: See available [options](#options) for changing how matches are performed
  193. * `returns` **{Boolean}**: Returns true if the string matches the glob pattern.
  194. **Example**
  195. ```js
  196. var nm = require('nanomatch');
  197. nm.isMatch(string, pattern[, options]);
  198. console.log(nm.isMatch('a.a', '*.a'));
  199. //=> true
  200. console.log(nm.isMatch('a.b', '*.a'));
  201. //=> false
  202. ```
  203. ### [.some](index.js#L205)
  204. Returns true if some of the elements in the given `list` match any of the given glob `patterns`.
  205. **Params**
  206. * `list` **{String|Array}**: The string or array of strings to test. Returns as soon as the first match is found.
  207. * `patterns` **{String|Array}**: One or more glob patterns to use for matching.
  208. * `options` **{Object}**: See available [options](#options) for changing how matches are performed
  209. * `returns` **{Boolean}**: Returns true if any patterns match `str`
  210. **Example**
  211. ```js
  212. var nm = require('nanomatch');
  213. nm.some(list, patterns[, options]);
  214. console.log(nm.some(['foo.js', 'bar.js'], ['*.js', '!foo.js']));
  215. // true
  216. console.log(nm.some(['foo.js'], ['*.js', '!foo.js']));
  217. // false
  218. ```
  219. ### [.every](index.js#L243)
  220. Returns true if every element in the given `list` matches at least one of the given glob `patterns`.
  221. **Params**
  222. * `list` **{String|Array}**: The string or array of strings to test.
  223. * `patterns` **{String|Array}**: One or more glob patterns to use for matching.
  224. * `options` **{Object}**: See available [options](#options) for changing how matches are performed
  225. * `returns` **{Boolean}**: Returns true if any patterns match `str`
  226. **Example**
  227. ```js
  228. var nm = require('nanomatch');
  229. nm.every(list, patterns[, options]);
  230. console.log(nm.every('foo.js', ['foo.js']));
  231. // true
  232. console.log(nm.every(['foo.js', 'bar.js'], ['*.js']));
  233. // true
  234. console.log(nm.every(['foo.js', 'bar.js'], ['*.js', '!foo.js']));
  235. // false
  236. console.log(nm.every(['foo.js'], ['*.js', '!foo.js']));
  237. // false
  238. ```
  239. ### [.any](index.js#L277)
  240. Returns true if **any** of the given glob `patterns` match the specified `string`.
  241. **Params**
  242. * `str` **{String|Array}**: The string to test.
  243. * `patterns` **{String|Array}**: One or more glob patterns to use for matching.
  244. * `options` **{Object}**: See available [options](#options) for changing how matches are performed
  245. * `returns` **{Boolean}**: Returns true if any patterns match `str`
  246. **Example**
  247. ```js
  248. var nm = require('nanomatch');
  249. nm.any(string, patterns[, options]);
  250. console.log(nm.any('a.a', ['b.*', '*.a']));
  251. //=> true
  252. console.log(nm.any('a.a', 'b.*'));
  253. //=> false
  254. ```
  255. ### [.all](index.js#L325)
  256. Returns true if **all** of the given `patterns` match the specified string.
  257. **Params**
  258. * `str` **{String|Array}**: The string to test.
  259. * `patterns` **{String|Array}**: One or more glob patterns to use for matching.
  260. * `options` **{Object}**: See available [options](#options) for changing how matches are performed
  261. * `returns` **{Boolean}**: Returns true if any patterns match `str`
  262. **Example**
  263. ```js
  264. var nm = require('nanomatch');
  265. nm.all(string, patterns[, options]);
  266. console.log(nm.all('foo.js', ['foo.js']));
  267. // true
  268. console.log(nm.all('foo.js', ['*.js', '!foo.js']));
  269. // false
  270. console.log(nm.all('foo.js', ['*.js', 'foo.js']));
  271. // true
  272. console.log(nm.all('foo.js', ['*.js', 'f*', '*o*', '*o.js']));
  273. // true
  274. ```
  275. ### [.not](index.js#L359)
  276. Returns a list of strings that _**do not match any**_ of the given `patterns`.
  277. **Params**
  278. * `list` **{Array}**: Array of strings to match.
  279. * `patterns` **{String|Array}**: One or more glob pattern to use for matching.
  280. * `options` **{Object}**: See available [options](#options) for changing how matches are performed
  281. * `returns` **{Array}**: Returns an array of strings that **do not match** the given patterns.
  282. **Example**
  283. ```js
  284. var nm = require('nanomatch');
  285. nm.not(list, patterns[, options]);
  286. console.log(nm.not(['a.a', 'b.b', 'c.c'], '*.a'));
  287. //=> ['b.b', 'c.c']
  288. ```
  289. ### [.contains](index.js#L394)
  290. Returns true if the given `string` contains the given pattern. Similar to [.isMatch](#isMatch) but the pattern can match any part of the string.
  291. **Params**
  292. * `str` **{String}**: The string to match.
  293. * `patterns` **{String|Array}**: Glob pattern to use for matching.
  294. * `options` **{Object}**: See available [options](#options) for changing how matches are performed
  295. * `returns` **{Boolean}**: Returns true if the patter matches any part of `str`.
  296. **Example**
  297. ```js
  298. var nm = require('nanomatch');
  299. nm.contains(string, pattern[, options]);
  300. console.log(nm.contains('aa/bb/cc', '*b'));
  301. //=> true
  302. console.log(nm.contains('aa/bb/cc', '*d'));
  303. //=> false
  304. ```
  305. ### [.matchKeys](index.js#L450)
  306. Filter the keys of the given object with the given `glob` pattern and `options`. Does not attempt to match nested keys. If you need this feature, use [glob-object](https://github.com/jonschlinkert/glob-object) instead.
  307. **Params**
  308. * `object` **{Object}**: The object with keys to filter.
  309. * `patterns` **{String|Array}**: One or more glob patterns to use for matching.
  310. * `options` **{Object}**: See available [options](#options) for changing how matches are performed
  311. * `returns` **{Object}**: Returns an object with only keys that match the given patterns.
  312. **Example**
  313. ```js
  314. var nm = require('nanomatch');
  315. nm.matchKeys(object, patterns[, options]);
  316. var obj = { aa: 'a', ab: 'b', ac: 'c' };
  317. console.log(nm.matchKeys(obj, '*b'));
  318. //=> { ab: 'b' }
  319. ```
  320. ### [.matcher](index.js#L479)
  321. Returns a memoized matcher function from the given glob `pattern` and `options`. The returned function takes a string to match as its only argument and returns true if the string is a match.
  322. **Params**
  323. * `pattern` **{String}**: Glob pattern
  324. * `options` **{Object}**: See available [options](#options) for changing how matches are performed.
  325. * `returns` **{Function}**: Returns a matcher function.
  326. **Example**
  327. ```js
  328. var nm = require('nanomatch');
  329. nm.matcher(pattern[, options]);
  330. var isMatch = nm.matcher('*.!(*a)');
  331. console.log(isMatch('a.a'));
  332. //=> false
  333. console.log(isMatch('a.b'));
  334. //=> true
  335. ```
  336. ### [.capture](index.js#L560)
  337. Returns an array of matches captured by `pattern` in `string, or`null` if the pattern did not match.
  338. **Params**
  339. * `pattern` **{String}**: Glob pattern to use for matching.
  340. * `string` **{String}**: String to match
  341. * `options` **{Object}**: See available [options](#options) for changing how matches are performed
  342. * `returns` **{Boolean}**: Returns an array of captures if the string matches the glob pattern, otherwise `null`.
  343. **Example**
  344. ```js
  345. var nm = require('nanomatch');
  346. nm.capture(pattern, string[, options]);
  347. console.log(nm.capture('test/*.js', 'test/foo.js'));
  348. //=> ['foo']
  349. console.log(nm.capture('test/*.js', 'foo/bar.css'));
  350. //=> null
  351. ```
  352. ### [.makeRe](index.js#L595)
  353. Create a regular expression from the given glob `pattern`.
  354. **Params**
  355. * `pattern` **{String}**: A glob pattern to convert to regex.
  356. * `options` **{Object}**: See available [options](#options) for changing how matches are performed.
  357. * `returns` **{RegExp}**: Returns a regex created from the given pattern.
  358. **Example**
  359. ```js
  360. var nm = require('nanomatch');
  361. nm.makeRe(pattern[, options]);
  362. console.log(nm.makeRe('*.js'));
  363. //=> /^(?:(\.[\\\/])?(?!\.)(?=.)[^\/]*?\.js)$/
  364. ```
  365. ### [.create](index.js#L658)
  366. Parses the given glob `pattern` and returns an object with the compiled `output` and optional source `map`.
  367. **Params**
  368. * `pattern` **{String}**: Glob pattern to parse and compile.
  369. * `options` **{Object}**: Any [options](#options) to change how parsing and compiling is performed.
  370. * `returns` **{Object}**: Returns an object with the parsed AST, compiled string and optional source map.
  371. **Example**
  372. ```js
  373. var nm = require('nanomatch');
  374. nm.create(pattern[, options]);
  375. console.log(nm.create('abc/*.js'));
  376. // { options: { source: 'string', sourcemap: true },
  377. // state: {},
  378. // compilers:
  379. // { ... },
  380. // output: '(\\.[\\\\\\/])?abc\\/(?!\\.)(?=.)[^\\/]*?\\.js',
  381. // ast:
  382. // { type: 'root',
  383. // errors: [],
  384. // nodes:
  385. // [ ... ],
  386. // dot: false,
  387. // input: 'abc/*.js' },
  388. // parsingErrors: [],
  389. // map:
  390. // { version: 3,
  391. // sources: [ 'string' ],
  392. // names: [],
  393. // mappings: 'AAAA,GAAG,EAAC,kBAAC,EAAC,EAAE',
  394. // sourcesContent: [ 'abc/*.js' ] },
  395. // position: { line: 1, column: 28 },
  396. // content: {},
  397. // files: {},
  398. // idx: 6 }
  399. ```
  400. ### [.parse](index.js#L697)
  401. Parse the given `str` with the given `options`.
  402. **Params**
  403. * `str` **{String}**
  404. * `options` **{Object}**
  405. * `returns` **{Object}**: Returns an AST
  406. **Example**
  407. ```js
  408. var nm = require('nanomatch');
  409. nm.parse(pattern[, options]);
  410. var ast = nm.parse('a/{b,c}/d');
  411. console.log(ast);
  412. // { type: 'root',
  413. // errors: [],
  414. // input: 'a/{b,c}/d',
  415. // nodes:
  416. // [ { type: 'bos', val: '' },
  417. // { type: 'text', val: 'a/' },
  418. // { type: 'brace',
  419. // nodes:
  420. // [ { type: 'brace.open', val: '{' },
  421. // { type: 'text', val: 'b,c' },
  422. // { type: 'brace.close', val: '}' } ] },
  423. // { type: 'text', val: '/d' },
  424. // { type: 'eos', val: '' } ] }
  425. ```
  426. ### [.compile](index.js#L745)
  427. Compile the given `ast` or string with the given `options`.
  428. **Params**
  429. * `ast` **{Object|String}**
  430. * `options` **{Object}**
  431. * `returns` **{Object}**: Returns an object that has an `output` property with the compiled string.
  432. **Example**
  433. ```js
  434. var nm = require('nanomatch');
  435. nm.compile(ast[, options]);
  436. var ast = nm.parse('a/{b,c}/d');
  437. console.log(nm.compile(ast));
  438. // { options: { source: 'string' },
  439. // state: {},
  440. // compilers:
  441. // { eos: [Function],
  442. // noop: [Function],
  443. // bos: [Function],
  444. // brace: [Function],
  445. // 'brace.open': [Function],
  446. // text: [Function],
  447. // 'brace.close': [Function] },
  448. // output: [ 'a/(b|c)/d' ],
  449. // ast:
  450. // { ... },
  451. // parsingErrors: [] }
  452. ```
  453. ### [.clearCache](index.js#L768)
  454. Clear the regex cache.
  455. **Example**
  456. ```js
  457. nm.clearCache();
  458. ```
  459. ## Options
  460. <details>
  461. <summary><strong>basename</strong></summary>
  462. ### options.basename
  463. Allow glob patterns without slashes to match a file path based on its basename. Same behavior as [minimatch](https://github.com/isaacs/minimatch) option `matchBase`.
  464. Type: `boolean`
  465. Default: `false`
  466. **Example**
  467. ```js
  468. nm(['a/b.js', 'a/c.md'], '*.js');
  469. //=> []
  470. nm(['a/b.js', 'a/c.md'], '*.js', {matchBase: true});
  471. //=> ['a/b.js']
  472. ```
  473. </details>
  474. <details>
  475. <summary><strong>bash</strong></summary>
  476. ### options.bash
  477. Enabled by default, this option enforces bash-like behavior with stars immediately following a bracket expression. Bash bracket expressions are similar to regex character classes, but unlike regex, a star following a bracket expression **does not repeat the bracketed characters**. Instead, the star is treated the same as an other star.
  478. Type: `boolean`
  479. Default: `true`
  480. **Example**
  481. ```js
  482. var files = ['abc', 'ajz'];
  483. console.log(nm(files, '[a-c]*'));
  484. //=> ['abc', 'ajz']
  485. console.log(nm(files, '[a-c]*', {bash: false}));
  486. ```
  487. </details>
  488. <details>
  489. <summary><strong>cache</strong></summary>
  490. ### options.cache
  491. Disable regex and function memoization.
  492. Type: `boolean`
  493. Default: `undefined`
  494. </details>
  495. <details>
  496. <summary><strong>dot</strong></summary>
  497. ### options.dot
  498. Match dotfiles. Same behavior as [minimatch](https://github.com/isaacs/minimatch) option `dot`.
  499. Type: `boolean`
  500. Default: `false`
  501. </details>
  502. <details>
  503. <summary><strong>failglob</strong></summary>
  504. ### options.failglob
  505. Similar to the `--failglob` behavior in Bash, throws an error when no matches are found.
  506. Type: `boolean`
  507. Default: `undefined`
  508. </details>
  509. <details>
  510. <summary><strong>ignore</strong></summary>
  511. ### options.ignore
  512. String or array of glob patterns to match files to ignore.
  513. Type: `String|Array`
  514. Default: `undefined`
  515. </details>
  516. <details>
  517. <summary><strong>matchBase</strong></summary>
  518. ### options.matchBase
  519. Alias for [options.basename](#options-basename).
  520. </details>
  521. <details>
  522. <summary><strong>nocase</strong></summary>
  523. ### options.nocase
  524. Use a case-insensitive regex for matching files. Same behavior as [minimatch](https://github.com/isaacs/minimatch).
  525. Type: `boolean`
  526. Default: `undefined`
  527. </details>
  528. <details>
  529. <summary><strong>nodupes</strong></summary>
  530. ### options.nodupes
  531. Remove duplicate elements from the result array.
  532. Type: `boolean`
  533. Default: `true` (enabled by default)
  534. **Example**
  535. Example of using the `unescape` and `nodupes` options together:
  536. ```js
  537. nm.match(['a/b/c', 'a/b/c'], '**');
  538. //=> ['abc']
  539. nm.match(['a/b/c', 'a/b/c'], '**', {nodupes: false});
  540. //=> ['a/b/c', 'a/b/c']
  541. ```
  542. </details>
  543. <details>
  544. <summary><strong>nonegate</strong></summary>
  545. ### options.noglobstar
  546. Disable matching with globstars (`**`).
  547. Type: `boolean`
  548. Default: `undefined`
  549. ```js
  550. nm(['a/b', 'a/b/c', 'a/b/c/d'], 'a/**');
  551. //=> ['a/b', 'a/b/c', 'a/b/c/d']
  552. nm(['a/b', 'a/b/c', 'a/b/c/d'], 'a/**', {noglobstar: true});
  553. //=> ['a/b']
  554. ```
  555. </details>
  556. <details>
  557. <summary><strong>nonegate</strong></summary>
  558. ### options.nonegate
  559. Disallow negation (`!`) patterns, and treat leading `!` as a literal character to match.
  560. Type: `boolean`
  561. Default: `undefined`
  562. </details>
  563. <details>
  564. <summary><strong>nonull</strong></summary>
  565. ### options.nonull
  566. Alias for [options.nullglob](#options-nullglob).
  567. </details>
  568. <details>
  569. <summary><strong>nullglob</strong></summary>
  570. ### options.nullglob
  571. If `true`, when no matches are found the actual (arrayified) glob pattern is returned instead of an empty array. Same behavior as [minimatch](https://github.com/isaacs/minimatch) option `nonull`.
  572. Type: `boolean`
  573. Default: `undefined`
  574. </details>
  575. <details>
  576. <summary><strong><a name="slash">slash</a></strong></summary>
  577. ### options.slash
  578. Customize the slash character(s) to use for matching.
  579. Type: `string|function`
  580. Default: `[/\\]` (forward slash and backslash)
  581. </details>
  582. <details>
  583. <summary><strong><a name="star">star</a></strong></summary>
  584. ### options.star
  585. Customize the star character(s) to use for matching. It's not recommended that you modify this unless you have advanced knowledge of the compiler and matching rules.
  586. Type: `string|function`
  587. Default: `[^/\\]*?`
  588. </details>
  589. <details>
  590. <summary><strong><a name="snapdragon">snapdragon</a></strong></summary>
  591. ### options.snapdragon
  592. Pass your own instance of [snapdragon](https://github.com/jonschlinkert/snapdragon) to customize parsers or compilers.
  593. Type: `object`
  594. Default: `undefined`
  595. </details>
  596. <details>
  597. <summary><strong>snapdragon</strong></summary>
  598. ### options.sourcemap
  599. Generate a source map by enabling the `sourcemap` option with the `.parse`, `.compile`, or `.create` methods.
  600. **Examples**
  601. ```js
  602. var nm = require('nanomatch');
  603. var res = nm.create('abc/*.js', {sourcemap: true});
  604. console.log(res.map);
  605. // { version: 3,
  606. // sources: [ 'string' ],
  607. // names: [],
  608. // mappings: 'AAAA,GAAG,EAAC,iBAAC,EAAC,EAAE',
  609. // sourcesContent: [ 'abc/*.js' ] }
  610. var ast = nm.parse('abc/**/*.js');
  611. var res = nm.compile(ast, {sourcemap: true});
  612. console.log(res.map);
  613. // { version: 3,
  614. // sources: [ 'string' ],
  615. // names: [],
  616. // mappings: 'AAAA,GAAG,EAAC,2BAAE,EAAC,iBAAC,EAAC,EAAE',
  617. // sourcesContent: [ 'abc/**/*.js' ] }
  618. ```
  619. </details>
  620. <details>
  621. <summary><strong>unescape</strong></summary>
  622. ### options.unescape
  623. Remove backslashes from returned matches.
  624. Type: `boolean`
  625. Default: `undefined`
  626. **Example**
  627. In this example we want to match a literal `*`:
  628. ```js
  629. nm.match(['abc', 'a\\*c'], 'a\\*c');
  630. //=> ['a\\*c']
  631. nm.match(['abc', 'a\\*c'], 'a\\*c', {unescape: true});
  632. //=> ['a*c']
  633. ```
  634. </details>
  635. <details>
  636. <summary><strong>unixify</strong></summary>
  637. ### options.unixify
  638. Convert path separators on returned files to posix/unix-style forward slashes.
  639. Type: `boolean`
  640. Default: `true`
  641. **Example**
  642. ```js
  643. nm.match(['a\\b\\c'], 'a/**');
  644. //=> ['a/b/c']
  645. nm.match(['a\\b\\c'], {unixify: false});
  646. //=> ['a\\b\\c']
  647. ```
  648. </details>
  649. ## Features
  650. Nanomatch has full support for standard Bash glob features, including the following "metacharacters": `*`, `**`, `?` and `[...]`.
  651. Here are some examples of how they work:
  652. | **Pattern** | **Description** |
  653. | --- | --- |
  654. | `*` | Matches any string except for `/`, leading `.`, or `/.` inside a path |
  655. | `**` | Matches any string including `/`, but not a leading `.` or `/.` inside a path. More than two stars (e.g. `***` is treated the same as one star, and `**` loses its special meaning | when it's not the only thing in a path segment, per Bash specifications) |
  656. | `foo*` | Matches any string beginning with `foo` |
  657. | `*bar*` | Matches any string containing `bar` (beginning, middle or end) |
  658. | `*.min.js` | Matches any string ending with `.min.js` |
  659. | `[abc]*.js` | Matches any string beginning with `a`, `b`, or `c` and ending with `.js` |
  660. | `abc?` | Matches `abcd` or `abcz` but not `abcde` |
  661. The exceptions noted for `*` apply to all patterns that contain a `*`.
  662. **Not supported**
  663. The following extended-globbing features are not supported:
  664. * [brace expansion](https://github.com/jonschlinkert/braces) (e.g. `{a,b,c}`)
  665. * [extglobs](https://github.com/jonschlinkert/extglob) (e.g. `@(a|!(c|d))`)
  666. * [POSIX brackets](https://github.com/jonschlinkert/expand-brackets) (e.g. `[[:alpha:][:digit:]]`)
  667. If you need any of these features consider using [micromatch](https://github.com/micromatch/micromatch) instead.
  668. ## Bash expansion libs
  669. Nanomatch is part of a suite of libraries aimed at bringing the power and expressiveness of [Bash's](https://www.gnu.org/software/bash/) matching and expansion capabilities to JavaScript, _and - as you can see by the [benchmarks](#benchmarks) - without sacrificing speed_.
  670. | **Related library** | **Matching Type** | **Example** | **Description** |
  671. | --- | --- | --- | --- |
  672. | `nanomatch` (you are here) | Wildcards | `*` | [Filename expansion](https://www.gnu.org/software/bash/manual/html_node/Filename-Expansion.html#Filename-Expansion), also referred to as globbing and pathname expansion, allows the use of [wildcards](#features) for matching. |
  673. | [expand-tilde](https://github.com/jonschlinkert/expand-tilde) | Tildes | `~` | [Tilde expansion](https://www.gnu.org/software/bash/manual/html_node/Tilde-Expansion.html#Tilde-Expansion) converts the leading tilde in a file path to the user home directory. |
  674. | [braces](https://github.com/jonschlinkert/braces) | Braces | `{a,b,c}` | [Brace expansion](https://www.gnu.org/software/bash/manual/html_node/Brace-Expansion.html) |
  675. | [expand-brackets](https://github.com/jonschlinkert/expand-brackets) | Brackets | `[[:alpha:]]` | [POSIX character classes](https://www.gnu.org/software/grep/manual/html_node/Character-Classes-and-Bracket-Expressions.html) (also referred to as POSIX brackets, or POSIX character classes) |
  676. | [extglob](https://github.com/jonschlinkert/extglob) | Parens | `!(a\ | b)` | [Extglobs](https://www.gnu.org/software/bash/manual/html_node/Pattern-Matching.html#Pattern-Matching) |
  677. | [micromatch](https://github.com/micromatch/micromatch) | All | all | Micromatch is built on top of the other libraries. |
  678. There are many resources available on the web if you want to dive deeper into how these features work in Bash.
  679. ## Benchmarks
  680. ### Running benchmarks
  681. Install dev dependencies:
  682. ```bash
  683. npm i -d && node benchmark
  684. ```
  685. ### Nanomatch vs. Minimatch vs. Multimatch
  686. ```bash
  687. # globstar-basic (182 bytes)
  688. minimatch x 69,512 ops/sec ±1.92% (88 runs sampled)
  689. multimatch x 63,376 ops/sec ±1.41% (89 runs sampled)
  690. nanomatch x 432,451 ops/sec ±0.92% (88 runs sampled)
  691. fastest is nanomatch (by 651% avg)
  692. # large-list-globstar (485686 bytes)
  693. minimatch x 34.02 ops/sec ±1.42% (59 runs sampled)
  694. multimatch x 33.58 ops/sec ±1.97% (58 runs sampled)
  695. nanomatch x 483 ops/sec ±1.06% (86 runs sampled)
  696. fastest is nanomatch (by 1429% avg)
  697. # long-list-globstar (194085 bytes)
  698. minimatch x 383 ops/sec ±0.74% (90 runs sampled)
  699. multimatch x 378 ops/sec ±0.59% (89 runs sampled)
  700. nanomatch x 990 ops/sec ±1.14% (85 runs sampled)
  701. fastest is nanomatch (by 260% avg)
  702. # negation-basic (132 bytes)
  703. minimatch x 242,145 ops/sec ±1.17% (89 runs sampled)
  704. multimatch x 76,403 ops/sec ±0.78% (92 runs sampled)
  705. nanomatch x 537,253 ops/sec ±1.44% (86 runs sampled)
  706. fastest is nanomatch (by 337% avg)
  707. # not-glob-basic (93 bytes)
  708. minimatch x 252,402 ops/sec ±1.33% (89 runs sampled)
  709. multimatch x 209,954 ops/sec ±1.30% (90 runs sampled)
  710. nanomatch x 1,716,468 ops/sec ±1.13% (86 runs sampled)
  711. fastest is nanomatch (by 742% avg)
  712. # star-basic (93 bytes)
  713. minimatch x 182,780 ops/sec ±1.41% (91 runs sampled)
  714. multimatch x 153,210 ops/sec ±0.72% (89 runs sampled)
  715. nanomatch x 599,621 ops/sec ±1.22% (90 runs sampled)
  716. fastest is nanomatch (by 357% avg)
  717. ```
  718. ## About
  719. <details>
  720. <summary><strong>Contributing</strong></summary>
  721. Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new).
  722. Please read the [contributing guide](.github/contributing.md) for advice on opening issues, pull requests, and coding standards.
  723. </details>
  724. <details>
  725. <summary><strong>Running Tests</strong></summary>
  726. 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:
  727. ```sh
  728. $ npm install && npm test
  729. ```
  730. </details>
  731. <details>
  732. <summary><strong>Building docs</strong></summary>
  733. _(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.)_
  734. To generate the readme, run the following command:
  735. ```sh
  736. $ npm install -g verbose/verb#dev verb-generate-readme && verb
  737. ```
  738. </details>
  739. ### Related projects
  740. You might also be interested in these projects:
  741. * [extglob](https://www.npmjs.com/package/extglob): Extended glob support for JavaScript. Adds (almost) the expressive power of regular expressions to glob… [more](https://github.com/micromatch/extglob) | [homepage](https://github.com/micromatch/extglob "Extended glob support for JavaScript. Adds (almost) the expressive power of regular expressions to glob patterns.")
  742. * [is-extglob](https://www.npmjs.com/package/is-extglob): Returns true if a string has an extglob. | [homepage](https://github.com/jonschlinkert/is-extglob "Returns true if a string has an extglob.")
  743. * [is-glob](https://www.npmjs.com/package/is-glob): Returns `true` if the given string looks like a glob pattern or an extglob pattern… [more](https://github.com/jonschlinkert/is-glob) | [homepage](https://github.com/jonschlinkert/is-glob "Returns `true` if the given string looks like a glob pattern or an extglob pattern. This makes it easy to create code that only uses external modules like node-glob when necessary, resulting in much faster code execution and initialization time, and a bet")
  744. * [micromatch](https://www.npmjs.com/package/micromatch): Glob matching for javascript/node.js. A drop-in replacement and faster alternative to minimatch and multimatch. | [homepage](https://github.com/micromatch/micromatch "Glob matching for javascript/node.js. A drop-in replacement and faster alternative to minimatch and multimatch.")
  745. ### Contributors
  746. | **Commits** | **Contributor** |
  747. | --- | --- |
  748. | 164 | [jonschlinkert](https://github.com/jonschlinkert) |
  749. | 1 | [devongovett](https://github.com/devongovett) |
  750. ### Author
  751. **Jon Schlinkert**
  752. * [linkedin/in/jonschlinkert](https://linkedin.com/in/jonschlinkert)
  753. * [github/jonschlinkert](https://github.com/jonschlinkert)
  754. * [twitter/jonschlinkert](https://twitter.com/jonschlinkert)
  755. ### License
  756. Copyright © 2018, [Jon Schlinkert](https://github.com/jonschlinkert).
  757. Released under the [MIT License](LICENSE).
  758. ***
  759. _This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.6.0, on February 18, 2018._