Skip to content

Commit cd712c6

Browse files
shoyuflevy9527
authored andcommitted
feat(milliseconds): 支持毫秒倒计时 (#6)
1 parent 80e01bb commit cd712c6

File tree

4 files changed

+49
-25
lines changed

4 files changed

+49
-25
lines changed

docs/format.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ custom output format
22

33
```vue
44
<template>
5-
<count-down :seconds="59" :days="1" format="dd天 hh:mm:ss" />
5+
<count-down :seconds="59" :days="1" format="dd天 hh:mm:ss.ms" />
66
</template>
77
```

src/count-down.vue

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@ export default {
3838
type: Number,
3939
default: 0
4040
},
41+
/**
42+
* remain milliseconds
43+
*/
44+
milliseconds: {
45+
type: Number,
46+
default: 0
47+
},
4148
/**
4249
* whether autoplay or not
4350
*/
@@ -47,13 +54,13 @@ export default {
4754
},
4855
/**
4956
* Output format.
50-
* Default: 'dd 天 hh 时 mm 分 ss 秒'. These dd, hh, mm & ss specifiers are optional.
51-
* The default value will change according to whether there are days, hours, minutes & seconds,
57+
* Default: 'dd 天 hh 时 mm 分 ss 秒'. These dd, hh, mm, ss & ms specifiers are optional.
58+
* The default value will change according to whether there are days, hours, minutes, seconds & milliseconds,
5259
* e.g., if user just pass minutes, then the default value will be 'mm 分 ss 秒'
5360
*
5461
* 输出格式。
55-
* 默认值:'dd 天 hh 时 mm 分 ss 秒'。dd、hh、mm和ss标识符都是可选的
56-
* 默认值会根据是否传入days, hours, minutes, seconds而变化
62+
* 默认值:'dd 天 hh 时 mm 分 ss 秒'。dd、hh、mm、ss 和 ms 标识符都是可选的
63+
* 默认值会根据是否传入days, hours, minutes, seconds 和 milliseconds而变化
5764
* 比如用户只传了minutes,那么默认值就变为'mm 分 ss 秒'
5865
*/
5966
format: {

src/util.js

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@ const formatSpecifiers = {
33
days: 'dd',
44
hours: 'hh',
55
minutes: 'mm',
6-
seconds: 'ss'
6+
seconds: 'ss',
7+
milliseconds: 'ms'
78
}
8-
const secondsIn = {
9-
days: 60 * 60 * 24,
10-
hours: 60 * 60,
11-
minutes: 60,
12-
seconds: 1
9+
const millisecondsIn = {
10+
days: 60 * 60 * 24 * 1000,
11+
hours: 60 * 60 * 1000,
12+
minutes: 60 * 1000,
13+
seconds: 1 * 1000,
14+
milliseconds: 1
1315
}
1416

1517
/**
@@ -34,8 +36,23 @@ function padStart(str, len, v) {
3436
return str
3537
}
3638

37-
function padZero(str) {
38-
return padStart(str + '', 2, '0')
39+
function padZero(str, padRange = 2) {
40+
return padStart(str + '', padRange, '0')
41+
}
42+
43+
/**
44+
* 天、小时、分钟、秒、毫秒格式化
45+
* @param {string} key
46+
* @param {number} value
47+
* @returns {string}
48+
*/
49+
function timeFormatter(key, value) {
50+
// 天、时、分、秒两位,毫秒三位
51+
if (key === 'milliseconds') {
52+
return padZero(value, 3)
53+
} else {
54+
return padZero(value)
55+
}
3956
}
4057

4158
export function toMilliseconds({
@@ -55,13 +72,12 @@ export function toMilliseconds({
5572
*/
5673
export function formatTime(time, format) {
5774
let result = format
58-
time = Math.ceil(time / 1000)
5975
// 注意顺序很重要。要先从大的时间单位开始构造字符串
6076
entries(formatSpecifiers).forEach(([k, specifier]) => {
6177
if (includes(result, specifier)) {
62-
const v = Math.floor(time / secondsIn[k])
63-
time -= v * secondsIn[k]
64-
result = result.replace(specifier, padZero(v))
78+
const v = Math.floor(time / millisecondsIn[k])
79+
time -= v * millisecondsIn[k]
80+
result = result.replace(specifier, timeFormatter(k, v))
6581
}
6682
})
6783
return result
@@ -73,9 +89,8 @@ export function formatTime(time, format) {
7389
* @return 数据对象,包含days, hours, minutes, seconds & milliseconds字段
7490
*/
7591
export function toTimeData(time) {
76-
const timeData = {milliseconds: time % 1000}
77-
time /= 1000
78-
entries(secondsIn).forEach(([k, v]) => {
92+
const timeData = {}
93+
entries(millisecondsIn).forEach(([k, v]) => {
7994
timeData[k] = Math.floor(time / v)
8095
time -= timeData[k] * v
8196
})

test/util.test.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11
import {formatTime, toTimeData, toMilliseconds} from '../src/util'
22

33
describe('formatTime', () => {
4-
test('正确解析日时分秒', () => {
4+
test('正确解析日时分秒毫秒', () => {
55
const t = toMilliseconds({
66
days: 2,
77
hours: 3,
88
minutes: 4,
9-
seconds: 5
9+
seconds: 5,
10+
milliseconds: 1
1011
})
11-
expect(formatTime(t, 'ddhhmmss')).toBe('02030405')
12+
expect(formatTime(t, 'ddhhmmssms')).toBe('02030405001')
1213
const t2 = toMilliseconds({
1314
days: 2,
1415
hours: 33,
1516
minutes: 4,
16-
seconds: -5
17+
seconds: -5,
18+
milliseconds: 1
1719
})
18-
expect(formatTime(t2, 'ddhhmmss')).toBe('03090355')
20+
expect(formatTime(t2, 'ddhhmmssms')).toBe('03090355001')
1921
})
2022
test('format仅传部分占位符的情况', () => {
2123
const t = toMilliseconds({

0 commit comments

Comments
 (0)