Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions packages/ts-codegen/__tests__/cleanse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,22 @@ import { writeFileSync } from 'fs';
import { sync as mkdirp } from 'mkdirp';

import { readSchemas } from '../src/utils';
import { cleanFor } from '../src/utils/cleanse';

const FIXTURE_DIR = __dirname + '/../../../__fixtures__';
const OUTPUT_DIR = __dirname + '/../../../__output__';

describe('cleanFor', () => {
it('works', () => {
expect(cleanFor('NftInfoResponse_for_Empty')).toEqual(
'NftInfoResponseForEmpty'
);
expect(cleanFor('UpdateCollectionInfoMsg_for_RoyaltyInfoResponse')).toEqual(
'UpdateCollectionInfoMsgForRoyaltyInfoResponse'
);
});
});

it('sg721', async () => {
const out = OUTPUT_DIR + '/sg721';
const schemaDir = FIXTURE_DIR + '/sg721/';
Expand Down
15 changes: 11 additions & 4 deletions packages/ts-codegen/src/utils/cleanse.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
import { pascal } from 'case';

const cleanFor = (str: string) => {
export const cleanFor = (str: string) => {
/*
1. look at first char after _for_
2. ONLY if you find capitals after, modify it
*/
while (/_[a-z]+_[A-Z]/.test(str)) {
const m = str.match(/(_[a-z]+_)[A-Z]/);
str = str.replace(m[1], pascal(m[1]));

// When we upgrade to eslint v9, we can remove this exception and
// rely on allExceptWhileTrue (https://eslint.org/docs/latest/rules/no-constant-condition)
// eslint-disable-next-line no-constant-condition
while (true) {
const match = str.match(/(_[a-z]+_)[A-Z]/);
if (!match) break;
// this replace is unsafe as it replaces the same text but maybe
// in a different location than the match
str = str.replace(match[1], pascal(match[1]));
}

return str;
Expand Down