MCPcopy Index your code
hub / github.com/formatjs/formatjs / createPartsFromList

Function createPartsFromList

packages/intl-listformat/index.ts:127–162  ·  view source on GitHub ↗
(
  internalSlotMap: WeakMap<ListFormat, ListFormatInternal>,
  lf: ListFormat,
  list: string[]
)

Source from the content-addressed store, hash-verified

125}
126
127function createPartsFromList(
128 internalSlotMap: WeakMap<ListFormat, ListFormatInternal>,
129 lf: ListFormat,
130 list: string[]
131) {
132 const size = list.length
133 if (size === 0) {
134 return []
135 }
136 if (size === 2) {
137 const pattern = getInternalSlot(internalSlotMap, lf, 'templatePair')
138 const first = {type: 'element', value: list[0]}
139 const second = {type: 'element', value: list[1]}
140 return deconstructPattern(pattern, {'0': first, '1': second})
141 }
142 const last = {
143 type: 'element',
144 value: list[size - 1],
145 }
146 let parts: Placeable[] | Placeable = last
147 let i = size - 2
148 while (i >= 0) {
149 let pattern
150 if (i === 0) {
151 pattern = getInternalSlot(internalSlotMap, lf, 'templateStart')
152 } else if (i < size - 2) {
153 pattern = getInternalSlot(internalSlotMap, lf, 'templateMiddle')
154 } else {
155 pattern = getInternalSlot(internalSlotMap, lf, 'templateEnd')
156 }
157 const head = {type: 'element', value: list[i]}
158 parts = deconstructPattern(pattern, {'0': head, '1': parts})
159 i--
160 }
161 return parts
162}
163
164function deconstructPattern(
165 pattern: string,

Callers 2

formatMethod · 0.85
formatToPartsMethod · 0.85

Calls 2

getInternalSlotFunction · 0.85
deconstructPatternFunction · 0.85

Tested by

no test coverage detected