Javascript method for changing snake_case to PascalCase

后端 未结 5 1454
遥遥无期
遥遥无期 2021-01-18 22:33

I\'m looking for a JS method that will turn snake_case into PascalCase while keeping slashes intact.

// examples:
post -> Post
a         


        
5条回答
  •  梦谈多话
    2021-01-18 23:01

    PascalCase is similar to camelCase. Just difference of first char.

    const snakeToCamel = str => str.replace( /([-_]\w)/g, g => g[ 1 ].toUpperCase() );
    const snakeToPascal = str => {
        let camelCase = snakeToCamel( str );
        let pascalCase = camelCase[ 0 ].toUpperCase() + camelCase.substr( 1 );
        return pascalCase;
    }
    console.log( snakeToPascal( "i_call_shop_session" ) );

    Input : i_call_shop_session

    Output : ICallShopSession

提交回复
热议问题