skip to Main Content

The purpose of filter() is to remove currentGroup matching with URLs from the array. It doesn’t matter there is numbers after Malappuram/123456/12 or not. Is there any best way to do it in ES6 ?

DEMO: https://jsbin.com/jozuqameto/edit?js,console

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/32156",
  "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(filterOwnLinks)
  .map(normalizeTme);

console.log(initialLinks);
console.log(urls);
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.min.js"></script>

2

Answers


  1. You can use URL api to parse url, and take the pathname from parse url object, and check if starts with currentGroup

    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/32156","http://t.me/keraladevelopers/42716","http://t.me/joinchat/NHNd1hcSMCoYlnZGSC_H7g","http://t.me/keraladevelopers/","http://t.me/hellomates/5"];
    const currentGroup = "Malappuram";
    const urls = [...new Set(initialLinks)]
    
    let final = urls.filter(url => {
      let parsed = new URL(url)
      let pattern = new RegExp(`^/${currentGroup}`,'i')
      return !pattern.test(parsed.pathname)
    })
    
    console.log(final)
    Login or Signup to reply.
  2. You could test with a simple regular expression within the Array.prototype.filter callback:

    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/32156",
      "http://t.me/keraladevelopers/42716",
      "http://t.me/joinchat/NHNd1hcSMCoYlnZGSC_H7g",
      "http://t.me/keraladevelopers/",
      "http://t.me/hellomates/5"
    ];
    
    const getCurrentGroupLinks = (links, regex) => {
      return links.filter(link => !regex.test(link));
    };
    
    console.log(getCurrentGroupLinks(initialLinks, /Malappuram/));
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search