Skip to content

Commit

Permalink
fix(data-source,utils): 显示条件编译对于undefined理解不正确
Browse files Browse the repository at this point in the history
  • Loading branch information
roymondchen committed Dec 4, 2023
1 parent 4870bab commit 75dd89f
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 28 deletions.
2 changes: 1 addition & 1 deletion packages/data-source/src/createDataSourceManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions packages/data-source/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export interface DataSourceOptions<T extends DataSourceSchema = DataSourceSchema
}

export interface HttpDataSourceSchema extends DataSourceSchema {
type: 'http';
options: HttpOptions;
responseOptions?: {
dataPath?: string;
Expand Down
2 changes: 1 addition & 1 deletion packages/data-source/tests/DataSourceMenager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});

Expand Down
40 changes: 14 additions & 26 deletions packages/utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[]) => {
Expand Down
10 changes: 10 additions & 0 deletions packages/utils/tests/unit/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});

0 comments on commit 75dd89f

Please sign in to comment.