fix: fix type error in datetime module
This commit is contained in:
@@ -10,6 +10,24 @@ export function lcm(a: number, b: number): number {
|
||||
return a / gcd(a, b) * b;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将字符串中的 `{n}` 占位符替换为对应的参数值
|
||||
* @param str 目标字符串,若为 `null`/`undefined`/空字符串则返回原值或空串(根据需求)
|
||||
* @param args 用于替换的值列表
|
||||
* @returns 格式化后的字符串
|
||||
*/
|
||||
export function format(str: string, ...args: any[]): string {
|
||||
// 若 str 为假值(null/undefined/''),直接返回空字符串
|
||||
if (!str) return '';
|
||||
|
||||
return str.replace(/\{(\d+)\}/g, (match, index) => {
|
||||
const idx = parseInt(index, 10);
|
||||
const value = args[idx];
|
||||
// 若参数存在且不为 null/undefined,则转为字符串,否则保留占位符
|
||||
return value != null ? String(value) : match;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Monday-first the day of week from given date instead of Sunday-first.
|
||||
* @param date The date for getting weekday.
|
||||
|
||||
Reference in New Issue
Block a user