字符串相关函数
trim
去除开头结尾空格
返回值 (value: string): string
参数
参数属性 | 说明 | 类型 |
---|---|---|
value | 字符串 | string |
js
import {trim} from '@dense-labs/utils'
console.log(trim(' hello world ')) // 'hello world'
console.log(trim(' ')) // ''
console.log(trim('hello world')) // 'hello world'
console.log(trim('')) // ''
console.log(trim(123)) // '123'
maskLeft
从字符串左侧开始对指定数量的字符进行脱敏处理
返回值 (str: string, numChars: number, symbol = '*'): string
参数
参数属性 | 说明 | 类型 | 默认值 |
---|---|---|---|
str | 目标字符串 | string | |
numChars | 需要脱敏的字符数量 | number | |
symbol | 替换的字符串,默认为 * | string | * |
js
import {maskLeft} from '@dense-labs/utils'
console.log(maskLeft('1234567890', 4, '*')) // ****567890
console.log(maskLeft('1234567890', -1)) // Invalid number of characters
console.log(maskLeft('1234567890', 12, '*')) // **********
console.log(maskLeft('1234567890', 0)) // 1234567890
maskRight
从字符串右侧开始对指定数量的字符进行脱敏处理
返回值 (str: string, numChars: number, symbol = '*'): string
参数
参数属性 | 说明 | 类型 | 默认值 |
---|---|---|---|
str | 目标字符串 | string | |
numChars | 需要脱敏的字符数量 | number | |
symbol | 替换的字符串,默认为 * | string | * |
js
import {maskRight} from '@dense-labs/utils'
console.log(maskRight('1234567890', 4, '*')) // 123456****
console.log(maskRight('1234567890', -1)) // Invalid number of characters
console.log(maskRight('1234567890', 20, '*')) // **********
console.log(maskRight('1234567890', 0)) // 1234567890
formatString
格式化字符串,替换其中的占位符 {index} 为相应的参数值
返回值 (str: string, agrs: (string | number)[]): string
参数
参数属性 | 说明 | 类型 | 默认值 |
---|---|---|---|
str | 格式化的字符串,包含零个或多个占位符 | string | |
agrs | 一个或多个参数,用于替换字符串中的占位符 | `(string | number)[]` |
js
import {formatString} from '@dense-labs/utils'
// 使用示例
console.log(formatString('Hello {0}', 'John')); // 输出: Hello John
console.log(formatString('Hello {0}, how are you {1}?', 'John', 'today', 6666)); // 输出: Hello John, how are you today?