2020-10-17 03:51:17 +00:00
|
|
|
import { VNodeTypes } from 'vue';
|
2020-09-02 14:44:16 +00:00
|
|
|
import padStart from 'lodash-es/padStart';
|
2019-05-25 09:18:04 +00:00
|
|
|
|
2020-10-17 03:51:17 +00:00
|
|
|
export type valueType = number | string;
|
|
|
|
export type countdownValueType = valueType | string;
|
|
|
|
|
|
|
|
export type Formatter =
|
|
|
|
| false
|
|
|
|
| 'number'
|
|
|
|
| 'countdown'
|
|
|
|
| (({ value, config }: { value: valueType; config?: FormatConfig }) => VNodeTypes);
|
|
|
|
|
|
|
|
export interface FormatConfig {
|
|
|
|
formatter?: Formatter;
|
|
|
|
decimalSeparator?: string;
|
|
|
|
groupSeparator?: string;
|
|
|
|
precision?: number;
|
|
|
|
prefixCls?: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface CountdownFormatConfig extends FormatConfig {
|
|
|
|
format?: string;
|
|
|
|
}
|
|
|
|
|
2019-05-25 09:18:04 +00:00
|
|
|
// Countdown
|
2020-10-17 03:51:17 +00:00
|
|
|
const timeUnits: [string, number][] = [
|
2019-05-25 09:18:04 +00:00
|
|
|
['Y', 1000 * 60 * 60 * 24 * 365], // years
|
|
|
|
['M', 1000 * 60 * 60 * 24 * 30], // months
|
|
|
|
['D', 1000 * 60 * 60 * 24], // days
|
|
|
|
['H', 1000 * 60 * 60], // hours
|
|
|
|
['m', 1000 * 60], // minutes
|
|
|
|
['s', 1000], // seconds
|
|
|
|
['S', 1], // million seconds
|
2020-10-17 03:51:17 +00:00
|
|
|
];
|
2019-05-25 09:18:04 +00:00
|
|
|
|
2020-10-17 03:51:17 +00:00
|
|
|
export function formatTimeStr(duration: number, format: string) {
|
|
|
|
let leftDuration: number = duration;
|
2019-05-25 09:18:04 +00:00
|
|
|
|
2021-06-10 15:04:57 +00:00
|
|
|
const escapeRegex = /\[[^\]]*]/g;
|
|
|
|
const keepList: string[] = (format.match(escapeRegex) || []).map(str => str.slice(1, -1));
|
2020-03-07 11:45:13 +00:00
|
|
|
const templateText = format.replace(escapeRegex, '[]');
|
|
|
|
|
|
|
|
const replacedText = timeUnits.reduce((current, [name, unit]) => {
|
2019-05-25 09:18:04 +00:00
|
|
|
if (current.indexOf(name) !== -1) {
|
|
|
|
const value = Math.floor(leftDuration / unit);
|
|
|
|
leftDuration -= value * unit;
|
2021-06-10 15:04:57 +00:00
|
|
|
return current.replace(new RegExp(`${name}+`, 'g'), (match: string) => {
|
2019-05-25 09:18:04 +00:00
|
|
|
const len = match.length;
|
|
|
|
return padStart(value.toString(), len, '0');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return current;
|
2020-03-07 11:45:13 +00:00
|
|
|
}, templateText);
|
|
|
|
|
|
|
|
let index = 0;
|
|
|
|
return replacedText.replace(escapeRegex, () => {
|
|
|
|
const match = keepList[index];
|
|
|
|
index += 1;
|
|
|
|
return match;
|
|
|
|
});
|
2019-05-25 09:18:04 +00:00
|
|
|
}
|
|
|
|
|
2020-10-17 03:51:17 +00:00
|
|
|
export function formatCountdown(value: countdownValueType, config: CountdownFormatConfig) {
|
2019-05-25 09:18:04 +00:00
|
|
|
const { format = '' } = config;
|
2021-06-10 15:04:57 +00:00
|
|
|
const target = new Date(value).getTime();
|
|
|
|
const current = Date.now();
|
2019-05-25 09:18:04 +00:00
|
|
|
const diff = Math.max(target - current, 0);
|
2021-06-10 15:04:57 +00:00
|
|
|
|
2019-05-25 09:18:04 +00:00
|
|
|
return formatTimeStr(diff, format);
|
|
|
|
}
|