@haixing_hu/naming-style

1.3.0 • Public • Published

@haixing_hu/naming-style

npm package License 中文文档 CircleCI Coverage Status

naming-style is a JavaScript library for converting the naming style of identifiers. It supports various programming languages' naming conventions, including Java, C++, and Python, facilitating the transition between different casing styles.

Contents

Installation

Install via npm:

npm install @haixing_hu/naming-style

Or through yarn:

yarn add @haixing_hu/naming-style

Usage Examples

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);     // Outputs "helloWorldBoy"

How to Use

Importing

Import the NamingStyle class:

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

Or import the global constants representing various naming styles:

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

Converting String Formats

Use the static instances of the NamingStyle class to convert string formats. For example, converting a lower-hyphen naming style other styles:

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');

Available Format Conversions

This library offers the following format constants for conversion:

  • NamingStyle.LOWER_HYPHEN: Lowercase letters separated by hyphens, e.g., "lower-hyphen". Commonly used in XML tag names.
  • NamingStyle.LOWER_UNDERSCORE: Lowercase letters separated by underscores, e.g., "lower_underscore". Commonly used in C++ and Python variable and attribute names.
  • NamingStyle.LOWER_CAMEL: Camel case with the first letter lowercase, e.g., "lowerCamel". Commonly used in Java variable and attribute names.
  • NamingStyle.UPPER_CAMEL: Camel case with the first letter uppercase, e.g., "UpperCamel". Commonly used in Java and C++ class names.
  • NamingStyle.UPPER_UNDERSCORE: Uppercase letters separated by underscores, e.g., "UPPER_UNDERSCORE". Commonly used in Java and C++ constant names.

Retrieving All Formats

Use the NamingStyle.values() method to get a list of all available format constants:

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

Getting a Format by Name

Use the NamingStyle.of(name) method to get a corresponding format object by name. This method accepts a string or a NamingStyle instance as an argument; string arguments are case-insensitive, and '-' and '_' are considered equivalent.

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);

If the provided name does not exist, an error will be thrown.

Shortcut Constants

In addition to using the NamingStyle class member constants, you can directly access different case styles through the following global constants:

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');

Contributing

If you encounter any issues or have suggestions for improvements, feel free to submit an issue or PR to our GitHub repository.

License

naming-style is licensed under Apache 2.0. For more details, please refer to the LICENSE file.

Package Sidebar

Install

npm i @haixing_hu/naming-style

Weekly Downloads

3

Version

1.3.0

License

Apache-2.0

Unpacked Size

2.91 MB

Total Files

32

Last publish

Collaborators

  • haixing-hu