UNPKG

8.22 kBJavaScriptView Raw
1// 转为json,目的是字符串去重
2window.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};
11function left_zero_4(str) {
12 if (str && str.length === 2) {
13 return '00' + str;
14 }
15 return str;
16}
17// 转为ASCII编码
18window.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};
25window.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// 转为unicode编码
31window.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};
37window.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// 转为UTF8编码
43window.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};
50window.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// 转为URI编码
56window.String.prototype.toURI = function () {
57 return encodeURI(this);
58};
59window.String.prototype.fromURI = function () {
60 return decodeURI(this);
61};
62// 转为URI全编码
63window.String.prototype.toURIComponent = function () {
64 return encodeURIComponent(this);
65};
66window.String.prototype.fromURIComponent = function () {
67 return decodeURIComponent(this);
68};
69// 地址栏编码,地址栏不允许有一些特殊字符,例如%,可用此方法规避此问题
70window.String.prototype.encode = function () {
71 return this.toASCII();
72};
73window.String.prototype.decode = function () {
74 return this.fromASCII();
75};
76
77// 去除字符串左右两端的空格
78window.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// 判断是否是#的形式
88window.String.prototype.isQueryId = function () {
89 if (!this.length) return false;
90 var idExpr = /^#([\w-]*)$/; // 匹配id(#id)
91 var match = idExpr.exec(this);
92 if (!match || !match[1]) {
93 return false;
94 }
95 return true;
96};
97// 判断是否是合法的日期 YYYY-MM-DD
98window.String.prototype.isDate = function (split) {
99 return new RegExp('^[0-9]{4}' + (split || '-') + '[0-9]{2}' + (split || '-') + '[0-9]{2}$').test(this);
100};
101// 判断是否是合法的月份 yyyy-MM
102window.String.prototype.isMonth = function (split) {
103 return new RegExp('^[0-9]{4}' + (split || '-') + '[0-9]{2}$').test(this);
104};
105// 判断是否是日期格式 YYYY-MM-DD hh:mm:ss 或 YYYY-MM-DD hh:mm
106window.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// 判断是否是时间格式 hh:mm 或 hh:mm:ss
110window.String.prototype.isTime = function () {
111 return new RegExp('^[0-9]{2}:[0-9]{2}(:[0-9]{2})?$').test(this);
112};
113// 转换成日期
114window.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// 判断是否包含class名称
145window.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// 添加class名称
162window.String.prototype.addClass = function (name) {
163 var str = String(this);
164 var className = 'class="' + name + '"';
165 if (this.indexOf('class=') > -1) {
166 // 如果有class,并且class名称不存在,则增加class
167 var res = this.match(/class=["'](.*)["']/);
168 if (res[1] && !res[1].hasClass(name)) {
169 // 新增class不存在,则新增
170 className = 'class="' + res[1] + ' ' + name + '"';
171 str = str.replace(/class=["'](.*)["']/, className);
172 return str;
173 }
174 return str;
175 } else {
176 // 如果没有class,则创建一个class
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// 清除img字符串的"https:"和"http:", 例如‘<img src="http:’转换后‘<img src="’
186window.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// 清除字符串的"https:"和"http:"
191window.String.prototype.clearProtocol = function () {
192 return this.replace(/https:/gim, '').replace(/http:/gim, '');
193};
194
195// 判断是否是queryId
196window.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// 判断是否是queryClass
206window.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// 判断是否是query标签
216window.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// 获取指定后缀的数值(允许是小数和整数),例如:'44px'.getUnitValue() / '44em'.getUnitValue('em')
226window.String.prototype.getUnitValue = function (argSuffix) {
227 // 默认后缀为px
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// 密码等级
236String.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};
242window.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