Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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 += getCharacterWeight(emoji.charAt(0), mergedOptions);
charIndex += emoji.length - 1;