fix: filter 过滤器 isTrue/isFalse 问题修复 (#5676)

* fix: filter 过滤器 isTrue 问题修复

* 修改

* 修改

* 修改
This commit is contained in:
sansiro 2022-11-04 15:47:23 +08:00 committed by GitHub
parent fdb09001e1
commit f87aee0719
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 62 additions and 13 deletions

View File

@ -327,6 +327,20 @@ test('evalute:conditional', () => {
)
).toBe(false);
expect(
evaluate(
'${a | isTrue : trueValue : falseValue}',
{
a: true,
trueValue: 5,
falseValue: 10
},
{
defaultFilter: 'raw'
}
)
).toBe(5);
expect(
evaluate(
'${a | isEquals: 1 : "1" |isEquals: 2 : "2" | isEquals: 3 : "3" }',

View File

@ -20,13 +20,14 @@ function conditionalFilter(
filterContext: FilterContext,
test: any,
trueValue: any,
falseValue: any
falseValue: any,
astOffset: number = 1
) {
(hasAlternate || test) && skipRestTest(filterContext.restFilters);
const result = test ? trueValue : falseValue;
const ast = test
? filterContext.filter?.args[1]
: filterContext.filter?.args[2];
? filterContext.filter?.args[0 + astOffset]
: filterContext.filter?.args[1 + astOffset];
return test || hasAlternate
? getStrOrVariable(result, filterContext.data, ast) ?? result
@ -464,7 +465,8 @@ extendsFilters({
this,
!!input,
trueValue,
falseValue
falseValue,
0
);
},
isFalse(input, trueValue, falseValue) {
@ -475,7 +477,8 @@ extendsFilters({
this,
!input,
trueValue,
falseValue
falseValue,
0
);
},
isMatch(input, matchArg, trueValue, falseValue) {

View File

@ -18,6 +18,27 @@ exports[`lexer:filter 1`] = `
]
`;
exports[`lexer:filter 2`] = `
[
"<Raw> $abc is ",
"<OpenScript> \${",
"<Identifier> abc",
"<OpenFilter> |",
"<Char> ",
"<Identifier> isTrue",
"<Char> ",
"<Punctuator> :",
"<Char> ",
"<Identifier> trueValue",
"<Char> ",
"<Punctuator> :",
"<Char> ",
"<Identifier> falseValue",
"<CloseScript> }",
"<EOF> undefined",
]
`;
exports[`lexer:simple 1`] = `
[
"<Raw> expression result is ",

View File

@ -34,6 +34,12 @@ test('lexer:filter', () => {
evalMode: false
})
).toMatchSnapshot();
expect(
getTokens('\\$abc is ${abc | isTrue : trueValue : falseValue}', {
evalMode: false
})
).toMatchSnapshot();
});
// test('lexer:test', () => {

View File

@ -547,22 +547,27 @@ export function lexer(input: string, options?: LexerOptions) {
return null;
}
// substring(index, index + 4) 在某些情况会匹配错误
// 比如变量名称为 trueValue
// ${value2|isTrue:trueValue:falseValue}
function literal() {
let keyword = input.substring(index, index + 4).toLowerCase();
// {4,10} 匹配长度就足够判断 ("true").length <= targetLength <= ("undefined").length + 1
const match = input.substring(index).match(/^\w{4,10}/);
if (!match) {
return null;
}
let keyword = match[0].toLowerCase();
let value: any = keyword;
let isLiteral = false;
if (keyword === 'true' || keyword === 'null') {
isLiteral = true;
value = keyword === 'true' ? true : null;
} else if (
(keyword = input.substring(index, index + 5).toLowerCase()) === 'false'
) {
} else if (keyword === 'false') {
isLiteral = true;
value = false;
} else if (
(keyword = input.substring(index, index + 9).toLowerCase()) ===
'undefined'
) {
} else if (keyword === 'undefined') {
isLiteral = true;
value = undefined;
}