All files / isFileName isFileName.ts

100% Statements 11/11
100% Branches 3/3
100% Functions 1/1
100% Lines 10/10

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                      1x 4x 4x 4x 1x   3x 1x   2x 1x   1x    
/**
 * @description 是否是文件名称,windows系统不支持一些特殊字符,例如`\ / : * ? " < > |`
 * @function isFileName
 * @param {string} val - 需要校验的字符串
 * @returns {Promise<string>} 返回Promise,如果string为''则代表验证通过
 * @author liukun <919590347@qq.com>
 * @example
 * import { isFileName } from '@wont/utils'
 * const isValid = await isFileName('1')  // isValid=''
 */
 
export const isFileName = (val: string): Promise<string> => {
  const allSpaceReg = /^\s*$/
  const specCharsReg = /[\\/:*?"<>|]/
  if (!val) {
    return Promise.resolve('')
  }
  if (specCharsReg.test(val)) {
    return Promise.resolve('文件名称不可输入\\ / : * ? " < > |')
  }
  if (allSpaceReg.test(val)) {
    return Promise.resolve('文件名称不可只输入空格')
  }
  return Promise.resolve('')
}