forked from date-fns/date-fns
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
roundingMethod
option to differenceInX functions (date-fns#2571) (
closes date-fns#2555) Added `roundingMethod` option to `differenceInHours`, `differenceInMinutes`, `differenceInQuarters`, `differenceInSeconds` and `differenceInWeeks` with `trunc` as the default method. Co-authored-by: Lucas Silva <lucas.silva@codeminer42.com> Co-authored-by: Tetiana <ttobin@protonmail.ch> Co-authored-by: Sasha Koss <koss@nocorp.me>
- Loading branch information
1 parent
8b76a5d
commit b884642
Showing
17 changed files
with
515 additions
and
204 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import type { RoundingMethod } from '../../types' | ||
|
||
type RoundingFn = typeof Math.round | ||
|
||
type RoundingFnsMap = { [method in RoundingMethod]: RoundingFn } | ||
|
||
const roundingMap: RoundingFnsMap = { | ||
ceil: Math.ceil, | ||
round: Math.round, | ||
floor: Math.floor, | ||
trunc: (value: number) => (value < 0 ? Math.ceil(value) : Math.floor(value)), // Math.trunc is not supported by IE | ||
} | ||
|
||
const defaultRoundingMethod: RoundingMethod = 'trunc' | ||
|
||
export function getRoundingMethod(method: RoundingMethod | undefined) { | ||
return method ? roundingMap[method] : roundingMap[defaultRoundingMethod] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.