-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: 增加@xpart-utils/string的template方法,以支撑log函数实现
- Loading branch information
Showing
13 changed files
with
172 additions
and
84 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,3 @@ | ||
strict-peer-dependencies=false | ||
auto-install-peers=true | ||
shamefully-hoist=true |
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
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
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
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
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
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
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
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
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,16 @@ | ||
import { describe, expect, it } from 'vitest' | ||
import { template } from '../src/template' | ||
|
||
describe('@xparcai-utils/string', () => { | ||
const nmtp = 'hello {0}, {1}好!' | ||
const jmtp = 'hello {name}, {time}好!' | ||
|
||
it('template', () => { | ||
expect(template(nmtp, 'xparcai', '早上')).toBe('hello xparcai, 早上好!') | ||
expect(template(jmtp, { name: 'xparcai', time: '早上' })).toBe('hello xparcai, 早上好!') | ||
expect(template(jmtp, { name: 'xparcai' }, '晚上')).toBe('hello xparcai, 晚上好!') | ||
expect(template(jmtp, key => (key !== 'time' ? { time: '中午' } : { name: 'xparcai' }))).toBe('hello name, time好!') | ||
expect(template(jmtp, key => (key === 'time' ? { time: '中午' } : {}), '[xparcai]')).toBe('hello [xparcai], 中午好!') | ||
expect(template(jmtp, () => ({}), 'xparcai')).toBe('hello xparcai, xparcai好!') | ||
}) | ||
}) |
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
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,42 @@ | ||
import { isFunction, isObject } from '@xparcai-utils/is' | ||
|
||
type Value = string | number | bigint | undefined | null | ||
type ValueObject = Record<string | number, Value> | ||
|
||
/** | ||
* 匿名字符串模板替换 | ||
* @param tp 字符串模板 | ||
* @param args 占位值 | ||
* @returns 替换后的字符串 | ||
*/ | ||
export function template(tp: string, ...args: Value[]): string | ||
|
||
/** | ||
* 混合字符串模板替换 | ||
* @param tp 字符串模板 | ||
* @param values 具名占位值集合,未提供值的统一使用 other 值填充 | ||
* @param other 匿名占位值,支持字符串和函数 | ||
* @returns 替换后的字符串 | ||
*/ | ||
export function template(tp: string, values: ValueObject | ((key: string) => ValueObject), other?: string | ((key: string) => string)): string | ||
|
||
/** | ||
* 实现函数 | ||
*/ | ||
export function template(tp: string, ...args: any[]): string { | ||
const [values, other] = args | ||
if (isFunction(values) || isObject(values)) { | ||
return tp.replace(/\{(\w+)\}/g, (_, key) => { | ||
const _values = isFunction(values) ? values(key) : values | ||
return _values[key] || ((isFunction(other) ? other(key) : other) ?? key) | ||
}) | ||
} | ||
else { | ||
return tp.replace(/\{(\d+)\}/g, (_, key) => { | ||
const index = Number(key) | ||
if (Number.isNaN(index)) | ||
return key | ||
return args[index] | ||
}) | ||
} | ||
} |
Oops, something went wrong.