1 |
|
2 | window.String.prototype.toJson = function (split) {
|
3 | var array = this.split(split);
|
4 | var json = {};
|
5 | for (var i in array) {
|
6 | var ary = array[i];
|
7 | json[ary] ? json[ary]++ : json[ary] = 1;
|
8 | }
|
9 | return json;
|
10 | };
|
11 | function left_zero_4(str) {
|
12 | if (str && str.length === 2) {
|
13 | return '00' + str;
|
14 | }
|
15 | return str;
|
16 | }
|
17 |
|
18 | window.String.prototype.toASCII = function () {
|
19 | var value = '';
|
20 | for (var i = 0; i < this.length; i++) {
|
21 | value += '\\u' + left_zero_4(parseInt(this.charCodeAt(i), 10).toString(16));
|
22 | }
|
23 | return value;
|
24 | };
|
25 | window.String.prototype.fromASCII = function () {
|
26 | return this.replace(/(\\u)(\w{1,4})/gi, function ($0) {
|
27 | return String.fromCharCode(parseInt(escape($0).replace(/(%5Cu)(\w{1,4})/g, "$2"), 16));
|
28 | });
|
29 | };
|
30 |
|
31 | window.String.prototype.toUnicode = function () {
|
32 | var value = '';
|
33 | for (var i = 0; i < this.length; i++) {
|
34 | value += '&#' + this.charCodeAt(i) + ';';
|
35 | }return value;
|
36 | };
|
37 | window.String.prototype.fromUnicode = function () {
|
38 | return this.replace(/(&#x)(\w{1,4});/gi, function ($0) {
|
39 | return String.fromCharCode(parseInt(escape($0).replace(/(%26%23x)(\w{1,4})(%3B)/g, "$2"), 16));
|
40 | });
|
41 | };
|
42 |
|
43 | window.String.prototype.toUTF8 = function () {
|
44 | var value = '';
|
45 | for (var i = 0; i < this.length; i++) {
|
46 | value += '&#x' + left_zero_4(parseInt(this.charCodeAt(i), 10).toString(16)) + ';';
|
47 | }
|
48 | return value;
|
49 | };
|
50 | window.String.prototype.fromUTF8 = function () {
|
51 | return this.replace(/(&#)(\d{1,6});/gi, function ($0) {
|
52 | return String.fromCharCode(parseInt(escape($0).replace(/(%26%23)(\d{1,6})(%3B)/g, "$2"), 10));
|
53 | });
|
54 | };
|
55 |
|
56 | window.String.prototype.toURI = function () {
|
57 | return encodeURI(this);
|
58 | };
|
59 | window.String.prototype.fromURI = function () {
|
60 | return decodeURI(this);
|
61 | };
|
62 |
|
63 | window.String.prototype.toURIComponent = function () {
|
64 | return encodeURIComponent(this);
|
65 | };
|
66 | window.String.prototype.fromURIComponent = function () {
|
67 | return decodeURIComponent(this);
|
68 | };
|
69 |
|
70 | window.String.prototype.encode = function () {
|
71 | return this.toASCII();
|
72 | };
|
73 | window.String.prototype.decode = function () {
|
74 | return this.fromASCII();
|
75 | };
|
76 |
|
77 |
|
78 | window.String.prototype.trim = function (trimPos) {
|
79 | if (trimPos === 'left') {
|
80 | return this.replace(/(^\s*)/g, '');
|
81 | } else if (trimPos === 'right') {
|
82 | return this.replace(/(\s*$)/g, '');
|
83 | }
|
84 | return this.replace(/(^\s*)|(\s*$)/g, '');
|
85 | };
|
86 |
|
87 |
|
88 | window.String.prototype.isQueryId = function () {
|
89 | if (!this.length) return false;
|
90 | var idExpr = /^#([\w-]*)$/;
|
91 | var match = idExpr.exec(this);
|
92 | if (!match || !match[1]) {
|
93 | return false;
|
94 | }
|
95 | return true;
|
96 | };
|
97 |
|
98 | window.String.prototype.isDate = function (split) {
|
99 | return new RegExp('^[0-9]{4}' + (split || '-') + '[0-9]{2}' + (split || '-') + '[0-9]{2}$').test(this);
|
100 | };
|
101 |
|
102 | window.String.prototype.isMonth = function (split) {
|
103 | return new RegExp('^[0-9]{4}' + (split || '-') + '[0-9]{2}$').test(this);
|
104 | };
|
105 |
|
106 | window.String.prototype.isDateTime = function (split) {
|
107 | return new RegExp('^[0-9]{4}' + (split || '-') + '[0-9]{2}' + (split || '-') + '[0-9]{2}\\s[0-9]{2}:[0-9]{2}(:[0-9]{2})?$').test(this);
|
108 | };
|
109 |
|
110 | window.String.prototype.isTime = function () {
|
111 | return new RegExp('^[0-9]{2}:[0-9]{2}(:[0-9]{2})?$').test(this);
|
112 | };
|
113 |
|
114 | window.String.prototype.toDate = function (dateSplit) {
|
115 | var date = new Date();
|
116 | var dateStrArr = [date.getFullYear(), date.getMonth(), date.getDate()];
|
117 | var timeStrArr = [date.getHours(), date.getMinutes(), date.getSeconds()];
|
118 | if (this.isDate()) {
|
119 | dateStrArr = this.split(dateSplit || '-');
|
120 | } else if (this.isDateTime()) {
|
121 | var tempDateStrArr = this.split(dateSplit || '-');
|
122 | var tempTimeStrArr = tempDateStrArr[2].split(' ')[1].split(':');
|
123 | dateStrArr[0] = tempDateStrArr[0];
|
124 | dateStrArr[1] = tempDateStrArr[1];
|
125 | dateStrArr[2] = tempDateStrArr[2].split(' ')[0];
|
126 | timeStrArr[0] = tempTimeStrArr[0];
|
127 | timeStrArr[1] = tempTimeStrArr[1];
|
128 | timeStrArr[2] = tempTimeStrArr[2] || timeStrArr[2];
|
129 | } else if (this.isMonth()) {
|
130 | tempDateStrArr = this.split(dateSplit || '-');
|
131 | dateStrArr[0] = tempDateStrArr[0];
|
132 | dateStrArr[1] = tempDateStrArr[1];
|
133 | } else if (this.isTime()) {
|
134 | tempTimeStrArr = this.split(':');
|
135 | timeStrArr[0] = tempTimeStrArr[0];
|
136 | timeStrArr[1] = tempTimeStrArr[1];
|
137 | timeStrArr[2] = tempTimeStrArr[2] || timeStrArr[2];
|
138 | }
|
139 | date.setYear(dateStrArr[0]);
|
140 | date.setMonth(dateStrArr[1] - 1, dateStrArr[2]);
|
141 | date.setHours(timeStrArr[0], timeStrArr[1], timeStrArr[2]);
|
142 | return date;
|
143 | };
|
144 |
|
145 | window.String.prototype.hasClass = function (name) {
|
146 | var classStr = this;
|
147 | if (this.indexOf('class=') > -1) {
|
148 | var res = classStr.match(/class=["'](.*)["']/);
|
149 | if (res[1]) {
|
150 | classStr = res[1];
|
151 | } else {
|
152 | classStr = '';
|
153 | }
|
154 | }
|
155 | var names = classStr.split(' ');
|
156 | for (var i = 0; i < names.length; i++) {
|
157 | if (names[i] === name) return true;
|
158 | }
|
159 | return false;
|
160 | };
|
161 |
|
162 | window.String.prototype.addClass = function (name) {
|
163 | var str = String(this);
|
164 | var className = 'class="' + name + '"';
|
165 | if (this.indexOf('class=') > -1) {
|
166 |
|
167 | var res = this.match(/class=["'](.*)["']/);
|
168 | if (res[1] && !res[1].hasClass(name)) {
|
169 |
|
170 | className = 'class="' + res[1] + ' ' + name + '"';
|
171 | str = str.replace(/class=["'](.*)["']/, className);
|
172 | return str;
|
173 | }
|
174 | return str;
|
175 | } else {
|
176 |
|
177 | res = str.match(/<\w+/);
|
178 | if (res[0]) {
|
179 | return str.replace(/<\w+/, res[0] + ' ' + className);
|
180 | }
|
181 | return str;
|
182 | }
|
183 | };
|
184 |
|
185 |
|
186 | window.String.prototype.clearImgProtocol = function () {
|
187 | return this.replace(/<img\s+src="https:/gim, '<img src="').replace(/<img\s+src="http:/gim, '<img src="');
|
188 | };
|
189 |
|
190 |
|
191 | window.String.prototype.clearProtocol = function () {
|
192 | return this.replace(/https:/gim, '').replace(/http:/gim, '');
|
193 | };
|
194 |
|
195 |
|
196 | window.String.prototype.isQueryId = function () {
|
197 | if (!this.length) return false;
|
198 | var idExpr = /^#([\w-]*)$/;
|
199 | var match = idExpr.exec(this);
|
200 | if (match && match.length > 0) {
|
201 | return match[1];
|
202 | }
|
203 | return false;
|
204 | };
|
205 |
|
206 | window.String.prototype.isQueryClass = function () {
|
207 | if (!this.length) return false;
|
208 | var classExpr = /^\.([\w-]*)$/;
|
209 | var match = classExpr.exec(this);
|
210 | if (match && match.length > 0) {
|
211 | return match[1];
|
212 | }
|
213 | return false;
|
214 | };
|
215 |
|
216 | window.String.prototype.isTag = function () {
|
217 | if (!this.length) return false;
|
218 | var tagExpr = /^<(\w+)\s*.*\/\w*>$/im;
|
219 | var match = tagExpr.exec(this);
|
220 | if (match && match.length > 0) {
|
221 | return true;
|
222 | }
|
223 | return false;
|
224 | };
|
225 |
|
226 | window.String.prototype.getUnitValue = function (argSuffix) {
|
227 |
|
228 | var suffix = argSuffix || 'px';
|
229 | var patt = new RegExp('^[+-]?(0|([1-9][0-9]*))(.[0-9]?)' + suffix + '$');
|
230 | if (patt.test(this)) {
|
231 | return this.substring(0, this.indexOf(suffix));
|
232 | }
|
233 | return 0;
|
234 | };
|
235 |
|
236 | String.charType = function (char) {
|
237 | if (char >= 48 && char <= 57) return 'number';
|
238 | if (char >= 65 && char <= 90) return 'capitalize';
|
239 | if (char >= 97 && char <= 122) return 'lowercase';
|
240 | else return 'other';
|
241 | };
|
242 | window.String.prototype.safeLvl = function () {
|
243 | if (this.length > 0 && this.length < 6) return 1;
|
244 | var mode = {};
|
245 | for (var i = 0; i < this.length; i++) {
|
246 | mode[String.charType(this.charCodeAt(i))] = '';
|
247 | }
|
248 | return Object.values(mode).length;
|
249 | }; |
\ | No newline at end of file |