Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify Luxom adders #597

Merged
merged 1 commit into from
Jan 25, 2022
Merged

Conversation

clytras
Copy link
Contributor

@clytras clytras commented Nov 24, 2021

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
Copy link

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.

@dmtrKovalenko dmtrKovalenko merged commit 2b5f6e1 into dmtrKovalenko:master Jan 25, 2022
@clytras clytras deleted the fix-luxon-adders branch January 25, 2022 15:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants