File size: 1,482 Bytes
1477a90
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
50
51
52
53
54
55
56
57
58
59
/**
 * Exceptions for PascalCase naming convention.
 */
const pascalCaseExceptions = ['OAuth2', 'URL', 'API', 'UI', 'ID']

/**
 * Checks whether a given value is in PascalCase
 * @param value the value to check
 * @returns true if the value is in PascalCase
 */
export function isPascalCaseNoAcronyms(value) {
  if (value === undefined || value === null || value === '') {
    return true
  }

  return new RegExp(
    `^(?:[A-Z][a-z0-9]+|${pascalCaseExceptions.join('|')})+[A-Z]?$|^[A-Z]+$`,
  ).test(value)
}

/**
 * Checks whether a given value is in camelCase
 * @param value the value to check
 * @returns true if the value is in camelCase
 */
export function isCamelCaseNoAcronyms(value) {
  if (value === undefined || value === null || value === '') {
    return true
  }

  return /^[^a-zA-Z0-9]?[a-z][a-z0-9]*([A-Z][a-z0-9]+)*[A-Z]?$/.test(value)
}

/**
 * Checks whether a given value is in snake_case
 * @param value the value to check
 * @returns true if the value is in snake_case
 */
export function isSnakeCase(value) {
  if (value === undefined || value === null || value === '') {
    return true
  }

  return /^([a-z0-9]+_)*[a-z0-9]+$/.test(value)
}

/**
 * Checks whether a given value is in kebab-case
 * @param value the value to check
 * @returns true if the value is in kebab-case
 */
export function isKebabCase(value) {
  if (value === undefined || value === null || value === '') {
    return true
  }

  return /^([a-z0-9]+(-[a-z0-9]+)*)$/.test(value)
}