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

Method breaksAt

packages/intl-segmenter/segmenter.ts:199–261  ·  view source on GitHub ↗
(
    position: number,
    input: string
  )

Source from the content-addressed store, hash-verified

197 }
198
199 public breaksAt(
200 position: number,
201 input: string
202 ): ReturnType<typeof breaksAtResult> {
203 const ruleSortedKeys = this.ruleSortedKeys
204 const rules = this.rules
205 const mergedSegmentationTypeValue = this.mergedSegmentationTypeValue
206
207 //artificial rule 0.2
208 if (position === 0) {
209 return breaksAtResult(true, '0.2')
210 }
211
212 if (position === input.length) {
213 //rule 0.3
214 return breaksAtResult(true, '0.3')
215 }
216
217 //artificial rule 0.1: js specific, due to es5 regex not being unicode aware
218 //number 0.1 chosen to mimic java implementation, but needs to execute after 0.2 and 0.3 to be inside the string bounds
219 if (isSurrogate(input, position)) {
220 return breaksAtResult(false, '0.1')
221 }
222
223 const stringBeforeBreak = input.substring(0, position)
224 const stringAfterBreak = input.substring(position)
225
226 //artificial rule 0.4: handle suppressions
227 if ('suppressions' in mergedSegmentationTypeValue) {
228 for (const suppressions of mergedSegmentationTypeValue.suppressions) {
229 if (stringBeforeBreak.trim().endsWith(suppressions)) {
230 return breaksAtResult(false, '0.4')
231 }
232 }
233 }
234
235 // loop through rules and find a match
236 for (const ruleKey of ruleSortedKeys) {
237 const {before, after, breaks} = rules[ruleKey]
238 // for debugging
239 // if (ruleKey === '16' && position === 4) {
240 // console.log({before, after, stringBeforeBreak, stringAfterBreak})
241 // }
242 if (before) {
243 if (!before.test(stringBeforeBreak)) {
244 //didn't match the before part, therfore skipping
245 continue
246 }
247 }
248
249 if (after) {
250 if (!after.test(stringAfterBreak)) {
251 //didn't match the after part, therfore skipping
252 continue
253 }
254 }
255
256 return breaksAtResult(breaks, ruleKey)

Callers 3

debugRunFunction · 0.95
nextMethod · 0.80
containingMethod · 0.80

Calls 2

breaksAtResultFunction · 0.85
isSurrogateFunction · 0.85

Tested by

no test coverage detected