Skip to content

Commit

Permalink
fix: error timestamp format #242
Browse files Browse the repository at this point in the history
  • Loading branch information
lijinke666 committed Jan 5, 2021
1 parent e88b857 commit 6ecbf73
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
4 changes: 3 additions & 1 deletion __tests__/tests/player.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,9 @@ describe('<ReactJkMusicPlayer/>', () => {
})
it('should print second return format time', () => {
assert(formatTime(30000) === '08:20:00')
assert(formatTime(60) === '00:60')
assert(formatTime(60) === '01:00')
assert(formatTime(120) === '02:00')
assert(formatTime(121) === '02:01')
assert(formatTime(140) === '02:20')
assert(formatTime(2 * 60 * 60) === '02:00:00')
assert(formatTime(2 * 60 * 60 + 30) === '02:00:30')
Expand Down
11 changes: 6 additions & 5 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
//秒转换成 时间格式
/* eslint-disable radix */
/* eslint-disable no-bitwise */
export function formatTime(second) {
let i = 0
let h = 0
let s = parseInt(second)
if (s > 60) {
if (s >= 60) {
i = parseInt(s / 60)
s = parseInt(s % 60)
if (i > 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(':')
else return [zero(i), zero(s)].join(':')
return [zero(i), zero(s)].join(':')
}

export function createRandomNum(minNum, maxNum) {
Expand All @@ -24,7 +25,7 @@ export function createRandomNum(minNum, maxNum) {
export function distinct(array) {
return array
.map((item) => JSON.stringify(item))
.filter((item, idx, arry) => idx === arry.indexOf(item))
.filter((item, idx, arr) => idx === arr.indexOf(item))
.map((item) => JSON.parse(item))
}

Expand Down

0 comments on commit 6ecbf73

Please sign in to comment.