|
3 | 3 | isValidIdentifierCamelized, |
4 | 4 | toCamelCase, |
5 | 5 | toPascalCase, |
| 6 | + toSnakeCase, |
| 7 | + toKebabCase, |
| 8 | + toConstantCase, |
6 | 9 | } from '../src'; |
7 | 10 |
|
8 | 11 | it('should convert strings to PascalCase', () => { |
@@ -95,3 +98,145 @@ describe('toCamelCase', () => { |
95 | 98 | expect(toCamelCase('hello___world--great')).toBe('helloWorldGreat'); |
96 | 99 | }); |
97 | 100 | }); |
| 101 | + |
| 102 | +describe('toSnakeCase', () => { |
| 103 | + test('converts camelCase to snake_case', () => { |
| 104 | + expect(toSnakeCase('helloWorld')).toBe('hello_world'); |
| 105 | + }); |
| 106 | + |
| 107 | + test('converts PascalCase to snake_case', () => { |
| 108 | + expect(toSnakeCase('HelloWorld')).toBe('hello_world'); |
| 109 | + }); |
| 110 | + |
| 111 | + test('converts kebab-case to snake_case', () => { |
| 112 | + expect(toSnakeCase('hello-world')).toBe('hello_world'); |
| 113 | + }); |
| 114 | + |
| 115 | + test('converts space separated to snake_case', () => { |
| 116 | + expect(toSnakeCase('hello world')).toBe('hello_world'); |
| 117 | + }); |
| 118 | + |
| 119 | + test('handles mixed case with numbers', () => { |
| 120 | + expect(toSnakeCase('version2APIKey')).toBe('version_2_api_key'); |
| 121 | + }); |
| 122 | + |
| 123 | + test('handles already snake_case strings', () => { |
| 124 | + expect(toSnakeCase('hello_world')).toBe('hello_world'); |
| 125 | + }); |
| 126 | + |
| 127 | + test('handles multiple separators together', () => { |
| 128 | + expect(toSnakeCase('hello___world--great')).toBe('hello_world_great'); |
| 129 | + }); |
| 130 | + |
| 131 | + test('handles empty string', () => { |
| 132 | + expect(toSnakeCase('')).toBe(''); |
| 133 | + }); |
| 134 | + |
| 135 | + test('removes leading and trailing underscores', () => { |
| 136 | + expect(toSnakeCase('_hello_world_')).toBe('hello_world'); |
| 137 | + }); |
| 138 | + |
| 139 | + test('handles single word', () => { |
| 140 | + expect(toSnakeCase('hello')).toBe('hello'); |
| 141 | + }); |
| 142 | + |
| 143 | + test('handles consecutive capitals', () => { |
| 144 | + expect(toSnakeCase('HTTPSConnection')).toBe('https_connection'); |
| 145 | + }); |
| 146 | +}); |
| 147 | + |
| 148 | +describe('toKebabCase', () => { |
| 149 | + test('converts camelCase to kebab-case', () => { |
| 150 | + expect(toKebabCase('helloWorld')).toBe('hello-world'); |
| 151 | + }); |
| 152 | + |
| 153 | + test('converts PascalCase to kebab-case', () => { |
| 154 | + expect(toKebabCase('HelloWorld')).toBe('hello-world'); |
| 155 | + }); |
| 156 | + |
| 157 | + test('converts snake_case to kebab-case', () => { |
| 158 | + expect(toKebabCase('hello_world')).toBe('hello-world'); |
| 159 | + }); |
| 160 | + |
| 161 | + test('converts space separated to kebab-case', () => { |
| 162 | + expect(toKebabCase('hello world')).toBe('hello-world'); |
| 163 | + }); |
| 164 | + |
| 165 | + test('handles mixed case with numbers', () => { |
| 166 | + expect(toKebabCase('version2APIKey')).toBe('version-2-api-key'); |
| 167 | + }); |
| 168 | + |
| 169 | + test('handles already kebab-case strings', () => { |
| 170 | + expect(toKebabCase('hello-world')).toBe('hello-world'); |
| 171 | + }); |
| 172 | + |
| 173 | + test('handles multiple separators together', () => { |
| 174 | + expect(toKebabCase('hello___world--great')).toBe('hello-world-great'); |
| 175 | + }); |
| 176 | + |
| 177 | + test('handles empty string', () => { |
| 178 | + expect(toKebabCase('')).toBe(''); |
| 179 | + }); |
| 180 | + |
| 181 | + test('removes leading and trailing hyphens', () => { |
| 182 | + expect(toKebabCase('-hello-world-')).toBe('hello-world'); |
| 183 | + }); |
| 184 | + |
| 185 | + test('handles single word', () => { |
| 186 | + expect(toKebabCase('hello')).toBe('hello'); |
| 187 | + }); |
| 188 | + |
| 189 | + test('handles consecutive capitals', () => { |
| 190 | + expect(toKebabCase('HTTPSConnection')).toBe('https-connection'); |
| 191 | + }); |
| 192 | +}); |
| 193 | + |
| 194 | +describe('toConstantCase', () => { |
| 195 | + test('converts camelCase to CONSTANT_CASE', () => { |
| 196 | + expect(toConstantCase('helloWorld')).toBe('HELLO_WORLD'); |
| 197 | + }); |
| 198 | + |
| 199 | + test('converts PascalCase to CONSTANT_CASE', () => { |
| 200 | + expect(toConstantCase('HelloWorld')).toBe('HELLO_WORLD'); |
| 201 | + }); |
| 202 | + |
| 203 | + test('converts kebab-case to CONSTANT_CASE', () => { |
| 204 | + expect(toConstantCase('hello-world')).toBe('HELLO_WORLD'); |
| 205 | + }); |
| 206 | + |
| 207 | + test('converts snake_case to CONSTANT_CASE', () => { |
| 208 | + expect(toConstantCase('hello_world')).toBe('HELLO_WORLD'); |
| 209 | + }); |
| 210 | + |
| 211 | + test('converts space separated to CONSTANT_CASE', () => { |
| 212 | + expect(toConstantCase('hello world')).toBe('HELLO_WORLD'); |
| 213 | + }); |
| 214 | + |
| 215 | + test('handles mixed case with numbers', () => { |
| 216 | + expect(toConstantCase('version2APIKey')).toBe('VERSION_2_API_KEY'); |
| 217 | + }); |
| 218 | + |
| 219 | + test('handles already CONSTANT_CASE strings', () => { |
| 220 | + expect(toConstantCase('HELLO_WORLD')).toBe('HELLO_WORLD'); |
| 221 | + }); |
| 222 | + |
| 223 | + test('handles multiple separators together', () => { |
| 224 | + expect(toConstantCase('hello___world--great')).toBe('HELLO_WORLD_GREAT'); |
| 225 | + }); |
| 226 | + |
| 227 | + test('handles empty string', () => { |
| 228 | + expect(toConstantCase('')).toBe(''); |
| 229 | + }); |
| 230 | + |
| 231 | + test('removes leading and trailing underscores', () => { |
| 232 | + expect(toConstantCase('_hello_world_')).toBe('HELLO_WORLD'); |
| 233 | + }); |
| 234 | + |
| 235 | + test('handles single word', () => { |
| 236 | + expect(toConstantCase('hello')).toBe('HELLO'); |
| 237 | + }); |
| 238 | + |
| 239 | + test('handles consecutive capitals', () => { |
| 240 | + expect(toConstantCase('HTTPSConnection')).toBe('HTTPS_CONNECTION'); |
| 241 | + }); |
| 242 | +}); |
0 commit comments