(input: DurationInput)
| 92 | * @since 2.0.0 |
| 93 | */ |
| 94 | export const decode = (input: DurationInput): Duration => { |
| 95 | if (isDuration(input)) { |
| 96 | return input |
| 97 | } else if (isNumber(input)) { |
| 98 | return millis(input) |
| 99 | } else if (isBigInt(input)) { |
| 100 | return nanos(input) |
| 101 | } else if (Array.isArray(input) && input.length === 2 && input.every(isNumber)) { |
| 102 | if (input[0] === -Infinity || input[1] === -Infinity || Number.isNaN(input[0]) || Number.isNaN(input[1])) { |
| 103 | return zero |
| 104 | } |
| 105 | |
| 106 | if (input[0] === Infinity || input[1] === Infinity) { |
| 107 | return infinity |
| 108 | } |
| 109 | |
| 110 | return nanos(BigInt(Math.round(input[0] * 1_000_000_000)) + BigInt(Math.round(input[1]))) |
| 111 | } else if (isString(input)) { |
| 112 | const match = DURATION_REGEX.exec(input) |
| 113 | if (match) { |
| 114 | const [_, valueStr, unit] = match |
| 115 | const value = Number(valueStr) |
| 116 | switch (unit) { |
| 117 | case "nano": |
| 118 | case "nanos": |
| 119 | return nanos(BigInt(valueStr)) |
| 120 | case "micro": |
| 121 | case "micros": |
| 122 | return micros(BigInt(valueStr)) |
| 123 | case "milli": |
| 124 | case "millis": |
| 125 | return millis(value) |
| 126 | case "second": |
| 127 | case "seconds": |
| 128 | return seconds(value) |
| 129 | case "minute": |
| 130 | case "minutes": |
| 131 | return minutes(value) |
| 132 | case "hour": |
| 133 | case "hours": |
| 134 | return hours(value) |
| 135 | case "day": |
| 136 | case "days": |
| 137 | return days(value) |
| 138 | case "week": |
| 139 | case "weeks": |
| 140 | return weeks(value) |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 | throw new Error("Invalid DurationInput") |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * @since 2.5.0 |
no test coverage detected