0
\$\begingroup\$

The solution found to filtering pathname with customising current normalizeTme() function. For sure it is not the ideal way of fixing. It would be great if any one can guide me with better option with less number of line code.

DEMO: https://codepen.io/athimannil/pen/vojVmL

const initialLinks = [
  "http://www.lchfmalayalam.com",
  "https://t.me/Malappuram",
  "https://t.me/keraladevelopers/42716",
  "http://www.whatsapp.com",
  "https://www.youtube.com/watch?v=BnbFRSyHIl4",
  "http://google.com",
  "https://t.me/joinchat/NHNd1hcSMCoYlnZGSC_H7g",
  "https://t.me/keraladevelopers/",
  "http://t.me/keraladevelopers",
  "http://athimannil.com/react/",
  "http://athimannil.info/",
  "https://t.me/hellomates/5",
  "http://t.me/Malappuram",
  "http://t.me/keraladevelopers/42716",
  "http://t.me/joinchat/NHNd1hcSMCoYlnZGSC_H7g",
  "http://t.me/keraladevelopers/",
  "http://t.me/hellomates/5"
];

const normalizeTme = R.replace(
  /^(?:@|(?:https?:\/\/)?(?:t\.me|telegram\.(?:me|dog))\/)(\w+)(\/.+)?/i,
  (_match, username, rest) => {
    return /^\/\d+$/.test(rest) ?
      `https://t.me/${username.toLowerCase()}` :
      `https://t.me/${username.toLowerCase()}${rest || ""}`;
  }
);

const filterOwnLinks = groupUsername => {
  return R.match(
    /^(?:@|(?:https?:\/\/)?(?:t\.me|telegram\.(?:me|dog))\/)(\w+)(\/.+)?/i,
    (_match, username, rest) => {
      if (username) {
        return currentGroup.toLowerCase() !== username.toLowerCase();
      }
      return true;
    }
  );
};

const currentGroup = "Malappuram";

const urls = R.uniq(initialLinks)
  .filter(links => {
    const matse = R.match(
      /^(?:@|(?:https?:\/\/)?(?:t\.me|telegram\.(?:me|dog))\/)(\w+)(\/.+)?/i,
      links
    );
    if (matse[1]) {
      return currentGroup.toLowerCase() !== matse[1].toLowerCase();
    }
    return true;
  })
  .map(normalizeTme);
console.log(initialLinks);
console.log(urls);
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.17.1/ramda.min.js"></script>

\$\endgroup\$
  • \$\begingroup\$ @ggorlen Updated the question codepen.io/athimannil/pen/vojVmL \$\endgroup\$ – Muhammed Aug 8 at 21:13
  • \$\begingroup\$ @ggorlen The code pen seems to working for me. Unfortunately I don't know how to import ramda in to the stackoverflow snippet, I have tried a lot 😔 \$\endgroup\$ – Muhammed Aug 8 at 21:39
  • 1
    \$\begingroup\$ @ggorlen Yes, finally snippet is working. Thanks a lot for your help \$\endgroup\$ – Muhammed Aug 8 at 22:16
  • 1
    \$\begingroup\$ Awesome. Looks great. I'd like to echo my original question: what do you mean by normalizing? I'm not entirely sure I understand what the code is supposed to do. \$\endgroup\$ – ggorlen Aug 8 at 22:19
  • \$\begingroup\$ @ggorlen Actually this is part of Telegram bot. I need to remove current group related links from the array list. You can see the original code here github.com/thedevs-network/the-guard-bot/blob/develop/handlers/… \$\endgroup\$ – Muhammed Aug 8 at 23:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.