Skip to content

Commit

Permalink
Refactor unit normalization
Browse files Browse the repository at this point in the history
  • Loading branch information
KorbinianKuhn committed Jul 23, 2018
1 parent 8fdc635 commit f378b52
Show file tree
Hide file tree
Showing 12 changed files with 189 additions and 111 deletions.
26 changes: 17 additions & 9 deletions src/add.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,36 @@
const {
MILLISECONDS_PER_HOUR,
MILLISECONDS_PER_MINUTE,
MILLISECONDS_PER_SECOND
MILLISECONDS_PER_SECOND,
UNIT_MILLISECOND,
UNIT_SECOND,
UNIT_MINUTE,
UNIT_HOUR,
UNIT_DAY,
UNIT_MONTH,
UNIT_QUARTER,
UNIT_YEAR
} = require('./internal/constants');

const { normalizeUnit } = require('./internal/normalizeUnit');

exports.add = (date, amount, unit = 'milliseconds') => {
switch (normalizeUnit(unit)) {
case 'milliseconds':
case UNIT_MILLISECOND:
return addMilliseconds(date, amount);
case 'seconds':
case UNIT_SECOND:
return addSeconds(date, amount);
case 'minutes':
case UNIT_MINUTE:
return addMinutes(date, amount);
case 'hours':
case UNIT_HOUR:
return addHours(date, amount);
case 'days':
case UNIT_DAY:
return addDays(date, amount);
case 'months':
case UNIT_MONTH:
return addMonths(date, amount);
case 'quarters':
case UNIT_QUARTER:
return addQuarters(date, amount);
case 'years':
case UNIT_YEAR:
return addYears(date, amount);
}
};
Expand Down
26 changes: 17 additions & 9 deletions src/diff.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,37 @@ const {
MILLISECONDS_PER_SECOND,
MILLISECONDS_PER_MINUTE,
MILLISECONDS_PER_DAY,
MILLISECONDS_PER_HOUR
MILLISECONDS_PER_HOUR,
UNIT_MILLISECOND,
UNIT_SECOND,
UNIT_MINUTE,
UNIT_HOUR,
UNIT_DAY,
UNIT_MONTH,
UNIT_QUARTER,
UNIT_YEAR
} = require('./internal/constants');

const { normalizeUnit } = require('./internal/normalizeUnit');
const { getQuarter } = require('./getQuarter');

exports.difference = (dateA, dateB, unit = 'milliseconds') => {
switch (normalizeUnit(unit)) {
case 'milliseconds':
case UNIT_MILLISECOND:
return differenceInMilliseconds(dateA, dateB);
case 'seconds':
case UNIT_SECOND:
return differenceInSeconds(dateA, dateB);
case 'minutes':
case UNIT_MINUTE:
return differenceInMinutes(dateA, dateB);
case 'hours':
case UNIT_HOUR:
return differenceInHours(dateA, dateB);
case 'days':
case UNIT_DAY:
return differenceInDays(dateA, dateB);
case 'months':
case UNIT_MONTH:
return differenceInMonths(dateA, dateB);
case 'quarters':
case UNIT_QUARTER:
return differenceInQuarters(dateA, dateB);
case 'years':
case UNIT_YEAR:
return differenceInYears(dateA, dateB);
}
};
Expand Down
23 changes: 16 additions & 7 deletions src/endOf.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,31 @@ const {
normalizeUnitWithoutMilliseconds
} = require('./internal/normalizeUnit');
const { getQuarter } = require('./getQuarter');
const {
UNIT_SECOND,
UNIT_MINUTE,
UNIT_HOUR,
UNIT_DAY,
UNIT_MONTH,
UNIT_QUARTER,
UNIT_YEAR
} = require('./internal/constants');

exports.startOf = (date, unit = 'second') => {
switch (normalizeUnitWithoutMilliseconds(unit)) {
case 'seconds':
case UNIT_SECOND:
return endOfSecond(date);
case 'minutes':
case UNIT_MINUTE:
return endOfMinute(date);
case 'hours':
case UNIT_HOUR:
return endOfHour(date);
case 'days':
case UNIT_DAY:
return endOfDay(date);
case 'months':
case UNIT_MONTH:
return endOfMonth(date);
case 'quarters':
case UNIT_QUARTER:
return endOfQuarter(date);
case 'years':
case UNIT_YEAR:
return endOfYear(date);
}
};
Expand Down
9 changes: 9 additions & 0 deletions src/internal/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,12 @@ exports.MILLISECONDS_PER_SECOND = 1000;
exports.MILLISECONDS_PER_MINUTE = 60 * 1000;
exports.MILLISECONDS_PER_HOUR = 60 * 60 * 1000;
exports.MILLISECONDS_PER_DAY = 24 * 60 * 60 * 1000;

exports.UNIT_MILLISECOND = 'millisecond';
exports.UNIT_SECOND = 'second';
exports.UNIT_MINUTE = 'minute';
exports.UNIT_HOUR = 'hout';
exports.UNIT_DAY = 'day';
exports.UNIT_MONTH = 'month';
exports.UNIT_QUARTER = 'quarter';
exports.UNIT_YEAR = 'year';
41 changes: 26 additions & 15 deletions src/internal/normalizeUnit.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,48 @@
const {
UNIT_MILLISECOND,
UNIT_SECOND,
UNIT_MINUTE,
UNIT_HOUR,
UNIT_DAY,
UNIT_MONTH,
UNIT_QUARTER,
UNIT_YEAR
} = require('./constants');

exports.normalizeUnit = unit => {
switch (unit) {
case 'millisecond':
case 'milliseconds':
case 'ms':
return 'milliseconds';
return UNIT_MILLISECOND;
case 'second':
case 'seconds':
case 's':
return 'seconds';
return UNIT_SECOND;
case 'minute':
case 'minutes':
case 'm':
return 'minutes';
return UNIT_MINUTE;
case 'hour':
case 'hours':
case 'h':
return 'hours';
return UNIT_HOUR;
case 'day':
case 'days':
case 'd':
return 'days';
return UNIT_DAY;
case 'month':
case 'months':
case 'M':
return 'months';
return UNIT_MONTH;
case 'quarter':
case 'quarters':
case 'Q':
return 'quarters';
return UNIT_QUARTER;
case 'year':
case 'years':
case 'y':
return 'years';
return UNIT_YEAR;
}

throw new Error(`invalid date unit: ${unit}`);
Expand All @@ -42,31 +53,31 @@ exports.normalizeUnitWithoutMilliseconds = unit => {
case 'second':
case 'seconds':
case 's':
return 'seconds';
return UNIT_SECOND;
case 'minute':
case 'minutes':
case 'm':
return 'minutes';
return UNIT_MINUTE;
case 'hour':
case 'hours':
case 'h':
return 'hours';
return UNIT_HOUR;
case 'day':
case 'days':
case 'd':
return 'days';
return UNIT_DAY;
case 'month':
case 'months':
case 'M':
return 'months';
return UNIT_MONTH;
case 'quarter':
case 'quarters':
case 'Q':
return 'quarters';
return UNIT_QUARTER;
case 'year':
case 'years':
case 'y':
return 'years';
return UNIT_YEAR;
}

throw new Error(`invalid date unit: ${unit}`);
Expand Down
26 changes: 17 additions & 9 deletions src/isAfter.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,37 @@ const {
MILLISECONDS_PER_SECOND,
MILLISECONDS_PER_MINUTE,
MILLISECONDS_PER_HOUR,
MILLISECONDS_PER_DAY
MILLISECONDS_PER_DAY,
UNIT_MILLISECOND,
UNIT_SECOND,
UNIT_MINUTE,
UNIT_HOUR,
UNIT_DAY,
UNIT_MONTH,
UNIT_QUARTER,
UNIT_YEAR
} = require('./internal/constants');
const { smallerEqualOrBigger } = require('./internal/smallerEqualOrBigger');
const { normalizeUnit } = require('./internal/normalizeUnit');
const { getQuarter } = require('./getQuarter');

exports.isAfter = (dateA, dateB, unit = 'milliseconds') => {
switch (normalizeUnit(unit)) {
case 'milliseconds':
case UNIT_MILLISECOND:
return isAfterMillisecond(dateA, dateB);
case 'seconds':
case UNIT_SECOND:
return isAfterSecond(dateA, dateB);
case 'minutes':
case UNIT_MINUTE:
return isAfterMinute(dateA, dateB);
case 'hours':
case UNIT_HOUR:
return isAfterHour(dateA, dateB);
case 'days':
case UNIT_DAY:
return isAfterDay(dateA, dateB);
case 'months':
case UNIT_MONTH:
return isAfterMonth(dateA, dateB);
case 'quarters':
case UNIT_QUARTER:
return isAfterQuarter(dateA, dateB);
case 'years':
case UNIT_YEAR:
return isAfterYear(dateA, dateB);
}
};
Expand Down
26 changes: 17 additions & 9 deletions src/isBefore.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,37 @@ const {
MILLISECONDS_PER_SECOND,
MILLISECONDS_PER_MINUTE,
MILLISECONDS_PER_HOUR,
MILLISECONDS_PER_DAY
MILLISECONDS_PER_DAY,
UNIT_MILLISECOND,
UNIT_SECOND,
UNIT_MINUTE,
UNIT_HOUR,
UNIT_DAY,
UNIT_MONTH,
UNIT_QUARTER,
UNIT_YEAR
} = require('./internal/constants');
const { smallerEqualOrBigger } = require('./internal/smallerEqualOrBigger');
const { normalizeUnit } = require('./internal/normalizeUnit');
const { getQuarter } = require('./getQuarter');

exports.isBefore = (dateA, dateB, unit = 'milliseconds') => {
switch (normalizeUnit(unit)) {
case 'milliseconds':
case UNIT_MILLISECOND:
return isBeforeMillisecond(dateA, dateB);
case 'seconds':
case UNIT_SECOND:
return isBeforeSecond(dateA, dateB);
case 'minutes':
case UNIT_MINUTE:
return isBeforeMinute(dateA, dateB);
case 'hours':
case UNIT_HOUR:
return isBeforeHour(dateA, dateB);
case 'days':
case UNIT_DAY:
return isBeforeDay(dateA, dateB);
case 'months':
case UNIT_MONTH:
return isBeforeMonth(dateA, dateB);
case 'quarters':
case UNIT_QUARTER:
return isBeforeQuarter(dateA, dateB);
case 'years':
case UNIT_YEAR:
return isBeforeYear(dateA, dateB);
}
};
Expand Down
26 changes: 18 additions & 8 deletions src/isSame.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,33 @@
const { normalizeUnit } = require('./internal/normalizeUnit');
const { getQuarter } = require('./getQuarter');
const {
UNIT_MILLISECOND,
UNIT_SECOND,
UNIT_MINUTE,
UNIT_HOUR,
UNIT_DAY,
UNIT_MONTH,
UNIT_QUARTER,
UNIT_YEAR
} = require('./internal/constants');

exports.isSame = (dateA, dateB, unit = 'millisecond') => {
switch (normalizeUnit(unit)) {
case 'milliseconds':
case UNIT_MILLISECOND:
return isSameMillisecond(dateA, dateB);
case 'seconds':
case UNIT_SECOND:
return isSameSecond(dateA, dateB);
case 'minutes':
case UNIT_MINUTE:
return isSameMinute(dateA, dateB);
case 'hours':
case UNIT_HOUR:
return isSameHour(dateA, dateB);
case 'days':
case UNIT_DAY:
return isSameDay(dateA, dateB);
case 'months':
case UNIT_MONTH:
return isSameMonth(dateA, dateB);
case 'quarters':
case UNIT_QUARTER:
return isSameQuarter(dateA, dateB);
case 'years':
case UNIT_YEAR:
return isSameYear(dateA, dateB);
}
};
Expand Down
Loading

0 comments on commit f378b52

Please sign in to comment.