# casex

> All in one, self expressive function for string case styles

Latest version **4.0.2** (published 2024-01-13) · MIT license · 0 weekly downloads

## Install

```sh
npm install casex
pnpm add casex
yarn add casex
bun add casex
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 4.0.2 |
| Published | 2024-01-13 |
| First published | 2018-04-04 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 6.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 106 |
| Author | Pedro Moreira |
| Maintainers | pedsmoreira |
| Keywords | casex, case, convert, string, text, string styles, case styles, case style pattern, separator |

## Links

- npm: https://www.npmjs.com/package/casex
- Repository: https://github.com/pedsmoreira/casex
- Homepage: https://github.com/pedsmoreira/casex#readme
- Issues: https://github.com/pedsmoreira/casex/issues
- npm.io page: https://npm.io/package/casex

## Alternatives

- [@mce/gif](https://npm.io/package/@mce/gif.md) — 2.6K weekly downloads
- [cleanse](https://npm.io/package/cleanse.md) — 173 weekly downloads
- [str](https://npm.io/package/str.md) — 127 weekly downloads
- [naming](https://npm.io/package/naming.md) — 95 weekly downloads
- [tap-telco-api](https://npm.io/package/tap-telco-api.md) — 19 weekly downloads

## Recent versions

- 4.0.2 (latest) — 2024-01-13
- 4.0.1 — 2024-01-13
- 4.0.0 — 2024-01-13
- 3.0.2 — 2023-01-21
- 3.0.1 — 2023-01-21
- 3.0.0 — 2018-10-15
- 2.0.0 — 2018-08-02
- 1.0.1 — 2018-07-25
- 1.0.0 — 2018-07-25
- 0.0.5 — 2018-06-07
- 0.0.3 — 2018-04-05
- 0.0.2 — 2018-04-04
- 0.0.1 — 2018-04-04

## README

# casex

All in one, self expressive pattern for string case styles

## Introduction

`casex` is a function that applies a case style given a pattern.

Instead of having a specific function for each case style, I provide a self-expressive pattern that represent the desired output. For example, kebab-case can be represented as `ca-se` and title case as `Ca Se`.

## Install

```sh
npm install --save casex
```

## Usage

```js
import { casex } from "casex";

casex({ text: "your text", pattern: "ca-se" }); // your-text
```

## How it works

### 1. 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 word
- `am`: 2nd+ word
- `the`: 2nd+ word
- `real`: 2nd+ word
- `John`: 2nd+ word
- `Doe`: 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:

```js
casex({ text: "foo.bar,baz", pattern: "Ca Se", delimiters: ".," }); // Foo Bar Baz
```

_Note: The default delimiters are: `A-Z\\s_-`.

### 2. Applying capitalization pattern and gluing words together

Let's take for example `Ca_se`:

- `C`: first letter of the first word
- `a`: second and subsequent letters of the first word
- `_`: anything between the first two and last two letters is `glue` and will be repeted between words
- `s`: first letter of the second and subsequent words
- `e`: second and subsequent letters of the second and subsequent words

_Note: You can use any other letters to describe the pattern, 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 text `i_am the-real JohnDoe`

**lowercase**

- Pattern: case
- Output: iamtherealjohndoe

**UPPERCASE**

- Pattern: CASE
- Output: IAMTHEREALJOHNDOE

**snake_case**

- Pattern: ca_se
- Output: i_am_the_real_john_doe

**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

## Previous versions

Although for most cases it will work just fine, `casex` 4.x is not fully compatible previous versions. If you need previous docs please refer to:

- [v0.x](https://github.com/pedsmoreira/casex/tree/0.x)
- [v1.x](https://github.com/pedsmoreira/casex/tree/1.x)
- [v2.x](https://github.com/pedsmoreira/casex/tree/2.x)
- [v3.x](https://github.com/pedsmoreira/casex/tree/3.x)

## License

casex is open-sourced software licensed under the [MIT License](./LICENSE.md).

---
_Source: https://npm.io/package/casex · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
