MCPcopy Create free account
hub / github.com/Effect-TS/effect / fromInputUnsafe

Function fromInputUnsafe

packages/effect/src/Duration.ts:275–361  ·  view source on GitHub ↗
(input: Input)

Source from the content-addressed store, hash-verified

273 * @since 4.0.0
274 */
275export const fromInputUnsafe = (input: Input): Duration => {
276 switch (typeof input) {
277 case "number":
278 return millis(input)
279 case "bigint":
280 return nanos(input)
281 case "string": {
282 if (input === "Infinity") {
283 return infinity
284 }
285 if (input === "-Infinity") {
286 return negativeInfinity
287 }
288 const match = DURATION_REGEXP.exec(input)
289 if (!match) break
290 const [_, valueStr, unit] = match
291 if (unit === "nano" || unit === "nanos") {
292 return nanos(parseNanos(valueStr, bigint1))
293 }
294 if (unit === "micro" || unit === "micros") {
295 return nanos(parseNanos(valueStr, bigint1e3))
296 }
297 switch (unit) {
298 case "milli":
299 case "millis":
300 return fromMillisString(valueStr, 1)
301 case "second":
302 case "seconds":
303 return fromMillisString(valueStr, 1_000)
304 case "minute":
305 case "minutes":
306 return fromMillisString(valueStr, 60_000)
307 case "hour":
308 case "hours":
309 return fromMillisString(valueStr, 3_600_000)
310 case "day":
311 case "days":
312 return fromMillisString(valueStr, 86_400_000)
313 case "week":
314 case "weeks":
315 return fromMillisString(valueStr, 604_800_000)
316 }
317 break
318 }
319 case "object": {
320 if (input === null) break
321 if (TypeId in input) return input as Duration
322 if (Array.isArray(input)) {
323 if (input.length !== 2 || !input.every(isNumber)) {
324 return invalid(input)
325 }
326 if (Number.isNaN(input[0]) || Number.isNaN(input[1])) {
327 return zero
328 }
329 if (input[0] === -Infinity || input[1] === -Infinity) {
330 return negativeInfinity
331 }
332 if (input[0] === Infinity || input[1] === Infinity) {

Callers 9

toMicrosFunction · 0.70
toMillisFunction · 0.70
toSecondsFunction · 0.70
toMinutesFunction · 0.70
toHoursFunction · 0.70
toDaysFunction · 0.70
toWeeksFunction · 0.70
toNanosUnsafeFunction · 0.70
toHrTimeFunction · 0.70

Calls 10

nanosFunction · 0.85
parseNanosFunction · 0.85
fromMillisStringFunction · 0.85
fromNanosNumberFunction · 0.85
sumObjectNanosFunction · 0.85
millisFunction · 0.70
invalidFunction · 0.70
makeFunction · 0.70
BigIntInterface · 0.70
execMethod · 0.45

Tested by

no test coverage detected