(input: Input)
| 226 | * If the input is not a valid `Duration.Input`, it throws an error. |
| 227 | * |
| 228 | * **Example** (Decoding duration inputs) |
| 229 | * |
| 230 | * ```ts import.meta.vitest |
| 231 | * import { Duration } from "effect" |
| 232 | * |
| 233 | * Duration.fromInputUnsafe(1000) // => Duration.millis(1000) |
| 234 | * Duration.fromInputUnsafe("5 seconds") // => Duration.seconds(5) |
| 235 | * Duration.fromInputUnsafe("Infinity") // => Duration.infinity |
| 236 | * Duration.fromInputUnsafe([2, 500_000_000]) // => Duration.nanos(2_500_000_000n) |
| 237 | * ``` |
| 238 | * |
| 239 | * @category constructors |
| 240 | * @since 4.0.0 |
| 241 | */ |
| 242 | export const fromInputUnsafe = (input: Input): Duration => { |
| 243 | switch (typeof input) { |
| 244 | case "number": |
| 245 | return millis(input) |
| 246 | case "bigint": |
| 247 | return nanos(input) |
| 248 | case "string": { |
| 249 | if (input === "Infinity") { |
| 250 | return infinity |
| 251 | } |
| 252 | if (input === "-Infinity") { |
| 253 | return negativeInfinity |
| 254 | } |
| 255 | const match = DURATION_REGEXP.exec(input) |
| 256 | if (!match) break |
| 257 | const [_, valueStr, unit] = match |
| 258 | if (unit === "nano" || unit === "nanos") { |
| 259 | return nanos(parseNanos(valueStr, bigint1)) |
| 260 | } |
| 261 | if (unit === "micro" || unit === "micros") { |
| 262 | return nanos(parseNanos(valueStr, bigint1e3)) |
| 263 | } |
| 264 | const value = Number(valueStr) |
| 265 | switch (unit) { |
| 266 | case "milli": |
| 267 | case "millis": |
| 268 | return millis(value) |
| 269 | case "second": |
| 270 | case "seconds": |
| 271 | return seconds(value) |
| 272 | case "minute": |
| 273 | case "minutes": |
| 274 | return minutes(value) |
| 275 | case "hour": |
| 276 | case "hours": |
| 277 | return hours(value) |
| 278 | case "day": |
| 279 | case "days": |
| 280 | return days(value) |
| 281 | case "week": |
| 282 | case "weeks": |
| 283 | return weeks(value) |
| 284 | } |
| 285 | break |
no test coverage detected
searching dependent graphs…