Skip to content

Commit cbad9bb

Browse files
committed
Refactor and test cleanFor
1 parent c15fa46 commit cbad9bb

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

packages/ts-codegen/__tests__/cleanse.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,22 @@ import { writeFileSync } from 'fs';
22
import { sync as mkdirp } from 'mkdirp';
33

44
import { readSchemas } from '../src/utils';
5+
import { cleanFor } from '../src/utils/cleanse';
56

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

10+
describe('cleanFor', () => {
11+
it('works', () => {
12+
expect(cleanFor('NftInfoResponse_for_Empty')).toEqual(
13+
'NftInfoResponseForEmpty'
14+
);
15+
expect(cleanFor('UpdateCollectionInfoMsg_for_RoyaltyInfoResponse')).toEqual(
16+
'UpdateCollectionInfoMsgForRoyaltyInfoResponse'
17+
);
18+
});
19+
});
20+
921
it('sg721', async () => {
1022
const out = OUTPUT_DIR + '/sg721';
1123
const schemaDir = FIXTURE_DIR + '/sg721/';

packages/ts-codegen/src/utils/cleanse.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
import { pascal } from 'case';
22

3-
const cleanFor = (str: string) => {
3+
export const cleanFor = (str: string) => {
44
/*
55
1. look at first char after _for_
66
2. ONLY if you find capitals after, modify it
77
*/
8-
while (/_[a-z]+_[A-Z]/.test(str)) {
9-
const m = str.match(/(_[a-z]+_)[A-Z]/);
10-
str = str.replace(m[1], pascal(m[1]));
8+
9+
// When we upgrade to eslint v9, we can remove this exception and
10+
// rely on allExceptWhileTrue (https://eslint.org/docs/latest/rules/no-constant-condition)
11+
// eslint-disable-next-line no-constant-condition
12+
while (true) {
13+
const match = str.match(/(_[a-z]+_)[A-Z]/);
14+
if (!match) break;
15+
// this replace is unsafe as it replaces the same text but maybe
16+
// in a different location than the match
17+
str = str.replace(match[1], pascal(match[1]));
1118
}
1219

1320
return str;

0 commit comments

Comments
 (0)