devextreme
Version:
JavaScript/TypeScript Component Suite for Responsive Web Development
118 lines (117 loc) • 5.25 kB
JavaScript
/**
* DevExtreme (esm/__internal/ui/number_box/number_box.caret.js)
* Version: 26.1.6
* Build date: Fri Oct 16 2026
*
* Copyright (c) 2012 - 2026 Developer Express Inc. ALL RIGHTS RESERVED
* Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
*/
import number from "../../../common/core/localization/number";
import {
escapeRegExp
} from "../../../core/utils/common";
import {
fitIntoRange
} from "../../../core/utils/math";
import {
getNthOccurrence,
getRealSeparatorIndex,
splitByIndex
} from "./utils";
const mockEscapedStubs = format => format.replace(/'([^']*)'/g, stub => stub.split("").map(() => " ").join("").substr(2));
const getDigitCountBeforeIndex = (index, text) => {
const decimalSeparator = number.getDecimalSeparator();
const regExp = new RegExp(`[^0-9${escapeRegExp(decimalSeparator)}]`, "g");
const textBeforePosition = text.slice(0, index);
return textBeforePosition.replace(regExp, "").length
};
const reverseText = text => text.split("").reverse().join("");
const getDigitPositionByIndex = (digitIndex, text) => {
if (!digitIndex) {
return -1
}
const regExp = /[0-9]/g;
let counter = 1;
let index = null;
let result = regExp.exec(text);
while (result) {
index = result.index;
if (counter >= digitIndex) {
return index
}
counter += 1;
result = regExp.exec(text)
}
return index ?? text.length
};
const trimNonNumericCharsFromEnd = text => text.replace(/[^0-9e]+$/, "");
const isSeparatorBasedString = text => 1 === text.length && !!text.match(/^[,.][0-9]*$/g);
export const getCaretBoundaries = (text, format) => {
if ("string" === typeof format) {
var _exec, _exec2;
const signParts = format.split(";");
const sign = number.getSign(text, format);
signParts[1] = signParts[1] || `-${signParts[0]}`;
const signFormat = mockEscapedStubs(signParts[sign < 0 ? 1 : 0]);
const prefixStubLength = (null === (_exec = /^[^#0.,]*/.exec(signFormat)) || void 0 === _exec ? void 0 : _exec[0].length) ?? 0;
const postfixStubLength = (null === (_exec2 = /[^#0.,]*$/.exec(signFormat)) || void 0 === _exec2 ? void 0 : _exec2[0].length) ?? 0;
return {
start: prefixStubLength,
end: text.length - postfixStubLength
}
}
return {
start: 0,
end: text.length
}
};
export const getCaretWithOffset = (caret, offset) => {
const range = "number" === typeof caret ? {
start: caret,
end: caret
} : caret;
return {
start: (range.start ?? 0) + offset,
end: (range.end ?? 0) + offset
}
};
export const getCaretInBoundaries = (caret, text, format) => {
const normalizedCaret = getCaretWithOffset(caret, 0);
const boundaries = getCaretBoundaries(text, format);
return {
start: fitIntoRange(normalizedCaret.start, boundaries.start, boundaries.end),
end: fitIntoRange(normalizedCaret.end, boundaries.start, boundaries.end)
}
};
export const getCaretAfterFormat = (text, formatted, caret, format) => {
const normalizedCaret = getCaretWithOffset(caret, 0);
const point = number.getDecimalSeparator();
const realSeparatorOccurrenceIndex = getRealSeparatorIndex(format).occurrence;
const pointPosition = isSeparatorBasedString(text) ? 0 : getNthOccurrence(text, point, realSeparatorOccurrenceIndex);
const newPointPosition = getNthOccurrence(formatted, point, realSeparatorOccurrenceIndex);
const textParts = splitByIndex(text, pointPosition);
const formattedParts = splitByIndex(formatted, newPointPosition);
const isCaretOnFloat = -1 !== pointPosition && normalizedCaret.start > pointPosition;
if (isCaretOnFloat) {
const relativeIndex = normalizedCaret.start - pointPosition - 1;
const digitsBefore = getDigitCountBeforeIndex(relativeIndex, textParts[1]);
const newPosition = formattedParts[1] ? newPointPosition + 1 + getDigitPositionByIndex(digitsBefore, formattedParts[1]) + 1 : formatted.length;
return getCaretInBoundaries(newPosition, formatted, format)
}
const formattedIntPart = trimNonNumericCharsFromEnd(formattedParts[0]);
const positionFromEnd = textParts[0].length - normalizedCaret.start;
const digitsFromEnd = getDigitCountBeforeIndex(positionFromEnd, reverseText(textParts[0]));
const newPositionFromEnd = getDigitPositionByIndex(digitsFromEnd, reverseText(formattedIntPart));
const newPositionFromBegin = formattedIntPart.length - (newPositionFromEnd + 1);
return getCaretInBoundaries(newPositionFromBegin, formatted, format)
};
export const isCaretInBoundaries = (caret, text, format) => {
const normalizedCaret = getCaretWithOffset(caret, 0);
const boundaries = getCaretInBoundaries(normalizedCaret, text, format);
return normalizedCaret.start >= boundaries.start && normalizedCaret.end <= boundaries.end
};
export const getCaretOffset = (previousText, newText, format) => {
const previousBoundaries = getCaretBoundaries(previousText, format);
const newBoundaries = getCaretBoundaries(newText, format);
return newBoundaries.start - previousBoundaries.start
};