问题描述:

In this kata, you will make a function that converts between camelCasesnake_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:

  1. js> changeCase("snakeCase", "snake")
  2. "snake_case"
  3. js> changeCase("some-lisp-name", "camel")
  4. "someLispName"
  5. js> changeCase("map_to_all", "kebab")
  6. "map-to-all"
  7. js> changeCase("doHTMLRequest", "kebab")
  8. "do-h-t-m-l-request"
  9. js> changeCase("invalid-inPut_bad", "kebab")
  10. undefined
  11. js> changeCase("valid-input", "huh???")
  12. undefined
  13. js> changeCase("", "camel")
  14. ""

我的思路:

先将不符合要求的筛选出来,return undefined;

然后通过swich(targetCase),分别转换。

我的答案:

通过为171 / 174。问题出现在部分不符合要求的不能识别。

  1. function changeCase(identifier, targetCase){
  2.  
  3. // Your code here!
  4. var a=["snake","camel","kebab"];
  5. var id=identifier.split("");
  6. if(identifier==""){return "";}
  7. 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;}
  8. switch(targetCase)
  9. {
  10. case "snake":
  11. for(var i=0;i<id.length;i++){
  12. if(/[A-Z]/.test(id[i])){
  13. id.splice(i,0,"_",id[i+1].toLowerCase());
  14. i++;
  15. }
  16. if(/[-]/.test(id[i])){
  17. id.splice(i,1,'_')
  18. }
  19. }
  20. return id.join("");
  21. break;
  22. case "camel":
  23. for(var i=0;i<id.length;i++){
  24. if(/-/.test(id[i]) || /_/.test(id[i])){
  25. id.splice(i,2,id[i+1].toUpperCase());
  26. //i=i-1;
  27. }
  28. }
  29. return id.join("");
  30. break;
  31. case "kebab":
  32. for(var i=0;i<id.length;i++){
  33. if(/[A-Z]/.test(id[i])){
  34. id.splice(i,0,"-");
  35. i++;
  36. }
  37. if(/[-]/.test(id[i])){
  38. id.splice(i,1,'-')
  39. }
  40. }
  41. return id.join("");
  42. break;
  43. }
  44. }

优秀答案:

(1)

  1. function changeCase(identifier, targetCase){
  2. if(!/^$|^[a-z]+(([A-Z][a-z]*)+|(-[a-z]+)*|(_[a-z]+)*)$/.test(identifier)) return undefined;
  3. switch(targetCase){
  4. case 'snake': return identifier.replace(/-([a-z])|([A-Z])/g, (_,x,y) => '_'+(x||y.toLowerCase()));
  5. case 'camel': return identifier.replace(/[-_]([a-z])/g, (_,x) => x.toUpperCase());
  6. case 'kebab': return identifier.replace(/_([a-z])|([A-Z])/g, (_,x,y) => '-'+(x||y.toLowerCase()));
  7. default: return undefined;
  8. }
  9. }

(2)

  1. function changeCase(i, t){
  2. if ( ( /[A-Z]/.test(i) + /[_]/.test(i) + /[-]/.test(i)) > 1 ) //这个好理解,当[A-Z],-,_,三者任意两个出现的时候,返回undefined
  3. return undefined;
  4. else if (t == "snake" )
  5. return i.replace(/[A-Z]/g,a=>'_'+a.toLowerCase()).replace(/-([a-z])/g,(_,a)=> '_'+a);//先大写转“_”+小写,然后将所有-x转成_+小写
  6. else if ( t== 'camel')
  7. return i.replace(/_/g,'-').replace(/-([a-z])/g,(_,a)=> a.toUpperCase()); //先_转-,然后将-后面的第一个字母大写
  8. else if (t== 'kebab' )
  9. return i.replace(/_/g,'-').replace(/[A-Z]/g,a=>'-'+a.toLowerCase());//先_转-,然后将大写字母小写
  10. }

(3)建议这个,方便好懂,前面两个正则略复杂,不易懂

将所有单词分开,放入到数组中。

当需要用的时候,按照snake/kebab/camel的格式连接。

  1. function changeCase(identifier, targetCase){
  2.  
  3. if (identifier === '') {
  4. return '';
  5. }
  6. if (/^[a-z-]+$/.test(identifier)) {
  7. identifier = identifier.split('-');
  8. }
  9. else if (/^[a-z_]+$/.test(identifier)) {
  10. identifier = identifier.split('_');
  11. }
  12. else if (/^[a-z][A-Za-z]+$/.test(identifier)) {
  13. identifier = identifier.replace(/([A-Z])/g, ' $1').toLowerCase().split(' '); //在大写字母前加空格,然后全部转小写,然后按空格分割
  14. }
  15. else {
  16. return undefined;
  17. }
  18.  
  19. switch (targetCase) {
  20.  
  21. case 'snake' : identifier = identifier.join('_'); break;
  22. case 'kebab' : identifier = identifier.join('-'); break;
  23. case 'camel' : identifier = identifier.map( (x,i) => i === 0 ? x : x[0].toUpperCase() + x.slice(1)).join(''); break;
  24.  
  25. default : return undefined
  26. }
  27.  
  28. return identifier;
    }

哈哈哈!

codewars--js--Convert all the cases!的更多相关文章

  1. moment.js & convert timestamps to date string in js

    moment.js & convert timestamps to date string in js https://momentjs.com/ moment().format('YYYY- ...

  2. [CodeWars][JS]实现链式加法

    在知乎上看到这样一个问题:http://www.zhihu.com/question/31805304; 简单地说就是实现这样一个add函数: add(x1)(x2)(x3)...(xn) == x1 ...

  3. [CodeWars][JS]实现大整数加法

    问题描述 实现‘字符串加法’,即将两个以字符串形式表示的数字相加,得到结果然后返回一个新的字符串. 例如:输入‘123’,‘321’,返回‘444’. 这样在进行两个任意大的整数相加的时候,既不会溢出 ...

  4. [CodeWars][JS]如何判断给定的数字是否整数

    问题描述: We are asking for a function to take a positive integer value, and return a list of all positi ...

  5. JS模块化库seajs体验

    seajs http://seajs.org/docs/en.html#intro https://github.com/seajs/seajs/releases Extremely simple e ...

  6. 读《Ext.JS.4.First.Look》随笔

    Ext JS 4是最大的改革已经取得了Ext框架.这些变化包括一个新类系统,引入一个新的平台,许多API变化和改进,和新组件,如新图表和新画组件.Ext JS 4是更快,更稳定,易于使用.(注意:Ex ...

  7. 【翻译】将Ext JS Grid转换为Excel表格

    原文:Converting an Ext 5 Grid to Excel Spreadsheet 稍微迟来的礼物--Ext JS Grid转为Excel代码,现在支持Ext JS 5! 功能包括: - ...

  8. 【深度学习】K-L 散度,JS散度,Wasserstein距离

    度量两个分布之间的差异 (一)K-L 散度 K-L 散度在信息系统中称为相对熵,可以用来量化两种概率分布 P 和 Q 之间的差异,它是非对称性的度量.在概率学和统计学上,我们经常会使用一种更简单的.近 ...

  9. 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 ...

  10. dwr入门

    dwr2.0的jar包,还需要同时导入log4j.jar和commons-loggin.jar 首先是配置文件: <!-- DWR配置 --> <servlet> <se ...

随机推荐

  1. python 验证客户端的合法性

    目的:对连接服务器的客户端进行判断 # Server import socket import hmac import os secret_key = bytes('tom', encoding='u ...

  2. Vue 编程式的导航

    1.应用场景 在同一路由的情况下,不同的参数之间进行切换 注意:别忘记初始化路由页面 2.用法 a.定义方法 b.实现方法 c.初始化路由页面 3.案例 <template> <di ...

  3. mysql本地连接远程连接不上

    首先测试linux下的端口有没有开通 /etc/init.d/iptables status 查看3306端口没有开通 使用以下命令开通 /sbin/iptables -I INPUT -p tcp ...

  4. [bzoj4824][洛谷P3757][Cqoi2017]老C的键盘

    Description 老 C 是个程序员. 作为一个优秀的程序员,老 C 拥有一个别具一格的键盘,据说这样可以大幅提升写程序的速度,还能让写出来的程序 在某种神奇力量的驱使之下跑得非常快.小 Q 也 ...

  5. python 安装虚拟环境virtualenv

    1.sudo apt install virtualenv 安装失败 2.sudo apt-get update 更新失败 提示: E: 仓库 “http://mirrors.aliyun.com/u ...

  6. swagger基本使用指南

    Maven依赖 <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-s ...

  7. java.io 包下的类有哪些 + 面试题

    java.io 包下的类有哪些 + 面试题 IO 介绍 IO 是 Input/Output 的缩写,它是基于流模型实现的,比如操作文件时使用输入流和输出流来写入和读取文件等. IO 分类 传统的 IO ...

  8. LUAMD5加密

    md5里的方法: C:\Windows\System32>lua Lua 5.1.4 Copyright (C) 1994-2008 Lua.org, PUC-Rio > require( ...

  9. Java集合XMind与注意事项

    Java中集合使用时的几个注意事项: 1.ArrayList和HashMap都具有扩容 ArrayList初始化数组长度为10,扩容后的容量为原来的1.5倍. HashMap初始化的数组长度为16,扩 ...

  10. Go语言之路—博客目录

    Go语言介绍 为什么你应该学习Go语言? 开发环境准备 从零开始搭建Go语言开发环境 VS Code配置Go语言开发环境 Go语言基础 Go语言基础之变量和常量 Go语言基础之基本数据类型 Go语言基 ...