Skip to content

引入

ts
import {
  getRandomString,
  uniqueFactory,
  checkStringLength,
  randomString,
  replaceAllPolyfill,
  cleanLineNumberPrefix,
  applySearchReplace,
  camelize,
  hyphenate,
  capitalize,
  titleize,
  lowerInitial,
  pascalCase,
  toUnicodeAt,
  toUnicode
} from 't-comm';

// 不支持 tree-shaking 的项目
import {
  getRandomString,
  uniqueFactory,
  checkStringLength,
  randomString,
  replaceAllPolyfill,
  cleanLineNumberPrefix,
  applySearchReplace,
  camelize,
  hyphenate,
  capitalize,
  titleize,
  lowerInitial,
  pascalCase,
  toUnicodeAt,
  toUnicode
} from 't-comm/lib/string/index';

// 只支持 ESM 的项目
import {
  getRandomString,
  uniqueFactory,
  checkStringLength,
  randomString,
  replaceAllPolyfill,
  cleanLineNumberPrefix,
  applySearchReplace,
  camelize,
  hyphenate,
  capitalize,
  titleize,
  lowerInitial,
  pascalCase,
  toUnicodeAt,
  toUnicode
} from 't-comm/es/string/index';

getRandomString

描述:获取随机字符串

参数

参数名类型描述
lengthnumber字符串长度,默认 32

返回: string

字符串

示例

ts
randomString()

randomString(16)

uniqueFactory

描述:创建唯一 ID 生成器工厂函数 返回一个函数,每次调用时生成递增的唯一 ID

参数

参数名描述
compName组件名称,用于生成 ID 的一部分
prefixID 前缀,默认为 't'

返回: 返回一个生成唯一 ID 的函数

示例

ts
const getUniqueId = uniqueFactory('button', 'my');
getUniqueId(); // 'my_button_0'
getUniqueId(); // 'my_button_1'
getUniqueId(); // 'my_button_2'

checkStringLength(str, [num])

描述:检查字符串长度

参数

参数名类型默认值描述
strstring字符串
[num]number30长度

示例

typescript
checkStringLength('123', 2) // true
checkStringLength('123', 3) // true
checkStringLength('123', 4) // false

randomString(length)

描述:获取随机字符串

参数

参数名类型描述
lengthnumber字符串长度,默认 32

返回: string

字符串

示例

ts
randomString()

randomString(16)

replaceAllPolyfill()

描述:polyfill for replaceAll

参数

示例

typescript
replaceAllPolyfill()

cleanLineNumberPrefix(text)

描述:清理 AI 输出 search/replace 块时常带的行号前缀 AI 在生成搜索字符串时,有时会无意把行号也带上,导致与源文件无法精确匹配。 本函数支持 5 种常见格式:

  • "12| code" → "code"
  • "12 | code" → "code"
  • "12 code" → "code" (多空格分隔,仅当首行匹配且行号递增时才认为是行号)
  • "12: code" → "code"
  • "12. code" → "code"

为避免误删(如某些 markdown / 注释开头是数字),仅当:

  • 首行能匹配到行号格式
  • 后续行行号严格递增(差值为 1) 时才视为带行号前缀,全部清理。

参数

参数名描述
text原始字符串(来自 AI 输出的 search/replace 块)

返回: 清理后的字符串;不像行号前缀时原样返回

applySearchReplace(source, search, replace)

描述:在源代码中查找 search 并替换为 replace,按 3 级策略匹配 策略:

    - 精确匹配:原样查找 search - 行级匹配:去除每行首尾空白后逐行对比(应对 AI 输出与源文件缩进不一致) - 子串匹配:查找首行 trim 后的子串位置,从该位置开始截取与 search 等长的范围替换

参数

参数名描述
source源代码字符串
search要查找的字符串(可能被 AI 带了行号前缀,函数会自动清理)
replace替换为的新字符串(同上,自动清理行号前缀)

返回: 替换后的字符串;查找失败返回 null

camelize(str, handleSnake)

描述:横线转驼峰命名,如果第一个字符是字母,则不处理。

参数

参数名类型描述
strstring输入字符串
handleSnakeboolean是否处理下划线,默认不处理

返回: string

处理后的字符串

示例

typescript
camelize('ab-cd-ef')

// => abCdEf

hyphenate(str)

描述:驼峰命名转横线命名:拆分字符串,使用 - 相连,并且转换为小写

参数

参数名类型描述
strstring输入字符串

返回: string

处理后的字符串

示例

typescript
hyphenate('abCd')

// => ab-cd

capitalize(str)

描述:字符串首位大写

参数

参数名类型描述
strstring输入字符串

返回: string

处理后的字符串

示例

typescript
capitalize('abc')

// => Abc

titleize(str)

描述:将每个单词的首字母转换为大写

参数

参数名类型描述
strstring输入字符串

返回: string

处理后的字符串

示例

typescript
titleize('my name is yang')

// My Name Is Yang

titleize('foo-bar')

// Foo-Bar

lowerInitial(str)

描述:首字母小写

参数

参数名类型描述
strstring输入字符串

返回: string

输出字符串

示例

typescript
lowerInitial('GroupId')

// groupId

pascalCase(str)

描述:用大驼峰,即 PascalCase 格式,来格式化字符串

参数

参数名描述
str字符串

返回: PascalCase 的字符串

示例

ts
pascalCase('ab-cd')
// AbCd

pascalCase('ab_cd')
// AbCd

toUnicodeAt(str, index)

描述:获取字符串指定下标的 unicode

参数

参数名类型描述
strstring字符串
indexnumberunicode 的下标

返回: string

data

示例

typescript
unicodeAt('ABC', 1)

// -> '\\u0042'

toUnicode(str)

描述:获取字符串的 unicode

参数

参数名类型描述
strstring字符串

返回: string

data

示例

typescript
toUnicode('ABC')

// -> '\\u0041\\u0042\\u0043'