deno.land / x / jose@v5.2.4 / lib / secs.ts

نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
const minute = 60const hour = minute * 60const day = hour * 24const week = day * 7const year = day * 365.25
const REGEX = /^(\+|\-)? ?(\d+|\d+\.\d+) ?(seconds?|secs?|s|minutes?|mins?|m|hours?|hrs?|h|days?|d|weeks?|w|years?|yrs?|y)(?: (ago|from now))?$/i
export default (str: string): number => { const matched = REGEX.exec(str)
if (!matched || (matched[4] && matched[1])) { throw new TypeError('Invalid time period format') }
const value = parseFloat(matched[2]) const unit = matched[3].toLowerCase()
let numericDate: number
switch (unit) { case 'sec': case 'secs': case 'second': case 'seconds': case 's': numericDate = Math.round(value) break case 'minute': case 'minutes': case 'min': case 'mins': case 'm': numericDate = Math.round(value * minute) break case 'hour': case 'hours': case 'hr': case 'hrs': case 'h': numericDate = Math.round(value * hour) break case 'day': case 'days': case 'd': numericDate = Math.round(value * day) break case 'week': case 'weeks': case 'w': numericDate = Math.round(value * week) break // years matched default: numericDate = Math.round(value * year) break }
if (matched[1] === '-' || matched[4] === 'ago') { return -numericDate }
return numericDate}
jose

Version Info

Tagged at
a month ago