npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@haixing_hu/naming-style

v1.3.1

Published

A JavaScript library for converting between different naming styles of identifiers.

Downloads

400

Readme

@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 文件。