(str: string)
| 15 | /^(\+|\-)? ?(\d+|\d+\.\d+) ?(seconds?|secs?|s|minutes?|mins?|m|hours?|hrs?|h|days?|d|weeks?|w|years?|yrs?|y)(?: (ago|from now))?$/i |
| 16 | |
| 17 | export function secs(str: string): number { |
| 18 | const matched = REGEX.exec(str) |
| 19 | |
| 20 | if (!matched || (matched[4] && matched[1])) { |
| 21 | throw new TypeError('Invalid time period format') |
| 22 | } |
| 23 | |
| 24 | const value = parseFloat(matched[2]) |
| 25 | const unit = matched[3].toLowerCase() |
| 26 | |
| 27 | let numericDate: number |
| 28 | |
| 29 | switch (unit) { |
| 30 | case 'sec': |
| 31 | case 'secs': |
| 32 | case 'second': |
| 33 | case 'seconds': |
| 34 | case 's': |
| 35 | numericDate = Math.round(value) |
| 36 | break |
| 37 | case 'minute': |
| 38 | case 'minutes': |
| 39 | case 'min': |
| 40 | case 'mins': |
| 41 | case 'm': |
| 42 | numericDate = Math.round(value * minute) |
| 43 | break |
| 44 | case 'hour': |
| 45 | case 'hours': |
| 46 | case 'hr': |
| 47 | case 'hrs': |
| 48 | case 'h': |
| 49 | numericDate = Math.round(value * hour) |
| 50 | break |
| 51 | case 'day': |
| 52 | case 'days': |
| 53 | case 'd': |
| 54 | numericDate = Math.round(value * day) |
| 55 | break |
| 56 | case 'week': |
| 57 | case 'weeks': |
| 58 | case 'w': |
| 59 | numericDate = Math.round(value * week) |
| 60 | break |
| 61 | // years matched |
| 62 | default: |
| 63 | numericDate = Math.round(value * year) |
| 64 | break |
| 65 | } |
| 66 | |
| 67 | if (matched[1] === '-' || matched[4] === 'ago') { |
| 68 | return -numericDate |
| 69 | } |
| 70 | |
| 71 | return numericDate |
| 72 | } |
| 73 | |
| 74 | function validateInput(label: string, input: number) { |
no outgoing calls
no test coverage detected
searching dependent graphs…