[statistic]:Optimized code

pull/22384/head
yang 2023-02-13 16:26:34 +08:00
parent 2154321a75
commit 2a49142965
1 changed files with 22 additions and 25 deletions

View File

@ -50,7 +50,7 @@ export default {
default: null default: null
}, },
value: { value: {
type: [String, Number], type: [String, Number, Date],
default: '' default: ''
}, },
prefix: { prefix: {
@ -90,43 +90,40 @@ export default {
watch: { watch: {
value: function() { value: function() {
this.branch(); this.branch();
},
groupSeparator() {
this.dispose();
},
mulriple() {
this.dispose();
} }
}, },
methods: { methods: {
branch() { branch() {
let { timeIndices, countDown, dispose} = this; let { timeIndices, countDown, dispose} = this;
timeIndices ? countDown() : dispose(); if (timeIndices) {
countDown(this.value.valueOf() || this.value);
} else {
dispose();
}
}, },
magnification(num, mulriple = 1000, groupSeparator = ',') { magnification(num, mulriple = 1000, groupSeparator = ',') {
// magnification factor // magnification factor
const level = String(mulriple).length - 1; const level = String(mulriple).length ;
const reg = new RegExp(`\\d{1,${level}}(?=(\\d{${level}})+$)`, 'g'); return num.replace(new RegExp(`(\\d)(?=(\\d{${level - 1}})+$)`, 'g'), `$1${groupSeparator}`);
const result = String(num)
.replace(reg, '$&,')
.split(',')
.join(groupSeparator);
return result;
}, },
dispose() { dispose() {
let { value, precision, groupSeparator, rate } = this; let { value, rate, groupSeparator } = this;
if (!isNumber(value)) return false; if (!isNumber(value)) return false;
let [integer, decimal] = String(value).split('.'); if (this.precision) {
if (precision) { value = value.toFixed(this.precision);
decimal = `${decimal || ''}${(1)
.toFixed(precision)
.replace('.', '')
.slice(1)}`;
decimal = decimal.slice(0, precision);
} }
let result = 0; let [integer, decimal] = String(value).split('.');
// 1000 multiplying power // 1000 multiplying power
if (groupSeparator) { if (groupSeparator) {
integer = this.magnification(integer, rate, groupSeparator); integer = this.magnification(integer, rate, groupSeparator);
} }
let result = `${integer}${decimal ? this.decimalSeparator + decimal : ''}`;
result = [integer, decimal].join(
decimal ? this.decimalSeparator : ''
);
this.disposeValue = result; this.disposeValue = result;
return result; return result;
}, },
@ -188,12 +185,12 @@ export default {
} }
return result; return result;
}, },
countDown() { countDown(timeVlaue) {
let {REFRESH_INTERVAL, timeTask, diffDate, formatTimeStr, stopTime, suspend} = this; let {REFRESH_INTERVAL, timeTask, diffDate, formatTimeStr, stopTime, suspend } = this;
if (timeTask) return; if (timeTask) return;
let than = this; let than = this;
this.timeTask = setInterval(()=> { this.timeTask = setInterval(()=> {
let diffTiem = diffDate(than.value, Date.now()); let diffTiem = diffDate(timeVlaue, Date.now());
than.disposeValue = formatTimeStr(diffTiem); than.disposeValue = formatTimeStr(diffTiem);
stopTime(diffTiem); stopTime(diffTiem);
}, REFRESH_INTERVAL); }, REFRESH_INTERVAL);