Created
November 5, 2014 11:19
-
-
Save youssman/745578062609e8acac9f to your computer and use it in GitHub Desktop.
Javascript convert camelcase to dash (hyphen)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function camelCaseToDash( myStr ) { | |
return myStr.replace( /([a-z])([A-Z])/g, '$1-$2' ).toLowerCase(); | |
} | |
var myStr = camelCaseToDash( 'thisString' ); | |
alert( myStr ); // => this-string |
"use strict";
/**
*
* @author xgqfrms
* @license MIT
* @copyright xgqfrms
* @created 2021-02-04
*
* @description camelCaseFormatter 驼峰命名转换器
* @augments
* @example
* @link https://www.cnblogs.com/xgqfrms/p/14366997.html
* @link https://vscode.xgqfrms.xyz/
*
*/
const log = console.log;
const camelCaseFormatter = (str = ``, debug = false) => {
let result = '';
for(let item of [...str]) {
if(item.charCodeAt() > 'a'.charCodeAt() || !Number.isNaN(+item)) {
result += item;
} else {
result += `-${item.toLocaleLowerCase()}`;
}
}
if(debug) {
log(`✅ result = `, result);
}
return result;
}
const str = 'costInV2';
// "costInV2"
camelCaseFormatter(str, true);
// node ./camelCase.js
// result = cost-in-v2
is there one that deals with paths? /CamelCase/Path/Here
to /camel-case/path/here
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I extended https://gist.github.com/youssman/745578062609e8acac9f#gistcomment-2698925 with numbers: