-
Notifications
You must be signed in to change notification settings - Fork 3
/
javascript.helpers.js
190 lines (162 loc) · 4.6 KB
/
javascript.helpers.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// ADD THE char TO LEFT/RIGHT OF STRING
String.prototype.pad = function(len, char, left){
char = char || 0;
left = left || 0;
var value = this.toString();
while(value.length < len){
var r = [
char+value,
value+char,
];
value = r[left];
}
return value;
}
// ADD THE char TO LEFT OF STRING
String.prototype.lpad = function(len, char){
return this.pad(len, char, 0);
}
// ADD THE char TO RIGHT OF STRING
String.prototype.rpad = function(len, char){
return this.pad(len, char, 1);
}
// CONVERT TO UPPER THE FIRST LETTER OF WORDS
String.prototype.ucwords = function(){
var str = this.toLowerCase();
return str.replace(/(\b[a-z])/ig, function($1){
return $1.toUpperCase();
});
}
// CONVERT CAMELCASE TO LOWER CASE STRING SEPARATED BY UNDERLINE
String.prototype.underscore = function(){
return this.replace(/([a-z])([A-Z])/g, function($0,$1,$2){
return $1+"_"+$2;
}).toLowerCase();
}
// CONVERT STRING SEPARATED BY UNDERLINE TO WORDS WITH FIRST LETTER UPPER
String.prototype.humanize = function() {
return this.replace('_', ' ').ucwords();
}
// CONVERT STRING SEPARATED BY UNDERLINE TO CAMELCASE
String.prototype.camelize = function(){
return this.replace('_', ' ').ucwords().replace(' ', '');
}
// CONVERT TO WORDS WITH FIRST LETTER UPPER
String.prototype._humanize = function(){
return this.underscore().humanize();
}
// '28/02/2017 15:12:33'.toDate('dd/mm/yyyy hh:ii:ss');
// CONVERT STRING TO DATE BY FOLLOW THE MASK PASSED
String.prototype.toDate = function(mask, complete){
var value = this.toString();
var regex = mask;
if(!complete) complete = 2000;
var types = ['Y', 'M', 'D', 'H', 'I', 'S'];
for(var i in types){
var r = new RegExp(types[i], 'ig');
var len = (mask.match(r) || []).length;
if(len > 0){
r = new RegExp(types[i]+'+', 'ig');
regex = regex.replace(r, '([0-9]{'+len+'})');
mask = mask.replace(r, +i+1);
}
}
mask = mask.replace(/\D/g, '');
var matchs = value.match(regex);
if(matchs == null){
console.log('ALERT!! the mask not has relation with content');
return null;
}
var date = [];
for(var i in types){
date.push((matchs[mask[i]] || 0));
}
if(date[0] < 1000){
date[0] = complete + +date[0];
}
var d = new Date(date[0], (+date[1]-1), date[2], date[3], date[4], date[5]);
var valid = true;
var value = [
d.getFullYear(),
(+d.getMonth()+1),
d.getDate(),
d.getHours(),
d.getMinutes(),
d.getSeconds(),
];
for(var i in value){
if(value[i] != date[i]){
valid = false;
break;
}
}
if(valid){
return d;
}else{
console.log('ALERT!! The date is Invalid');
return null;
}
}
// date.toDate('dd/mm/yyyy hh:ii:ss');
// CONVERT DATE TO STRING BY FOLLOW THE MASK PASSED
Date.prototype.toDate = function(mask){
var value = {
D : this.getDate().toString().lpad(2),
M : (+this.getMonth()+1).toString().lpad(2),
Y : this.getFullYear().toString().lpad(4),
H : this.getHours().toString().lpad(2),
I : this.getMinutes().toString().lpad(2),
S : this.getSeconds().toString().lpad(2),
}
for(var i in value){
var r = new RegExp(i, 'ig');
var len = (mask.match(r) || []).length;
if(len > 0){
r = new RegExp(i+'+', 'ig');
var replace = value[i];
mask = mask.replace(r, replace);
}
}
return mask;
}
Date.prototype.addDay = function(time){
this.setDate(+this.getDate()+time);
return this;
}
Date.prototype.addMonth = function(time){
this.setMonth(+this.getMonth()+time);
return this;
}
Date.prototype.addYear = function(time){
this.setFullYear(+this.getFullYear()+time);
return this;
}
Date.prototype.addHours = function(time){
this.setHours(+this.getHours()+time);
return this;
}
Date.prototype.addMinutes = function(time){
this.setMinutes(+this.getMinutes()+time);
return this;
}
Date.prototype.addSeconds = function(time){
this.setSeconds(+this.getSeconds()+time);
return this;
}
// REMOVE FROM ARRAY THE INDEX PASSED
Array.prototype.remove = function(index){
this.splice(index, 1)
}
// RETURN THE KEYS FROM OBJECT
function getKeys($this){
var keys = [];
for(var i in $this){
if($this.hasOwnProperty(i)){
keys.push(i);
}
}
return keys;
}
Object.prototype.getKeys = function(){
return getKeys(this);
}