MCPcopy Create free account
hub / github.com/effect-app/libs / fromInputUnsafe

Function fromInputUnsafe

repos/effect/packages/effect/src/Duration.ts:228–307  ·  view source on GitHub ↗
(input: Input)

Source from the content-addressed store, hash-verified

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 */
242export 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

Callers 8

toMillisFunction · 0.85
toSecondsFunction · 0.85
toMinutesFunction · 0.85
toHoursFunction · 0.85
toDaysFunction · 0.85
toWeeksFunction · 0.85
toNanosUnsafeFunction · 0.85
toHrTimeFunction · 0.85

Calls 13

millisFunction · 0.85
nanosFunction · 0.85
parseNanosFunction · 0.85
secondsFunction · 0.85
minutesFunction · 0.85
hoursFunction · 0.85
daysFunction · 0.85
weeksFunction · 0.85
invalidFunction · 0.85
roundTiesAwayFromZeroFunction · 0.85
execMethod · 0.80
NumberInterface · 0.70

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…