UNPKG

react-native-advanced-input-mask

Version:

Text input mask for React Native on iOS, Android and web. Synchronous and easy formatting without hustle

60 lines (58 loc) 1.72 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.calculateAffinityOfMask = calculateAffinityOfMask; var _enums = require("../../enums"); /** * Calculates an affinity score based on the specified strategy. */ function calculateAffinityOfMask(strategy, mask, text) { switch (strategy) { case _enums.AFFINITY_CALCULATION_STRATEGY.WHOLE_STRING: { const result = mask.apply(text); return result.affinity; } case _enums.AFFINITY_CALCULATION_STRATEGY.PREFIX: { const result = mask.apply(text); return prefixIntersection(result.formattedText.string, text.string).length; } case _enums.AFFINITY_CALCULATION_STRATEGY.CAPACITY: { if (text.string.length > mask.totalTextLength()) { return Number.MIN_SAFE_INTEGER; } return text.string.length - mask.totalTextLength(); } case _enums.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