5
5
</template >
6
6
7
7
<script >
8
+ import { isString } from " ../utils" ;
9
+
8
10
export default {
9
11
name: " Counter" ,
10
12
props: {
@@ -50,9 +52,18 @@ export default {
50
52
return this .duration === 0 ? this .difference : this .difference / this .duration ;
51
53
},
52
54
delimiter () {
53
- return this .value .toString ().search (" ," ) >= 0 ? " ," : " ." ;
55
+ return this .value .toString ().includes (" ," ) ? " ," : " ." ;
54
56
},
55
57
formattedValue () {
58
+ if (isString (this .value )) {
59
+ let [preFormat] = this .value .toString ().replace (/ \s / g , " " ).split (this .delimiter );
60
+ preFormat = [... preFormat].fill (" 0" ).join (" " );
61
+ const [pre , post ] = this .currentValue
62
+ .toFixed (this .decimalsCount )
63
+ .replace (" ." , this .delimiter )
64
+ .split (this .delimiter );
65
+ return ` ${ preFormat .slice (pre .length )}${ pre}${ post ? this .delimiter + post : " " } ` ;
66
+ }
56
67
return this .currentValue .toFixed (this .countDecimals ()).replace (" ." , this .delimiter );
57
68
},
58
69
delay () {
@@ -64,6 +75,10 @@ export default {
64
75
countProgress () {
65
76
return (Math .abs (this .currentDifference - this .difference ) * 100 ) / (this .difference || 1 );
66
77
},
78
+ decimalsCount () {
79
+ if (! isString (this .value ) && this .value % 1 === 0 ) return 0 ;
80
+ return this .value .toString ().replace (/ \s / g , " " ).split (this .delimiter )[1 ].length ;
81
+ },
67
82
counterProps () {
68
83
return {
69
84
currentValue: parseFloat (this .formattedValue ),
@@ -85,7 +100,7 @@ export default {
85
100
methods: {
86
101
countDecimals () {
87
102
if (this .value % 1 === 0 ) return 0 ;
88
- return this .value .toString ().split (this .delimiter )[1 ].length ;
103
+ return this .value .toString ().replace ( / \s / g , " " ). split (this .delimiter )[1 ].length ;
89
104
},
90
105
count (timeStamp ) {
91
106
if (! this .startTime ) {
0 commit comments