in js/src/parseTweet.js [30:83]
const parseTweet = function(text = '', options = configs.defaults) {
const mergedOptions = Object.keys(options).length ? options : configs.defaults;
const { defaultWeight, emojiParsingEnabled, scale, maxWeightedTweetLength, transformedURLLength } = mergedOptions;
const normalizedText = typeof String.prototype.normalize === 'function' ? text.normalize() : text;
// Hash all entities by their startIndex for fast lookup
const urlEntitiesMap = transformEntitiesToHash(extractUrlsWithIndices(normalizedText));
const emojiEntitiesMap = emojiParsingEnabled ? transformEntitiesToHash(extractEmojiWithIndices(normalizedText)) : [];
const tweetLength = normalizedText.length;
let weightedLength = 0;
let validDisplayIndex = 0;
let valid = true;
// Go through every character and calculate weight
for (let charIndex = 0; charIndex < tweetLength; charIndex++) {
// If a url begins at the specified index handle, add constant length
if (urlEntitiesMap[charIndex]) {
const { url, indices } = urlEntitiesMap[charIndex];
weightedLength += transformedURLLength * scale;
charIndex += url.length - 1;
} else if (emojiParsingEnabled && emojiEntitiesMap[charIndex]) {
const { text: emoji, indices } = emojiEntitiesMap[charIndex];
weightedLength += defaultWeight;
charIndex += emoji.length - 1;
} else {
charIndex += isSurrogatePair(normalizedText, charIndex) ? 1 : 0;
weightedLength += getCharacterWeight(normalizedText.charAt(charIndex), mergedOptions);
}
// Only test for validity of character if it is still valid
if (valid) {
valid = !hasInvalidCharacters(normalizedText.substring(charIndex, charIndex + 1));
}
if (valid && weightedLength <= maxWeightedTweetLength * scale) {
validDisplayIndex = charIndex;
}
}
weightedLength = weightedLength / scale;
valid = valid && weightedLength > 0 && weightedLength <= maxWeightedTweetLength;
const permillage = Math.floor((weightedLength / maxWeightedTweetLength) * 1000);
const normalizationOffset = text.length - normalizedText.length;
validDisplayIndex += normalizationOffset;
return {
weightedLength,
valid,
permillage,
validRangeStart: 0,
validRangeEnd: validDisplayIndex,
displayRangeStart: 0,
displayRangeEnd: text.length > 0 ? text.length - 1 : 0
};
};