Skip to content

Commit

Permalink
Add mergeAmbiguousCharacters option
Browse files Browse the repository at this point in the history
  • Loading branch information
blakeembrey committed Nov 17, 2023
1 parent e590910 commit 07a152f
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 8 deletions.
4 changes: 3 additions & 1 deletion packages/change-case/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,11 @@ All methods accept an `options` object as the second argument:

- `delimiter?: string` The character to use between words. Default depends on method, e.g. `_` in snake case.
- `locale?: string[] | string | false` Lower/upper according to specified locale, defaults to host environment. Set to `false` to disable.
- `separateNumbers?: boolean` Splits `foo123` into `foo 123` instead of keeping them together. Defaults to `true`.
- `separateNumbers?: boolean` Splits `foo123` into `foo 123` instead of keeping them together. Defaults to `false`.
- `prefixCharacters?: string` Retain at the beginning of the string. Defaults to `""`. Example: use `"_"` to keep the underscores in `__typename`.

By default, `pascalCase` and `snakeCase` separate ambiguous characters with `_`. For example, `V1.2` would become `V1_2` instead of `V12`. If you prefer them merged you can set `mergeAmbiguousCharacters` to `true`.

### Split

**Change case** exports a `split` utility which can be used to build other case functions. It accepts a string and returns each "word" as an array. For example:
Expand Down
13 changes: 13 additions & 0 deletions packages/change-case/src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -350,4 +350,17 @@ describe("change case", () => {
expect(snakeCase(input, options)).toEqual(result.snakeCase);
});
}

describe("pascal case merge option", () => {
it("should merge numbers", () => {
const input = "version 1.2.10";

expect(camelCase(input, { mergeAmbiguousCharacters: true })).toEqual(
"version1210",
);
expect(pascalCase(input, { mergeAmbiguousCharacters: true })).toEqual(
"Version1210",
);
});
});
});
31 changes: 24 additions & 7 deletions packages/change-case/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,40 @@ const DEFAULT_PREFIX_CHARACTERS = "";
*/
export type Locale = string[] | string | false | undefined;

/**
* Options used for converting strings to pascal/camel case.
*/
export interface PascalCaseOptions extends Options {
mergeAmbiguousCharacters?: boolean;
}

/**
* Options used for converting strings to any case.
*/
export interface Options extends SplitOptions {
locale?: Locale;
delimiter?: string;
prefixCharacters?: string;
}

/**
* Options used for splitting strings into word segments.
*/
export interface SplitOptions {
separateNumbers?: boolean;
}

/**
* Split any cased input strings into an array of words.
*/
export function split(input: string, options: SplitOptions = {}) {
const { separateNumbers } = options;
export function split(input: string, options?: SplitOptions) {
let result = input.trim();

result = result
.replace(SPLIT_LOWER_UPPER_RE, SPLIT_REPLACE_VALUE)
.replace(SPLIT_UPPER_UPPER_RE, SPLIT_REPLACE_VALUE);

if (separateNumbers) {
if (options?.separateNumbers) {
result = result
.replace(SPLIT_NUMBER_LOWER_RE, SPLIT_REPLACE_VALUE)
.replace(SPLIT_LETTER_NUMBER_RE, SPLIT_REPLACE_VALUE);
Expand Down Expand Up @@ -75,11 +87,13 @@ export function noCase(input: string, options?: Options) {
/**
* Convert a string to camel case (`fooBar`).
*/
export function camelCase(input: string, options?: Options) {
export function camelCase(input: string, options?: PascalCaseOptions) {
const prefix = getPrefix(input, options?.prefixCharacters);
const lower = lowerFactory(options?.locale);
const upper = upperFactory(options?.locale);
const transform = pascalCaseTransformFactory(lower, upper);
const transform = options?.mergeAmbiguousCharacters
? capitalCaseTransformFactory(lower, upper)
: pascalCaseTransformFactory(lower, upper);
return (
prefix +
split(input, options)
Expand All @@ -94,14 +108,17 @@ export function camelCase(input: string, options?: Options) {
/**
* Convert a string to pascal case (`FooBar`).
*/
export function pascalCase(input: string, options?: Options) {
export function pascalCase(input: string, options?: PascalCaseOptions) {
const prefix = getPrefix(input, options?.prefixCharacters);
const lower = lowerFactory(options?.locale);
const upper = upperFactory(options?.locale);
const transform = options?.mergeAmbiguousCharacters
? capitalCaseTransformFactory(lower, upper)
: pascalCaseTransformFactory(lower, upper);
return (
prefix +
split(input, options)
.map(pascalCaseTransformFactory(lower, upper))
.map(transform)
.join(options?.delimiter ?? "")
);
}
Expand Down

0 comments on commit 07a152f

Please sign in to comment.