Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

🐫 Swift Case Conversions — camelCase PascalCase UpperCamelCase kebab-case snake_case CONSTANT_CASE Train-Case Ada_Case COBOL-CASE Dot.notation Path/case Space case Capital Case lower case UPPER CASE

License

NotificationsYou must be signed in to change notification settings

mesqueeb/CaseAnything

Repository files navigation

.package(url: "https://github.com/mesqueeb/CaseAnything", from: "0.1.5")

14 case changing functions: camelCase, kebab-case, PascalCaseand more...
A simple integration with nano package size. (SMALL footprint!)

Motivation

I created this package because the other package that change cases do not cover all the 14 possibilities mine does.

Some features I focused on:

  • small footprint
  • awesome code popup documentation
  • complete coverage with unit testing
  • 0 dependencies
  • CaseAnything is used in...

  • Famous Mac appPopclip 💊
  • Lottie-player ∫
  • OpenAPI CLIPortman 👨🏽‍🚀
  • and100s more...
  • Usage

    import CaseAnything

    CaseAnything has different behaviour if the string you pass has spaces or not.

    • Without spaces it will split and format on every "part" it can detect
    • With spaces it will split and format on every "word" based on those spaces

    Strings without spaces

    NameInput exampleOutput example
    🐪 camelCasecamelCase("$catDog")catDog
    🐫 PascalCase
    UpperCamelCase
    pascalCase("$catDog")
    upperCamelCase("$catDog")
    CatDog
    🥙 kebab-casekebabCase("$catDog")cat-dog
    🐍 snake_casesnakeCase("$catDog")cat_dog
    📣 CONSTANT_CASEconstantCase("$catDog")CAT_DOG
    🚂 Train-CasetrainCase("$catDog")Cat-Dog
    🕊 Ada_CaseadaCase("$catDog")Cat_Dog
    👔 COBOL-CASEcobolCase("$catDog")CAT-DOG
    📍 Dot.notationdotNotation("$catDog")cat.Dog
    📂 Path/casepathCase("$catDog")$cat/Dog
    🛰 Space casespaceCase("$catDog")$cat Dog
    🏛 Capital CasecapitalCase("$catDog")$Cat Dog
    🔡 lower caselowerCase("$catDog")$cat dog
    🔠 UPPER CASEupperCase("$catDog")$CAT DOG

    Special Characters

    You can see that most functions by default remove special characters, and some functions keep special characters.

    functions thatremove special characters*functions thatkeep special characters*
  • camelCase
  • pascalCase
  • kebabCase
  • snakeCase
  • constantCase
  • trainCase
  • adaCase
  • cobolCase
  • dotNotation
  • pathCase
  • spaceCase
  • capitalCase
  • lowerCase
  • upperCase
  • *You can control wether or not tokeep or remove special characters like so:

    // default:camelCase("$catDog")=="catDog"// force keeping special characters:camelCase("$catDog", keepSpecialCharacters:true)=="$catDog"// default:pathCase("$catDog")=="$cat/Dog"// force removing special characters:pathCase("$catDog", keepSpecialCharacters:false)=="cat/Dog"

    Case Changing

    These casesdo not change the casing of the words:

    • dotNotation
    • pathCase
    • spaceCase
    // default:dotNotation("$catDog")=="cat.Dog"// force lower case:dotNotation("$catDog").toLowerCase()=="cat.dog"

    Strings with spaces

    As soon as there is a space in the target string, it will regard the input as asentence and only split each part at the spaces.

    NameInput exampleOutput example
    🐪 camelCasecamelCase("I'm O.K.!")imOk
    🐫 PascalCase
    UpperCamelCase
    pascalCase("I'm O.K.!")
    upperCamelCase("I'm O.K.!")
    ImOk
    🥙 kebab-casekebabCase("I'm O.K.!")im-ok
    🐍 snake_casesnakeCase("I'm O.K.!")im_ok
    📣 CONSTANT_CASEconstantCase("I'm O.K.!")IM_OK
    🚂 Train-CasetrainCase("I'm O.K.!")Im-Ok
    🕊 Ada_CaseadaCase("I'm O.K.!")Im_Ok
    👔 COBOL-CASEcobolCase("I'm O.K.!")IM-OK
    📍 Dot.notationdotNotation("I'm O.K.!")Im.OK
    📂 Path/casepathCase("I'm O.K.!")I'm/O.K.!
    🛰 Space casespaceCase("I'm O.K.!")I'm O.K.!
    🏛 Capital CasecapitalCase("I'm O.K.!")I'm O.k.!
    🔡 lower caselowerCase("I'm O.K.!")i'm o.k.!
    🔠 UPPER CASEupperCase("I'm O.K.!")I'M O.K.!

    Also note, that multiple sequential spaces are treated as one space.

    Keep only certain special characters

    Instead of removing all special characters, you can opt to keep some special characters.

    In the example below we see:

    • input:$cat-dog
    • desired output:$CatDog
    pascalCase("$cat-dog", keepSpecialCharacters:false)// CatDog   → not what we wantpascalCase("$cat-dog", keepSpecialCharacters:true)// $Cat-Dog → not what we wantpascalCase("$cat-dog", keep:["$"])// $CatDog  → desired output

    Convert special characters into alphabet

    I have extended regular alphabet with the most commonLatin-1 Supplement special characters.

    The coolest thing about this library is that it will"convert" special characters into regular alphabet for the cases used as variable names! 😎

    // CONVERTS special characters:camelCase("Çâfé Ågård")=="cafeAgard"pascalCase("Çâfé Ågård")=="CafeAgard"kebabCase("Çâfé Ågård")=="cafe-agard"snakeCase("Çâfé Ågård")=="cafe_agard"constantCase("Çâfé Ågård")=="CAFE_AGARD"trainCase("Çâfé Ågård")=="Cafe-Agard"adaCase("Çâfé Ågård")=="Cafe_Agard"cobolCase("Çâfé Ågård")=="CAFE-AGARD"dotNotation("Çâfé Ågård")=="Cafe.Agard"// DOES NOT convert special characters:spaceCase("Çâfé Ågård")=="Çâfé Ågård"pathCase("Çâfé Ågård")=="Çâfé/Ågård"lowerCase("Çâfé Ågård")=="çâfé ågård"upperCase("Çâfé Ågård")=="ÇÂFÉ ÅGÅRD"capitalCase("Çâfé Ågård")=="Çâfé Ågård"

    Code Docs

    I have made sure there is great documentation available on hover!

    docs preview

    Keyboard shortcuts

    WithBetter Touch Tool you can set up keyboard shortcuts to convert selected text with JavaScript. This repo provides an easy to install preset that has shortcuts for pascal, kebab and camel case! (thanks to@AndrewKoch) It even supports multi-cursors in VSCode!

    Here is an example triggering keyboard shortcuts to convert the selected text to PascalCase; kebab-case; camelCase:

    keyboard shortcuts example

    You can download the BTT preset from the source code:CaseAnything.bttpreset.

    Source code

    What keeps my package small, is that literally just uses a regex to separate "words".

    // the source code is similar to:publicfunc splitOnSpecialChars(string:String)->[String]{letsplitRegex=try!NSRegularExpression(pattern:"^[a-z]+|[A-Z][a-z]+|[a-z]+|[0-9]+|[A-Z]+(?![a-z])")return splitRegex.matches(in: string, range:NSRange(string.startIndex..., in: string))}

    The actual regex used is a little bit more comprehensive and can be foundhere.

    About

    🐫 Swift Case Conversions — camelCase PascalCase UpperCamelCase kebab-case snake_case CONSTANT_CASE Train-Case Ada_Case COBOL-CASE Dot.notation Path/case Space case Capital Case lower case UPPER CASE

    Topics

    Resources

    License

    Stars

    Watchers

    Forks

    Sponsor this project

     

    Packages

    No packages published

    Languages


    [8]ページ先頭

    ©2009-2025 Movatter.jp