Skip to content

camel

将字符串转换为驼峰命名格式。

语法

typescript
camel(str: string): string

参数

  • str (string): 要转换的字符串

返回值

  • string: 驼峰命名格式的字符串

示例

基本用法

typescript
import { camel } from 'radash'

camel('hello world') // 'helloWorld'
camel('hello-world') // 'helloWorld'
camel('hello_world') // 'helloWorld'
camel('Hello World') // 'helloWorld'

处理各种分隔符

typescript
import { camel } from 'radash'

camel('user-name') // 'userName'
camel('user_name') // 'userName'
camel('user name') // 'userName'
camel('userName') // 'userName'
camel('UserName') // 'userName'

处理特殊字符

typescript
import { camel } from 'radash'

camel('first-name') // 'firstName'
camel('last_name') // 'lastName'
camel('email address') // 'emailAddress'
camel('phone-number') // 'phoneNumber'
camel('date_of_birth') // 'dateOfBirth'

处理数字

typescript
import { camel } from 'radash'

camel('user-123') // 'user123'
camel('api-v2') // 'apiV2'
camel('version-1-0') // 'version10'
camel('test-123-abc') // 'test123Abc'

处理缩写

typescript
import { camel } from 'radash'

camel('user-id') // 'userId'
camel('api-url') // 'apiUrl'
camel('http-request') // 'httpRequest'
camel('json-data') // 'jsonData'

处理连续分隔符

typescript
import { camel } from 'radash'

camel('hello--world') // 'helloWorld'
camel('hello__world') // 'helloWorld'
camel('hello  world') // 'helloWorld'
camel('hello---world') // 'helloWorld'

处理首字母大写

typescript
import { camel } from 'radash'

camel('Hello World') // 'helloWorld'
camel('HELLO WORLD') // 'helloWorld'
camel('HelloWorld') // 'helloWorld'
camel('HELLO_WORLD') // 'helloWorld'

处理空字符串

typescript
import { camel } from 'radash'

camel('') // ''
camel('   ') // ''
camel('---') // ''
camel('___') // ''

处理单个单词

typescript
import { camel } from 'radash'

camel('hello') // 'hello'
camel('Hello') // 'hello'
camel('HELLO') // 'hello'
camel('world') // 'world'

处理复杂字符串

typescript
import { camel } from 'radash'

camel('user-profile-settings') // 'userProfileSettings'
camel('api-endpoint-config') // 'apiEndpointConfig'
camel('database-connection-string') // 'databaseConnectionString'
camel('http-request-handler') // 'httpRequestHandler'

处理包含数字的字符串

typescript
import { camel } from 'radash'

camel('user-123-profile') // 'user123Profile'
camel('api-v2-endpoint') // 'apiV2Endpoint'
camel('test-1-2-3') // 'test123'
camel('version-1-0-0') // 'version100'

处理特殊符号

typescript
import { camel } from 'radash'

camel('user@domain.com') // 'user@domain.com'
camel('file-name.txt') // 'fileName.txt'
camel('path/to/file') // 'path/to/file'
camel('query?param=value') // 'query?param=value'

处理Unicode字符

typescript
import { camel } from 'radash'

camel('café-latte') // 'caféLatte'
camel('naïve-user') // 'naïveUser'
camel('résumé-data') // 'résuméData'
camel('über-user') // 'überUser'

注意事项

  1. 首字母小写: 驼峰命名总是以小写字母开头
  2. 分隔符: 支持空格、连字符、下划线作为分隔符
  3. 数字: 数字会被保留,但不会作为分隔符
  4. 特殊字符: 非分隔符的特殊字符会被保留

与其他函数的区别

  • camel: 转换为驼峰命名(首字母小写)
  • pascal: 转换为帕斯卡命名(首字母大写)
  • snake: 转换为蛇形命名
  • kebab: 转换为短横线命名

性能

  • 时间复杂度: O(n),其中 n 是字符串长度
  • 空间复杂度: O(n)
  • 适用场景: 变量命名、属性名转换

Released under the MIT License.