MCPcopy
hub / github.com/formatjs/formatjs / compareNumeric

Function compareNumeric

packages/intl-collator/compare.ts:466–514  ·  view source on GitHub ↗
(
  left: string,
  right: string,
  slots: IntlCollatorInternal
)

Source from the content-addressed store, hash-verified

464}
465
466function compareNumeric(
467 left: string,
468 right: string,
469 slots: IntlCollatorInternal
470): number {
471 // ECMA-402 numeric=true / Unicode extension kn=true compares digit runs by
472 // numeric value instead of lexical code unit order.
473 // https://tc39.es/ecma402/#sec-initializecollator
474 const leftParts = left.split(ASCII_DIGIT_RUN)
475 const rightParts = right.split(ASCII_DIGIT_RUN)
476 const leftDigits = left.match(ASCII_DIGIT_RUN) || []
477 const rightDigits = right.match(ASCII_DIGIT_RUN) || []
478 const length = Math.max(leftParts.length, rightParts.length)
479
480 for (let i = 0; i < length; i++) {
481 const partResult = comparePreparedStrings(
482 leftParts[i] || '',
483 rightParts[i] || '',
484 slots
485 )
486 if (partResult) {
487 return partResult
488 }
489
490 const leftNumber = leftDigits[i]
491 const rightNumber = rightDigits[i]
492 if (leftNumber === undefined || rightNumber === undefined) {
493 continue
494 }
495
496 const normalizedLeftNumber = leftNumber.replace(LEADING_ZERO_RE, '') || '0'
497 const normalizedRightNumber =
498 rightNumber.replace(LEADING_ZERO_RE, '') || '0'
499 const lengthResult =
500 normalizedLeftNumber.length - normalizedRightNumber.length
501 if (lengthResult) {
502 return lengthResult < 0 ? -1 : 1
503 }
504 const digitResult = compareStrings(
505 normalizedLeftNumber,
506 normalizedRightNumber
507 )
508 if (digitResult) {
509 return digitResult
510 }
511 }
512
513 return 0
514}
515
516function compareCaseFirst(
517 left: string,

Callers 1

compareCollatorStringsFunction · 0.85

Calls 2

comparePreparedStringsFunction · 0.85
compareStringsFunction · 0.85

Tested by

no test coverage detected