Skip to content

Commit

Permalink
fix: fix style expression logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Aarebecca committed Jun 27, 2023
1 parent a2a17de commit 6ba21f5
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,10 @@ const ExpressionGroup: React.FunctionComponent<{
</Col>
<Col span={7}>
<Form.Item {...restField} name={[name, 'value']}>
{propertyType === 'string' ? (
<Input size="small" />
) : (
{propertyType === 'number' ? (
<InputNumber size="small" style={{ width: '100%' }} />
) : (
<Input size="small" />
)}
</Form.Item>
</Col>
Expand Down
63 changes: 29 additions & 34 deletions packages/gi-sdk/src/process/filterByRules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ export const formatProperties = (node: {
}
//@ts-ignore
return node || {};
// return node.data || {};
};

// lite version of lodash.get
Expand All @@ -53,39 +52,35 @@ function get<T = any>(obj: any, path: string): T | undefined {
}

const filterByExpression = (data: Record<string, string | number>, expression: Expression): boolean => {
if (!expression) {
return false;
}
if (!expression) return false;

const { name = '', operator, value } = expression;
const formatted: string | number = value;

const propertyValue = get(data, name);

if (propertyValue === undefined) return false;

if (operator === 'eql') {
return propertyValue === formatted;
} else if (operator === 'not-eql') {
return propertyValue !== formatted;
} else if (operator === 'contain') {
return propertyValue.indexOf(`${formatted}`) > -1;
} else if (operator === 'not-contain') {
return propertyValue.indexOf(`${formatted}`) === -1;
} else if (operator === 'gt') {
return Number(propertyValue) > Number(formatted);
} else if (operator === 'gte') {
return Number(propertyValue) >= Number(formatted);
} else if (operator === 'lt') {
return Number(propertyValue) < Number(formatted);
} else if (operator === 'lte') {
return Number(propertyValue) <= Number(formatted);
const formatted = String(value);
const propertyValue = String(get(data, name));

switch (operator) {
case 'eql':
return propertyValue === formatted;
case 'not-eql':
return propertyValue !== formatted;
case 'contain':
return propertyValue.indexOf(`${formatted}`) > -1;
case 'not-contain':
return propertyValue.indexOf(`${formatted}`) === -1;
case 'gt':
return Number(propertyValue) > Number(value);
case 'gte':
return Number(propertyValue) >= Number(value);
case 'lt':
return Number(propertyValue) < Number(value);
case 'lte':
return Number(propertyValue) <= Number(value);
default:
return false;
}

return false;
};

export const filterByTopRule = (node, rule: Condition) => {
export const filterByTopRule = (item, rule: Condition) => {
const { logic, expressions } = rule;

// 未配置规则一律通过
Expand All @@ -94,12 +89,12 @@ export const filterByTopRule = (node, rule: Condition) => {
}

return logic === true
? expressions.every(item => filterByExpression(formatProperties(node), item))
: expressions.some(item => filterByExpression(formatProperties(node), item));
? expressions.every(exp => filterByExpression(formatProperties(item), exp))
: expressions.some(exp => filterByExpression(formatProperties(item), exp));
};

export const filterByRules = (nodes, rule) => {
return nodes.filter(node => {
return filterByTopRule(node, rule);
export const filterByRules = (items, rule) => {
return items.filter(item => {
return filterByTopRule(item, rule);
});
};

0 comments on commit 6ba21f5

Please sign in to comment.