Skip to content

Commit f0dc472

Browse files
committed
Separate utils javascript
1 parent 2aea155 commit f0dc472

File tree

3 files changed

+21
-16
lines changed

3 files changed

+21
-16
lines changed

app.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
function getHourTime(h) {
2+
return h >= 12 ? 'PM' : 'AM';
3+
}
4+
5+
function getZeroPad(n) {
6+
return (parseInt(n, 10) >= 10 ? '' : '0') + n;
7+
}
8+
19
var Clock = { template: "<div class=clock><div class=clock__hours><span class=clock__hourtime>{{hourtime}}</span> {{hours}}</div><div class=clock__minutes>{{minutes}}</div></div>",
210
data: function data() {
311
return {
@@ -16,17 +24,11 @@ var Clock = { template: "<div class=clock><div class=clock__hours><span class=cl
1624
var now = new Date();
1725

1826
self.hours = now.getHours();
19-
self.minutes = self.getZeroPad(now.getMinutes());
20-
self.hourtime = self.getHourTime(self.hours);
27+
self.minutes = getZeroPad(now.getMinutes());
28+
self.hourtime = getHourTime(self.hours);
2129
self.hours = self.hours % 12 || 12;
2230

2331
setTimeout(self.updateDateTime, 1000);
24-
},
25-
getHourTime: function getHourTime(h) {
26-
return h >= 12 ? 'PM' : 'AM';
27-
},
28-
getZeroPad: function getZeroPad(n) {
29-
return (parseInt(n, 10) >= 10 ? '' : '0') + n;
3032
}
3133
}
3234
};

src/components/Clock.vue

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
</template>
1010

1111
<script>
12+
import { getHourTime, getZeroPad } from '../utils/helpers.js'
13+
1214
export default {
1315
data () {
1416
return {
@@ -26,17 +28,11 @@ export default {
2628
let now = new Date()
2729
2830
self.hours = now.getHours()
29-
self.minutes = self.getZeroPad(now.getMinutes())
30-
self.hourtime = self.getHourTime(self.hours)
31+
self.minutes = getZeroPad(now.getMinutes())
32+
self.hourtime = getHourTime(self.hours)
3133
self.hours = self.hours % 12 || 12
3234
3335
setTimeout(self.updateDateTime, 1000)
34-
},
35-
getHourTime (h) {
36-
return h >= 12 ? 'PM' : 'AM'
37-
},
38-
getZeroPad (n) {
39-
return (parseInt(n, 10) >= 10 ? '' : '0') + n
4036
}
4137
}
4238
}

src/utils/helpers.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export function getHourTime (h) {
2+
return h >= 12 ? 'PM' : 'AM'
3+
}
4+
5+
export function getZeroPad (n) {
6+
return (parseInt(n, 10) >= 10 ? '' : '0') + n
7+
}

0 commit comments

Comments
 (0)