Skip to content

Commit

Permalink
split compose time into separate package, added tests for compose time
Browse files Browse the repository at this point in the history
  • Loading branch information
catc committed Jan 17, 2018
1 parent 80b9c13 commit 17c8a55
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 11 deletions.
13 changes: 2 additions & 11 deletions src/components/Timepicker.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import debounce from 'lodash/debounce'
import Radium, { StyleRoot } from 'radium'

import parseTime from '../helpers/parse-time'
import composeTime from '../helpers/compose-time'
import ClockWrapper from './ClockWrapper'
import Time from './Time'

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

getTime(){
const state = this.state;
const meridiemAdd = state.meridiem === 'pm' ? 12 : 0
const paddedMinute = ('0' + state.minute).slice(-2)
return {
formatted: `${state.hour}:${paddedMinute} ${state.meridiem}`,
formattedSimple: `${state.hour}:${paddedMinute}`,
formatted24: `${state.hour + meridiemAdd}:${paddedMinute}`,
hour: state.hour,
hour24: state.hour + meridiemAdd,
minute: state.minute,
meridiem: state.meridiem
}
return composeTime(state.hour, state.minute, state.meridiem);
}

handleTimeChange(unit, val, canChangeUnit){
Expand Down
9 changes: 9 additions & 0 deletions src/helpers/__tests__/compose-time.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import compose from '../compose-time';

describe('helpers/compose-time', () => {
test('formatted24', () => {
expect( compose(1, 30, 'am') ).toHaveProperty('formatted24', '1:30')
expect( compose(1, 30, 'pm') ).toHaveProperty('formatted24', '13:30')
expect( compose(12, 30, 'am') ).toHaveProperty('formatted24', '0:30')
})
});
File renamed without changes.
21 changes: 21 additions & 0 deletions src/helpers/compose-time.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

export default function composeTime(hour, minute, meridiem){
const paddedMinute = ('0' + minute).slice(-2)

let hour24 = hour;
if (meridiem === 'pm'){
hour24 = hour + 12;
} else if (meridiem === 'am' && hour === 12){
hour24 = 0;
}

return {
formatted: `${hour}:${paddedMinute} ${meridiem}`,
formattedSimple: `${hour}:${paddedMinute}`,
formatted24: `${hour24}:${paddedMinute}`,
hour: hour,
hour24: hour24,
minute: minute,
meridiem: meridiem
}
}

0 comments on commit 17c8a55

Please sign in to comment.