Skip to content

Commit

Permalink
feat: [#4] 支持评分字段
Browse files Browse the repository at this point in the history
  • Loading branch information
Leizhenpeng committed Aug 12, 2023
1 parent 231b985 commit 28280ac
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 19 deletions.
28 changes: 19 additions & 9 deletions utils/BaseSchema/dataWriter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,17 @@ const tableInfo: TableProps = {
// 复选框
{
id: 'fldueXn0k2', name: 'is_novel', property: {}, type: 7,
}
},
{ id: 'fldueXn0k2', name: 'score', property: {}, type: 99004 },

],
};

const resultExample = { content: 'TypeChat is awesome!', sentiment: 'positive' };
const resultExample2 = { content: '我喜欢吃苹果和橘子', fruit: ['苹果', '橘子'] };
const resultExample3 = { Height: 180 };
const resultExample4 = { is_novel: true };
const resultExample5 = { score: 4 };

describe('dataWriter init', () => {
it('should init', function () {
Expand Down Expand Up @@ -122,37 +125,44 @@ describe('parse multi select', () => {
{
id: 'optdh2v28L',
text: '橘子',
}
},
]);
});
})
});

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 boolean', () => {
const core = new DataWriter(tableInfo);
it('should parse boolean field', () => {
expect(core.load(resultExample4).parseOneField(tableInfo.fields[4])).toEqual(true);
});
});

describe('parse score', () => {
const core = new DataWriter(tableInfo);
it('should parse score field', () => {
expect(core.load(resultExample5).parseOneField(tableInfo.fields[5])).toEqual(4);
});
})
describe('parse all type', () => {
const core = new DataWriter(tableInfo);
it('should parse all', () => {
expect(core.load(resultExample).recordFormat).toEqual(
{
["fld8t4vEEN"] : {
['fld8t4vEEN']: {
type: 'text',
text: 'TypeChat is awesome!',
},
["fld1g8LMRX"] : {
id:'opt3UsIu8H',
['fld1g8LMRX']: {
id: 'opt3UsIu8H',
text: 'positive',
}
})
},
});
});
});
14 changes: 7 additions & 7 deletions utils/BaseSchema/dataWriter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export class DataWriter {
exportNumber(number: number) {
return number;
}

exportBoolean(boolean: boolean) {
return boolean;
}
Expand All @@ -53,7 +54,7 @@ export class DataWriter {
return null;
}
const labelId = option.id;
return labelId
return labelId;
}

getItem(field: IBaseFieldMeta['name']) {
Expand All @@ -62,7 +63,7 @@ export class DataWriter {


parseOneField(field: IBaseFieldMeta) {
const itemValue = this.getItem(field.name) as any
const itemValue = this.getItem(field.name) as any;
if (!itemValue) {
return null;
}
Expand All @@ -71,7 +72,7 @@ export class DataWriter {
return this.exportText(itemValue);
}
//数字
if (field.type === 2) {
if (field.type === 2 || field.type === 99004) {
return this.exportNumber(itemValue);
}
//单选
Expand All @@ -84,7 +85,7 @@ export class DataWriter {
const labelMeta = itemValue.map((v: string) => {
const labelId = this.findSelectLabelId(field, v);
return [v, labelId];
})
});
return labelMeta.map((v: any) => this.exportSelect(v[0], v[1]));
}
// 复选框
Expand All @@ -95,16 +96,15 @@ export class DataWriter {
}



get recordFormat() {
const fields = this.tableInfo.fields;
const res={} as any
const res = {} as any;
fields.forEach(f => {
const parsed = this.parseOneField(f);
if (parsed) {
res[f.id] = parsed;
}
})
});
return res;
}

Expand Down
10 changes: 8 additions & 2 deletions utils/BaseSchema/tableParser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ const tableInfo: TableProps = {
// 单选框
{
id: '"fldueXn0k2"', name: 'is_novel', property: {}, type: 7,
}
},
//评分
{ id: 'fldueXn0k2', name: 'score', property: {}, type: 99004 }
],
};

Expand Down Expand Up @@ -97,10 +99,14 @@ describe('BaseSchema class format', () => {

it('should format number field', function () {
expect(core.formatNumberField(tableInfo.fields[3])).toBe(`Height: number;`);
})
});

it('should format boolean field', function () {
expect(core.formatBooleanField(tableInfo.fields[4])).toBe(`is_novel: boolean;`);
});

it('should format grade fields', function () {
expect(core.formatNumberField(tableInfo.fields[5])).toBe(`score: number;`);
})

it('should format title', function () {
Expand Down
4 changes: 3 additions & 1 deletion utils/BaseSchema/tableParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,13 @@ export class TableParser {
return `${ iBaseFieldMeta.name }: boolean;`;
}


formatField(field: IBaseFieldMeta) {
switch (field.type) {
case 1:
return this.formatStringField(field);
case 2:
case 2 :
case 99004:
return this.formatNumberField(field);
case 3:
return this.formatSelectField(field);
Expand Down

0 comments on commit 28280ac

Please sign in to comment.