-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
134 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { StringTemplate } from './StringTemplate' | ||
|
||
describe('StringTemplate', () => { | ||
test('ok', () => { | ||
expect( | ||
StringTemplate.render('{用户}爱你', { | ||
用户: 'hello', | ||
}), | ||
).toBe('hello爱你') | ||
expect( | ||
StringTemplate.render('{用户}爱你', { | ||
用户: () => 'hello', | ||
}), | ||
).toBe('hello爱你') | ||
expect( | ||
StringTemplate.render('{用户:2}爱你', { | ||
用户: (id: number) => `hello${id}`, | ||
}), | ||
).toBe('hello2爱你') | ||
expect( | ||
StringTemplate.render('{用户:2,xx}爱你', { | ||
用户: (id: number, type: string) => `hello${id}${type}`, | ||
}), | ||
).toBe('hello2xx爱你') | ||
expect( | ||
StringTemplate.render( | ||
`{用户性别}爱你{{用户性别==='男'?'啊':'哟' }}`, | ||
{ | ||
用户性别: '男', | ||
}, | ||
{ | ||
code: true, | ||
}, | ||
), | ||
).toBe('男爱你啊') | ||
expect( | ||
StringTemplate.render( | ||
`{用户性别}爱你{{用户性别==='男'?'啊':'哟' }}`, | ||
{ | ||
用户性别: '女', | ||
}, | ||
{ | ||
code: true, | ||
}, | ||
), | ||
).toBe('女爱你哟') | ||
expect( | ||
StringTemplate.render('{用户}爱你,${用户}', { | ||
用户: 'hello', | ||
}), | ||
).toBe('hello爱你,$hello') | ||
expect( | ||
StringTemplate.render( | ||
'{用户}爱你,${用户}', | ||
{ | ||
用户: 'hello', | ||
}, | ||
{ | ||
code: true, | ||
}, | ||
), | ||
).toBe('hello爱你,${用户}') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
export interface StringTemplateRenderOptions { | ||
/** 是否启用代码渲染,需环境支持 eval */ | ||
code?: boolean | ||
} | ||
|
||
/** | ||
* 字符串模板。 | ||
*/ | ||
export class StringTemplate { | ||
/** | ||
* 渲染字符串模板。语法: | ||
* | ||
* - 用 `{key}` 直接替换; | ||
* - 用 `{key:param1,param2}` 执行函数替换; | ||
* - 用 `{{key==='test'?'hi':'hello'}}` 执行代码替换(内部使用 eval 实现,需开启选项里的 `code` 参数)。 | ||
* | ||
* @param template 要渲染的模板 | ||
* @param data 渲染数据 | ||
* @param options 渲染选项 | ||
* @returns 返回渲染后字符串 | ||
*/ | ||
static render( | ||
template: string, | ||
data: Record<string, any>, | ||
options?: StringTemplateRenderOptions, | ||
) { | ||
const enableCode = !!options?.code | ||
const keys = Object.keys(data) | ||
for (const key of keys) { | ||
template = | ||
typeof data[key] === 'function' | ||
? template.replace( | ||
new RegExp(`\\{${key}(:.+?)?\\}`, 'g'), | ||
(_, params: string) => { | ||
return data[key].call( | ||
null, | ||
...(params ? params.substring(1) : '').split(','), | ||
) | ||
}, | ||
) | ||
: enableCode | ||
? template.replace(new RegExp(`(?<!\\$)\\{${key}\\}`, 'g'), data[key]) | ||
: template.replaceAll(`{${key}}`, data[key]) | ||
} | ||
if (enableCode) { | ||
template = template.replace(/\{\{(.+?)\}\}/g, (_, code) => { | ||
return eval(` | ||
(() => { | ||
const {${keys.join(',')}} = arguments[1]; | ||
return ${code}; | ||
})() | ||
`) | ||
}) | ||
} | ||
return template | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters