|
| 1 | +# lucky-case |
| 2 | + |
| 3 | +The lucky javascript library to identify and convert strings from any letter case to another. Plus some extra functions. |
| 4 | + |
| 5 | +It's a port of my ruby gem [lucky_case](https://github.com/magynhard/lucky_case). |
| 6 | + |
| 7 | +Useful when working with conventions, where class names, method names and file names needs to be converted. |
| 8 | + |
| 9 | +* Converters: Only characters, numbers, dashes and underlines are allowed inside a string. |
| 10 | +* 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) |
| 11 | + |
| 12 | +# Contents |
| 13 | + |
| 14 | +* [Usage](#usage) |
| 15 | +* [Installation](#installation) |
| 16 | +* [Documentation](#documentation) |
| 17 | +* [Contributing](#contributing) |
| 18 | + |
| 19 | +<a name="usage"></a> |
| 20 | +## Usage |
| 21 | + |
| 22 | +You can either use the static LuckyCase class with its method or optionally monkey patch the String class. |
| 23 | + |
| 24 | +### Approach 1: Using the static class |
| 25 | +```javascript |
| 26 | +// node js |
| 27 | +const LuckyCase = require('lucky-case'); |
| 28 | +// browser |
| 29 | +<script type="text/javascript" src="js/lib/lucky-case.min.js"></script> |
| 30 | + |
| 31 | +// converters |
| 32 | +LuckyCase.toSnakeCase('PascalToSnake') // => 'pascal_to_snake' |
| 33 | +LuckyCase.toUpperSnakeCase('Train-To-Upper-Snake') // => 'TRAIN_TO_UPPER_SNAKE' |
| 34 | +LuckyCase.toPascalCase('snake_to_pascal') // => 'SnakeToPascal' |
| 35 | +LuckyCase.toCamelCase('dash-to-camel-case') // => 'dashToCamelCase' |
| 36 | +LuckyCase.toDashCase('PascalToDashCase') // => 'pascal-to-dash-case' |
| 37 | +LuckyCase.toUpperDashCase('PascalToUpperDash') // => 'PASCAL-TO-UPPER-DASH' |
| 38 | +LuckyCase.toTrainCase('snake_to_train_case') // => 'Snake-To-Train-Case' |
| 39 | +LuckyCase.toWordCase('PascalToWordCase') // => 'pascal to word case' |
| 40 | +LuckyCase.toUpperWordCase('PascalToUpperWord') // => 'PASCAL TO UPPER WORD' |
| 41 | +LuckyCase.toCapitalWordCase('snake_to_capital_word') // => 'Snake To Capital Word' |
| 42 | +LuckyCase.toSentenceCase('snake_to_sentence_case') // => 'Snake to sentence case' |
| 43 | +LuckyCase.toMixedCase('example_snake_string') // => 'Example-snake_STRING' |
| 44 | + |
| 45 | +// converter by type |
| 46 | +LuckyCase.convertCase('some_snake', 'PASCAL_CASE') // => 'SomeSnake' |
| 47 | + |
| 48 | +// transformers |
| 49 | +LuckyCase.toLowerCase('Some_FuckingShit') // => 'some_fuckingshit' |
| 50 | +LuckyCase.toUpperCase('Some_FuckingShit') // => 'SOME_FUCKINGSHIT' |
| 51 | +LuckyCase.toCapital('example') // => 'Example' |
| 52 | +LuckyCase.capitalize('example') // => 'Example' |
| 53 | +LuckyCase.swapCase('SomeSwappy_Case-Example') // => 'sOMEsWAPPY-cASE_eXAMPLE' |
| 54 | +LuckyCase.constantize('some_constant') // => SomeConstant |
| 55 | +LuckyCase.constantize('SOME_CONSTANT') // => SomeConstant |
| 56 | +LuckyCase.constantize('some/path_example/folder') // => SomePathExampleFolder |
| 57 | +LuckyCase.deconstantize(SomeConstant) // => 'someConstant' // default caseType: 'CAMEL_CASE' |
| 58 | +LuckyCase.deconstantize(SomeConstant, caseType: 'SNAKE_CASE') // => 'some_constant' |
| 59 | + |
| 60 | +// identifiers |
| 61 | +LuckyCase.case('this_can_only_be_snake_case') // => 'SNAKE_CASE' |
| 62 | +LuckyCase.cases('validformultiple') // => [ 'SNAKE_CASE', 'CAMEL_CASE', 'DASH_CASE', 'WORD_CASE' ] |
| 63 | + |
| 64 | +// checkers |
| 65 | +LuckyCase.isSnakeCase('valid_snake_case') // => true |
| 66 | +LuckyCase.isUpperSnakeCase('UPPER_SNAKE') // => true |
| 67 | +LuckyCase.isPascalCase('PascalCase') // => true |
| 68 | +LuckyCase.isCamelCase('toCamelCase') // => true |
| 69 | +LuckyCase.isDashCase('dash-case') // => true |
| 70 | +LuckyCase.isUpperDashCase('DASH-CASE') // => true |
| 71 | +LuckyCase.isTrainCase('Train-Case') // => true |
| 72 | +LuckyCase.isWordCase('word case') // => true |
| 73 | +LuckyCase.isUpperWordCase('UPPER WORD CASE') // => true |
| 74 | +LuckyCase.isCapitalWordCase('Capital Word Case') // => true |
| 75 | +LuckyCase.isSentenceCase('Sentence case string') // => true |
| 76 | +LuckyCase.isMixedCase('mixed_Case') // => true |
| 77 | +LuckyCase.isUpperCase('UPPER50984') // => true |
| 78 | +LuckyCase.isLowerCase('lower_cheese') // => true |
| 79 | +LuckyCase.isCapital('Some') // => true |
| 80 | +LuckyCase.isCapitalized('some') // => false |
| 81 | +LuckyCase.isValidCaseType('SNAKE_CASE') // => true |
| 82 | +LuckyCase.isValidCaseType('APPLE_CASE') // => false |
| 83 | +LuckyCase.isValidCaseString('validString') // => true |
| 84 | +LuckyCase.isValidCaseString('1nV4lid$tring') // => false |
| 85 | +``` |
| 86 | + |
| 87 | +### Approach 2: Monkey patch the string class |
| 88 | + |
| 89 | +With monkey patching you can access the same methods (except deconstantize, isValidCaseType) of LuckyCase directly from strings. |
| 90 | + |
| 91 | +Because the method 'case' and 'cases' are so general and could lead to conflicts, they are called 'letterCase' and 'letterCases' at strings. |
| 92 | + |
| 93 | +```javascript |
| 94 | +// node js |
| 95 | +const LuckyCase = require('lucky-case/string'); |
| 96 | +// browser |
| 97 | +<script type="text/javascript" src="js/lib/lucky-case.string.min.js"></script> |
| 98 | + |
| 99 | +let a = 'ExampleString' |
| 100 | + |
| 101 | +a.isPascalCase() // => true |
| 102 | +a.toSnakeCase() // => 'example_string' |
| 103 | +a // => 'ExampleString' |
| 104 | + |
| 105 | +// identifiers |
| 106 | +// got a other method name here because 'case' might be to common and cause conflicts |
| 107 | +b = 'example' |
| 108 | +b.letterCase() // => 'SNAKE_CASE' |
| 109 | +b.letterCases() // => [ 'SNAKE_CASE', 'CAMEL_CASE', 'DASH_CASE', 'WORD_CASE' ] |
| 110 | +``` |
| 111 | + |
| 112 | + |
| 113 | + |
| 114 | + |
| 115 | + |
| 116 | +<a name="installation"></a> |
| 117 | +## Installation |
| 118 | + |
| 119 | +### Option 1: node js yarn |
| 120 | + |
| 121 | +In your project root directory execute the following command: |
| 122 | +```bash |
| 123 | +yarn add lucky-case |
| 124 | +``` |
| 125 | + |
| 126 | +### Option 2: node js npm |
| 127 | + |
| 128 | +In your project root directory execute the following command: |
| 129 | +```bash |
| 130 | +npm install lucky-case |
| 131 | +``` |
| 132 | + |
| 133 | +### Option 3: Browser |
| 134 | + |
| 135 | +Download the luck-case.min.js or lucky-case.string.min.js (string monkey patching) from the folder `dist` |
| 136 | +put it in an appropriate folder, e.g. `js/lib` |
| 137 | +and reference it with an script tag in your project: |
| 138 | +```html |
| 139 | +<script type="text/javascript" src="js/lib/lucky-case.min.js"></script> |
| 140 | +``` |
| 141 | + |
| 142 | +Optionally you can add the source file to your build pipeline, if you are using webpack, brunch or any other packager. |
| 143 | + |
| 144 | + |
| 145 | +<a name="documentation"></a> |
| 146 | +## Documentation |
| 147 | +Check out the jsdoc at our github page |
| 148 | +<a href="http://magynhard.github.io/lucky-case">http://magynhard.github.io/lucky-case</a> |
| 149 | + |
| 150 | + |
| 151 | + |
| 152 | + |
| 153 | + |
| 154 | +<a name="contributing"></a> |
| 155 | +## Contributing |
| 156 | + |
| 157 | +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. |
| 158 | + |
0 commit comments