Skip to content

Commit cb4532b

Browse files
committed
create initial js port of lucky_case
0 parents  commit cb4532b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+33915
-0
lines changed

.babelrc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"presets": [
3+
[
4+
"env",
5+
{
6+
"modules": false
7+
}
8+
]
9+
],
10+
"plugins": [
11+
"transform-es3-member-expression-literals"
12+
]
13+
}

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.idea
2+
node_modules
3+
/docs/*magynhard*.js.html
4+
/jaguarjs-jsdoc/

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) Matthäus J. N. Beyrle
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
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

Comments
 (0)