Skip to content

Commit 86e61f0

Browse files
committed
feat(charCase): add new functions
new functions - snakeCase - camelCase - kebabCase - dotCase - pascalCase - capitalize
1 parent 13c0568 commit 86e61f0

File tree

4 files changed

+236
-17
lines changed

4 files changed

+236
-17
lines changed

cli.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ const usageString = `
1313
1414
Functions
1515
charCase.invert
16+
charCase.snakeCase
17+
charCase.camelCase
18+
charCase.kebabCase
19+
charCase.dotCase
20+
charCase.pascalCase
21+
charCase.capitalize
1622
compressor.pack
1723
compressor.unpack
1824
compressor.signature

readme.md

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,16 @@ this <code>00010000 00000100</code>. This means that <code>&#39;A&#39;</code> (<
170170
## stringMutilator/charCase
171171
Functions for manipulating the letter case of strings.
172172

173+
174+
* [stringMutilator/charCase](#module_stringMutilator/charCase)
175+
* [~invert(string, [every])](#module_stringMutilator/charCase..invert) ⇒ <code>string</code>
176+
* [~snakeCase(string)](#module_stringMutilator/charCase..snakeCase) ⇒ <code>string</code>
177+
* [~camelCase(string)](#module_stringMutilator/charCase..camelCase) ⇒ <code>string</code>
178+
* [~kebabCase(string)](#module_stringMutilator/charCase..kebabCase) ⇒ <code>string</code>
179+
* [~dotCase(string)](#module_stringMutilator/charCase..dotCase) ⇒ <code>string</code>
180+
* [~pascalCase(string)](#module_stringMutilator/charCase..pascalCase) ⇒ <code>string</code>
181+
* [~capitalize(string)](#module_stringMutilator/charCase..capitalize) ⇒ <code>string</code>
182+
173183
<a name="module_stringMutilator/charCase..invert"></a>
174184

175185
### stringMutilator/charCase~invert(string, [every]) ⇒ <code>string</code>
@@ -187,6 +197,102 @@ Invert the case of letters in a string.
187197
stringMutilator.charCase.invert('Hello World!');
188198
// > 'hELLO wORLD!'
189199
```
200+
<a name="module_stringMutilator/charCase..snakeCase"></a>
201+
202+
### stringMutilator/charCase~snakeCase(string) ⇒ <code>string</code>
203+
Change the case of given string to "Snake Case".
204+
205+
**Kind**: inner method of [<code>stringMutilator/charCase</code>](#module_stringMutilator/charCase)
206+
207+
| Param | Type |
208+
| --- | --- |
209+
| string | <code>string</code> |
210+
211+
**Example**
212+
```js
213+
stringMutilator.charCase.snakeCase('Hello World');
214+
// > 'hello_world'
215+
```
216+
<a name="module_stringMutilator/charCase..camelCase"></a>
217+
218+
### stringMutilator/charCase~camelCase(string) ⇒ <code>string</code>
219+
Change the case of the given string to "Camel Case".
220+
221+
**Kind**: inner method of [<code>stringMutilator/charCase</code>](#module_stringMutilator/charCase)
222+
223+
| Param | Type |
224+
| --- | --- |
225+
| string | <code>string</code> |
226+
227+
**Example**
228+
```js
229+
stringMutilator.charCase.camelCase('Hello World');
230+
// > 'helloWorld'
231+
```
232+
<a name="module_stringMutilator/charCase..kebabCase"></a>
233+
234+
### stringMutilator/charCase~kebabCase(string) ⇒ <code>string</code>
235+
Change the case of the given string to "Kebap Case".
236+
237+
**Kind**: inner method of [<code>stringMutilator/charCase</code>](#module_stringMutilator/charCase)
238+
239+
| Param | Type |
240+
| --- | --- |
241+
| string | <code>string</code> |
242+
243+
**Example**
244+
```js
245+
stringMutilator.charCase.kebabCase('Hello World');
246+
// > 'hello-world'
247+
```
248+
<a name="module_stringMutilator/charCase..dotCase"></a>
249+
250+
### stringMutilator/charCase~dotCase(string) ⇒ <code>string</code>
251+
Change the case of the given string to "Dot Case".
252+
253+
**Kind**: inner method of [<code>stringMutilator/charCase</code>](#module_stringMutilator/charCase)
254+
255+
| Param | Type |
256+
| --- | --- |
257+
| string | <code>string</code> |
258+
259+
**Example**
260+
```js
261+
stringMutilator.charCase.dotCase('Hello World');
262+
// > 'hello.world'
263+
```
264+
<a name="module_stringMutilator/charCase..pascalCase"></a>
265+
266+
### stringMutilator/charCase~pascalCase(string) ⇒ <code>string</code>
267+
Change the case of the given string to "Pascal Case".
268+
269+
**Kind**: inner method of [<code>stringMutilator/charCase</code>](#module_stringMutilator/charCase)
270+
271+
| Param | Type |
272+
| --- | --- |
273+
| string | <code>string</code> |
274+
275+
**Example**
276+
```js
277+
stringMutilator.charCase.pascalCase('Hello World');
278+
// > 'HelloWorld'
279+
```
280+
<a name="module_stringMutilator/charCase..capitalize"></a>
281+
282+
### stringMutilator/charCase~capitalize(string) ⇒ <code>string</code>
283+
Change the case of the given string to "Capitalized".
284+
285+
**Kind**: inner method of [<code>stringMutilator/charCase</code>](#module_stringMutilator/charCase)
286+
287+
| Param | Type |
288+
| --- | --- |
289+
| string | <code>string</code> |
290+
291+
**Example**
292+
```js
293+
stringMutilator.charCase.capitalize('hello world');
294+
// > 'Hello World'
295+
```
190296
<a name="module_stringMutilator/compressor"></a>
191297

192298
## stringMutilator/compressor
@@ -613,6 +719,12 @@ $ string-mutilator --help
613719
compressor.pack
614720
compressor.unpack
615721
compressor.signature
722+
charCase.snakeCase
723+
charCase.camelCase
724+
charCase.kebabCase
725+
charCase.dotCase
726+
charCase.pascalCase
727+
charCase.capitalize
616728
flipBits
617729
gobbledygook
618730
jumble

src/char-case.js

Lines changed: 112 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,28 +12,123 @@
1212
* stringMutilator.charCase.invert('Hello World!');
1313
* // > 'hELLO wORLD!'
1414
*/
15-
const invert = (string, every = 0) => string.replace(
16-
/[a-z]/ig,
17-
(char, index) => String.fromCharCode(
18-
char.charCodeAt() ^ 32 * +(!every || index % every === 0)
19-
)
20-
);
21-
22-
// TODO:
23-
// * SCREAMING_SNAKE_CASE
24-
// * snake_case
25-
// * camelCase
26-
// * kebab-case
27-
// * dot.case
28-
// * PascalCase
29-
// * Capitalize
15+
const invert = (string, every = 0) =>
16+
string
17+
.replace(
18+
/[a-z]/ig,
19+
(char, index) => String.fromCharCode(
20+
char.charCodeAt() ^ 32 * +(!every || index % every === 0)
21+
)
22+
);
23+
24+
const punctuationRegExp = /([-_. ])([a-z])/ig;
25+
26+
/**
27+
* Replacement helper function for `snakeCase`, `kebabCase` and `dotCase`.
28+
* @private
29+
* @param {string} string
30+
* @param {string} char
31+
* @return {string}
32+
*/
33+
const casePunctuationReplace = (string, char) =>
34+
string
35+
.replace(punctuationRegExp, `${char}$2`)
36+
.replace(/([a-z])([A-Z])/g, `$1${char}$2`)
37+
.replace(/([A-Z])([A-Z])/g, `$1${char}$2`);
38+
39+
/**
40+
* Change the case of given string to "Snake Case".
41+
* @param {string} string
42+
* @return {string}
43+
* @example
44+
* stringMutilator.charCase.snakeCase('Hello World');
45+
* // > 'hello_world'
46+
*/
47+
const snakeCase = string =>
48+
casePunctuationReplace(string, '_')
49+
.toLowerCase();
50+
51+
/**
52+
* Change the case of the given string to "Camel Case".
53+
* @param {string} string
54+
* @return {string}
55+
* @example
56+
* stringMutilator.charCase.camelCase('Hello World');
57+
* // > 'helloWorld'
58+
*/
59+
const camelCase = string =>
60+
string
61+
.replace(punctuationRegExp, (m, a, b) => b.toUpperCase())
62+
.replace(/^([A-Z])/, (m, a) => a.toLowerCase());
63+
64+
/**
65+
* Change the case of the given string to "Kebap Case".
66+
* @param {string} string
67+
* @return {string}
68+
* @example
69+
* stringMutilator.charCase.kebabCase('Hello World');
70+
* // > 'hello-world'
71+
*/
72+
const kebabCase = string =>
73+
casePunctuationReplace(string, '-')
74+
.toLowerCase();
75+
76+
/**
77+
* Change the case of the given string to "Dot Case".
78+
* @param {string} string
79+
* @return {string}
80+
* @example
81+
* stringMutilator.charCase.dotCase('Hello World');
82+
* // > 'hello.world'
83+
*/
84+
const dotCase = string =>
85+
casePunctuationReplace(string, '.')
86+
.toLowerCase();
87+
88+
/**
89+
* Change the case of the given string to "Pascal Case".
90+
* @param {string} string
91+
* @return {string}
92+
* @example
93+
* stringMutilator.charCase.pascalCase('Hello World');
94+
* // > 'HelloWorld'
95+
*/
96+
const pascalCase = string =>
97+
string
98+
.replace(punctuationRegExp, (m, a, b) => b.toUpperCase())
99+
.replace(/^([A-Z])/, (m, a) => a.toUpperCase());
100+
101+
/**
102+
* Change the case of the given string to "Capitalized".
103+
* @param {string} string
104+
* @return {string}
105+
* @example
106+
* stringMutilator.charCase.capitalize('hello world');
107+
* // > 'Hello World'
108+
*/
109+
const capitalize = string =>
110+
string
111+
.replace(punctuationRegExp, (m, a, b) => `${a}${b.toUpperCase()}`)
112+
.replace(/^([a-z])/, (m, a) => a.toUpperCase());
30113

31114
export default
32115
{
33-
invert
116+
invert,
117+
snakeCase,
118+
camelCase,
119+
kebabCase,
120+
dotCase,
121+
pascalCase,
122+
capitalize
34123
};
35124

36125
export
37126
{
38-
invert
127+
invert,
128+
snakeCase,
129+
camelCase,
130+
kebabCase,
131+
dotCase,
132+
pascalCase,
133+
capitalize
39134
};

template-readme.hbs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,12 @@ $ string-mutilator --help
138138
compressor.pack
139139
compressor.unpack
140140
compressor.signature
141+
charCase.snakeCase
142+
charCase.camelCase
143+
charCase.kebabCase
144+
charCase.dotCase
145+
charCase.pascalCase
146+
charCase.capitalize
141147
flipBits
142148
gobbledygook
143149
jumble

0 commit comments

Comments
 (0)