Skip to content

Commit

Permalink
Capitalize output (#7)
Browse files Browse the repository at this point in the history
* Add toCapitalized helper and tests

* Add capitalize option to transformType

* Add capitalize option to generateTypes

* Add capitalize option to cli

* Reverted deleted test

* Correct order
  • Loading branch information
gewfy authored Oct 10, 2022
1 parent 8f9ffcf commit 7359b6b
Show file tree
Hide file tree
Showing 7 changed files with 311 additions and 20 deletions.
194 changes: 194 additions & 0 deletions src/__snapshots__/generate.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -387,3 +387,197 @@ export interface KitchenSink {
}
"
`;

exports[`Output testing should support capitalization of type names 1`] = `
"/* eslint-disable */
/* tslint:disable */
export interface Posts {
title: string;
draft: boolean;
date: string;
image?: string;
}
export interface Faq {
title: string;
}
export interface General_Posts {
front_limit: string;
author: string;
thumb?: string;
}
export interface General {
site_title: string;
posts: General_Posts;
}
export interface Authors_Authors {
name: string;
description: string;
}
export interface Authors {
authors: Authors_Authors[];
}
export type KitchenSink_Select_Options = \\"a\\" | \\"b\\" | \\"c\\";
export type KitchenSink_Select_Multiple_Options = \\"a\\" | \\"b\\" | \\"c\\";
export type KitchenSink_Select_Numeric_Options = 1 | 2 | 3;
export type KitchenSink_Object_Select_Options = \\"a\\" | \\"b\\" | \\"c\\";
export interface KitchenSink_Object {
post: any;
string: string;
boolean: boolean;
text: string;
number: string;
markdown: string;
datetime: string;
date: string;
image: string;
file: any;
select: KitchenSink_Object_Select_Options;
}
export type KitchenSink_List_Select_Options = \\"a\\" | \\"b\\" | \\"c\\";
export type KitchenSink_List_Object_Select_Options = \\"a\\" | \\"b\\" | \\"c\\";
export type KitchenSink_List_Object_List_Select_Options = \\"a\\" | \\"b\\" | \\"c\\";
export type KitchenSink_List_Object_List_Object_Select_Options = \\"a\\" | \\"b\\" | \\"c\\";
export interface KitchenSink_List_Object_List_Object {
string: string;
boolean: boolean;
text: string;
number: string;
markdown: string;
datetime: string;
date: string;
image: string;
file: any;
select: KitchenSink_List_Object_List_Object_Select_Options;
}
export interface KitchenSink_List_Object_List {
post: any;
string: string;
boolean: boolean;
text: string;
number: string;
markdown: string;
datetime: string;
date: string;
image: string;
file: any;
select: KitchenSink_List_Object_List_Select_Options;
hidden: any;
object: KitchenSink_List_Object_List_Object;
}
export interface KitchenSink_List_Object {
string: string;
boolean: boolean;
text: string;
number: string;
markdown: string;
datetime: string;
date: string;
image: string;
file: any;
select: KitchenSink_List_Object_Select_Options;
list: KitchenSink_List_Object_List[];
}
export interface KitchenSink_List {
string: string;
boolean: boolean;
text: string;
number: string;
markdown: string;
datetime: string;
date: string;
image: string;
file: any;
select: KitchenSink_List_Select_Options;
object: KitchenSink_List_Object;
}
export interface KitchenSink_Typed_List_Type_2_Object_Nested {
number: string;
}
export type KitchenSink_Typed_List_Type_2_Object_Select_Options = \\"a\\" | \\"b\\" | \\"c\\";
export interface KitchenSink_Typed_List_Type_1_Object {
type: \\"type_1_object\\";
string: string;
boolean: boolean;
text: string;
}
export interface KitchenSink_Typed_List_Type_2_Object {
type: \\"type_2_object\\";
number: string;
nested: KitchenSink_Typed_List_Type_2_Object_Nested;
select: KitchenSink_Typed_List_Type_2_Object_Select_Options;
datetime: string;
markdown: string;
}
export interface KitchenSink_Typed_List_Type_3_Object {
type: \\"type_3_object\\";
date: string;
image: string;
file: any;
}
export interface KitchenSink_Typed_List_Type_4_Object {
type: \\"type_4_object\\";
name: string;
}
export interface KitchenSink_Code {
code: string;
lang: string;
}
export interface KitchenSink_Code_Alt {
alt: string;
typescript: string;
}
export interface KitchenSink {
post: any;
title: string;
boolean: boolean;
map: string;
text: string;
number: string;
markdown: string;
datetime: string;
date: string;
color: string;
colorEditable: string;
image: string;
file: any;
select: KitchenSink_Select_Options;
select_multiple: KitchenSink_Select_Multiple_Options[];
select_numeric: KitchenSink_Select_Numeric_Options;
hidden: any;
object: KitchenSink_Object;
list: KitchenSink_List[];
typed_list: (KitchenSink_Typed_List_Type_1_Object | KitchenSink_Typed_List_Type_2_Object | KitchenSink_Typed_List_Type_3_Object | KitchenSink_Typed_List_Type_4_Object)[];
code: KitchenSink_Code;
code_alt: KitchenSink_Code_Alt;
snippet: string;
}
"
`;
10 changes: 8 additions & 2 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,27 @@ const args = yargs
default: true,
describe: `use 'label_singular' or 'label' as interface name`,
type: "boolean",
})
.option("capitalize", {
demandOption: false,
default: false,
describe: "capitalize type names",
type: "boolean",
}).argv;

let spinner: ora.Ora;

export const run = async (): Promise<void> => {
try {
const { input, output = OUTPUT_FILENAME, label } = await args;
const { input, output = OUTPUT_FILENAME, label, capitalize } = await args;

spinner = ora("Loading config").start();

const collections = loadConfig(input);

spinner.succeed().start("Generating types");

const types = generateTypes(collections, { label });
const types = generateTypes(collections, { label, capitalize });

spinner.succeed().start("Saving file");

Expand Down
6 changes: 6 additions & 0 deletions src/generate.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,10 @@ describe("Output testing", () => {

expect(generateTypes(collections, { label: true })).toMatchSnapshot();
});

it("should support capitalization of type names", () => {
const collections = loadConfig("kitchen-sink.yml");

expect(generateTypes(collections, { capitalize: true })).toMatchSnapshot();
});
});
2 changes: 1 addition & 1 deletion src/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const generateTypes = (
return collections
.flatMap(pullCollection)
.map(resolveWidget)
.reduce(transformType({ label: !!options.label }), [[], []])
.reduce(transformType({ label: !!options.label, capitalize: !!options.capitalize }), [[], []])
.flat()
.map(resolveRelations)
.map(formatType)
Expand Down
1 change: 1 addition & 0 deletions src/types/options.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export interface NetlifyTsOptions {
label?: boolean;
capitalize?: boolean;
}
75 changes: 74 additions & 1 deletion src/widget/transform.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
pullType,
sortTypes,
TransformState,
toCapitalized,
} from "./transform";

describe("Widget transformation", () => {
Expand Down Expand Up @@ -546,6 +547,50 @@ describe("Widget transformation", () => {
});
});

describe("'capitalize' option", () => {
it("should capitalize names", () => {
expect(
parse(
{
name: "users",
required: true,
multiple: true,
singularLabel: "User",
type: [
{ name: "name", type: "string", required: true, multiple: false },
{ name: "active", type: "boolean", required: true, multiple: false },
],
},
{ prefix: "parent", capitalize: true },
),
).toEqual([
["users: Parent_Users[];"],
["interface Parent_Users { name: string; active: boolean; }"],
]);
});

it("should work together with label option", () => {
expect(
parse(
{
name: "users",
required: true,
multiple: true,
singularLabel: "user",
type: [
{ name: "name", type: "string", required: true, multiple: false },
{ name: "active", type: "boolean", required: true, multiple: false },
],
},
{ prefix: "parent", capitalize: true, label: true },
),
).toEqual([
["users: Parent_User[];"],
["interface Parent_User { name: string; active: boolean; }"],
]);
});
});

it("should parse typed lists with labels", () => {
expect(
parse(
Expand Down Expand Up @@ -578,7 +623,13 @@ describe("Widget transformation", () => {
required: true,
multiple: false,
type: [
{ name: "id", singularLabel: "my_id", type: "number", required: true, multiple: false },
{
name: "id",
singularLabel: "my_id",
type: "number",
required: true,
multiple: false,
},
],
},
],
Expand Down Expand Up @@ -648,3 +699,25 @@ describe("Pull typename", () => {
expect(pullType("one: parent_list_one;")).toEqual("parent_list_one");
});
});

describe("Capitalization", () => {
it("should capitalize words separated by spaces", () => {
const str = "space separated string";
expect(toCapitalized(str)).toBe("Space Separated String");
});

it("should capitalize words separated by dashes", () => {
const str = "dash-separated-string";
expect(toCapitalized(str)).toBe("Dash-Separated-String");
});

it("should capitalize words separated by underscores", () => {
const str = "underscore_separated_string";
expect(toCapitalized(str)).toBe("Underscore_Separated_String");
});

it("should handle mixed separators", () => {
const str = "string with-mixed_separators";
expect(toCapitalized(str)).toBe("String With-Mixed_Separators");
});
});
Loading

0 comments on commit 7359b6b

Please sign in to comment.