variable-name-conversion
Version:
To easily convert a variable name to camelCase, kebab-case, etc.
115 lines (114 loc) • 4.02 kB
TypeScript
import type { Camelize, Snakify, Replace, Titleize, ParseRaw, IsUpperCase } from "./types.js";
/**
* Variable name conversion helper class.
*
* Used to easily convert a variable name to camelCase, kebab-case, etc.
*/
export default class VariableName<
TRaw extends string,
TKeepCase extends boolean = false,
T extends ParseRaw<TRaw> = ParseRaw<TRaw>,
> {
#private;
readonly keepCase: TKeepCase;
/**
* @param str - Any form of the variable name to be converted.
* @param keepCase - Should it maintain case sensitivity when converting to kebab-case, snake_case, etc.? Defaults to false.
*/
constructor(str: TRaw, keepCase?: TKeepCase);
/**
* @param copy - Reuse the string in an existed variable name class instance.
* @param keepCase - Should it maintain case sensitivity when converting to kebab-case, snake_case, etc.? Defaults to false.
*/
constructor(copy: VariableName<TRaw>, keepCase?: TKeepCase);
/**
* Reset the value.
* @param str - Any form of the variable name.
*/
private set value(value);
/**
* Convert to kebab-case.
*/
get kebab(): TKeepCase extends true ? Replace<T, "_", "-"> : Lowercase<Replace<T, "_", "-">>;
/**
* Convert to snake_case.
*/
get snake(): TKeepCase extends true ? Replace<T, "_", "_"> : Lowercase<Replace<T, "_", "_">>;
/**
* Convert to CONSTANT_CASE.
*/
get const(): Uppercase<Replace<T, "_", "_">>;
/**
* Convert to PascalCase.
*/
get pascal(): Capitalize<Camelize<T, "_", TKeepCase>>;
/**
* Convert to camelCase.
*/
get camel(): Camelize<T, "_", TKeepCase>;
/**
* Convert to lowercase without any separators.
*/
get lower(): Lowercase<Replace<T, "_", "">>;
/**
* Convert to UPPERCASE without any separators.
*/
get upper(): Uppercase<Replace<T, "_", "">>;
/**
* Convert to word case, separated by spaces, all in lowercase.
*/
get words(): TKeepCase extends true ? Replace<T, "_", " "> : Lowercase<Replace<T, "_", " ">>;
/**
* Convert to Sentence case, separated by spaces, with only the first letter of the sentence capitalized.
*/
get sentence(): Capitalize<TKeepCase extends true ? Replace<T, "_", " "> : Lowercase<Replace<T, "_", " ">>>;
/**
* Convert to Title Case, separated by spaces, with all first letters of words capitalized.
*/
get title(): Titleize<TKeepCase extends true ? Replace<T, "_", " "> : Lowercase<Replace<T, "_", " ">>>;
/**
* Convert to --css-custom-property-name-form, which is kebab-case with two dashes as the prefix.
*/
get cssVar(): `--${this["kebab"]}`;
/**
* Convert to css-property-name-form / -webkit-css-property-name-form, which just like kebab-case,
* but if the first word is in "webkit", "moz", "ms", "o", it will use one dash as the prefix.
*/
get cssProp(): `${Lowercase<this["kebab"]> extends `webkit${string}` | `moz${string}` | `ms${string}` | `o${string}` ? "-" : ""}${this["kebab"]}`;
/**
* Convert to dot.case.
*/
get dot(): TKeepCase extends true ? Replace<T, "_", "."> : Lowercase<Replace<T, "_", ".">>;
/**
* Convert to *nix path/case.
*/
get path(): TKeepCase extends true ? Replace<T, "_", "/"> : Lowercase<Replace<T, "_", "/">>;
/**
* Convert to DOS path\\alt\\case.
*/
get pathAlt(): TKeepCase extends true ? Replace<T, "_", "\\"> : Lowercase<Replace<T, "_", "\\">>;
/**
* Convert to Train-Case.
*/
get train(): Titleize<Replace<TKeepCase extends true ? T : Lowercase<T>, "_", "-">, "-">;
/**
* Convert to COBOL-CASE.
*/
get cobol(): Uppercase<Replace<T, "_", "-">>;
/**
* Convert to Pascal_Snake_Case.
*/
get pascalSnake(): Titleize<Replace<TKeepCase extends true ? T : Lowercase<T>, "_", "_">, "_">;
/**
* Convert to camel_Snake_Case.
*/
get camelSnake(): Snakify<this["camel"]>;
/**
* Check if all letters in the string are uppercase (ignoring numbers, punctuation, etc.).
* @param str - String.
* @returns Are all letters uppercase?
*/
static areAllUpper<T extends string, R extends IsUpperCase<T>>(str: T): R;
toString(): T;
toJSON(): Camelize<T, "_", TKeepCase>;
}