codewars--js--Convert all the cases!
问题描述:
In this kata, you will make a function that converts between camelCase, snake_case, and kebab-case.
You must write a function that changes to a given case. It must be able to handle all three case types:
js> changeCase("snakeCase", "snake")
"snake_case"
js> changeCase("some-lisp-name", "camel")
"someLispName"
js> changeCase("map_to_all", "kebab")
"map-to-all"
js> changeCase("doHTMLRequest", "kebab")
"do-h-t-m-l-request"
js> changeCase("invalid-inPut_bad", "kebab")
undefined
js> changeCase("valid-input", "huh???")
undefined
js> changeCase("", "camel")
""
我的思路:
先将不符合要求的筛选出来,return undefined;
然后通过swich(targetCase),分别转换。
我的答案:
通过为171 / 174。问题出现在部分不符合要求的不能识别。
function changeCase(identifier, targetCase){
// Your code here!
var a=["snake","camel","kebab"];
var id=identifier.split("");
if(identifier==""){return "";}
if(a.indexOf(targetCase)=="-1"|| (identifier.indexOf("-")!=-1 &&identifier.indexOf("_")!=-1 || (identifier.indexOf("-")!=-1 && identifier.indexOf(/A-Z/)!=-1) ||(identifier.indexOf(/A-Z/)!=-1 && identifier.indexOf("_")!=-1))){return undefined;}
switch(targetCase)
{
case "snake":
for(var i=0;i<id.length;i++){
if(/[A-Z]/.test(id[i])){
id.splice(i,0,"_",id[i+1].toLowerCase());
i++;
}
if(/[-]/.test(id[i])){
id.splice(i,1,'_')
}
}
return id.join("");
break;
case "camel":
for(var i=0;i<id.length;i++){
if(/-/.test(id[i]) || /_/.test(id[i])){
id.splice(i,2,id[i+1].toUpperCase());
//i=i-1;
}
}
return id.join("");
break;
case "kebab":
for(var i=0;i<id.length;i++){
if(/[A-Z]/.test(id[i])){
id.splice(i,0,"-");
i++;
}
if(/[-]/.test(id[i])){
id.splice(i,1,'-')
}
}
return id.join("");
break;
}
}
优秀答案:
(1)
function changeCase(identifier, targetCase){
if(!/^$|^[a-z]+(([A-Z][a-z]*)+|(-[a-z]+)*|(_[a-z]+)*)$/.test(identifier)) return undefined;
switch(targetCase){
case 'snake': return identifier.replace(/-([a-z])|([A-Z])/g, (_,x,y) => '_'+(x||y.toLowerCase()));
case 'camel': return identifier.replace(/[-_]([a-z])/g, (_,x) => x.toUpperCase());
case 'kebab': return identifier.replace(/_([a-z])|([A-Z])/g, (_,x,y) => '-'+(x||y.toLowerCase()));
default: return undefined;
}
}
(2)
function changeCase(i, t){
if ( ( /[A-Z]/.test(i) + /[_]/.test(i) + /[-]/.test(i)) > 1 ) //这个好理解,当[A-Z],-,_,三者任意两个出现的时候,返回undefined
return undefined;
else if (t == "snake" )
return i.replace(/[A-Z]/g,a=>'_'+a.toLowerCase()).replace(/-([a-z])/g,(_,a)=> '_'+a);//先大写转“_”+小写,然后将所有-x转成_+小写
else if ( t== 'camel')
return i.replace(/_/g,'-').replace(/-([a-z])/g,(_,a)=> a.toUpperCase()); //先_转-,然后将-后面的第一个字母大写
else if (t== 'kebab' )
return i.replace(/_/g,'-').replace(/[A-Z]/g,a=>'-'+a.toLowerCase());//先_转-,然后将大写字母小写
}
(3)建议这个,方便好懂,前面两个正则略复杂,不易懂
将所有单词分开,放入到数组中。
当需要用的时候,按照snake/kebab/camel的格式连接。
function changeCase(identifier, targetCase){
if (identifier === '') {
return '';
}
if (/^[a-z-]+$/.test(identifier)) {
identifier = identifier.split('-');
}
else if (/^[a-z_]+$/.test(identifier)) {
identifier = identifier.split('_');
}
else if (/^[a-z][A-Za-z]+$/.test(identifier)) {
identifier = identifier.replace(/([A-Z])/g, ' $1').toLowerCase().split(' '); //在大写字母前加空格,然后全部转小写,然后按空格分割
}
else {
return undefined;
}
switch (targetCase) {
case 'snake' : identifier = identifier.join('_'); break;
case 'kebab' : identifier = identifier.join('-'); break;
case 'camel' : identifier = identifier.map( (x,i) => i === 0 ? x : x[0].toUpperCase() + x.slice(1)).join(''); break;
default : return undefined
}
return identifier;
}
哈哈哈!
codewars--js--Convert all the cases!的更多相关文章
- moment.js & convert timestamps to date string in js
moment.js & convert timestamps to date string in js https://momentjs.com/ moment().format('YYYY- ...
- [CodeWars][JS]实现链式加法
在知乎上看到这样一个问题:http://www.zhihu.com/question/31805304; 简单地说就是实现这样一个add函数: add(x1)(x2)(x3)...(xn) == x1 ...
- [CodeWars][JS]实现大整数加法
问题描述 实现‘字符串加法’,即将两个以字符串形式表示的数字相加,得到结果然后返回一个新的字符串. 例如:输入‘123’,‘321’,返回‘444’. 这样在进行两个任意大的整数相加的时候,既不会溢出 ...
- [CodeWars][JS]如何判断给定的数字是否整数
问题描述: We are asking for a function to take a positive integer value, and return a list of all positi ...
- JS模块化库seajs体验
seajs http://seajs.org/docs/en.html#intro https://github.com/seajs/seajs/releases Extremely simple e ...
- 读《Ext.JS.4.First.Look》随笔
Ext JS 4是最大的改革已经取得了Ext框架.这些变化包括一个新类系统,引入一个新的平台,许多API变化和改进,和新组件,如新图表和新画组件.Ext JS 4是更快,更稳定,易于使用.(注意:Ex ...
- 【翻译】将Ext JS Grid转换为Excel表格
原文:Converting an Ext 5 Grid to Excel Spreadsheet 稍微迟来的礼物--Ext JS Grid转为Excel代码,现在支持Ext JS 5! 功能包括: - ...
- 【深度学习】K-L 散度,JS散度,Wasserstein距离
度量两个分布之间的差异 (一)K-L 散度 K-L 散度在信息系统中称为相对熵,可以用来量化两种概率分布 P 和 Q 之间的差异,它是非对称性的度量.在概率学和统计学上,我们经常会使用一种更简单的.近 ...
- Hacker's guide to Neural Networks
Hacker's guide to Neural Networks Hi there, I'm a CS PhD student at Stanford. I've worked on Deep Le ...
- dwr入门
dwr2.0的jar包,还需要同时导入log4j.jar和commons-loggin.jar 首先是配置文件: <!-- DWR配置 --> <servlet> <se ...
随机推荐
- Java入门 - 语言基础 - 12.Number和Math类
原文地址:http://www.work100.net/training/java-number-math.html 更多教程:光束云 - 免费课程 Number和Math类 序号 文内章节 视频 1 ...
- 网络通信-基本概念:网络、IP地址、端口、socket
目录 网络通信 1 网络 1.1 网络定义 1.2 使用网络的目的 1.3 总结 2 IP地址 2.1 ip地址的作用 2.2 ip地址的分类 3 端口 3.1 什么是端口 3.2 端口号 3.3 端 ...
- Python中关于__main__变量的问题
在Python代码的编写中,经常会用到这么一句: if __name__ == "__main__": .... 这句代码之前的语句在整个模块被其他文件调用的时候会被运行,而这句代 ...
- 优雅写Java之三(IO与文本解析)
一.资源相关 二.路径与文件 读文件: String fileName = "str.txt"; Files.lines(Paths.get(fileName), Standard ...
- Maven: 把聚合工程下的项目导入 Ecplise
1.右键点击import 2.Import Existing Maven Projects 3.选择要导入的工程 4.完成
- Spring初识、新建工程
1.spring与三层架构的关系: spring负责管理项目中的所有对象,是一个一站式的框架,容器中的对象决定了spring的功能. 2.spring核心架构 Spring框架主要由六个模块组成,在开 ...
- 学习记录(安装spark)
根据林子雨老师提供的教程安装spark,用的是网盘里下载的课程软件 将文件通过ftp传到ubantu中 根据教程修改配置文件,并成功安装spark 在修改配置文件的时候出现了疏忽,导致找不到该文件,最 ...
- mybatis缓存,从一个“灵异”事件说起
刚准备下班走人,被一开发同事叫住,让帮看一个比较奇怪的问题:Mybatis同一个Mapper接口的查询方法,第一次返回与第二次返回结果不一样,百思不得其解! 问题 Talk is cheap. Sho ...
- FFMPEG学习----使用SDL构建音频播放器
ffmpeg版本:ffmpeg-20160413-git-0efafc5 #include <stdio.h> #include <stdlib.h> #include < ...
- pandas使用的25个技巧
本文翻译自https://nbviewer.jupyter.org/github/justmarkham/pandas-videos/blob/master/top_25_pandas_trick ...