-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.js
199 lines (173 loc) · 4.75 KB
/
util.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
191
192
193
194
195
196
197
198
199
/**
* 日期格式化
* @param fmt
* @returns {string}
*/
Date.prototype.format = function (fmt="yyyy-MM-dd") { //author: meizz
let o = {
"M+": this.getMonth() + 1, //月份
"d+": this.getDate(), //日
"H+": this.getHours(), //小时
"m+": this.getMinutes(), //分
"s+": this.getSeconds(), //秒
"q+": Math.floor((this.getMonth() + 3) / 3), //季度
"S": this.getMilliseconds() //毫秒
};
if (/(y+)/.test(fmt)) {
fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
}
for (let k in o) {
if (new RegExp("(" + k + ")").test(fmt)) {
fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
}
}
let week = {
"0" : "日",
"1" : "一",
"2" : "二",
"3" : "三",
"4" : "四",
"5" : "五",
"6" : "六"
};
if(/(E+)/.test(fmt)){
fmt=fmt.replace(RegExp.$1, ((RegExp.$1.length>1) ? (RegExp.$1.length>2 ? "星期" : "周") : "")+week[this.getDay()+""]);
}
return fmt;
};
/**
* 获取范围随机数
* @param min
* @param max
* @returns {number}
*/
Math.getRandomFromRange=function (min, max) {
let r = this.random() * (max - min);
let re = this.round(r + min);
re = this.max(this.min(re, max), min);
return re;
};
/**
* 四则运算
* @param num1 第一个操作数
* @param num2 第二个操作数
* @param method
* @returns {string}
* @constructor
*/
Math.callFloat = function(num1, num2, method="+") {
let re = /\s/g;
let a = num1.toString().replace(re, "");
let b = num2.toString().replace(re, "");
a = a.replaceAll(',', '');
b = b.replaceAll(',', '');
//修复乘法小数点过长溢出问题 切除6位小数点以后的数据
a = a.indexOf(".") > 0 ? a.substr(0, a.indexOf(".") + 7) : a;
b = b.indexOf(".") > 0 ? b.substr(0, b.indexOf(".") + 7) : b;
let cutN = "10000000000000";
let cutZero = "00000000000";
let dot = ".";
//将小数输入为整数
let adot = a.indexOf(dot) > 0 ? a.length - a.indexOf(dot) : 0;
let bdot = b.indexOf(dot) > 0 ? b.length - b.indexOf(dot) : 0;
let cutA = adot > 0 ? parseInt(cutN.substr(0, adot)) : 1;
let cutB = bdot > 0 ? parseInt(cutN.substr(0, bdot)) : 1;
let maxcutAB = Math.max(cutA, cutB);
let mincutAB = Math.min(cutA, cutB);
let cutAB = maxcutAB / mincutAB;
let numA = 0; let numB = 0;
let inzero = cutZero.substr(0, (cutAB.toString()).length - 1);
let cutLen = (maxcutAB.toString()).length - 1;
if (adot == bdot) {
numA = parseInt(a.replace(dot, ""), 10);
numB = parseInt(b.replace(dot, ""), 10);
} else if (adot > bdot) {
numA = parseInt(a.replace(dot, ""), 10);
numB = parseInt(b.replace(dot, "") + inzero, 10);
} else {
numB = parseInt(b.replace(dot, ""), 10);
numA = parseInt(a.replace(dot, "") + inzero, 10);
}
let numAB = "0.0";
let lastN = "0.0";
switch (method) {
case "+": //加
numAB = ((numA + numB) / maxcutAB).toString();
break;
case "-": //减
numAB = ((numA - numB) / maxcutAB).toString();
break;
case "*": //星号
case "×": //乘号
case "x": //x
numAB = (Math.abs(numA * numB)).toString();
cutLen = cutLen * 2;
break;
case "÷": //除号
case "/": //除
numAB = (numA / numB).toString();
break;
}
//对乘法的特别处理
if (method === "×"||method === "*") {
if (numAB.length <= cutLen) {
numAB = cutZero.substr(0, cutLen - numAB.length + 1) + numAB;
}
if (Math.abs(numA * numB) != numA * numB) {
numAB = "-" + numAB;
}
numAB = numAB.slice(0, numAB.length - cutLen) + "." + numAB.slice(numAB.length - cutLen);
}
return parseFloat(numAB).toString();
};
/**
* 去除两边空格
* @returns {string}
*/
String.prototype.trim = function(){return this.replace(/(^\s*)|(\s*$)/g, "");};
/**
* 去除左边空格
* @returns {string}
*/
String.prototype.leftTrim = function(){return this.replace(/(^\s*)/g, "");};
/**
* 去除右边空格
* @returns {string}
*/
String.prototype.rightTrim = function(){return this.replace(/(\s*$)/g, "");};
/**
* 替换
* @param s1
* @param s2
* @returns {string}
*/
String.prototype.replaceAll = function (s1, s2) {
let r = new RegExp(s1.replace(/([\(\)\[\]\{\}\^\$\+\-\*\?\.\"\'\|\/\\])/g, "\\$1"), "ig");
return this.replace(r, s2);
};
/**
* 数组根据属性排序
* @param option
* @returns {any}
*/
Array.prototype.sortByAttr=function (option) {
option = Object.assign({attr:'',type:'asc'},option);
let {type,attr} = option;
let a = this;
let temp=a[0];
for(let i=0;i<a.length-1;i++){
for(let j=0;j<a.length-1-i;j++){
let tmpA = a[j],tmpB=a[j+1];
if(attr && ''!==attr){
tmpA = a[j][attr];
tmpB = a[j+1][attr];
}
if(tmpA>tmpB){
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
return type==='desc'?a.reverse():a;
};