|
| 1 | +/** |
| 2 | + * Copyright 2025 Angus.Fenying <[email protected]> |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +const REGEX_LOWER_SNAKE_CASE = /^[a-z][a-z0-9]*(_[a-z0-9]+)*$/; |
| 18 | +const REGEX_UPPER_SNAKE_CASE = /^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$/; |
| 19 | + |
| 20 | +/** |
| 21 | + * Check if a string is in Upper Snake Case format (e.g. `UPPER_SNAKE_CASE`, `A1_B2_C3`). |
| 22 | + * |
| 23 | + * @param text The string to check. |
| 24 | + * |
| 25 | + * @returns `true` if the string is in Upper Snake Case format, otherwise `false`. |
| 26 | + * |
| 27 | + * @example `isUpperSnakeCase('UPPER_SNAKE_CASE') -> true` |
| 28 | + * @example `isUpperSnakeCase('lower_snake_case') -> false` |
| 29 | + * @example `isUpperSnakeCase('A1_B2_C3') -> true` |
| 30 | + * @example `isUpperSnakeCase('1A_B2') -> false` |
| 31 | + * @example `isUpperSnakeCase('') -> false` |
| 32 | + */ |
| 33 | +export function isUpperSnakeCase(text: string): boolean { |
| 34 | + |
| 35 | + return REGEX_UPPER_SNAKE_CASE.test(text); |
| 36 | +} |
| 37 | + |
| 38 | +/** |
| 39 | + * Check if a string is in Lower Snake Case format (e.g. `lower_snake_case`, `a1_b2_c3`). |
| 40 | + * |
| 41 | + * @param text The string to check. |
| 42 | + * |
| 43 | + * @returns `true` if the string is in Lower Snake Case format, otherwise `false`. |
| 44 | + * |
| 45 | + * @example `isLowerSnakeCase('lower_snake_case') -> true` |
| 46 | + * @example `isLowerSnakeCase('UPPER_SNAKE_CASE') -> false` |
| 47 | + * @example `isLowerSnakeCase('a1_b2_c3') -> true` |
| 48 | + * @example `isLowerSnakeCase('1a_b2') -> false` |
| 49 | + * @example `isLowerSnakeCase('') -> false` |
| 50 | + */ |
| 51 | +export function isLowerSnakeCase(text: string): boolean { |
| 52 | + |
| 53 | + return REGEX_LOWER_SNAKE_CASE.test(text); |
| 54 | +} |
| 55 | + |
| 56 | +const REGEX_LOWER_CAMEL_CASE = /^[a-z][a-zA-Z0-9]*$/; |
| 57 | +const REGEX_UPPER_CAMEL_CASE = /^[A-Z][a-zA-Z0-9]*$/; |
| 58 | + |
| 59 | +/** |
| 60 | + * Check if a string is in Lower Camel Case format (e.g. `lowerCamelCase`, `a1b2c3`). |
| 61 | + * |
| 62 | + * @param text The string to check. |
| 63 | + * |
| 64 | + * @returns `true` if the string is in Lower Camel Case format, otherwise `false`. |
| 65 | + * |
| 66 | + * @example `isLowerCamelCase('lowerCamelCase') -> true` |
| 67 | + * @example `isLowerCamelCase('UpperCamelCase') -> false` |
| 68 | + * @example `isLowerCamelCase('a1b2c3') -> true` |
| 69 | + * @example `isLowerCamelCase('1aB2') -> false` |
| 70 | + * @example `isLowerCamelCase('') -> false` |
| 71 | + */ |
| 72 | +export function isLowerCamelCase(text: string): boolean { |
| 73 | + |
| 74 | + return REGEX_LOWER_CAMEL_CASE.test(text); |
| 75 | +} |
| 76 | + |
| 77 | +/** |
| 78 | + * Check if a string is in Upper Camel Case format (e.g. `UpperCamelCase`, `A1B2C3`). |
| 79 | + * |
| 80 | + * @param text The string to check. |
| 81 | + * |
| 82 | + * @returns `true` if the string is in Upper Camel Case format, otherwise `false`. |
| 83 | + * |
| 84 | + * @example `isUpperCamelCase('UpperCamelCase') -> true` |
| 85 | + * @example `isUpperCamelCase('lowerCamelCase') -> false` |
| 86 | + * @example `isUpperCamelCase('A1B2C3') -> true` |
| 87 | + * @example `isUpperCamelCase('1A2B') -> false` |
| 88 | + * @example `isUpperCamelCase('') -> false` |
| 89 | + * |
| 90 | + * @alias isPascalCase |
| 91 | + * @see {@link isPascalCase} |
| 92 | + */ |
| 93 | +export function isUpperCamelCase(text: string): boolean { |
| 94 | + |
| 95 | + return REGEX_UPPER_CAMEL_CASE.test(text); |
| 96 | +} |
| 97 | + |
| 98 | +/** |
| 99 | + * Check if a string is in Pascal Case format (same as Upper Camel Case). |
| 100 | + * |
| 101 | + * @param text The string to check. |
| 102 | + * |
| 103 | + * @returns `true` if the string is in Pascal Case format, otherwise `false`. |
| 104 | + * |
| 105 | + * @example `isPascalCase('PascalCase') -> true` |
| 106 | + * @example `isPascalCase('pascalCase') -> false` |
| 107 | + * @example `isPascalCase('Pascal_Case') -> false` |
| 108 | + * @example `isPascalCase('') -> false` |
| 109 | + * |
| 110 | + * @alias isUpperCamelCase |
| 111 | + * @see {@link isUpperCamelCase} |
| 112 | + */ |
| 113 | +export const isPascalCase = isUpperCamelCase; |
0 commit comments