背景:
git提交代码时需要验证新增的文件名称是否复合规范。具体规范如下:
全部采用小写方式, 以中划线分隔,用 kebab-case方式 (小写、短横线连接方式) 正例:
scripts / styles / components / images / utils / layouts / demo-styles / demo-scripts / img / doc
反例:script / style / demo_scripts / demoStyles / imgs / docs
【特殊】VUE 的项目中的 components 中的组件目录使用PascalCase格式(与驼峰法相似,单词大写开头)
因此需要验证普通文件名是否符合中划线命名法(kebab-case)以及vvue的component下的文件夹 大驼峰命名法(CamelCase)
中划线命名法(kebab-case):
思路:
1、排除文件名有空格
2、排除大写字母
3、连接方式为其他方式比如:—— _ . 等(反推:命名中只能有小写字母和-)
4、排除关键字:script style imgs docs等等
结论:
1、限定以小写字母开头,并且中间只包括小写字母和-,以小写字母结尾:/^[a-z]{1}[a-z-]+[a-z]$/。
2、排除关键字。
// 关键字
const KEYWORDS = ["script","style","imgs","docs","break","else","new","var","case","finally","return","void","catch","for","switch","while","continue","function","this","with","default","if","throw","delete","in","try","do","instranceof","typeof","abstract","enum","int","short","boolean","export","interface","static","byte","extends","long","super","char","final","native","synchronized","class","float","package","throws","const","goto","private ","transient","debugger","implements","protected ","volatile","double","import","public"]
// 正则:限定以小写字母开头,并且中间只包括小写字母和-,以小写字母结尾
let reg = /^[a-z]{1}[a-z-]*?[a-z]$/;
// /^[a-z]{1}[a-z-0-9]*?[a-z0-9]$/ (这个是限定以小写字母开头,中间小写or数字,结尾数字。两位数的情况下中间可以省略)
// 要判断的文件名
let fileName = `xxxxx`
if(KEYWORDS.indexOf(fileName) > -1){
// 文件名是关键字
}else if( reg.test(fileName) ){ // 判断:限定以小写字母开头,并且中间只包括小写字母和-,以小写字母结尾
// 符合要求
}else{
// 不符合要求
}
大驼峰命名法(CamelCase):
// 正则:限定以大写字母开头,并且中间只包括大写or小写字母,以小写字母结尾
let reg = /^[A-Z]{1}[a-zA-Z]*?[a-z]$/;
// 关键字
const KEYWORDS = ["script","style","imgs","docs","break","else","new","var","case","finally","return","void","catch","for","switch","while","continue","function","this","with","default","if","throw","delete","in","try","do","instranceof","typeof","abstract","enum","int","short","boolean","export","interface","static","byte","extends","long","super","char","final","native","synchronized","class","float","package","throws","const","goto","private ","transient","debugger","implements","protected ","volatile","double","import","public"]
// 大写首字母字母的关键字
let up_keywords = KEYWORDS.map(item=>{
return item.charAt(0).toUpperCase() + item.slice(1);
})
// 要判断的文件名
let fileName = `xxxxx`
if(up_keywords.indexOf(fileName) > -1){
// 文件名是关键字
}else if( reg.test(fileName) ){ // 判断:限定以小写字母开头,并且中间只包括小写字母和-,以小写字母结尾
// 符合要求
}else{
// 不符合要求
}
小驼峰命名法(lowerCamelCase):
// 正则:限定以大写字母开头,并且中间只包括大写or小写字母,以小写字母结尾
let reg = /^[a-z]{1}[a-zA-Z]*?[a-z]$/;
// 关键字
const KEYWORDS = ["script","style","imgs","docs","break","else","new","var","case","finally","return","void","catch","for","switch","while","continue","function","this","with","default","if","throw","delete","in","try","do","instranceof","typeof","abstract","enum","int","short","boolean","export","interface","static","byte","extends","long","super","char","final","native","synchronized","class","float","package","throws","const","goto","private ","transient","debugger","implements","protected ","volatile","double","import","public"]
// 要判断的文件名
let fileName = `xxxxx`
if(KEYWORDS .indexOf(fileName) > -1){
// 文件名是关键字
}else if( reg.test(fileName) ){ // 判断:限定以小写字母开头,并且中间只包括小写字母和-,以小写字母结尾
// 符合要求
}else{
// 不符合要求
}