-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
117 lines (102 loc) · 2.68 KB
/
index.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*
___ ___ _____ ___ _ _
/ __|__ _| _ \__|_ _| |_ _| \| |__ _
| (__/ _` | _(_-< | || '_| || .` / _` |
\___\__,_|_| /__/ |_||_||___|_|\_\__, |
|___/
*/
//*******************************************************************
'use strict';
//*******************************************************************
var cap = function(str,c) {
var nstr = '';
if (c === 'same') {
return str;
}
else if (c === 'none') {
return '';
}
else if (c === 'proper') {
return str;
}
else if (c === 'title') {
return str.replace(/\w([^-\s]*)/g, function(txt){
return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
});
}
else if (c === 'sentence') {
return str.charAt(0).toUpperCase() + str.substr(1).toLowerCase();
}
else if (c === 'upper') {
return str.toUpperCase();
}
else if (c === 'lower') {
return str.toLowerCase();
}
else if (c === 'camel') {
return str.charAt(0).toLowerCase() + cap(str, 'pascal').substr(1);
}
else if (c === 'pascal') {
return cap(str, 'title').replace(/ /g, '');
}
else if (c === 'snake') {
return str.replace(/ /g, '_');
}
else if (c === 'python') {
return str.toUpperCase().replace(/ /g, '_');
}
else if (c === 'leet') {
return str.toLowerCase().replace(/i/g, '1').replace(/e/g, '3').replace(/o/g, '0').replace(/l/g, '£').replace(/f/g, 'ƒ').replace(/s/g, '$').replace(/n/g, 'И');
}
else if (c === 'reverse') {
return str.split('').reverse().join('');
}
else if (c === 'crazy') {
for (var i = 0; i < str.length; i++){
if (i % 2 === 0) {
nstr += str.charAt(i).toLowerCase();
}
else {
nstr += str.charAt(i).toUpperCase();
}
}
return nstr;
}
else if (c === 'random') {
for (var j = 0; j < str.length; j++){
var rnd = Math.random();
if (rnd > 0.5) {
nstr += str.charAt(j).toLowerCase();
}
else {
nstr += str.charAt(j).toUpperCase();
}
}
return nstr;
}
else{
return str;
}
};
//*******************************************************************
var string = function(s, opt) {
if (!s) {
console.log('s false : ' + s);
return false;
}
else if (typeof s !== 'string') {
console.log('s typeof : ' + s);
return false;
}
else {
if (opt) {
s = cap(s, opt);
}
else {
s = cap(s, 'same');
}
return s;
}
};
//*******************************************************************
module.exports = string;