Repository navigation
Expand file tree
/
Copy pathdataUtil.js
More file actions
executable file
·168 lines (155 loc) · 3.61 KB
/
Copy pathdataUtil.js
File metadata and controls
executable file
·168 lines (155 loc) · 3.61 KB
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
function sortByPinYin(arr) {
arr.sort(
function compareFunction(param1, param2) {
return param1.localeCompare(param2)
}
)
return arr
}
function pySegSort(arr) {
if(!String.prototype.localeCompare)
return null
let letters = '*abcdefghjklmnopqrstwxyz'.split('')
let zh = '阿八嚓哒妸发旮哈讥咔垃麻拏噢妑七呥扨它穵夕丫帀'.split('')
let segs = []
let curr
letters.forEach(function(item,i){
curr = {letter: item, data:[]}
arr.forEach(function(item2){
if((!zh[i-1] || zh[i-1].localeCompare(item2) <= 0) && item2.localeCompare(zh[i]) == -1) {
curr.data.push(item2)
}
})
if(curr.data.length) {
segs.push(curr)
curr.data.sort(function(a,b){
return a.localeCompare(b)
})
}
})
return segs
}
function formatPhone(value) {
return value.replace(/[&\|\\\*^%$#@\-\(\)\s]/g,'')
}
function checkMobile(value){
if(!(/^1[3|4|5|8][0-9]\d{4,8}$/.test(value))){
return false
}
return true
}
function CheckTel(value){//电话验证
let reg=/^([0-9]|[-])+$/g
let isValid
isValid=reg.exec(value)
if (!isValid) {
return false
}
return true
}
const DataType = {
'[object String]': 'String',
'[object Number]': 'Number',
'[object Boolean]': 'Boolean',
'[object Undefined]': 'Undefined',
'[object Null]': 'Null',
'[object Function]': 'Function',
'[object Date]': 'Date',
'[object Array]': 'Array',
'[object RegExp]': 'RegExp',
'[object Error]': 'Error',
'[object HTMLDocument]': 'HTMLDocument',
'[object global]': 'global',
}
function getType(data) {
let type = Object.prototype.toString.call(data)
return DataType[type]
}
function colorRgba (str, n = 1) {
let reg = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/
let sColor = str.toLowerCase()
if (sColor && reg.test(sColor)) {
if (sColor.length === 4) {
let sColorNew = "#"
for (let i = 1; i < 4; i += 1) { //例如:#eee,#fff等
sColorNew += sColor.slice(i, i + 1).concat(sColor.slice(i, i + 1))
}
sColor = sColorNew
}
let sColorChange = {}
for (let i = 1; i < 7; i += 2) {
let key = i === 1 ? 'r' : i === 3 ? 'g' : 'b'
sColorChange[key] = parseInt("0x" + sColor.slice(i, i + 2))
}
sColorChange['a'] = n
return sColorChange
} else {
return sColor
}
}
function checkColor(str) {
let reg = /^#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$/
return reg.test(str)
}
function colorHex(obj) {
return "#" + ((1 << 24) + (obj.r << 16) + (obj.g << 8) + obj.b).toString(16).slice(1)
}
const chnNumChar = {
零:0,
一:1,
二:2,
三:3,
四:4,
五:5,
六:6,
七:7,
八:8,
九:9,
}
const chnNameValue = {
十:{value:10, secUnit:false},
百:{value:100, secUnit:false},
千:{value:1000, secUnit:false},
万:{value:10000, secUnit:true},
亿:{value:100000000, secUnit:true},
}
function ChineseToNumber(chnStr){
let rtn = 0
let section = 0
let number = 0
let secUnit = false
let str = chnStr.split('')
for(let i = 0; i < str.length; i++){
let num = chnNumChar[str[i]]
if(typeof num !== 'undefined'){
number = num
if(i === str.length - 1){
section += number
}
}else{
let unit = chnNameValue[str[i]].value
secUnit = chnNameValue[str[i]].secUnit
if(secUnit){
section = (section + number ) * unit
rtn += section
section = 0
}else{
section += ((number + (i === 0 ? 1 : 0)) * unit)
}
number = 0
}
}
return rtn + section
}
export default {
sortByPinYin,
pySegSort,
formatPhone,
checkMobile,
CheckTel,
getType,
colorRgba,
colorHex,
ChineseToNumber,
checkColor,
}