All files / utils isCuid.ts

100% Statements 2/2
100% Branches 6/6
100% Functions 1/1
100% Lines 2/2

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 2723x                             9x                      
const re = /^c[0-9a-z]+$/
 
/**
 * 检测传入值是否是 Cuid。
 *
 * @param value 要检测的值
 * @returns 返回检测结果
 * @example
 * ```typescript
 * isCuid('1') // => false
 * isCuid('cjld2cjxh0000qzrmn831i7rn') // => true
 * ```
 * @see https://github.com/paralleldrive/cuid
 */
export function isCuid(value: string): boolean {
  return (
    !!value &&
    typeof value === 'string' &&
    // 注意: Cuid长度并不保证是固定的25位
    // https://github.com/paralleldrive/cuid/issues/51
    value.length >= 20 &&
    value.length <= 30 &&
    value[0] === 'c' &&
    re.test(value)
  )
}