3.1.0 • Published 5 months ago

case-anything v3.1.0

Weekly downloads
2,884
License
MIT
Repository
github
Last release
5 months ago

Case anything 🐫

npm i case-anything

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

Motivation

I created this package because most other packages that do simple case changing are so big...

Usage

case-anything supports tree-shaking and is side-effect free!

// just import the functions you need like so:
import { camelCase, kebabCase } from 'case-anything'

case-anything 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
🐫 PascalCaseUpperCamelCasepascalCase('$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.

*You can control wether or not to keep 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 cases do 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 a sentence and only split each part at the spaces.

NameInput exampleOutput example
🐪 camelCasecamelCase("I'm O.K.!")imOk
🐫 PascalCaseUpperCamelCasepascalCase("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 want

pascalCase('$cat-dog', { keepSpecialCharacters: true })
// $Cat-Dog → not what we want

pascalCase('$cat-dog', { keep: ['$'] })
// $CatDog  → desired output

Convert special characters into alphabet

I have extended regular alphabet with the most common Latin-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'

JSDocs

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

jsdocs preview

Keyboard shortcuts

With Better 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: case-anything.bttpreset.

Package size

We'll compare this package with blakeembrey/change-case, a very famous package on npm.

case-anythingchange-case
camelCase1.1K (572)27.2K (6K)
pascalCase1.1K (561)27.4K (6.1K)
kebabCase1.1K (541)26.8K (5.9K)
snakeCase1.1K (540)26.8K (5.9K)
constantCase1.1K (540)27.2K (6K)
pathCase1K (530)26.8K (5.9K)

Source code

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

// the source code is similar to:
export function splitOnSpecialChars(string: string): any[] {
  return string.match(/^[a-z]+|[A-Z][a-z]+|[a-z]+|[0-9]+|[A-Z]+(?![a-z])/g)
}

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

Meet the family (more tiny utils with TS support)

@apeiwan/pi-playds-actions@everything-registry/sub-chunk-1297@translated/mymodelsvue-intellisensesammichruckenvoluptatesporrojsonthisidl-for-vscodeormgenopenapi-formatpino-logfmtpi-playnode-opcua-convert-nodeset-to-javascriptquasar-ui-component-info-cardquasar-ui-easy-formsresgensocgts-protots-proto-with-defaults@alvadorncorp/nestjs-serializer-interceptor@blitzar/components@blitzar/form@catalysisdev/core@tenrok/vue-intellisense@tenrok/vue-intellisense-scripts@boydaihungst/vue-intellisense-scripts@deep-foundation/sdk@deep-foundation/deepcase-app@dcl/ts-proto@danils/project-builder-cli@flatfile/configure@eventstore-ui/icon-manager@gestaltjs/core@infinitebrahmanuniverse/nolb-case@lottiefiles/eslint-plugin@lightprotocol/cli@lightprotocol/zk-compression-cli@lightprotocol/zk.js@mtvproject/ts-proto@nestjs-mod/common@nestjs-mod/docker-compose@nestjs-mod/flyway@nestjs-mod/prisma@nestjs-mod/reports@nestjs-mod/schematics@monolayer/factor-four@monolayer/pg@jchpro/nest-mongoose@jdpnielsen/assemble@jsxui/react@jsxui/system@jsxui/csscrossnotedank-twitch-apiconficcase-naming-convertercdsgen@pbuilder/astro-cli@pbuilder/clieslint-plugin-vue-kebab-class-naming@proto-graphql/codegen-core@regen-network/ts-proto@terminal-nerds/snippets-string@techsky/map-object@techsky/object-keys-case@poppinss/utils@pikabohenkeai/ts-proto@planetar/doc-builder@planetar/example-card@planetar/api-card@yaegassy/coc-laravel@zalastax/nolb-case@vinsurs/case-anything@vkcn/eslint-plugin@vue-intellisense/scripts@viteplay/plugin@xeito/cli@sudophunk/v4-client@swiss-army-knife/utilitiesbbl-vue-intellisenseasyncapi-format@remirror/core-helpers@sentio/ts-protofacilisdignissimos
3.1.0

5 months ago

3.0.1

5 months ago

3.0.0

5 months ago

2.1.12

1 year ago

2.1.13

1 year ago

2.1.11

2 years ago

1.1.5

3 years ago

1.1.4

3 years ago

2.1.2

3 years ago

2.1.1

3 years ago

2.1.4

3 years ago

2.1.3

3 years ago

2.1.6

3 years ago

2.1.5

3 years ago

2.1.8

3 years ago

2.1.7

3 years ago

2.1.0

3 years ago

2.0.0

3 years ago

2.1.9

3 years ago

2.1.10

3 years ago

1.1.3

4 years ago

1.1.2

4 years ago

1.1.1

5 years ago

1.0.2

5 years ago

1.1.0

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago

0.3.1

5 years ago

0.3.0

5 years ago

0.2.0

5 years ago

0.1.0

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago

0.0.0

5 years ago