我有一个事件处理程序,可以提供按下的键。我希望在不按住 shift
键时获得等效字符。
我发现对于不同的特殊字符,大小写的字符代码差异是不同的。
对于字母,在 js 中,我执行 value.toLowerCase()
但这仅适用于字母,不适用于特殊字符。
由于没有找到任何自动化的方法,我最终手动编写了这个映射:
const getLowerCaseMap = (val:string)=>{
//function gets a string of length 1
switch(val){
case '~' : return '`' ;
case '{' : return '[' ;
case '}' : return ']' ;
case '"' : return "'" ;
case ':' : return ";" ;
case '>' : return "." ;
case '<' : return "," ;
case '?' : return "/" ;
case '|' : return "\\" ;
case '_' : return "-" ;
case '!' : return '1' ;
case '@' : return '2' ;
case '#' : return '3' ;
case '$' : return '4' ;
case '%' : return '5' ;
case '^' : return '6' ;
case '&' : return '7' ;
case '*' : return '8' ;
case '(' : return '9' ;
case ')' : return '0' ;
default : return val.toLowerCase()
}
}
javascript 或 hack 中是否有任何函数可以在没有上述映射函数的情况下进行编写?
最佳答案
更短(更快?)的版本。函数内没有映射器定义
const mapper = { '~': '`', '{': '[', '}': ']', '"': "'", ':': ",", '>': ".", '<': ",", '?': "/", '|': "\\", '_': "-", '!': '1', '@': '2', '#': '3', '$': '4', '%': '5', '^': '6', '&': '7', '*': '8', '(': '9', ')': '0', }
Object.freeze(mapper); // making @Code_Maniac happier
const getLowerCaseMap = (val => mapper[val] || val.toLowerCase())
console.log(getLowerCaseMap('A'))
console.log(getLowerCaseMap('('))
关于javascript - 将大写特殊字符转换为小写特殊字符的最简单方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57131066/