How to use twemoji-parser - 1 common examples

To help you get started, we’ve selected a few twemoji-parser examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github twitter / twitter-text / js / src / parseTweet.js View on Github external
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;

twemoji-parser

Parser for identifying Twemoji in text

MIT
Latest version published 3 years ago

Package Health Score

51 / 100
Full package analysis

Popular twemoji-parser functions

Similar packages