Skip to content

Commit 17c8a55

Browse files
committed
split compose time into separate package, added tests for compose time
1 parent 80b9c13 commit 17c8a55

File tree

4 files changed

+32
-11
lines changed

4 files changed

+32
-11
lines changed

src/components/Timepicker.jsx

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import debounce from 'lodash/debounce'
44
import Radium, { StyleRoot } from 'radium'
55

66
import parseTime from '../helpers/parse-time'
7+
import composeTime from '../helpers/compose-time'
78
import ClockWrapper from './ClockWrapper'
89
import Time from './Time'
910

@@ -43,17 +44,7 @@ export class Timepicker extends React.Component {
4344

4445
getTime(){
4546
const state = this.state;
46-
const meridiemAdd = state.meridiem === 'pm' ? 12 : 0
47-
const paddedMinute = ('0' + state.minute).slice(-2)
48-
return {
49-
formatted: `${state.hour}:${paddedMinute} ${state.meridiem}`,
50-
formattedSimple: `${state.hour}:${paddedMinute}`,
51-
formatted24: `${state.hour + meridiemAdd}:${paddedMinute}`,
52-
hour: state.hour,
53-
hour24: state.hour + meridiemAdd,
54-
minute: state.minute,
55-
meridiem: state.meridiem
56-
}
47+
return composeTime(state.hour, state.minute, state.meridiem);
5748
}
5849

5950
handleTimeChange(unit, val, canChangeUnit){
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import compose from '../compose-time';
2+
3+
describe('helpers/compose-time', () => {
4+
test('formatted24', () => {
5+
expect( compose(1, 30, 'am') ).toHaveProperty('formatted24', '1:30')
6+
expect( compose(1, 30, 'pm') ).toHaveProperty('formatted24', '13:30')
7+
expect( compose(12, 30, 'am') ).toHaveProperty('formatted24', '0:30')
8+
})
9+
});

src/helpers/compose-time.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
2+
export default function composeTime(hour, minute, meridiem){
3+
const paddedMinute = ('0' + minute).slice(-2)
4+
5+
let hour24 = hour;
6+
if (meridiem === 'pm'){
7+
hour24 = hour + 12;
8+
} else if (meridiem === 'am' && hour === 12){
9+
hour24 = 0;
10+
}
11+
12+
return {
13+
formatted: `${hour}:${paddedMinute} ${meridiem}`,
14+
formattedSimple: `${hour}:${paddedMinute}`,
15+
formatted24: `${hour24}:${paddedMinute}`,
16+
hour: hour,
17+
hour24: hour24,
18+
minute: minute,
19+
meridiem: meridiem
20+
}
21+
}

0 commit comments

Comments
 (0)