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'
注意事項
- 首字母小寫: 駝峰命名總是以小寫字母開頭
- 分隔符: 支持空格、連字符、下劃線作為分隔符
- 數字: 數字會被保留,但不會作為分隔符
- 特殊字符: 非分隔符的特殊字符會被保留
與其他函數的區別
camel
: 轉換為駝峰命名(首字母小寫)pascal
: 轉換為帕斯卡命名(首字母大寫)snake
: 轉換為蛇形命名kebab
: 轉換為短橫線命名
性能
- 時間復雜度: O(n),其中 n 是字符串長度
- 空間復雜度: O(n)
- 適用場景: 變量命名、屬性名轉換