搜尋套件、儲存庫與 APP 分類

套件詳情v2.2.0

change_case

一個用於補充 camelCase、PascalCase、首字母大寫、snake_case、param-case、CONSTANT_CASE 等遺漏方法的 String 擴展。

AndroidiOSWebmacOSWindowsLinux
近 30 天下載
18.1萬
喜歡數
71
Pub 分數
160

安裝套件

flutter pub add change_case

套件介紹

根據套件公開資料產生的選型評估,僅供參考。

change_case 套件透過為 Dart 中的 String 類別新增便利方法,擴展了文字在各種大小寫格式之間轉換的功能,包含 camelCase、PascalCase、snake_case、param-case 與 CONSTANT_CASE。它簡化了應用程式開發中常見的字串轉換任務,例如格式化 API 回應、產生動態識別碼,或確保程式碼庫中命名慣例的一致性。此套件輕量且支援所有主要平台,非常適合需要強大文字處理功能的跨平台 Flutter 應用。

適用情境

  • 格式化 API 回應鍵
  • 產生一致的變數名稱
  • 將使用者輸入轉換為標準化格式
  • 建立友好的URL字串
  • 處理設定鍵命名
  • 簡化程式碼產生工作流程

主要特色

  • 擴展 String 以提供轉換方法
  • 支援多種大小寫格式
  • 輕量且易於整合
  • 跨平台相容性
  • 無外部依賴
  • 乾淨、易讀的語法

適合對象

  • 需要字串格式化的Flutter開發人員
  • 具有嚴格命名約定的專案
  • API資料處理管道
  • 程式碼產生工具
  • 需要 URL 或識別碼規範化應用

使用建議

  • 不處理複雜的語言規則
  • 限於預定義的大小寫模式
  • 不適合進階文字處理
  • 需要手動處理邊緣情況
  • 無內建的無效輸入驗證

原始 README

以下為關聯原始碼儲存庫的 README 原文快照,最新內容請造訪原始碼儲存庫。

Change Case

style: very good analysis License: MIT codecov style: effective dart

Transform a string between camelCase, PascalCase, Capital Case, snake_case, param-case, CONSTANT_CASE and others.


Core

These functions come with package:change_case

toCamelCase

Transform into a string with the separator denoted by the next word capitalized.

'test string'.toCamelCase(); // 'testString'
toCapitalCase

Transform into a space separated string with each word capitalized.

'test string'.toCapitalCase(); // 'Test String'
toConstantCase

Transform into upper case string with an underscore between words.

'test string'.toConstantCase(); // 'TEST_STRING'
toDotCase

Transform into a lower case string with a period between words.

'test string'.toDotCase(); // 'test.string'
toHeaderCase

Transform into a dash separated string of capitalized words.

'test string'.toHeaderCase(); // 'Test-String'
toNoCase

Transform into a lower cased string with spaces between words.

'testString'.toNoCase(); // 'test string'
toParamCase

Transform into a lower cased string with dashes between words.

'test string'.toParamCase(); // 'test-string'
toPascalCase

Transform into a string of capitalized words without separators.

'test string'.toPascalCase(); // 'TestString'
toPathCase

Transform into a lower case string with slashes between words.

'test string'.toPathCase(); // 'test/string'

// provide a separator override default '/'
'test string'.toPathCase(r'\'); // 'test\string'

toSentenceCase

Transform into a lower case with spaces between words, then capitalize the string.

'testString'.toSentenceCase(); // 'Test string'
toSnakeCase

Transform into a lower case string with underscores between words.

'test string'.toSnakeCase(); // 'test_string'

Other Case Utilities

toTitleCase

Transform a string into title case following English rules.

'a simple test'.toTitleCase(); // 'A Simple Test'
toSwapCase

Transform a string by swapping every character from upper to lower case, or lower to upper case.

'Test String'.toSwapCase(); // 'tEST sTRING'
isLowerCase

Returns true if the string is lower case only.

'test string'.isLowerCase(); // true
isUpperCase

Returns true if the string is upper case only.

'test string'.isUpperCase(); // false
toLowerFirstCase

Transforms the string with the first character in lower cased.

'TEST'.toLowerFirstCase(); // 'tEST'
toUpperFirstCase

Transforms the string with the first character in upper cased.

'test'.toUpperFirstCase(); // 'Test'
toSpongeCase

Transform into a string with random capitalization applied.

'Test String'.toSpongeCase(); // 'tEst stRINg'

Configuration

package:change_case has a configuration file, ChangeCaseConfig, that you can use to update splitPatterns, stripPatterns, and the placeholder which will be applied to most function listed above.

I don't suggest changing these unless you know what you are doing 😁

Patterns

Change case uses RegExp to split & replace characters of the string. While they are great at what they do, you may want to update or add new patterns.

These patterns can be updated by using the ChangeCaseConfig.setUp(splitPatterns: [...], stripPatterns: [...])

The default patterns are:

Note: (?:•)* is used to match any "•" (placeholder) character.

  • Split

    • ChangeCaseConfig.lowerOrNumToUpperPattern

      // matches lowercase or numeric char to uppercase char
      RegExp('([a-z0-9])(?:•)*([A-Z])')
      
    • ChangeCaseConfig.lowerOrNumToUpperPattern

      // matches uppercase char to uppercase followed by lowercase char
      RegExp('([A-Z])(?:•)*([A-Z][a-z])')
      
  • Strip

    • ChangeCaseConfig.upperToLowerPattern

      // matches any non-alphanumeric char
      RegExp('[^A-Z0-9]+', caseSensitive: false)
      
    • ChangeCaseConfig.lowerToNumOrUpperPattern (Not included in default, but can be used to add or replace)

      // matches lowercase char to numeric or uppercase char
      // changes the behavior to `word2019 -> word 2019` and `minifyURLs -> minify urls`
      RegExp('([a-z])([A-Z0-9])')
      

Placeholder

can be configured with ChangeCaseConfig.setUp(placeholder: ...)

The placeholder is a string that is added and used as a reference to configure each case.

The default placeholder is "•"

For example:

final string = 'test stringCase';
print(string.toCamelCase()); // expected: 'testStringCase'

// runs splitPatterns, returns "test string•Case"
// runs stripPatterns, returns "test•string•Case"
// runs camel case logic, returns "testStringCase"

This string needs to be a unique character that is not used in the string.

Related

所屬分類

依賴

依賴清單 1 項

原始碼儲存庫

使用此套件的專案