MCPcopy Index your code
hub / github.com/github/relative-time-element / from

Method from

src/duration.ts:58–73  ·  view source on GitHub ↗
(durationLike: unknown)

Source from the content-addressed store, hash-verified

56 }
57
58 static from(durationLike: unknown): Duration {
59 if (typeof durationLike === 'string') {
60 const str = String(durationLike).trim()
61 const factor = str.startsWith('-') ? -1 : 1
62 const parsed = str
63 .match(durationRe)
64 ?.slice(1)
65 .map(x => (Number(x) || 0) * factor)
66 if (!parsed) return new Duration()
67 return new Duration(...parsed)
68 } else if (typeof durationLike === 'object') {
69 const {years, months, weeks, days, hours, minutes, seconds, milliseconds} = durationLike as Record<string, number>
70 return new Duration(years, months, weeks, days, hours, minutes, seconds, milliseconds)
71 }
72 throw new RangeError('invalid duration')
73 }
74
75 static compare(one: unknown, two: unknown): -1 | 0 | 1 {
76 const now = Date.now()

Callers 4

duration.tsFile · 0.80
compareMethod · 0.80
getUnitFactorFunction · 0.80

Calls

no outgoing calls

Tested by

no test coverage detected