linkmore-design
Version:
π πlmη»δ»ΆεΊγπ
54 lines (53 loc) β’ 1.61 kB
JavaScript
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
import padStart from 'lodash/padStart';
// Countdown
var timeUnits = [['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
];
export function formatTimeStr(duration, format) {
var leftDuration = duration;
var escapeRegex = /\[[^\]]*]/g;
var keepList = (format.match(escapeRegex) || []).map(function (str) {
return str.slice(1, -1);
});
var templateText = format.replace(escapeRegex, '[]');
var replacedText = timeUnits.reduce(function (current, _ref) {
var _ref2 = _slicedToArray(_ref, 2),
name = _ref2[0],
unit = _ref2[1];
if (current.includes(name)) {
var _value = Math.floor(leftDuration / unit);
leftDuration -= _value * unit;
return current.replace(new RegExp("".concat(name, "+"), 'g'), function (match) {
var len = match.length;
return padStart(_value.toString(), len, '0');
});
}
return current;
}, templateText);
var index = 0;
return replacedText.replace(escapeRegex, function () {
var match = keepList[index];
index += 1;
return match;
});
}
export function formatCountdown(value, config) {
var _config$format = config.format,
format = _config$format === void 0 ? '' : _config$format;
var target = new Date(value).getTime();
var current = Date.now();
var diff = Math.max(target - current, 0);
return formatTimeStr(diff, format);
}