-
-
Notifications
You must be signed in to change notification settings - Fork 211
/
Copy pathutils.js
112 lines (98 loc) · 2.72 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
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
/* eslint-disable no-param-reassign */
/* eslint-disable radix */
/* eslint-disable no-bitwise */
export function formatTime(second) {
let i = 0
let h = 0
let s = parseInt(second)
if (s >= 60) {
i = parseInt(s / 60)
s = parseInt(s % 60)
if (i >= 60) {
h = parseInt(i / 60)
i = parseInt(i % 60)
}
}
// 补零
const zero = (v) => (v >> 0 < 10 ? `0${v}` : v)
if (h > 0) return [zero(h), zero(i), zero(s)].join(':')
return [zero(i), zero(s)].join(':')
}
export function createRandomNum(minNum, maxNum) {
return parseInt(Math.random() * (maxNum - minNum + 1) + minNum, 10)
}
export function distinct(array) {
return array
.map((item) => JSON.stringify(item))
.filter((item, idx, arr) => idx === arr.indexOf(item))
.map((item) => JSON.parse(item))
}
export const arrayEqual = (arr1) => (arr2) =>
JSON.stringify(arr1) === JSON.stringify(arr2)
const s4 = () => {
return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1)
}
// Generate a pseudo-GUID by concatenating random hexadecimal.
export const uuId = () => {
return `${s4() + s4()}-${s4()}-${s4()}-${s4()}-${s4()}${s4()}${s4()}`
}
export const isSafari = () => {
return (
/Safari/.test(navigator.userAgent) && !/Chrome/.test(navigator.userAgent)
)
}
// https://stackoverflow.com/a/9039885/2789451
export function isIOS() {
return (
[
'iPad Simulator',
'iPhone Simulator',
'iPod Simulator',
'iPad',
'iPhone',
'iPod',
].includes(navigator.platform) ||
// iPad on iOS 13 detection
(navigator.userAgent.includes('Mac') && 'ontouchend' in document)
)
}
// https://stackoverflow.com/questions/7451508/html5-audio-playback-with-fade-in-and-fade-out
export function swing(p) {
return 0.5 - Math.cos(p * Math.PI) / 2
}
export function adjustVolume(
element,
startVolume,
endVolume,
{ duration = 1000, easing = swing, interval = 13 } = {},
callback,
) {
let delta = endVolume - startVolume
if (!delta || !duration || !easing || !interval || isIOS()) {
element.volume = endVolume
callback()
return { fadeInterval: undefined, updateIntervalEndVolume: undefined }
}
const ticks = Math.floor(duration / interval)
let tick = 1
const updateIntervalEndVolume = (newVolume) => {
endVolume = newVolume
}
const timer = setInterval(() => {
// End volume may have changed in middle of fading
const newDelta = endVolume - startVolume
if (newDelta !== delta) {
delta = newDelta
}
element.volume = startVolume + easing(tick / ticks) * delta
if (++tick >= ticks) {
element.volume = endVolume
clearInterval(timer)
callback()
}
}, interval)
return {
fadeInterval: timer,
updateIntervalEndVolume,
}
}