press-pix
Version:
基于 PixUI 的 Press 组件库
36 lines (28 loc) • 759 B
text/typescript
const { hasOwnProperty } = Object.prototype;
function hasOwn(obj, key) {
return hasOwnProperty.call(obj, key);
}
const RE_NARGS = /(%|)\{\{\s*([0-9a-zA-Z_]+)\s*\}\}/g;
/**
* String format template
*/
export function template(string: string, ...rawArgs: any[]) {
let args: any = rawArgs;
if (args.length === 1 && typeof args[0] === 'object') {
args = args[0];
}
if (!args?.hasOwnProperty) {
args = {};
}
return string.replace(RE_NARGS, (match, prefix, i, index) => {
if (string[index - 1] === '{'
&& string[index + match.length] === '}') {
return i;
}
const result = hasOwn(args, i) ? args[i] : null;
if (result === null || result === undefined) {
return '';
}
return result;
});
}