-
Notifications
You must be signed in to change notification settings - Fork 175
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #198 from github/make-duration-class
Make duration class
- Loading branch information
Showing
4 changed files
with
140 additions
and
24 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,62 @@ | ||
const duration = /^[-+]?P(?:(\d+)Y)?(?:(\d+)M)?(?:(\d+)W)?(?:(\d+)D)?(?:T(?:(\d+)H)?(?:(\d+)M)?(?:(\d+)S)?)?$/ | ||
const durationRe = /^[-+]?P(?:(\d+)Y)?(?:(\d+)M)?(?:(\d+)W)?(?:(\d+)D)?(?:T(?:(\d+)H)?(?:(\d+)M)?(?:(\d+)S)?)?$/ | ||
|
||
export const isDuration = (str: string) => duration.test(str) | ||
export const isDuration = (str: string) => durationRe.test(str) | ||
|
||
export function applyDuration(date: Date, str: string): Date | null { | ||
const r = new Date(date) | ||
str = String(str).trim() | ||
const factor = str.startsWith('-') ? -1 : 1 | ||
const parsed = String(str) | ||
.trim() | ||
.match(duration) | ||
?.slice(1) | ||
.map(x => (Number(x) || 0) * factor) | ||
if (!parsed) return null | ||
const [years, months, weeks, days, hours, minutes, seconds] = parsed | ||
export class Duration { | ||
constructor( | ||
public years = 0, | ||
public months = 0, | ||
public weeks = 0, | ||
public days = 0, | ||
public hours = 0, | ||
public minutes = 0, | ||
public seconds = 0 | ||
) {} | ||
|
||
abs() { | ||
return new Duration( | ||
Math.abs(this.years), | ||
Math.abs(this.months), | ||
Math.abs(this.weeks), | ||
Math.abs(this.days), | ||
Math.abs(this.hours), | ||
Math.abs(this.minutes), | ||
Math.abs(this.seconds) | ||
) | ||
} | ||
|
||
r.setFullYear(r.getFullYear() + years) | ||
r.setMonth(r.getMonth() + months) | ||
r.setDate(r.getDate() + weeks * 7 + days) | ||
r.setHours(r.getHours() + hours) | ||
r.setMinutes(r.getMinutes() + minutes) | ||
r.setSeconds(r.getSeconds() + seconds) | ||
static from(durationLike: unknown): Duration { | ||
if (typeof durationLike === 'string') { | ||
const str = String(durationLike).trim() | ||
const factor = str.startsWith('-') ? -1 : 1 | ||
const parsed = str | ||
.match(durationRe) | ||
?.slice(1) | ||
.map(x => (Number(x) || 0) * factor) | ||
if (!parsed) return new Duration() | ||
return new Duration(...parsed) | ||
} else if (typeof durationLike === 'object') { | ||
const {years, months, weeks, days, hours, minutes, seconds} = durationLike as Record<string, number> | ||
return new Duration(years, months, weeks, days, hours, minutes, seconds) | ||
} | ||
throw new RangeError('invalid duration') | ||
} | ||
} | ||
|
||
export function applyDuration(date: Date, duration: Duration): Date | null { | ||
const r = new Date(date) | ||
r.setFullYear(r.getFullYear() + duration.years) | ||
r.setMonth(r.getMonth() + duration.months) | ||
r.setDate(r.getDate() + duration.weeks * 7 + duration.days) | ||
r.setHours(r.getHours() + duration.hours) | ||
r.setMinutes(r.getMinutes() + duration.minutes) | ||
r.setSeconds(r.getSeconds() + duration.seconds) | ||
return r | ||
} | ||
|
||
export function withinDuration(a: Date, b: Date, str: string): boolean { | ||
const absStr = str.replace(/^[-+]/, '') | ||
const sign = a < b ? '-' : '' | ||
const threshold = applyDuration(a, `${sign}${absStr}`) | ||
const duration = Duration.from(str).abs() | ||
const threshold = applyDuration(a, duration) | ||
if (!threshold) return true | ||
return Math.abs(Number(threshold) - Number(a)) > Math.abs(Number(a) - Number(b)) | ||
} |
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