change_case 2.2.0 copy "change_case: ^2.2.0" to clipboard
change_case: ^2.2.0 copied to clipboard

An extension on String for the missing methods for camelCase, PascalCase, Capital Case, snake_case, param-case, CONSTANT_CASE and others.

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'
copied to clipboard

toCapitalCase #

Transform into a space separated string with each word capitalized.

'test string'.toCapitalCase(); // 'Test String'
copied to clipboard

toConstantCase #

Transform into upper case string with an underscore between words.

'test string'.toConstantCase(); // 'TEST_STRING'
copied to clipboard

toDotCase #

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

'test string'.toDotCase(); // 'test.string'
copied to clipboard

toHeaderCase #

Transform into a dash separated string of capitalized words.

'test string'.toHeaderCase(); // 'Test-String'
copied to clipboard

toNoCase #

Transform into a lower cased string with spaces between words.

'testString'.toNoCase(); // 'test string'
copied to clipboard

toParamCase #

Transform into a lower cased string with dashes between words.

'test string'.toParamCase(); // 'test-string'
copied to clipboard

toPascalCase #

Transform into a string of capitalized words without separators.

'test string'.toPascalCase(); // 'TestString'
copied to clipboard

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'

copied to clipboard

toSentenceCase #

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

'testString'.toSentenceCase(); // 'Test string'
copied to clipboard

toSnakeCase #

Transform into a lower case string with underscores between words.

'test string'.toSnakeCase(); // 'test_string'
copied to clipboard

Other Case Utilities #

toTitleCase #

Transform a string into title case following English rules.

'a simple test'.toTitleCase(); // 'A Simple Test'
copied to clipboard

toSwapCase #

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

'Test String'.toSwapCase(); // 'tEST sTRING'
copied to clipboard

isLowerCase #

Returns true if the string is lower case only.

'test string'.isLowerCase(); // true
copied to clipboard

isUpperCase #

Returns true if the string is upper case only.

'test string'.isUpperCase(); // false
copied to clipboard

toLowerFirstCase #

Transforms the string with the first character in lower cased.

'TEST'.toLowerFirstCase(); // 'tEST'
copied to clipboard

toUpperFirstCase #

Transforms the string with the first character in upper cased.

'test'.toUpperFirstCase(); // 'Test'
copied to clipboard

toSpongeCase #

Transform into a string with random capitalization applied.

'Test String'.toSpongeCase(); // 'tEst stRINg'
copied to clipboard

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])')
      
      copied to clipboard
    • ChangeCaseConfig.lowerOrNumToUpperPattern

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

    • ChangeCaseConfig.upperToLowerPattern

      // matches any non-alphanumeric char
      RegExp('[^A-Z0-9]+', caseSensitive: false)
      
      copied to clipboard
    • 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])')
      
      copied to clipboard

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"
copied to clipboard

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

57
likes
160
points
75k
downloads

Publisher

verified publishermrgnhnt.com

Weekly Downloads

2024.09.13 - 2025.03.28

An extension on String for the missing methods for camelCase, PascalCase, Capital Case, snake_case, param-case, CONSTANT_CASE and others.

Repository (GitHub)

Topics

#formatting #case #casing #camelcase #snakecase

Documentation

API reference

License

MIT (license)

Dependencies

meta

More

Packages that depend on change_case