Skip to content

Commit 95c3b40

Browse files
committed
refactor alignToOrigin()
1 parent e5bbd27 commit 95c3b40

File tree

1 file changed

+3
-17
lines changed
  • packages/cubejs-backend-shared/src

1 file changed

+3
-17
lines changed

packages/cubejs-backend-shared/src/time.ts

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -78,31 +78,17 @@ export function subtractInterval(date: moment.Moment, interval: ParsedInterval):
7878
*/
7979
export const alignToOrigin = (startDate: moment.Moment, interval: ParsedInterval, origin: moment.Moment): moment.Moment => {
8080
let alignedDate = startDate.clone();
81-
let intervalOp;
82-
let isIntervalNegative = false;
83-
84-
let offsetDate = addInterval(origin, interval);
85-
86-
// The easiest way to check the interval sign
87-
if (offsetDate.isBefore(origin)) {
88-
isIntervalNegative = true;
89-
}
90-
91-
offsetDate = origin.clone();
81+
let offsetDate = origin.clone();
9282

9383
if (startDate.isBefore(origin)) {
94-
intervalOp = isIntervalNegative ? addInterval : subtractInterval;
95-
9684
while (offsetDate.isAfter(startDate)) {
97-
offsetDate = intervalOp(offsetDate, interval);
85+
offsetDate = subtractInterval(offsetDate, interval);
9886
}
9987
alignedDate = offsetDate;
10088
} else {
101-
intervalOp = isIntervalNegative ? subtractInterval : addInterval;
102-
10389
while (offsetDate.isBefore(startDate)) {
10490
alignedDate = offsetDate.clone();
105-
offsetDate = intervalOp(offsetDate, interval);
91+
offsetDate = addInterval(offsetDate, interval);
10692
}
10793

10894
if (offsetDate.isSame(startDate)) {

0 commit comments

Comments
 (0)