Skip to content

Commit 1e6d227

Browse files
authored
Treat all non alphanumeric characters as delimiters (#11)
* Better capitalization regex * Better camelCase regex * Better toDelimiter regex * Updated tests with breaking change
1 parent fd2b9bc commit 1e6d227

File tree

3 files changed

+30
-26
lines changed

3 files changed

+30
-26
lines changed

src/__snapshots__/generate.spec.ts.snap

+2-2
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ export interface KitchenSink_TypedList_type3Object {
345345
file: string;
346346
}
347347
348-
export interface KitchenSink_TypedList_type_4_object {
348+
export interface KitchenSink_TypedList_type4Object {
349349
type: \\"type_4_object\\";
350350
name: string;
351351
}
@@ -380,7 +380,7 @@ export interface KitchenSink {
380380
hidden: any;
381381
object: KitchenSink_Object;
382382
list: KitchenSink_List[];
383-
typed_list: (KitchenSink_TypedList_Type1Object | KitchenSink_TypedList_Type2Object | KitchenSink_TypedList_type3Object | KitchenSink_TypedList_type_4_object)[];
383+
typed_list: (KitchenSink_TypedList_Type1Object | KitchenSink_TypedList_Type2Object | KitchenSink_TypedList_type3Object | KitchenSink_TypedList_type4Object)[];
384384
code: KitchenSink_CodeBlock;
385385
code_alt: KitchenSink_CodeAltBlock;
386386
snippet: string;

src/widget/transform.spec.ts

+19-18
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import {
77
sortTypes,
88
TransformState,
99
toCapitalized,
10+
toCamelCase,
11+
toDelimiter,
1012
} from "./transform";
1113

1214
describe("Widget transformation", () => {
@@ -661,10 +663,10 @@ describe("Widget transformation", () => {
661663
{ prefix: "parent", label: true },
662664
),
663665
).toEqual([
664-
["list: (parent_my_list_typeOne | parent_my_list_type_two)[];"],
666+
["list: (parent_myList_typeOne | parent_myList_typeTwo)[];"],
665667
[
666-
`interface parent_my_list_typeOne { __typename: "type_one"; key: string; }`,
667-
`interface parent_my_list_type_two { __typename: "two"; id: number; }`,
668+
`interface parent_myList_typeOne { __typename: "type_one"; key: string; }`,
669+
`interface parent_myList_typeTwo { __typename: "two"; id: number; }`,
668670
],
669671
]);
670672
});
@@ -723,24 +725,23 @@ describe("Pull typename", () => {
723725
});
724726
});
725727

726-
describe("Capitalization", () => {
727-
it("should capitalize words separated by spaces", () => {
728-
const str = "space separated string";
729-
expect(toCapitalized(str)).toBe("Space Separated String");
730-
});
731-
732-
it("should capitalize words separated by dashes", () => {
733-
const str = "dash-separated-string";
734-
expect(toCapitalized(str)).toBe("Dash-Separated-String");
728+
describe("toCamelCase", () => {
729+
it("should camelCase words separated by any non-alphanumeric characters", () => {
730+
const str = "this is a_string.with!non-alphanumeric#delimiters";
731+
expect(toCamelCase(str)).toBe("thisIsAStringWithNonAlphanumericDelimiters");
735732
});
733+
});
736734

737-
it("should capitalize words separated by underscores", () => {
738-
const str = "underscore_separated_string";
739-
expect(toCapitalized(str)).toBe("Underscore_Separated_String");
735+
describe("toCapitalized", () => {
736+
it("should capitalize words separated by any non-alphanumeric characters", () => {
737+
const str = "this is a_string.with!non-alphanumeric#delimiters";
738+
expect(toCapitalized(str)).toBe("This Is A_String.With!Non-Alphanumeric#Delimiters");
740739
});
740+
});
741741

742-
it("should handle mixed separators", () => {
743-
const str = "string with-mixed_separators";
744-
expect(toCapitalized(str)).toBe("String With-Mixed_Separators");
742+
describe("toDelimiter", () => {
743+
it("replace any non-alphanumeric character with custom delimiter", () => {
744+
const str = "this is a_string.with!non-alphanumeric#delimiters";
745+
expect(toDelimiter(str, "_")).toBe("this_is_a_string_with_non_alphanumeric_delimiters");
745746
});
746747
});

src/widget/transform.ts

+9-6
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,22 @@ export const getWidgetName = (
1111
delimiter: string,
1212
): string =>
1313
getName(
14-
useLabel
15-
? (widget.singularLabel || widget.label || widget.name).replace(/\s./gi, toCamelCase)
16-
: widget.name,
14+
useLabel ? toCamelCase(widget.singularLabel || widget.label || widget.name) : widget.name,
1715
capitalize,
1816
delimiter,
1917
);
2018

21-
export const toCamelCase = (str: string): string => str.slice(1).toUpperCase();
19+
export const toCamelCase = (str: string): string =>
20+
str.replace(/([^a-z\d]+)([a-z\d])/gi, (match, delimiter, char) => char.toUpperCase());
2221

2322
export const toCapitalized = (str: string) =>
24-
str.replace(/(^|[\s-_])(\w)/g, (match, separator, char) => `${separator}${char.toUpperCase()}`);
23+
str.replace(
24+
/(^|[^a-z\d])([a-z\d])/gi,
25+
(match, delimiter, char) => `${delimiter}${char.toUpperCase()}`,
26+
);
2527

26-
export const toDelimiter = (str: string, delimiter: string) => str.replace(/[\s-_]/g, delimiter);
28+
export const toDelimiter = (str: string, delimiter: string) =>
29+
str.replace(/[^a-z\d]+/gi, delimiter);
2730

2831
export const wrapEnum = (item: number | string): string =>
2932
typeof item === "number" ? `${item}` : `"${item}"`;

0 commit comments

Comments
 (0)