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

Function getUnitFactor

src/relative-time-element.ts:28–56  ·  view source on GitHub ↗
(el: RelativeTimeElement)

Source from the content-addressed store, hash-verified

26}
27
28function getUnitFactor(el: RelativeTimeElement): number {
29 const date = el.date
30 if (!date) return Infinity
31 const now = Date.now()
32 if (el.format === 'duration' || el.format === 'elapsed') {
33 const precision = el.precision
34 if (precision === 'second') {
35 return 1000
36 } else if (precision === 'minute') {
37 return 60 * 1000
38 }
39 }
40 const ms = Math.abs(now - date.getTime())
41 let factor = 60 * 60 * 1000
42 if (ms < 60 * 1000) {
43 factor = 1000
44 } else if (ms < 60 * 60 * 1000) {
45 factor = 60 * 1000
46 }
47 const threshold = getExplicitThreshold(el)
48 if (el.format === 'micro' && threshold) {
49 const thresholdDuration = Duration.from(threshold)
50 const signedThresholdDuration = date.getTime() > now ? negateDuration(thresholdDuration) : thresholdDuration
51 const thresholdTime = applyDuration(date, signedThresholdDuration).getTime()
52 const msUntilThreshold = thresholdTime - now
53 if (msUntilThreshold > 0) factor = Math.min(factor, msUntilThreshold)
54 }
55 return factor
56}
57
58function negateDuration(duration: Duration): Duration {
59 return new Duration(

Callers 2

observeMethod · 0.85
updateMethod · 0.85

Calls 5

getExplicitThresholdFunction · 0.85
negateDurationFunction · 0.85
applyDurationFunction · 0.85
absMethod · 0.80
fromMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…