-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
86 lines (77 loc) · 1.91 KB
/
utils.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
/**
* slice string by gap number
* {param : str | string, gap | number}
* str = '6228480402564890018' gap = default
* callback ["6228", "4804", "0256", "4890", "018"]
*/
export const serialize = (str, gap = 4) => {
if(Object.prototype.toString.call(str) === '[object String]' && Object.prototype.toString.call(gap) === '[object Number]'){
const arr = []
let index = 0
return function self() {
arr.push(str.slice(index, index + gap))
index +=gap
if(str.slice(index, index + gap) !=''){
self()
}
return arr
}
}
}
/**
* chunk array
* {params : array | array, process | function, context | 执行环节的上下文 }
* eg:
* array = [12,123,1234,453,436,23,23,5,4123,45,346,5634,2234,345,342]
* process = (item) => { console.log(item)}
*/
export const chunk = (array, process, context) => {
const delay = () => {
const item = array.shift()
process.call(context, item)
if(array.length > 0){
setTimeout(delay, 100)
}
}
setTimeout(delay, 100)
}
/*
* return random string
* eg str = 'ABCDE'
* */
export const randomLetter = (digit = 1) => {
const temp = [];
let str = '';
for(let i = 0; i< digit; i++){
temp.push(Math.floor(Math.random()*6 + 10).toString(16).toLocaleUpperCase())
}
str = temp.join('')
return str
}
/**
* input array
* filter repeated ele
* return array
*/
export const repeatedEle = (array) => {
const temp = [];
array.map((item,index)=> {
if(!temp.includes(item)){
temp.push(item)
}
})
console.log('temp',temp)
return temp
}
/*
* Array order
* */
export const arrowOrder = (array) => {
let _array = []
if(Object.prototype.toString.call(array) === '[object Array]'){
_array = array.sort((a,b)=> {
return a - b
})
}
return _array
}