Casex is now part of DXTR.DEV
I (Pedro) am migrating several of my packages to my new company DXTR.DEV, where I'll ship tools and apps that improve developer experience.Besides being moved, Casex now has Typescript types and a new function signature with named exports.
New GitHub repo: https://github.com/dxtr-dot-dev/casex
NPM package: @dxtr.dev/casex
Demo
Previous versions
Although for most cases it will work just fine, casex 3.x is not fully compatible previous versions. If you need previous docs please refer to:Install
š¦ 343B gzipednpm install --save casex
or https://unpkg.com/casex
Usage
import casex from 'casex';
casex(text, pattern);
casex('john doe', 'Ca Se'); // John Doe
How it works
- Breaking text into words
By default, casex uses capitalizations (A-Z
), -
, _
and spaces (\s
) to break the text into words.Let's take for example
i_am the-real JohnDoe
:i
: 1st wordam
: 2nd+ wordthe
: 2nd+ wordreal
: 2nd+ wordJohn
: 2nd+ wordDoe
: 2nd+ word
1.1 Custom delimiters
The default will likely work for most of your cases, but if you wish, you can provide custom delimiters:casex('foo.bar,baz', 'Ca Se', '.,'); // Foo Bar Baz
Note: The default delimiters are:
A-Z\\s_-
.- Applying capitalization pattern and gluing words together
Let's take for example Ca_se
:C
: first letter of the first worda
: second and subsequent letters of the first word_
: anything between the first two and last two letters isglue
and will be repeted between wordss
: first letter of the second and subsequent wordse
: second and subsequent letters of the second and subsequent words
Confusing? Check out the demo and/or examples below. I'm sure you'll get the hang of it :)
Note: You could use any other letters to describe, such as
aa$aa
or na_me
. What matters is that it takes the first two and last two letters for checking capitalization and whatever is in the middle is "glue".2.1 Special transformations
Besides using lower and uppercase letters, you can also use:*
: Do not change word-
: Remove word
Examples
For these examples I'll use the texti_am the-real JohnDoe
lowercase
- Pattern: case
- Output: iamtherealjohndoe
UPPERCASE
- Pattern: CASE
- Output: IAMTHEREALJOHNDOE
snakecase
- Pattern: case
- Output: i
spinal-case
- Pattern: ca-se
- Output: i-am-the-real-john-doe
camelCase
- Pattern: caSe
- Output: iAmTheRealJohnDoe
UpperCamelCase
- Pattern: CaSe
- Output: IAmTheRealJohnDoe
Sentence case
- Pattern: Ca se
- Output: I am the real john doe
Title Case
- Pattern: Ca Se
- Output: I Am The Real John Doe
Weird Example
- Pattern: Ca12 34Se
- Output: I12 34Am12 34The12 34Real12 34John12 34Doe
Examples with special characters
Capitalize first letter- Pattern: C\* \*\
- Output: I am the real John Doe
Initials
- Input: John Doe
- Pattern: C-S-
- Output: JD