Skip to content

Commit

Permalink
Simplify helper function call, update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
ndresx committed May 15, 2017
1 parent 7d58de7 commit a3b84a5
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 21 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ However, if it needs to be more precise, the `intervalDelay` can be set to somet
In certain cases you might want to base off the calculations on a millisecond basis. The `precision` prop, which defaults to `0`, can be used to refine this calculation. While the default value simply strips the milliseconds part (e.g.: `10123`ms => `10000`ms), a precision of `3` leads to `10123`ms.

### `children`
This component also considers the child that may live within the `<Countdown></Countdown>` element, which, in case it's available, replaces the countdown's component state once it's complete. Here's an [example](#using-a-react-child-for-the-completed-state) that showcases its usage.
This component also considers the child that may live within the `<Countdown></Countdown>` element, which, in case it's available, replaces the countdown's component state once it's complete. Moreover, an additional prop called `countdown` is set and contains data similar to what the [`renderer`](#renderer) callback would receive. Here's an [example](#using-a-react-child-for-the-completed-state) that showcases its usage.

<a name="renderer"></a>
### `renderer(props)`
Expand Down
42 changes: 29 additions & 13 deletions src/Countdown.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,19 @@ export const zeroPad = (value, length = 2) => {
/**
* Calculates the time difference between a given end date and the current date.
*
* @param {any} date Date or timestamp representation of the end date.
* @param {any} [now=Date.now] Alternative function for returning the current date.
* @param {Date|string|number} date Date or timestamp representation of the end date.
* @param {Object} [{ now = Date.now, precision = 0, controlled = false }={}]
* {function} [date=Date.now] Alternative function for returning the current date.
* {number} [precision=0] The precision on a millisecond basis.
* {boolean} [controlled=false] Defines whether the calculated value is already provided as the time difference or not.
* @param {number} [precision=0] The precision on a millisecond basis.
* @param {boolean} [controlled=false] Defines whether the calculated value is already provided as the time difference or not.
* @returns Object that includes details about the time difference.
*/
export const getTimeDifference = (date, now = Date.now, precision = 0, controlled = false) => {
export const getTimeDifference = (
date,
{ now = Date.now, precision = 0, controlled = false } = {}
) => {
const startDate = typeof date === 'string' ? new Date(date) : date;
const total = parseInt(
(Math.max(0, controlled ? startDate : startDate - now()) / 1000).toFixed(
Expand Down Expand Up @@ -54,8 +60,13 @@ export const getTimeDifference = (date, now = Date.now, precision = 0, controlle
export default class Countdown extends React.Component {
constructor(props) {
super(props);
const { date, now, precision, controlled } = this.props;
this.state = {
...getTimeDifference(props.date, props.now, props.precision, this.props.controlled),
...getTimeDifference(date, {
now,
precision,
controlled,
}),
};
}

Expand All @@ -66,8 +77,13 @@ export default class Countdown extends React.Component {
}

componentWillReceiveProps(nextProps) {
const { date, now, precision, controlled } = nextProps;
this.setDeltaState(
getTimeDifference(nextProps.date, nextProps.now, nextProps.precision, nextProps.controlled)
getTimeDifference(date, {
now,
precision,
controlled,
})
);
}

Expand Down Expand Up @@ -113,18 +129,18 @@ export default class Countdown extends React.Component {
}

tick = () => {
const delta = getTimeDifference(
this.props.date,
this.props.now,
this.props.precision,
this.props.controlled
);
const { date, now, precision, controlled, onTick } = this.props;
const delta = getTimeDifference(date, {
now,
precision,
controlled,
});
this.setDeltaState({
...delta,
});

if (this.props.onTick && delta.total > 0) {
this.props.onTick(delta);
if (onTick && delta.total > 0) {
onTick(delta);
}
};

Expand Down
14 changes: 7 additions & 7 deletions src/Countdown.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ describe('<Countdown />', () => {

it('should trigger onTick and onComplete callbacks', () => {
const onTick = jest.fn(stats => {
expect(stats).toEqual(getTimeDifference(wrapperDate, Date.now));
expect(stats).toEqual(getTimeDifference(wrapperDate));
});

const onComplete = jest.fn(stats => {
Expand Down Expand Up @@ -184,11 +184,11 @@ describe('getTimeDifference', () => {
});

it('should return a time difference of 0s if values for start and current date are the same', () => {
expect(getTimeDifference(Date.now(), Date.now)).toEqual({
expect(getTimeDifference(Date.now())).toEqual({
...defaultStats,
completed: true,
});
expect(getTimeDifference(Date.now() + 10, () => Date.now() + 10)).toEqual({
expect(getTimeDifference(Date.now() + 10, { now: () => Date.now() + 10 })).toEqual({
...defaultStats,
completed: true,
});
Expand All @@ -207,7 +207,7 @@ describe('getTimeDifference', () => {
});

it('should calculate the time difference with a precision of 3', () => {
expect(getTimeDifference(Date.now() + timeDiff, undefined, 3)).toEqual({
expect(getTimeDifference(Date.now() + timeDiff, { precision: 3 })).toEqual({
total: timeDiff,
days: 1,
hours: 1,
Expand All @@ -220,7 +220,7 @@ describe('getTimeDifference', () => {

it('should calculate the time difference by passing a date string', () => {
Date.now = jest.fn(() => new Date('Thu Dec 22 2016 00:36:07').getTime());
expect(getTimeDifference('Thu Dec 23 2017 01:38:10:456', undefined, 3)).toEqual({
expect(getTimeDifference('Thu Dec 23 2017 01:38:10:456', { precision: 3 })).toEqual({
total: 31626123456,
days: 366,
hours: 1,
Expand All @@ -233,7 +233,7 @@ describe('getTimeDifference', () => {

it('should calculate the time difference when controlled is true', () => {
const total = 91120003;
expect(getTimeDifference(total, undefined, undefined, true)).toEqual({
expect(getTimeDifference(total, { controlled: true })).toEqual({
total: total - 3,
days: 1,
hours: 1,
Expand All @@ -243,7 +243,7 @@ describe('getTimeDifference', () => {
completed: false,
});

expect(getTimeDifference(total, undefined, 3, true)).toEqual({
expect(getTimeDifference(total, { precision: 3, controlled: true })).toEqual({
total,
days: 1,
hours: 1,
Expand Down

0 comments on commit a3b84a5

Please sign in to comment.