Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The if directive implementation may cause wrong result #13

Closed
scarletsky opened this issue Apr 14, 2021 · 2 comments
Closed

The if directive implementation may cause wrong result #13

scarletsky opened this issue Apr 14, 2021 · 2 comments
Assignees
Labels
bug Something isn't working

Comments

@scarletsky
Copy link

Currently the if directive is implemented by the following function:

export function ifComparator(params: IParamsMap, rawCondition: string): boolean {
    const keys = Object.keys(params);
    const values = keys.reduce((v, key) => v.concat(params[key]), []);
    const comparator = new Function(...keys, `return ${rawCondition};`);
    return comparator(...values);
}

v.concat may cause wrong result if the value type is Array. Just a little snippet:

const testObj = { a: 1, b: [0, 2, 3] };
const keys = Object.keys(testObj);
const values = keys.reduce((v, key) => v.concat(testObj[key]), []);

keys is ["a", "b"]
values is [1, 0, 2, 3]
(new Function(...keys, 'return b;'))(...values) will return 0, and this is an unexpected result.

A more robust way is to use push instead of concat.

@afterwind-io
Copy link
Owner

@scarletsky
这个情况倒是的确没想到...

数组的问题已经修复了,补丁对应版本为1.1.4。

关于这个concat的问题,其实直接用map就行了:

const values = keys.map((key) => params[key]);

@scarletsky
Copy link
Author

@afterwind-io 哈哈,map 也可以。我也是刚好碰到打包出现异常的时候才留意到这个问题。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants