Skip to content

Commit

Permalink
casing
Browse files Browse the repository at this point in the history
  • Loading branch information
pyramation committed Apr 13, 2024
1 parent 74a6d3a commit 71ed1c8
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 30 deletions.
12 changes: 6 additions & 6 deletions __fixtures__/output/assetlist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ interface Asset {
deprecated?: boolean;
description?: string;
extended_description?: string;
denom_units: Denomunit[];
denom_units: DenomUnit[];
type_asset?: string;
address?: string;
base: string;
Expand Down Expand Up @@ -36,7 +36,7 @@ interface Asset {
twitter?: string;
};
}
interface Denomunit {
interface DenomUnit {
denom: string;
exponent: number;
aliases?: string[];
Expand All @@ -45,7 +45,7 @@ interface Pointer {
chain_name: string;
base_denom?: string;
}
interface Ibctransition {
interface IbcTransition {
type: string;
counterparty: {
chain_name: string;
Expand All @@ -57,7 +57,7 @@ interface Ibctransition {
path: string;
};
}
interface Ibccw20transition {
interface IbcCw20Transition {
type: string;
counterparty: {
chain_name: string;
Expand All @@ -71,7 +71,7 @@ interface Ibccw20transition {
path: string;
};
}
interface Nonibctransition {
interface NonIbcTransition {
type: string;
counterparty: {
chain_name: string;
Expand All @@ -83,7 +83,7 @@ interface Nonibctransition {
};
provider: string;
}
interface Asset Lists {
interface AssetLists {
$schema?: string;
chain_name: string;
assets: Asset[];
Expand Down
10 changes: 5 additions & 5 deletions __fixtures__/output/chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ interface Explorer {
tx_page?: string;
account_page?: string;
}
interface Feetoken {
interface FeeToken {
denom: string;
fixed_min_gas_price?: number;
low_gas_price?: number;
Expand All @@ -25,14 +25,14 @@ interface Feetoken {
ibc_transfer?: number;
};
}
interface Stakingtoken {
interface StakingToken {
denom: string;
}
interface Pointer {
chain_name: string;
base_denom?: string;
}
interface Cosmos Chain {
interface CosmosChain {
$schema?: string;
chain_name: string;
chain_id: string;
Expand All @@ -57,10 +57,10 @@ interface Cosmos Chain {
slip44?: number;
alternative_slip44s?: number[];
fees?: {
fee_tokens: Feetoken[];
fee_tokens: FeeToken[];
};
staking?: {
staking_tokens: Stakingtoken[];
staking_tokens: StakingToken[];
lock_duration?: {
blocks?: number;
time?: string;
Expand Down
12 changes: 6 additions & 6 deletions __tests__/__snapshots__/assetlist.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ exports[`assetlist 1`] = `
deprecated?: boolean;
description?: string;
extended_description?: string;
denom_units: Denomunit[];
denom_units: DenomUnit[];
type_asset?: string;
address?: string;
base: string;
Expand Down Expand Up @@ -39,7 +39,7 @@ exports[`assetlist 1`] = `
twitter?: string;
};
}
interface Denomunit {
interface DenomUnit {
denom: string;
exponent: number;
aliases?: string[];
Expand All @@ -48,7 +48,7 @@ interface Pointer {
chain_name: string;
base_denom?: string;
}
interface Ibctransition {
interface IbcTransition {
type: string;
counterparty: {
chain_name: string;
Expand All @@ -60,7 +60,7 @@ interface Ibctransition {
path: string;
};
}
interface Ibccw20transition {
interface IbcCw20Transition {
type: string;
counterparty: {
chain_name: string;
Expand All @@ -74,7 +74,7 @@ interface Ibccw20transition {
path: string;
};
}
interface Nonibctransition {
interface NonIbcTransition {
type: string;
counterparty: {
chain_name: string;
Expand All @@ -86,7 +86,7 @@ interface Nonibctransition {
};
provider: string;
}
interface Asset Lists {
interface AssetLists {
$schema?: string;
chain_name: string;
assets: Asset[];
Expand Down
10 changes: 5 additions & 5 deletions __tests__/__snapshots__/chain.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ interface Explorer {
tx_page?: string;
account_page?: string;
}
interface Feetoken {
interface FeeToken {
denom: string;
fixed_min_gas_price?: number;
low_gas_price?: number;
Expand All @@ -28,14 +28,14 @@ interface Feetoken {
ibc_transfer?: number;
};
}
interface Stakingtoken {
interface StakingToken {
denom: string;
}
interface Pointer {
chain_name: string;
base_denom?: string;
}
interface Cosmos Chain {
interface CosmosChain {
$schema?: string;
chain_name: string;
chain_id: string;
Expand All @@ -60,10 +60,10 @@ interface Cosmos Chain {
slip44?: number;
alternative_slip44s?: number[];
fees?: {
fee_tokens: Feetoken[];
fee_tokens: FeeToken[];
};
staking?: {
staking_tokens: Stakingtoken[];
staking_tokens: StakingToken[];
lock_duration?: {
blocks?: number;
time?: string;
Expand Down
59 changes: 59 additions & 0 deletions __tests__/case.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { toCamelCase, toPascalCase } from "../src/utils";

describe('toPascalCase', () => {
test('converts normal string', () => {
expect(toPascalCase('hello_world')).toBe('HelloWorld');
});

test('converts string with multiple underscores and mixed case', () => {
expect(toPascalCase('Object_ID')).toBe('ObjectID');
expect(toPascalCase('Postgre_SQL_View')).toBe('PostgreSQLView');
expect(toPascalCase('postgre_sql_view')).toBe('PostgreSqlView');
});

test('handles string with multiple separators together', () => {
expect(toPascalCase('hello___world--great')).toBe('HelloWorldGreat');
});

test('handles single word', () => {
expect(toPascalCase('word')).toBe('Word');
});

test('handles empty string', () => {
expect(toPascalCase('')).toBe('');
});

test('handles string with numbers', () => {
expect(toPascalCase('version1_2_3')).toBe('Version123');
});
});

describe('toCamelCase', () => {
test('converts hyphenated string', () => {
expect(toCamelCase('hello-world')).toBe('helloWorld');
});

test('converts underscored string', () => {
expect(toCamelCase('hello_world')).toBe('helloWorld');
});

test('converts spaces', () => {
expect(toCamelCase('hello world')).toBe('helloWorld');
});

test('handles mixed separators', () => {
expect(toCamelCase('hello-world_now what')).toBe('helloWorldNowWhat');
});

test('handles empty string', () => {
expect(toCamelCase('')).toBe('');
});

test('handles string starting with separators', () => {
expect(toCamelCase('-hello_world')).toBe('helloWorld');
});

test('handles string with multiple separators together', () => {
expect(toCamelCase('hello___world--great')).toBe('helloWorldGreat');
});
});
9 changes: 1 addition & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// @ts-nocheck
import * as t from "@babel/types";
import generate from "@babel/generator";
import { toPascalCase } from "./utils";

interface JSONSchema {
title: string;
Expand Down Expand Up @@ -178,11 +179,3 @@ function resolveRefType(ctx: SchemaTSContext, ref: string, schema: JSONSchema):

throw new Error(`Reference ${ref} not found in definitions.`);
}

function toPascalCase(str: string): string {
return str.replace(/\w+/g, (w) => w[0].toUpperCase() + w.slice(1).toLowerCase()).replace(/_/g, '');
}

function toCamelCase(key: string): string {
return key.replace(/[-_\s]+(.)?/g, (_, c) => c ? c.toUpperCase() : '');
}
13 changes: 13 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export function toPascalCase(str: string) {
return str.replace(/(^|_|\s|-)(\w)/g, (_: any, __: any, letter: string) => letter.toUpperCase()).replace(/[_\s-]/g, '');
}

export function toCamelCase(key: string) {
return key
// First, remove all leading non-alphabet characters
.replace(/^[^a-zA-Z]+/, '')
// Convert what follows a separator into upper case
.replace(/[-_\s]+(.)?/g, (_, c) => c ? c.toUpperCase() : '')
// Ensure the first character of the result is always lowercase
.replace(/^./, (c) => c.toLowerCase());
}

0 comments on commit 71ed1c8

Please sign in to comment.