dmtrKovalenko / date-io

Abstraction over common javascript date management libraries
MIT License
726 stars 90 forks source link

Simplify Luxom adders #597

Closed clytras closed 2 years ago

clytras commented 3 years ago

The code for the adders is rather complicated, unnecessary and it will also impact performance on massive calculations.

With simple math, if we add a negative number, it will be subtracted like 10 + -5 = 5, same goes for Luxon DateTime.plus, we can subtract negative values by simple adding them like DateTime.now().plus({ days: -2 }) and it will subtract 2 days.

Here is a sandbox that demonstrates this https://stackblitz.com/edit/luxon-react-tuw2ft?file=index.js

I made this PR to change all adders from this:

public addWeeks = (date: DateTime, count: number) => {
  return count < 0
    ? date.minus({ weeks: Math.abs(count) })
    : date.plus({ weeks: count });
};

into this:

public addWeeks = (date: DateTime, count: number) => {
  return date.plus({ weeks: count });
};
codecov-commenter commented 3 years ago

Codecov Report

Merging #597 (f28b856) into master (e70582f) will not change coverage. The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff            @@
##            master      #597   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           16        16           
  Lines         1460      1450   -10     
  Branches       198       186   -12     
=========================================
- Hits          1460      1450   -10     
Impacted Files Coverage Δ
packages/luxon/src/luxon-utils.ts 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update e70582f...f28b856. Read the comment docs.