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

Method formatToParts

src/duration-format-ponyfill.ts:98–127  ·  view source on GitHub ↗
(duration: Duration)

Source from the content-addressed store, hash-verified

96 }
97
98 formatToParts(duration: Duration): DurationPart[] {
99 const list: string[] = []
100 const options = this.#options
101 const style = options.style
102 const locale = options.locale
103 for (const [unit, nfUnit] of partsTable) {
104 const value = duration[unit]
105 if (options[`${unit}Display`] === 'auto' && !value) continue
106 const unitStyle = options[unit]
107 const nfOpts =
108 unitStyle === '2-digit'
109 ? twoDigitFormatOptions
110 : unitStyle === 'numeric'
111 ? {}
112 : {style: 'unit' as const, unit: nfUnit, unitDisplay: unitStyle}
113
114 let formattedValue = new Intl.NumberFormat(locale, nfOpts).format(value)
115
116 // Custom handling for narrow month formatting to use "mo" instead of "m"
117 if (unit === 'months' && (unitStyle === 'narrow' || (style === 'narrow' && formattedValue.endsWith('m')))) {
118 formattedValue = formattedValue.replace(/(\d+)m$/, '$1mo')
119 }
120
121 list.push(formattedValue)
122 }
123 return new ListFormat(locale, {
124 type: 'unit',
125 style: style === 'digital' ? 'short' : style,
126 }).formatToParts(list)
127 }
128
129 format(duration: Duration) {
130 return this.formatToParts(duration)

Callers 2

formatMethod · 0.95

Calls 1

formatMethod · 0.45

Tested by

no test coverage detected