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

Add clamp function #2498

Merged
merged 5 commits into from Jul 23, 2021
Merged

Add clamp function #2498

merged 5 commits into from Jul 23, 2021

Conversation

Liam-Tait
Copy link
Contributor

Add a function to clamp a date based on an interval.

This function ensures that a date is bounded by a minimum and maximum

@tan75 tan75 self-assigned this Jun 4, 2021
@Liam-Tait
Copy link
Contributor Author

Liam-Tait commented Jun 7, 2021

For the curried version of this function. Currying from the right makes sense for my use case. I have an interval that I want to clamp multiple values to.

const contractInterval = {start, end}
const clampToContract =  clamp(allowed)

const queryStartDate = new Date();
const queryEndDate = new Date();

const start = clampToContract(queryStartDate)
const end = clampToContract(queryEndDate)

@Liam-Tait Liam-Tait closed this Jun 7, 2021
@Liam-Tait Liam-Tait reopened this Jun 7, 2021
@Liam-Tait
Copy link
Contributor Author

Sorry @tan75, I accidentally closed the PR

Copy link
Contributor

@tan75 tan75 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @Liam-Tait
this function is a great idea and it might be very useful! Thank you for your PR! 👍
Could you please make the examples consistent with our current example convention - e.g. see this one below:
Next Monday example
Now the convention is to use const and please delete the 'javascript' mention.
As well, please format the examples and the 'Docs' part of the file - it's good to have them consistent with the nextMonday example. Let us know if anything is unclear.

src/clamp/index.ts Outdated Show resolved Hide resolved
@tan75
Copy link
Contributor

tan75 commented Jun 10, 2021

@Liam-Tait
could you please provide any use cases for clamp?

@Liam-Tait
Copy link
Contributor Author

Hi @Liam-Tait
this function is a great idea and it might be very useful! Thank you for your PR! 👍

Thanks!

Could you please make the examples consistent with our current example convention - e.g. see this one below:
Next Monday example
Now the convention is to use const and please delete the 'javascript' mention.
As well, please format the examples and the 'Docs' part of the file - it's good to have them consistent with the nextMonday example. Let us know if anything is unclear.

I think I have matched the convention. Let me know if this correct

  • use const instead of var
  • removed javascript mention
  • removed backtick code section
  • inline parameters instead of using seperate variables
  • Add comment for the question the function is answering
  • Add comment for the returned value

@Liam-Tait
Copy link
Contributor Author

@Liam-Tait
could you please provide any use cases for clamp?

My use case is for querying a list of data based on a contract.

The query has a minDate and maxDate
The contract has a start and end
The data has a date

I only want to retrieve the data within the period of a contract.
In order to do this i want to clamp the query minDate and max Date to the contract interval.

Ideally this would be done at the database layer, however that option is not available to me.

const minDate = clamp(requestedMin, contractInterval)
const maxDate = clamp(requestedMax, contractInterval)
const result = query(minDate, maxDate)

Useful for when there is an allowed period of time (interval) and a user input of date and instead of causing an error bounding to the allowed range

@Liam-Tait
Copy link
Contributor Author

Updated! Please take a look

Copy link
Contributor

@tan75 tan75 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Liam-Tait Thank you for this PR! 👍
Assigned to @kossnocorp for release

@kossnocorp kossnocorp mentioned this pull request Jul 23, 2021
10 tasks
@kossnocorp kossnocorp merged commit 5304dcd into date-fns:master Jul 23, 2021
@Liam-Tait Liam-Tait deleted the add-clamp-function branch October 7, 2021 10:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants