Case anything 🐫
npm i case-anything
camelCase, kebabCase, ... 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...
I wanted to try my hand at the smallest iteration possible.
Meet the family
Usage
case-anything supports tree-shaking.
import { camelCase, pascalCase, kebabCase, snakeCase, constantCase, pathCase } from 'case-anything'
const testString = 'PonytaVaporeon_Poliwrath-BUTTERFREE'
camelCase('ponyta-vaporeon-poliwrath-butterfree')
=== 'ponytaVaporeonPoliwrathButterfree'
pascalCase('ponytaVaporeonPoliwrathButterfree')
=== 'PonytaVaporeonPoliwrathButterfree'
kebabCase('ponytaVaporeonPoliwrathButterfree')
=== 'ponyta-vaporeon-poliwrath-butterfree'
snakeCase('ponytaVaporeonPoliwrathButterfree')
=== 'ponyta_vaporeon_poliwrath_butterfree'
constantCase('ponytaVaporeonPoliwrathButterfree')
=== 'PONYTA_VAPOREON_POLIWRATH_BUTTERFREE'
pathCase('ponytaVaporeonPoliwrathButterfree')
=== 'Ponyta/Vaporeon/Poliwrath/BUTTERFREE'
Package size
We'll compare this package with blakeembrey/change-case, a very famous package on npm.
| case-anything | Second Header |
---|
camelCase | 1.1K (572) | 27.2K (6K) |
pascalCase | 1.1K (561) | 27.4K (6.1K) |
kebabCase | 1.1K (541) | 26.8K (5.9K) |
snakeCase | 1.1K (540) | 26.8K (5.9K) |
constantCase | 1.1K (540) | 27.2K (6K) |
pathCase | 1K (530) | 26.8K (5.9K) |
Source code
It is literally just this code:
function getParts (string) {
return string.match(/^[a-z]+|[A-Z][a-z]+|[A-Z]+|[a-z]+/g)
}
export function camelCase (string) {
return getParts(string)
.reduce((result, match, index) => {
return (index === 0)
? match.toLowerCase()
: result + match[0].toUpperCase() + match.slice(1).toLowerCase()
}, '')
}
export function kebabCase (string) {
return getParts(string)
.join('-').toLowerCase()
}