1 | module.exports = function (str, from, to) {
|
2 | if (from === "dash") {
|
3 | if (to === "lodash") {
|
4 | str = str.replace(/\-/g, "_");
|
5 | } else {
|
6 | str = str.replace(/\-(\w)/g, function (all, letter) {
|
7 | return letter.toUpperCase();
|
8 | })
|
9 | }
|
10 | }
|
11 |
|
12 | if(from === "lodash") {
|
13 | if (to === "dash") {
|
14 | str = str.replace(/_/g, "-");
|
15 | } else {
|
16 | str = str.replace(/_(\w)/g, function (all, letter) {
|
17 | return letter.toUpperCase();
|
18 | })
|
19 | }
|
20 | }
|
21 |
|
22 | if(from === "camel" || from === "upper"){
|
23 | if(to === "dash"){
|
24 | str = str.replace(/([A-Z])/g,"-$1").toLowerCase().replace(/^-/,'');
|
25 | }
|
26 | if(to === "lodash"){
|
27 | str = str.replace(/([A-Z])/g,"_$1").toLowerCase().replace(/^_/,'');
|
28 | }
|
29 | if(to === 'camel'){
|
30 | str = str.replace(/^([A-Z])/,function (all, $1){
|
31 | if($1){
|
32 | return $1.toLowerCase() + all.substr(1);
|
33 | }
|
34 | return all;
|
35 | })
|
36 | }
|
37 | }
|
38 |
|
39 | if(to === "upper"){
|
40 | str = str[0].toUpperCase() + [].slice.call(str,1).join("")
|
41 | }
|
42 |
|
43 | return str
|
44 | }
|