修复 resolveVariable 嵌套用法问题并补充用率

This commit is contained in:
liaoxuezhi 2019-05-05 11:58:05 +08:00
parent a98ee4264b
commit 51322fda9f
2 changed files with 19 additions and 3 deletions

View File

@ -1,7 +1,8 @@
import {
prettyBytes,
escapeHtml,
formatDuration
formatDuration,
resolveVariableAndFilter
} from '../../src/utils/tpl-builtin';
test('tpl-builtin:prettyBytes', () => {
@ -18,5 +19,20 @@ test('tpl-builtin:formatDuration', () => {
expect(formatDuration(1)).toEqual('1秒');
expect(formatDuration(61)).toEqual('1分1秒');
expect(formatDuration(233233)).toEqual('3天17时47分13秒');
})
})
test('tpl-bultin:resolveVariableAndFilter', () => {
const data = {
a: 1,
b: '2',
c: {
'1': 'first',
'2': 'second'
}
};
expect(resolveVariableAndFilter('${a}', data, '| raw')).toEqual(1);
expect(resolveVariableAndFilter('${b}', data, '| raw')).toEqual('2');
expect(resolveVariableAndFilter('${c.${a}}', data, '| raw')).toEqual('first');
expect(resolveVariableAndFilter('${c.${b}}', data, '| raw')).toEqual('second');
});

View File

@ -312,7 +312,7 @@ export const resolveVariableAndFilter = (
return undefined;
}
const m = /^(\\)?\$(?:([a-z0-9_.]+)|{([^}{]+)})$/i.exec(path);
const m = /^(\\)?\$(?:([a-z0-9_.]+)|{([\s\S]+)})$/i.exec(path);
if (!m) {
return undefined;