MCPcopy
hub / github.com/formatjs/formatjs / containing

Method containing

packages/intl-segmenter/segmenter.ts:456–515  ·  view source on GitHub ↗
(positionInput: number)

Source from the content-addressed store, hash-verified

454 }
455
456 containing(positionInput: number):
457 | {
458 segment: string
459 index: number
460 input: string
461 isWordLike?: boolean
462 }
463 | undefined {
464 if (typeof positionInput === 'bigint') {
465 throw TypeError('Index must not be a BigInt')
466 }
467
468 let position = Number(positionInput)
469
470 //https://tc39.es/ecma262/#sec-tointegerorinfinity
471 // 2. If number is NaN, +0𝔽, or -0𝔽, return 0.
472 if (isNaN(position) || !position) {
473 position = 0
474 }
475 // 5. Let integer be floor(abs(ℝ(number))).
476 // 6. If number < -0𝔽, set integer to -integer.
477 position = Math.floor(Math.abs(position)) * (position < 0 ? -1 : 1)
478
479 if (position < 0 || position >= this.input.length) {
480 return undefined
481 }
482
483 //find previous break point
484 let previousBreakPoint = 0
485 if (position === 0) {
486 previousBreakPoint = 0
487 } else {
488 const checkString = this.input
489 for (let cursor = position; cursor >= 0; cursor--) {
490 const {breaks} = this.segmenter.breaksAt(cursor, checkString)
491 if (breaks) {
492 previousBreakPoint = cursor
493 break
494 }
495 }
496 }
497 let checkString = this.input.substring(previousBreakPoint)
498 //find next break point
499 for (let cursor = 1; cursor <= checkString.length; cursor++) {
500 const {breaks, matchingRule} = this.segmenter.breaksAt(
501 cursor,
502 checkString
503 )
504 if (breaks) {
505 const segment = checkString.substring(0, cursor)
506 return createSegmentDataObject(
507 this.segmenter,
508 segment,
509 previousBreakPoint,
510 this.input,
511 matchingRule
512 )
513 }

Callers 1

runFunction · 0.80

Calls 4

createSegmentDataObjectFunction · 0.85
floorMethod · 0.80
absMethod · 0.80
breaksAtMethod · 0.80

Tested by

no test coverage detected