-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcases.ts
67 lines (52 loc) · 2.1 KB
/
cases.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import {toLower, toUpper, camelCase, capitalize, kebabCase, replace, snakeCase, upperFirst} from 'lodash';
import * as utils from '../utils';
/**
* Normalizes given text into spaced words by words boundary. Word boundary is any special char, or capital case.
*/
function splitWords(content: string) {
const parts: string[] = [];
content.split(/[./\-_ ]/).forEach(split => {
const values = split.split(/([A-Z][a-z]+)/).map(val => val.trim()).filter(val => val !== '');
parts.push(...values);
});
return parts.join(' ');
}
function processWords(content: string, handler: (words: string) => string): string {
return content.split(utils.eol).map(line => handler(splitWords(line))).join(utils.eol);
}
export function convertToLowerCase(content: string): string {
return toLower(content);
}
export function convertToUpperCase(content: string): string {
return toUpper(content);
}
export function convertToCapitalCase(content: string): string {
return capitalize(content);
}
export function convertToCamelCase(content: string): string {
return processWords(content, words => camelCase(words));
}
export function convertToPascalCase(content: string): string {
return processWords(content, words => upperFirst(camelCase(words)));
}
export function convertToSnakeCase(content: string): string {
return processWords(content, words => snakeCase(words));
}
export function convertToConstantCase(content: string): string {
return processWords(content, words => toUpper(snakeCase(words)));
}
export function convertToKebabCase(content: string): string {
return processWords(content, words => kebabCase(words));
}
export function convertToDotCase(content: string): string {
return processWords(content, words => replace(words, / /g, '.'));
}
export function convertToPathCase(content: string): string {
return processWords(content, words => replace(words, / /g, '/'));
}
export function convertToSpaceCase(content: string): string {
return processWords(content, words => words);
}
export function convertToSentenceCase(content: string): string {
return processWords(content, words => upperFirst(words));
}