Unmerged
93 / 100
diff --git a/components/statistic/__tests__/index.test.js b/components/statistic/__tests__/index.test.js
index a6755b9e2..f01c2bc85 100644
--- a/components/statistic/__tests__/index.test.js
+++ b/components/statistic/__tests__/index.test.js
@@ -3,8 +3,14 @@ import { asyncExpect } from '@/tests/utils';
import MockDate from 'mockdate';
import moment from 'moment';
import Statistic from '..';
+import { formatTimeStr } from '../utils';
+import { sleep } from '../../../tests/utils';
+import mountTest from '../../../tests/shared/mountTest';
describe('Statistic', () => {
+ mountTest(Statistic);
+ mountTest(Statistic.Countdown);
+
beforeAll(() => {
MockDate.set(moment('2018-11-28 00:00:00'));
});
@@ -104,4 +110,9 @@ describe('Statistic', () => {
// expect(instance.countdownId).toBe(undefined);
});
});
+ describe('utils', () => {
+ it('format should support escape', () => {
+ expect(formatTimeStr(1000 * 60 * 60 * 24, 'D [Day]')).toBe('1 Day');
+ });
+ });
});
diff --git a/components/statistic/demo/countdown.md b/components/statistic/demo/countdown.md
index 51e875774..f646b75f9 100644
--- a/components/statistic/demo/countdown.md
+++ b/components/statistic/demo/countdown.md
@@ -41,7 +41,7 @@ Countdown component.
},
methods: {
onFinish() {
- console.log('over');
+ console.log('finished!');
},
},
};
diff --git a/components/statistic/utils.js b/components/statistic/utils.js
index 0a6efb40c..25e6be2d7 100644
--- a/components/statistic/utils.js
+++ b/components/statistic/utils.js
@@ -14,10 +14,14 @@ const timeUnits = [
['S', 1], // million seconds
];
-function formatTimeStr(duration, format) {
+export function formatTimeStr(duration, format) {
let leftDuration = duration;
- return timeUnits.reduce((current, [name, unit]) => {
+ const escapeRegex = /\[[^\]]*\]/g;
+ const keepList = (format.match(escapeRegex) || []).map(str => str.slice(1, -1));
+ const templateText = format.replace(escapeRegex, '[]');
+
+ const replacedText = timeUnits.reduce((current, [name, unit]) => {
if (current.indexOf(name) !== -1) {
const value = Math.floor(leftDuration / unit);
leftDuration -= value * unit;
@@ -27,7 +31,14 @@ function formatTimeStr(duration, format) {
});
}
return current;
- }, format);
+ }, templateText);
+
+ let index = 0;
+ return replacedText.replace(escapeRegex, () => {
+ const match = keepList[index];
+ index += 1;
+ return match;
+ });
}
export function formatCountdown(value, config) {