-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathString.js
60 lines (49 loc) · 1.72 KB
/
String.js
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
/**
* Converts camel case string to title case
*
* @param {string} string the camel case string to convert to title case
* @param {boolean} upperCase whether or not final string is all in upper case
* @return {string} the title case version of the input string
*/
const fromCamelCaseToTitleCase = (string, upperCase) => {
let titleCase = '';
for (let s of string) {
const isUpperCase = s === s.toUpperCase();
const isFirstLetter = titleCase === '';
if (isFirstLetter) titleCase += s.toUpperCase();
else if (isUpperCase) titleCase += ' ' + s.toUpperCase();
else if (upperCase) titleCase += s.toUpperCase();
else titleCase += s;
}
return titleCase;
}
Object.defineProperty(this, 'fromCamelCaseToTitleCase', {value: fromCamelCaseToTitleCase, enumerable : true});
/**
* Converts title case string to camel case
*
* @param {string} string the title case string to convert to camel case
* @return {string} the camel case version of the input string
*/
const fromTitleCaseToCamelCase = (string) => {
let camelCase = '';
let wasSpace = false;
for (let s of string) {
const isSpace = s === ' ';
if (isSpace) wasSpace = true;
else {
camelCase += wasSpace ? s.toUpperCase() : s.toLowerCase();
wasSpace = false;
}
}
return camelCase;
}
Object.defineProperty(this, 'fromTitleCaseToCamelCase', {value: fromTitleCaseToCamelCase, enumerable : true});
const leftPadString = (string, desiredLength = 2, substringForPadding = '0') => {
let padding = '';
for (let i = 0; i < desiredLength; i++) {
padding += substringForPadding;
}
string = padding + string;
return string.slice(-2);
}
Object.defineProperty(this, 'leftPadString', {value: leftPadString, enumerable : true});