Skip to content

Commit

Permalink
feat: [#4] support number
Browse files Browse the repository at this point in the history
  • Loading branch information
Leizhenpeng committed Aug 9, 2023
1 parent 062c5cb commit 2645da1
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 5 deletions.
10 changes: 10 additions & 0 deletions utils/BaseSchema/dataWriter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,15 @@ const tableInfo: TableProps = {
}],
},
},
{
id: 'fldueXn0k2', name: 'Height', property: {}, type: 2,
},
],
};

const resultExample = { content: 'TypeChat is awesome!', sentiment: 'positive' };
const resultExample2 = { content: '我喜欢吃苹果和橘子', fruit: ['苹果', '橘子'] };
const resultExample3 = { Height: 180 };
describe('dataWriter init', () => {
it('should init', function () {
expect(new DataWriter(tableInfo)).toBeDefined();
Expand Down Expand Up @@ -116,6 +120,12 @@ describe('parse multi select', () => {
});
})

describe('parse number', () => {
const core = new DataWriter(tableInfo);
it('should parse number field', () => {
expect(core.load(resultExample3).parseOneField(tableInfo.fields[3])).toEqual(180);
});
})

describe('parse all type', () => {
const core = new DataWriter(tableInfo);
Expand Down
8 changes: 8 additions & 0 deletions utils/BaseSchema/dataWriter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ export class DataWriter {
};
}

exportNumber(number: number) {
return number;
}

exportSelect(label: string, labelId: string) {
return {
id: labelId,
Expand Down Expand Up @@ -63,6 +67,10 @@ export class DataWriter {
if (field.type === 1) {
return this.exportText(itemValue);
}
//数字
if (field.type === 2) {
return this.exportNumber(itemValue);
}
//单选
if (field.type === 3) {
const labelId = this.findSelectLabelId(field, itemValue);
Expand Down
12 changes: 7 additions & 5 deletions utils/BaseSchema/tableParser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ const tableInfo: TableProps = {
}],
},
},
// 数字
{
id: 'fldueXn0k2', name: 'Height', property: {}, type: 2,
},
],
};

Expand Down Expand Up @@ -87,11 +91,9 @@ describe('BaseSchema class format', () => {
expect(core.formatMultiSelectField(tableInfo.fields[2])).toBe(`fruit: ("橘子" | "苹果" | "菠萝")[];`);
});

it('should format field', function () {
expect(core.formatField(tableInfo.fields[0])).toBe('content: string;');
expect(core.formatField(tableInfo.fields[1])).toBe(`sentiment: "negative" | "neutral" | "positive";`);
expect(core.formatField(tableInfo.fields[2])).toBe(`fruit: ("橘子" | "苹果" | "菠萝")[];`);
});
it('should format number field', function () {
expect(core.formatNumberField(tableInfo.fields[3])).toBe(`Height: number;`);
})

it('should format title', function () {
expect(core.formatTitle()).toBe('export interface SentimentResponse {');
Expand Down
8 changes: 8 additions & 0 deletions utils/BaseSchema/tableParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,16 @@ export class TableParser {
const optionsStr = options.map((o: any) => `"${o.name}"`).join(' | ');
return `${field.name}: (${optionsStr})[];`;
}
formatNumberField(iBaseFieldMeta: IBaseFieldMeta) {
return `${iBaseFieldMeta.name}: number;`;
}

formatField(field: IBaseFieldMeta) {
switch (field.type) {
case 1:
return this.formatStringField(field);
case 2:
return this.formatNumberField(field);
case 3:
return this.formatSelectField(field);
case 4:
Expand All @@ -58,6 +64,8 @@ export class TableParser {
}
}



formatTitle() {
return `export interface ${this.title} {`;
}
Expand Down

0 comments on commit 2645da1

Please sign in to comment.