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

Function fromInputUnsafe

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

Source from the content-addressed store, hash-verified

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
286 }
287 case "object": {
288 if (input === null) break
289 if (TypeId in input) return input as Duration
290 if (Array.isArray(input)) {
291 if (input.length !== 2 || !input.every(isNumber)) {
292 return invalid(input)
293 }
294 if (Number.isNaN(input[0]) || Number.isNaN(input[1])) {
295 return zero
296 }
297 if (input[0] === -Infinity || input[1] === -Infinity) {
298 return negativeInfinity
299 }

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