NamingConvention [命名规则]
# 介绍
程序设计的中命名规则(naming convention)是电脑程序设计的源代码针对标识符的名称字符串进行定义(即 “命名”)而规定的一系列规则。通常是为了提高源代码的易读性、易认性、程序效率以及可维护性。命名规则根据各个程序语言的规格、内存大小等硬件制约、编辑器以及集成开发环境的功能等等会有各种制约。
# camelCase
检查是否是驼峰写法。驼峰式大小写(Camel-Case,Camel Case,camel case),计算机程序编写时的一套命名规则(惯例)。当变量名和函数名称是由二个或多个单字连结在一起,而构成的唯一识别字时,利用 “驼峰式大小写” 来表示,可以增加变数和函式的可读性。
检查 camelCase:
// CheckCamelCase method checks the given string is in camelCase or not.
// Problem Source & Explanation: https://en.wikipedia.org/wiki/Camel_case
/**
* checkCamelCase method returns true if the string in camelCase, else return the false.
* @param {String} varName the name of the variable to check.
* @returns `Boolean` return true if the string is in camelCase, else return false.
*/
const checkCamelCase = (varName) => {
// firstly, check that input is a string or not.
if (typeof varName !== 'string') {
throw new TypeError('Argument is not a string.')
}
const pat = /^[a-z][A-Za-z]*$/
return pat.test(varName)
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# flatcase
检查 flatcase:
// checkFlatCase method checks if the given string is in flatcase or not. Flatcase is a convention
// where all letters are in lowercase, and there are no spaces between words.
// thisvariable is an example of flatcase. In camelCase it would be thisVariable, snake_case this_variable and so on.
// Problem Source & Explanation: https://en.wikipedia.org/wiki/Naming_convention_(programming)
/**
* checkFlatCase method returns true if the string in flatcase, else return the false.
* @param {string} varname the name of the variable to check.
* @returns {boolean} return true if the string is in flatcase, else return false.
*/
const checkFlatCase = (varname) => {
// firstly, check that input is a string or not.
if (typeof varname !== 'string') {
return new TypeError('Argument is not a string.')
}
const pat = /^[a-z]*$/
return pat.test(varname)
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# kebab-case
检查 kebab-case:
// CheckKebabCase method checks the given string is in kebab-case or not.
// Problem Source & Explanation: https://en.wikipedia.org/wiki/Naming_convention_(programming)
/**
* CheckKebabCase method returns true if the string in kebab-case, else return the false.
* @param {String} varName the name of the variable to check.
* @returns `Boolean` return true if the string is in kebab-case, else return false.
*/
const CheckKebabCase = (varName) => {
// firstly, check that input is a string or not.
if (typeof varName !== 'string') {
return new TypeError('Argument is not a string.')
}
const pat = /(\w+)-(\w)([\w-]*)/
return pat.test(varName) && !varName.includes('_')
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# snake_case
检查 snake_case:
// CheckSnakeCase method checks the given string is in snake_case or not.
// Problem Source & Explanation: https://en.wikipedia.org/wiki/Naming_convention_(programming)
/**
* checkSnakeCase method returns true if the string in snake_case, else return the false.
* @param {String} varName the name of the variable to check.
* @returns `Boolean` return true if the string is in snake_case, else return false.
*/
const checkSnakeCase = (varName) => {
// firstly, check that input is a string or not.
if (typeof varName !== 'string') {
throw new TypeError('Argument is not a string.')
}
const pat = /(.*?)_([a-zA-Z])*/
return pat.test(varName)
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# PascalCase
检查 PascalCase:
// CheckPascalCase method checks the given string is in PascalCase or not.
// Problem Source & Explanation: https://www.theserverside.com/definition/Pascal-case
/**
* CheckPascalCase method returns true if the string in PascalCase, else return the false.
* @param {String} VarName the name of the variable to check.
* @returns `Boolean` return true if the string is in PascalCase, else return false.
*/
const CheckPascalCase = (VarName) => {
// firstly, check that input is a string or not.
if (typeof VarName !== 'string') {
return new TypeError('Argument is not a string.')
}
const pat = /^[A-Z][A-Za-z]*$/
return pat.test(VarName)
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 参考
编辑 (opens new window)
上次更新: 2022/10/20, 20:03:22