# name-styles **Repository Path**: jamesfancy/name-styles ## Basic Information - **Project Name**: name-styles - **Description**: A extendable utility to convert a string into some styles: camelCase, PascalCase, hyphen-case, snake_case - **Primary Language**: JavaScript - **License**: MIT - **Default Branch**: main - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 2 - **Forks**: 1 - **Created**: 2017-03-27 - **Last Updated**: 2022-07-13 ## Categories & Tags **Categories**: Uncategorized **Tags**: Case-Convert, name-style, camel, pascal, kebab ## README # name-styles > Author: James Fan (jamesfancy@126.com) > > Source: [name-styles on gitee.com](https://gitee.com/jamesfancy/name-styles) A utility to convert a string into some styles: camelCase, PascalCase, kebab-case, snake\_case, _underscore\_case preserving leading underscores, etc. TypeScript supported. ## Installation ### Install by npm/yarn ```shell npm add name-styles yarn add name-styles ``` ## Quick Start ### Normal styles - `camel(s)`, camel case - `pascal(s)`, pascal case - `kebab(s)`, kebab case, alias `hyphen()` - `snake(s)`, snake case ```javascript import { camel, pascal, kebab, snake } from "name-styles"; const s = "Hello Name-Styles"; camel(s); // helloNameStyles pascal(s); // HelloNameStyles kebab(s); // hello-name-styles snake(s); // hello_name_styles ``` ### Special underscore style *underscore* style is similar to *snake* style, except that * it persists leading underscores * it does NOT merge middle underscores Note that all spaces or kebabs should be transformed to underscore, and leading ones are kept. For example: ```js import { snake, underscore } from "name-styles"; const s = "-hello-world by--james"; snake(s); // hello_world_by_james underscore(s); // _hello_world_by__james ``` ### Special constant style *CONSTANT* style matches C/C++ constant convention. It support two style constant which predicate by the second parameter. The first style is like *snake* except all letters are upper case. Another style is like *underscore* except all letters are upper case. The first style is default style. For example: ```js import { constant } from "name-styles"; const s = "-hello-world by--james"; constant(s); // HELLO_WORLD_BY_JAMES constant(s, true); // _HELLO_WORLD_BY__JAMES ``` How can I use the second style in the uniform interface with only one parameters? Currying can help. For example: ```js import { constant } from "name-styles"; const myConstant = s => constant(s, true); myConverters.push(myConstant); ``` ## Q&A ### 1\. How to get a sentence style result The string in *kebab* or *snake* style can be easily converted to sentence style ```js const { kebab } from "name-styles"; const s = "ThisIsASentence"; const sentence = kebab(s) .replace(/-/g, " ") .replace(/^./, ch => ch.toUpperCase()); // This is a sentence ```