File size: 1,793 Bytes
41af422
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import type { UserRole } from '../storage/model'
import { TextAudioType } from '../storage/model'
import type { TextAuditServiceProvider } from './textAudit'

export function isNumber<T extends number>(value: T | unknown): value is number {
  return Object.prototype.toString.call(value) === '[object Number]'
}

export function isString<T extends string>(value: T | unknown): value is string {
  return Object.prototype.toString.call(value) === '[object String]'
}

export function isNotEmptyString(value: any): boolean {
  return typeof value === 'string' && value.length > 0
}

export function isBoolean<T extends boolean>(value: T | unknown): value is boolean {
  return Object.prototype.toString.call(value) === '[object Boolean]'
}

export function isFunction<T extends (...args: any[]) => any | void | never>(value: T | unknown): value is T {
  return Object.prototype.toString.call(value) === '[object Function]'
}

export function isEmail(value: any): boolean {
  return isNotEmptyString(value) && /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/.test(value)
}

export function isTextAuditServiceProvider(value: any): value is TextAuditServiceProvider {
  return value === 'baidu' // || value === 'ali'
}

export function isTextAudioType(value: any): value is TextAudioType {
  return (
    value === TextAudioType.None
    || value === TextAudioType.Request
    || value === TextAudioType.Response
    || value === TextAudioType.All
  )
}

export function hasAnyRole(userRoles: UserRole[] | undefined, roles: UserRole[]): boolean {
  if (!userRoles || userRoles.length === 0 || !roles || roles.length === 0)
    return false

  const roleNames = roles.map(role => role.toString())
  return roleNames.some(roleName => userRoles.some(userRole => userRole.toString() === roleName))
}