1.3.0 • Published 1 month ago

@haixing_hu/naming-style v1.3.0

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
1 month ago

@haixing_hu/naming-style

npm package License English Document CircleCI Coverage Status

naming-style 是一个JavaScript库,用于转换标志符的命名风格。它支持多种编程语言的命名规范, 包括Java、C++和Python,能够方便地在不同的大小写风格之间进行转换。

目录

安装方法

通过 npm 安装:

npm install @haixing_hu/naming-style

或者通过 yarn 安装:

yarn add @haixing_hu/naming-style

使用示例

import NamingStyle from '@haixing_hu/naming-style';

const str = 'hello-world-boy';
const converted = NamingStyle.LOWER_HYPHEN.to(NamingStyle.LOWER_CAMEL, str);
console.log(converted);     // 输出 "helloWorldBoy"

使用方法

导入

导入NamingStyle类:

import NamingStyle from '@haixing_hu/naming-style';

或者导入表示各种命名风格的全局常量:

import {
  LOWER_HYPHEN,
  LOWER_UNDERSCORE,
  LOWER_CAMEL,
  UPPER_CAMEL,
  UPPER_UNDERSCORE,
} from '@haixing_hu/naming-style';

转换字符串格式

使用 NamingStyle 类的静态实例来转换字符串格式。例如,将 lower-hyphen 命名风格的字符串 转换为其他风格:

import NamingStyle from '@haixing_hu/naming-style';

expect(NamingStyle.LOWER_HYPHEN.to(NamingStyle.LOWER_HYPHEN, 'hello-world')).toBe('hello-world');
expect(NamingStyle.LOWER_HYPHEN.to(NamingStyle.LOWER_UNDERSCORE, 'hello-world')).toBe('hello_world');
expect(NamingStyle.LOWER_HYPHEN.to(NamingStyle.LOWER_CAMEL, 'hello-world')).toBe('helloWorld');
expect(NamingStyle.LOWER_HYPHEN.to(NamingStyle.UPPER_CAMEL, 'hello-world')).toBe('HelloWorld');
expect(NamingStyle.LOWER_HYPHEN.to(NamingStyle.UPPER_UNDERSCORE, 'hello-world')).toBe('HELLO_WORLD');

可用的格式转换

本函数库提供以下格式常量,并允许在它们之间进行转换:

  • NamingStyle.LOWER_HYPHEN:使用连字符分隔的小写字母,例如 "lower-hyphen"。 此命名风格常用于 XML 的标签名。
  • NamingStyle.LOWER_UNDERSCORE:使用下划线分隔的小写字母,例如 "lower_underscore"。 此命名风格常用于 C++ 和 Python 的变量名和属性名。
  • NamingStyle.LOWER_CAMEL:首字母小写的驼峰命名法,例如 "lowerCamel"。 此命名风格常用于 Java 的变量名和属性名。
  • NamingStyle.UPPER_CAMEL:首字母大写的驼峰命名法,例如 "UpperCamel"
    此命名风格常用于 Java 和 C++ 的类名。
  • NamingStyle.UPPER_UNDERSCORE:使用下划线分隔的大写字母,例如 "UPPER_UNDERSCORE"。 此命名风格常用于 Java 和 C++ 的常量名。

获取所有格式

使用 NamingStyle.values() 方法可获取所有可用的格式常量列表:

const formats = NamingStyle.values();
expect(formats).toEqual([
  NamingStyle.LOWER_HYPHEN,
  NamingStyle.LOWER_UNDERSCORE,
  NamingStyle.LOWER_CAMEL,
  NamingStyle.UPPER_CAMEL,
  NamingStyle.UPPER_UNDERSCORE,
]);

根据名称获取格式

使用 NamingStyle.of(name) 方法可根据名称获取对应的格式对象。 该方法接受一个字符串或一个 NamingStyle 实例作为参数;字符串参数大小写不敏感,'-''_'被视为等同。

let format = NamingStyle.of('lower-camel');
expect(format).toBe(NamingStyle.LOWER_CAMEL);
format = NamingStyle.of('LOWER-CAMEL');
expect(format).toBe(NamingStyle.LOWER_CAMEL);
format = NamingStyle.of('lower_camel');
expect(format).toBe(NamingStyle.LOWER_CAMEL);
format = NamingStyle.of('LOWER_CAMEL');
expect(format).toBe(NamingStyle.LOWER_CAMEL);
format = NamingStyle.of(NamingStyle.LOWER_CAMEL);
expect(format).toBe(NamingStyle.LOWER_CAMEL);

如果提供的名称不存在,将抛出错误。

快捷常量

除了使用 NamingStyle 类成员常量,还可以通过以下全局常量直接访问不同的大小写格式:

import { 
  LOWER_HYPHEN,
  LOWER_UNDERSCORE,
  LOWER_CAMEL, 
  UPPER_CAMEL, 
  UPPER_UNDERSCORE, 
} from '@haixing_hu/naming-style';

expect(LOWER_HYPHEN.to(LOWER_HYPHEN, 'hello-world')).toBe('hello-world');
expect(LOWER_HYPHEN.to(LOWER_UNDERSCORE, 'hello-world')).toBe('hello_world');
expect(LOWER_HYPHEN.to(LOWER_CAMEL, 'hello-world')).toBe('helloWorld');
expect(LOWER_HYPHEN.to(UPPER_CAMEL, 'hello-world')).toBe('HelloWorld');
expect(LOWER_HYPHEN.to(UPPER_UNDERSCORE, 'hello-world')).toBe('HELLO_WORLD');

贡献方法

如果你发现任何问题或有改进建议,欢迎提交 issue 或者 PR 到本项目的 GitHub 仓库

版权协议

naming-style 采用 Apache 2.0 许可证。详细信息请查阅 LICENSE 文件。

1.3.0

1 month ago

1.2.1

1 month ago

1.2.0

5 months ago

1.1.0

5 months ago

1.0.1

5 months ago

1.0.0

5 months ago