Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
levy committed Nov 6, 2019
2 parents 0a768da + c517bef commit 5cbca30
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 32 deletions.
9 changes: 9 additions & 0 deletions .all-contributorsrc
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@
"doc",
"test"
]
},
{
"login": "shoyuf",
"name": "Shoyuf",
"avatar_url": "https://avatars3.githubusercontent.com/u/27998490?v=4",
"profile": "https://shoyuf.top",
"contributions": [
"code"
]
}
],
"contributorsPerLine": 7
Expand Down
2 changes: 0 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ install:
- yarn test
script:
- ./build.sh
after_success:
- GREN_GITHUB_TOKEN=$GITHUB_TOKEN yarn release
after_script:
- ./notify.sh
cache: yarn
Expand Down
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,12 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d

<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
<!-- prettier-ignore -->
<table><tr><td align="center"><a href="https://donaldshen.github.io/portfolio"><img src="https://avatars3.githubusercontent.com/u/19591950?v=4" width="100px;" alt="Donald Shen"/><br /><sub><b>Donald Shen</b></sub></a><br /><a href="https://github.com/FEMessage/count-down/commits?author=donaldshen" title="Code">💻</a> <a href="https://github.com/FEMessage/count-down/commits?author=donaldshen" title="Documentation">📖</a> <a href="https://github.com/FEMessage/count-down/commits?author=donaldshen" title="Tests">⚠️</a></td></tr></table>
<table>
<tr>
<td align="center"><a href="https://donaldshen.github.io/portfolio"><img src="https://avatars3.githubusercontent.com/u/19591950?v=4" width="100px;" alt="Donald Shen"/><br /><sub><b>Donald Shen</b></sub></a><br /><a href="https://github.com/FEMessage/count-down/commits?author=donaldshen" title="Code">💻</a> <a href="https://github.com/FEMessage/count-down/commits?author=donaldshen" title="Documentation">📖</a> <a href="https://github.com/FEMessage/count-down/commits?author=donaldshen" title="Tests">⚠️</a></td>
<td align="center"><a href="https://shoyuf.top"><img src="https://avatars3.githubusercontent.com/u/27998490?v=4" width="100px;" alt="Shoyuf"/><br /><sub><b>Shoyuf</b></sub></a><br /><a href="https://github.com/FEMessage/count-down/commits?author=shoyuf" title="Code">💻</a></td>
</tr>
</table>

<!-- ALL-CONTRIBUTORS-LIST:END -->

Expand Down
3 changes: 0 additions & 3 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,3 @@
yarn stdver

yarn build

git remote add github https://$GITHUB_TOKEN@github.com/FEMessage/count-down.git > /dev/null 2>&1
git push github HEAD:master --follow-tags
2 changes: 1 addition & 1 deletion docs/format.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ custom output format

```vue
<template>
<count-down :seconds="59" :days="1" format="dd天 hh:mm:ss" />
<count-down :seconds="59" :days="1" format="dd天 hh:mm:ss.ms" />
</template>
```
8 changes: 7 additions & 1 deletion notify.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,21 @@ then
exit 1
fi

git remote add github https://$GITHUB_TOKEN@github.com/FEMessage/count-down.git > /dev/null 2>&1
git push github HEAD:master --follow-tags

GREN_GITHUB_TOKEN=$GITHUB_TOKEN yarn release

url=https://api.github.com/repos/FEMessage/count-down/releases/latest
resp_tmp_file=resp.tmp

curl -H "Authorization: token $GITHUB_TOKEN" $url > $resp_tmp_file

html_url=$(sed -n 5p $resp_tmp_file | sed 's/\"html_url\"://g' | awk -F '"' '{print $2}')
body=$(grep body < $resp_tmp_file | sed 's/\"body\"://g;s/\"//g')
version=$(echo $html_url | awk -F '/' '{print $NF}')

msg='{"msgtype": "markdown", "markdown": {"title": "count-down更新", "text": "@所有人\n# [count-down]('$html_url')\n'$body'"}}'
msg='{"msgtype": "markdown", "markdown": {"title": "count-down更新", "text": "@所有人\n# [count-down('$version')]('$html_url')\n'$body'"}}'

curl -X POST https://oapi.dingtalk.com/robot/send\?access_token\=$DINGTALK_ROBOT_TOKEN -H 'Content-Type: application/json' -d "$msg"

Expand Down
15 changes: 11 additions & 4 deletions src/count-down.vue
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ export default {
type: Number,
default: 0
},
/**
* remain milliseconds
*/
milliseconds: {
type: Number,
default: 0
},
/**
* whether autoplay or not
*/
Expand All @@ -47,13 +54,13 @@ export default {
},
/**
* Output format.
* Default: 'dd 天 hh 时 mm 分 ss 秒'. These dd, hh, mm & ss specifiers are optional.
* The default value will change according to whether there are days, hours, minutes & seconds,
* Default: 'dd 天 hh 时 mm 分 ss 秒'. These dd, hh, mm, ss & ms specifiers are optional.
* The default value will change according to whether there are days, hours, minutes, seconds & milliseconds,
* e.g., if user just pass minutes, then the default value will be 'mm 分 ss 秒'
*
* 输出格式。
* 默认值:'dd 天 hh 时 mm 分 ss 秒'。dd、hh、mm和ss标识符都是可选的
* 默认值会根据是否传入days, hours, minutes, seconds而变化
* 默认值:'dd 天 hh 时 mm 分 ss 秒'。dd、hh、mm、ss 和 ms 标识符都是可选的
* 默认值会根据是否传入days, hours, minutes, seconds 和 milliseconds而变化
* 比如用户只传了minutes,那么默认值就变为'mm 分 ss 秒'
*/
format: {
Expand Down
45 changes: 30 additions & 15 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ const formatSpecifiers = {
days: 'dd',
hours: 'hh',
minutes: 'mm',
seconds: 'ss'
seconds: 'ss',
milliseconds: 'ms'
}
const secondsIn = {
days: 60 * 60 * 24,
hours: 60 * 60,
minutes: 60,
seconds: 1
const millisecondsIn = {
days: 60 * 60 * 24 * 1000,
hours: 60 * 60 * 1000,
minutes: 60 * 1000,
seconds: 1 * 1000,
milliseconds: 1
}

/**
Expand All @@ -34,8 +36,23 @@ function padStart(str, len, v) {
return str
}

function padZero(str) {
return padStart(str + '', 2, '0')
function padZero(str, padRange = 2) {
return padStart(str + '', padRange, '0')
}

/**
* 天、小时、分钟、秒、毫秒格式化
* @param {string} key
* @param {number} value
* @returns {string}
*/
function timeFormatter(key, value) {
// 天、时、分、秒两位,毫秒三位
if (key === 'milliseconds') {
return padZero(value, 3)
} else {
return padZero(value)
}
}

export function toMilliseconds({
Expand All @@ -55,13 +72,12 @@ export function toMilliseconds({
*/
export function formatTime(time, format) {
let result = format
time = Math.ceil(time / 1000)
// 注意顺序很重要。要先从大的时间单位开始构造字符串
entries(formatSpecifiers).forEach(([k, specifier]) => {
if (includes(result, specifier)) {
const v = Math.floor(time / secondsIn[k])
time -= v * secondsIn[k]
result = result.replace(specifier, padZero(v))
const v = Math.floor(time / millisecondsIn[k])
time -= v * millisecondsIn[k]
result = result.replace(specifier, timeFormatter(k, v))
}
})
return result
Expand All @@ -73,9 +89,8 @@ export function formatTime(time, format) {
* @return 数据对象,包含days, hours, minutes, seconds & milliseconds字段
*/
export function toTimeData(time) {
const timeData = {milliseconds: time % 1000}
time /= 1000
entries(secondsIn).forEach(([k, v]) => {
const timeData = {}
entries(millisecondsIn).forEach(([k, v]) => {
timeData[k] = Math.floor(time / v)
time -= timeData[k] * v
})
Expand Down
12 changes: 7 additions & 5 deletions test/util.test.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
import {formatTime, toTimeData, toMilliseconds} from '../src/util'

describe('formatTime', () => {
test('正确解析日时分秒', () => {
test('正确解析日时分秒毫秒', () => {
const t = toMilliseconds({
days: 2,
hours: 3,
minutes: 4,
seconds: 5
seconds: 5,
milliseconds: 1
})
expect(formatTime(t, 'ddhhmmss')).toBe('02030405')
expect(formatTime(t, 'ddhhmmssms')).toBe('02030405001')
const t2 = toMilliseconds({
days: 2,
hours: 33,
minutes: 4,
seconds: -5
seconds: -5,
milliseconds: 1
})
expect(formatTime(t2, 'ddhhmmss')).toBe('03090355')
expect(formatTime(t2, 'ddhhmmssms')).toBe('03090355001')
})
test('format仅传部分占位符的情况', () => {
const t = toMilliseconds({
Expand Down

0 comments on commit 5cbca30

Please sign in to comment.