1 Star 2 Fork 1

边城 / name-styles

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

name-styles

Author: James Fan (jamesfancy@126.com)

Source: name-styles on gitee.com

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

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
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:

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:

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:

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

const { kebab } from "name-styles";

const s = "ThisIsASentence";
const sentence = kebab(s)
    .replace(/-/g, " ")
    .replace(/^./, ch => ch.toUpperCase());
// This is a sentence
The MIT License (MIT) Copyright (c) 2017 边城 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

A extendable utility to convert a string into some styles: camelCase, PascalCase, hyphen-case, snake_case 展开 收起
JavaScript 等 2 种语言
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
JavaScript
1
https://gitee.com/jamesfancy/name-styles.git
git@gitee.com:jamesfancy/name-styles.git
jamesfancy
name-styles
name-styles
main

搜索帮助