# lucky-case

> The lucky javascript library to identify and convert strings from any letter case to another

Latest version **1.1.9** (published 2026-08-17) · MIT license · 0 weekly downloads

## Install

```sh
npm install lucky-case
pnpm add lucky-case
yarn add lucky-case
bun add lucky-case
```

## Health

**Score 55/100 (C)** — status: active.

Positive: esm support; no vulnerabilities; recently updated.

Warnings: low downloads; no types.

## Facts

| | |
|---|---|
| Version | 1.1.9 |
| Published | 2026-08-17 |
| First published | 2020-12-07 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 236 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 3 |
| Author | Matthäus J. N. Beyrle |
| Maintainers | magynhard |
| Keywords | javascript, string, letter, case, transformation, conversion, snake, case, camel, dash, upper, lower, train, pascal, word, capital, sentence |

## Links

- npm: https://www.npmjs.com/package/lucky-case
- Repository: https://github.com/magynhard/lucky-case
- Homepage: https://github.com/magynhard/lucky-case#readme
- Issues: https://github.com/magynhard/lucky-case/issues
- npm.io page: https://npm.io/package/lucky-case

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

- 1.1.9 (latest) — 2026-08-17
- 1.1.8 — 2022-06-01
- 1.1.7 — 2022-05-20
- 1.1.6 — 2022-05-20
- 1.1.3 — 2021-12-21
- 1.1.2 — 2021-01-10
- 1.1.1 — 2021-01-10
- 1.1.0 — 2020-12-13
- 1.0.8 — 2020-12-13
- 1.0.7 — 2020-12-12
- 1.0.6 — 2020-12-12
- 1.0.5 — 2020-12-12
- 1.0.4 — 2020-12-07

## README

# lucky-case
[![npm package](https://img.shields.io/npm/v/lucky-case?color=default&style=plastic&logo=npm&logoColor=red)](https://www.npmjs.com/package/lucky-case)
[![GitHub release (latest by date)](https://img.shields.io/github/v/release/magynhard/lucky-case?color=default&label=browser&logo=javascript&style=plastic)](https://github.com/magynhard/lucky-case/releases)
![downloads](https://img.shields.io/npm/dt/lucky-case?color=blue&style=plastic)
[![License: MIT](https://img.shields.io/badge/License-MIT-gold.svg?style=plastic&logo=mit)](LICENSE)

> The lucky javascript library to identify and convert strings from any letter case to another. Plus some extra functions.

It's a port of my ruby gem [lucky_case](https://github.com/magynhard/lucky_case).

Useful when working with conventions, where class names, method names and file names needs to be converted.

* Converters: Only characters, numbers, dashes and underlines are allowed inside a string.
* Must not start with dash or number, underlines at the beginning are allowed by default and can be allowed/removed/controlled by parameter (when used for private methods for example)

### Support for UTF-8 / Unicode
As JavaScript has no unicode regular expression support like Ruby, UTF-8/Unicode-Ranges for non-latin letters have been included,
to ensure proper case detection.

This functionality has not been extensively tested and is therefore experimental in nature.

# Contents

* [Usage](#usage)
* [Installation](#installation)
* [Documentation](#documentation)
* [Contributing](#contributing)

<a name="usage"></a>
## Usage

You can either use the static LuckyCase class with its method or optionally monkey patch the String class.

### Approach 1: Using the static class
```javascript
// node js
const LuckyCase = require('lucky-case');
// browser
<script type="text/javascript" src="js/lib/lucky-case.min.js"></script>

// converters
LuckyCase.toSnakeCase('PascalToSnake')                  // => 'pascal_to_snake'
LuckyCase.toUpperSnakeCase('Train-To-Upper-Snake')      // => 'TRAIN_TO_UPPER_SNAKE'
LuckyCase.toPascalCase('snake_to_pascal')               // => 'SnakeToPascal'
LuckyCase.toCamelCase('dash-to-camel-case')             // => 'dashToCamelCase'
LuckyCase.toDashCase('PascalToDashCase')                // => 'pascal-to-dash-case'
LuckyCase.toUpperDashCase('PascalToUpperDash')          // => 'PASCAL-TO-UPPER-DASH'
LuckyCase.toTrainCase('snake_to_train_case')            // => 'Snake-To-Train-Case'
LuckyCase.toWordCase('PascalToWordCase')                // => 'pascal to word case'
LuckyCase.toUpperWordCase('PascalToUpperWord')          // => 'PASCAL TO UPPER WORD'
LuckyCase.toCapitalWordCase('snake_to_capital_word')    // => 'Snake To Capital Word'
LuckyCase.toSentenceCase('snake_to_sentence_case')      // => 'Snake to sentence case'
LuckyCase.toMixedCase('example_snake_string')           // => 'Example-snake_STRING'

// converter by type
LuckyCase.convertCase('some_snake', 'PASCAL_CASE')      // => 'SomeSnake'

// transformers
LuckyCase.toLowerCase('Some_FuckingShit')               // => 'some_fuckingshit'
LuckyCase.toUpperCase('Some_FuckingShit')               // => 'SOME_FUCKINGSHIT'
LuckyCase.toCapital('example')                          // => 'Example'
LuckyCase.capitalize('exAmple')                         // => 'ExAmple'
LuckyCase.decapitalize('ExAmple')                       // => 'exAmple'
LuckyCase.swapCase('SomeSwappy_Case-Example')           // => 'sOMEsWAPPY-cASE_eXAMPLE'
LuckyCase.constantize('some_constant')                  // => SomeConstant
LuckyCase.constantize('SOME_CONSTANT')                  // => SomeConstant
LuckyCase.constantize('some/path_example/folder')       // => SomePathExampleFolder
LuckyCase.deconstantize(SomeConstant)                   // => 'someConstant' // default caseType: 'CAMEL_CASE'
LuckyCase.deconstantize(SomeConstant, caseType: 'SNAKE_CASE')  // => 'some_constant'

// identifiers
LuckyCase.case('this_can_only_be_snake_case')           // => 'SNAKE_CASE'
LuckyCase.cases('validformultiple')                     // => [ 'SNAKE_CASE', 'CAMEL_CASE', 'DASH_CASE', 'WORD_CASE' ]

// checkers
LuckyCase.isSnakeCase('valid_snake_case')               // => true
LuckyCase.isUpperSnakeCase('UPPER_SNAKE')               // => true
LuckyCase.isPascalCase('PascalCase')                    // => true
LuckyCase.isCamelCase('toCamelCase')                    // => true
LuckyCase.isDashCase('dash-case')                       // => true
LuckyCase.isUpperDashCase('DASH-CASE')                  // => true
LuckyCase.isTrainCase('Train-Case')                     // => true
LuckyCase.isWordCase('word case')                       // => true
LuckyCase.isUpperWordCase('UPPER WORD CASE')            // => true
LuckyCase.isCapitalWordCase('Capital Word Case')        // => true
LuckyCase.isSentenceCase('Sentence case string')        // => true
LuckyCase.isMixedCase('mixed_Case')                     // => true
LuckyCase.isUpperCase('UPPER50984')                     // => true
LuckyCase.isLowerCase('lower_cheese')                   // => true
LuckyCase.isCapital('Some')                             // => true
LuckyCase.isCapitalized('some')                         // => false
LuckyCase.isNotCapital('soMe')                          // => true
LuckyCase.isDecapitalized('somE')                       // => true
LuckyCase.isValidCaseType('SNAKE_CASE')                 // => true
LuckyCase.isValidCaseType('APPLE_CASE')                 // => false
LuckyCase.isValidCaseString('validString')              // => true
LuckyCase.isValidCaseString('1nV4lid$tring')            // => false
```

### Approach 2: Monkey patch the string class

With monkey patching you can access the same methods (except `deconstantize`, `isValidCaseType`) of LuckyCase directly from strings.

Because the methods `case` and `cases` are so general and could lead to conflicts, they are called `letterCase` and `letterCases` at strings.

```javascript
// node js
const LuckyCase = require('lucky-case/string');
// browser
<script type="text/javascript" src="js/lib/lucky-case.string.min.js"></script>

let a = 'ExampleString'

a.isPascalCase()                        // => true
a.toSnakeCase()                         // => 'example_string'
a                                       // => 'ExampleString'

// identifiers
// got a other method name here because 'case' might be to common and cause conflicts
b = 'example'
b.letterCase()                          // => 'SNAKE_CASE'
b.letterCases()                         // => [ 'SNAKE_CASE', 'CAMEL_CASE', 'DASH_CASE', 'WORD_CASE' ]
```





<a name="installation"></a>
## Installation

### Option 1: node js yarn

In your project root directory execute the following command:
```bash
yarn add lucky-case
```

### Option 2: node js npm

In your project root directory execute the following command:
```bash
npm install lucky-case
```

### Option 3: Browser

Download the latest `lucky-case.min.js` or `lucky-case.string.min.js` (string monkey patching version) from the [release page](https://github.com/magynhard/lucky-case/releases) and
put it in an appropriate folder of your project, e.g. `js/lib`
and reference it with an script tag in your project:
```html
<script type="text/javascript" src="js/lib/lucky-case.min.js"></script>
```

Optionally you then should add the source file to your build pipeline, if you are using webpack, brunch or any other packager.

  
<a name="documentation"></a>    
## Documentation
Check out the *jsdoc* documentations:
* [LuckyCase static class version](doc/lucky-case.jsdoc.md)
* [LuckyCase string monkey patched version](doc/lucky-case-string.jsdoc.md)





<a name="contributing"></a>    
## Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/magynhard/lucky-case. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.

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