From 9cece2197065004c4c3afe3c27825d95a040af78 Mon Sep 17 00:00:00 2001 From: liaoxuezhi Date: Sun, 5 May 2019 11:58:05 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20resolveVariable=20?= =?UTF-8?q?=E5=B5=8C=E5=A5=97=E7=94=A8=E6=B3=95=E9=97=AE=E9=A2=98=E5=B9=B6?= =?UTF-8?q?=E8=A1=A5=E5=85=85=E7=94=A8=E7=8E=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- __tests__/utils/tpl-builtin.test.ts | 20 ++++++++++++++++++-- src/utils/tpl-builtin.ts | 2 +- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/__tests__/utils/tpl-builtin.test.ts b/__tests__/utils/tpl-builtin.test.ts index 6a081dce..61061c63 100644 --- a/__tests__/utils/tpl-builtin.test.ts +++ b/__tests__/utils/tpl-builtin.test.ts @@ -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秒'); +}) -}) \ No newline at end of file +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'); +}); \ No newline at end of file diff --git a/src/utils/tpl-builtin.ts b/src/utils/tpl-builtin.ts index ebf7f19e..a5301bc2 100644 --- a/src/utils/tpl-builtin.ts +++ b/src/utils/tpl-builtin.ts @@ -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;