Skip to content

Instantly share code, notes, and snippets.

@youssman
Created November 5, 2014 11:19
Show Gist options
  • Save youssman/745578062609e8acac9f to your computer and use it in GitHub Desktop.
Save youssman/745578062609e8acac9f to your computer and use it in GitHub Desktop.
Javascript convert camelcase to dash (hyphen)
function camelCaseToDash( myStr ) {
return myStr.replace( /([a-z])([A-Z])/g, '$1-$2' ).toLowerCase();
}
var myStr = camelCaseToDash( 'thisString' );
alert( myStr ); // => this-string
@xgqfrms
Copy link

xgqfrms commented Feb 4, 2021

"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

@bilogic
Copy link

bilogic commented May 5, 2024

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