Skip to content

Commit 0db0fba

Browse files
committed
camelCaseFn
1 parent 08f294c commit 0db0fba

File tree

6 files changed

+11
-7
lines changed

6 files changed

+11
-7
lines changed

__tests__/assetlist.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ it('assetlist', () => {
1212
it('assetlist camelCase', () => {
1313
const code = generateTypeScript(schema as any, {
1414
useSingleQuotes: true,
15-
useCamelCase: true
15+
camelCase: true
1616
});
1717
expect(code).toMatchSnapshot();
1818
writeFileSync(__dirname + '/../__fixtures__/output/assetlist.camel.ts', code);

__tests__/chain.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ it('chain', () => {
1212
it('chain camelCase', () => {
1313
const code = generateTypeScript(schema as any, {
1414
useSingleQuotes: true,
15-
useCamelCase: true
15+
camelCase: true
1616
});
1717
expect(code).toMatchSnapshot();
1818
writeFileSync(__dirname + '/../__fixtures__/output/chain.camel.ts', code);

__tests__/contract.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ it('contract', () => {
1212
it('contract camelCase', () => {
1313
const code = generateTypeScript(schema as any, {
1414
useSingleQuotes: true,
15-
useCamelCase: true
15+
camelCase: true
1616
});
1717
expect(code).toMatchSnapshot();
1818
writeFileSync(__dirname + '/../__fixtures__/output/contract.camel.ts', code);

__tests__/memo.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ it('memo', () => {
1212
it('memo camelCase', () => {
1313
const code = generateTypeScript(schema as any, {
1414
useSingleQuotes: true,
15-
useCamelCase: true
15+
camelCase: true
1616
});
1717
expect(code).toMatchSnapshot();
1818
writeFileSync(__dirname + '/../__fixtures__/output/memo.camel.ts', code);

src/context.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import deepmerge from 'deepmerge';
33

44
export interface SchemaTSOptions {
55
useSingleQuotes: boolean;
6-
useCamelCase: boolean;
6+
camelCase?: boolean; // defaults to false
7+
camelCaseFn?: (str: string) => string; // optional function to convert keys to camelCase
78
strictTypeSafety: boolean; // true uses { [k: string]: unknown; }, false uses any
89
}
910

@@ -44,6 +45,7 @@ export class SchemaTSContext implements SchemaTSContextI {
4445

4546
export const defaultOptions: SchemaTSOptions = {
4647
useSingleQuotes: true,
47-
useCamelCase: false,
48+
camelCase: false,
49+
camelCaseFn: null,
4850
strictTypeSafety: true
4951
};

src/schema.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,9 @@ function createPropertySignature(
128128
): t.TSPropertySignature {
129129

130130
const isIdent = isValidIdentifier(key);
131-
const name = ctx.options.useCamelCase ? toCamelCase(key) : key;
131+
let camelCaseFn: (str: string) => string = toCamelCase;
132+
if (ctx.options.camelCaseFn) camelCaseFn = ctx.options.camelCaseFn;
133+
const name = ctx.options.camelCase ? camelCaseFn(key) : key;
132134
const propType = getTypeForProp(ctx, prop, required, schema);
133135
const identifier = isIdent ? t.identifier(name) : t.stringLiteral(key);
134136
const propSig = t.tsPropertySignature(

0 commit comments

Comments
 (0)