react-native-advanced-input-mask
Version:
Text input mask for React Native on iOS, Android and web. Synchronous and easy formatting without hustle
54 lines (53 loc) • 1.58 kB
JavaScript
import { AFFINITY_CALCULATION_STRATEGY } from "../../enums";
/**
* Calculates an affinity score based on the specified strategy.
*/
export function calculateAffinityOfMask(strategy, mask, text) {
switch (strategy) {
case AFFINITY_CALCULATION_STRATEGY.WHOLE_STRING:
{
const result = mask.apply(text);
return result.affinity;
}
case AFFINITY_CALCULATION_STRATEGY.PREFIX:
{
const result = mask.apply(text);
return prefixIntersection(result.formattedText.string, text.string).length;
}
case AFFINITY_CALCULATION_STRATEGY.CAPACITY:
{
if (text.string.length > mask.totalTextLength()) {
return Number.MIN_SAFE_INTEGER;
}
return text.string.length - mask.totalTextLength();
}
case AFFINITY_CALCULATION_STRATEGY.EXTRACTED_VALUE_CAPACITY:
{
const result = mask.apply(text);
const extractedValueLength = result.extractedValue.length;
if (extractedValueLength > mask.totalValueLength()) {
return Number.MIN_SAFE_INTEGER;
}
return extractedValueLength - mask.totalValueLength();
}
default:
return 0;
}
}
/**
* Finds common prefix between two strings.
*/
function prefixIntersection(str1, str2) {
if (!str1 || !str2) {
return "";
}
let endIndex = 0;
while (endIndex < str1.length && endIndex < str2.length) {
if (str1[endIndex] !== str2[endIndex]) {
break;
}
endIndex++;
}
return str1.substring(0, endIndex);
}
//# sourceMappingURL=affinityCalculationStrategy.js.map