企业🤖AI Agent构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
>minimatch模块:文件匹配工具;node-glob模块依赖;node-glob模块又被众多知名的构建工具如:fis3 glup,grunt等依赖; >匹配规则 * 匹配任意数量的字符,但不匹配/ ? 匹配单个字符,但不匹配/ ** 匹配任意数量的字符,包括/,只要它是路径中唯一的一部分 {} 允许使用一个逗号分割的列表或者表达式 ! 在模式的开头用于否定一个匹配模式(即排除与模式匹配的信 息) >构造函数:Minimatch(pattern,options) var minimatch = require('minimatch') var Class = minimatch.Minimacth var pattern = 'a/{c,d}/*.js'; var options = { nocomment:true, }; minimatch.defaults(options); var match = new Match(pattern); var keys = ['pattern','options'] keys.forEach(function (key,index){ console.log('\n') console.log(key+':',match[key]) }) >实例属性说明 * pattern: 当前模式,在实例化时定义; * options: 配置项;两种方式定义 var minimatch = require('minimatch'); var options = { nonull:true }; var pattren = 'a/{b,c/d}/.js'; //--第一种方式 var Class = minimatch.Minimatch; var match = new Class(pattern,options); console.log(match.options); //---第二种方式 var Class = minimatch.defaults(options).Minimatch; var match = new Class(pattern,{nocomment:true}); console.log(match.options); //---注意事项,一下方式将不会定义options var Class = minimatch.Minimatch; minimatch.defaults(options); var match = new Class(pattren); console.log(match.options); * set:根据匹配模式生成的数组项,通过‘/’来分割; var pattern = 'a/{b,c}/d'; var match = new Class(pattern); console.log(match.set); //---输出: [ [ 'a', 'b', 'd' ], //--'a/b/d'; [ 'a', 'c', 'd' ] //--'a/c/d' ] * regexp: 由pattren生成的正则,需要调用实例的makeRe()后才生成 var pattren = 'a/{b,c}/d'; var match = new Match(pattern); console.log(match.regexp);//---输出null match.makeRe(); console.log(match.regexp);//--输出正则/^(?:a\/b\/d|a\/c\/d)$/ * comment: 是否是注释;当pattern的以'#'开始时为true;此时set,regexp属性不会生成; var pattern = '#a/{b,c}/d'; var match = new Class(pattern); console.log(match.comment) //--true * negate:模式取反 var pattern = '!a/{b,c}/d'; var match = new Class(pattern); console.log(match.negate) //--输出true * empty: pattern是否为空; var pattern = ''; var match = new Class(pattern); console.log(match.empty) //--输出true; >实例方法: * match(filename) : 是否匹配pattren; var minimatch = require('minimatch') var Class = minimatch.Minimatch; var pattern = 'a/{b,c}/*.js'; var match = new Class(pattern); var files = ['a/b/app.js','a/b/c/app.js','a/c/home.js']; files.forEach(function (file,index){ var isok = minimatch.match(file); console.log(isok,file,index); }) //--输出 true 'a/b/app.js' 0 false 'a/b/c/app.js' 1 true 'a/c/home.js' 2 * makeRe(): 生成pattern的正则式,不需要传入参数 var minimatch = require('minimatch') var Class = minimatch.Minimatch; var pattern = 'a/{b,c}/*.js'; var match = new Class(pattern); match.makeRe(); console.log(match.regexp) //--输出 /^(?:a\/b\/(?!\.)(?=.)[^\/]*?\.js|a\/c\/(?!\.)(?=.)[^\/]*?\.js)$/ >模块静态方法,即挂在载minimatch身上的方法: * match(fileList,pattern,option):匹配文件;返回匹配的file项 var minimatch = require('minimatch') var pattern = 'a/{b,c}/*.js'; var files=[ 'home.js', 'a/b/app.js', 'a/c/home.js', ] var results = minimatch.match(files,pattern); console.log(results) //--输出 [ 'a/b/app.js', 'a/c/home.js' ] * makeRe(pattern,options) : 生成pattren的正则表达式 var minimatch = require('minimatch') var pattern = 'a/{b,c}/*.js'; var results = minimatch.makeRe(pattern); console.log(results) //---输出 /^(?:a\/b\/(?!\.)(?=.)[^\/]*?\.js|a\/c\/(?!\.)(?=.)[^\/]*?\.js) * filter(pattren,options) : 返回一个函数,函数接受文件参数,执行文件是否满足pattern; var minimatch = require('minimatch') var pattern = 'a/{b,c}/*.js'; var files=[ 'home.js', 'a/b/app.js', 'a/c/home.js', ] var match = minimatch.filter(pattern) var results = files.filter(match) console.log(results); //---输出 [ 'a/b/app.js', 'a/c/home.js' ] * minimatch(file,pattern,options):file是否满足pattern;返回boolean; var minimatch = require('minimatch') var pattern = 'a/{b,c}/*.js'; var files=[ 'home.js', 'a/b/app.js', 'a/c/home.js', ] var results = files.map(function (file){ return minimatch(file,pattern) }); console.log(results) //---输出 [ false, true, true ] >配置参数options;所有配置项默认为false; * nonull : 在minimatch.match没有匹配到文件时,默认返回[]空数组;设置为true后,则返回[pattern]; var minimatch = require('minimatch') var pattern = '!a/{b,c}/*.js'; var options = { nonull:true, } var files=[ 'a/b/app.js', 'a/c/home.js', ] var results = minimatch.match(files,pattern,options) console.log(results) //--输出[ '!a/{b,c}/*.js' ]