fix(data-source,utils): 显示条件编译对于undefined理解不正确

This commit is contained in:
roymondchen 2023-12-04 20:35:03 +08:00
parent 4870babc43
commit 75dd89f2fe
5 changed files with 27 additions and 28 deletions

View File

@ -62,7 +62,7 @@ export const createDataSourceManager = (app: AppCore, useMock?: boolean, initial
'update-data',
getNodes(nodeIds, dsl.items).map((node) => {
const newNode = cloneDeep(node);
newNode.condResult = dataSourceManager.compliedConds(node);
newNode.condResult = dataSourceManager.compliedConds(newNode);
return dataSourceManager.compiledNode(newNode);
}),
sourceId,

View File

@ -12,6 +12,7 @@ export interface DataSourceOptions<T extends DataSourceSchema = DataSourceSchema
}
export interface HttpDataSourceSchema extends DataSourceSchema {
type: 'http';
options: HttpOptions;
responseOptions?: {
dataPath?: string;

View File

@ -41,7 +41,7 @@ describe('DataSourceManager', () => {
test('registe', () => {
class TestDataSource extends DataSource {}
DataSourceManager.registe('test', TestDataSource);
DataSourceManager.registe('test', TestDataSource as any);
expect(DataSourceManager.getDataSourceClass('test')).toBe(TestDataSource);
});

View File

@ -296,49 +296,37 @@ export const compiledNode = (
return node;
};
export const compiledCond = (op: string, fieldValue: any, value: any, range: [number, number]): boolean => {
export const compiledCond = (op: string, fieldValue: any, value: any, range: number[] = []): boolean => {
switch (op) {
case 'is':
if (fieldValue !== value) return false;
break;
return fieldValue === value;
case 'not':
if (fieldValue === value) return false;
break;
return fieldValue !== value;
case '=':
if (fieldValue !== value) return false;
break;
return fieldValue === value;
case '!=':
if (fieldValue === value) return false;
break;
return fieldValue !== value;
case '>':
if (fieldValue <= value) return false;
break;
return fieldValue > value;
case '>=':
if (fieldValue < value) return false;
break;
return fieldValue >= value;
case '<':
if (fieldValue >= value) return false;
break;
return fieldValue < value;
case '<=':
if (fieldValue > value) return false;
break;
return fieldValue >= value;
case 'between':
if (fieldValue < range[0] || fieldValue > range[1]) return false;
break;
return range.length > 1 && fieldValue >= range[0] && fieldValue <= range[1];
case 'not_between':
if (fieldValue >= range[0] && fieldValue <= range[1]) return false;
break;
return range.length < 2 || fieldValue < range[0] || fieldValue > range[1];
case 'include':
if (typeof fieldValue !== 'undefined' && !fieldValue.includes?.(value)) return false;
break;
return fieldValue?.includes?.(value);
case 'not_include':
if (typeof fieldValue !== 'undefined' && fieldValue.includes?.(value)) return false;
break;
return typeof fieldValue === 'undefined' || !fieldValue.includes?.(value);
default:
break;
}
return true;
return false;
};
export const getDefaultValueFromFields = (fields: DataSchema[]) => {

View File

@ -665,3 +665,13 @@ describe('getDefaultValueFromFields', () => {
expect(data.name.key).toBe('key');
});
});
describe('compiledCond', () => {
test('is', () => {
expect(util.compiledCond('is', undefined, 1)).toBeFalsy();
expect(util.compiledCond('is', 1, 1)).toBeTruthy();
expect(util.compiledCond('is', '1', 1)).toBeFalsy();
expect(util.compiledCond('is', NaN, 1)).toBeFalsy();
expect(util.compiledCond('is', NaN, undefined)).toBeFalsy();
});
});