Advertisement
pcriativa

helpers/friendly-url

Sep 17th, 2020
1,050
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. static friendlyUrl(input = null, ligature = null) {
  2.         if (input === null) return;
  3.  
  4.         const withAccent =
  5.             'ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝŔÞßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþÿŕ';
  6.         const withoutAccent =
  7.             'AAAAAAACEEEEIIIIDNOOOOOOUUUUYRsBaaaaaaaceeeeiiiionoooooouuuuybyr';
  8.         const specialCharactere = ` !"#$%&'()*+,-./:;<=>?@[\\]^_\`{|}~`;
  9.  
  10.         let newString = '';
  11.  
  12.         for (let i = 0; i < input.length; i++) {
  13.             let change = false;
  14.  
  15.             for (let a = 0; a < withAccent.length; a++) {
  16.                 if (input.substr(i, 1) === withAccent.substr(a, 1)) {
  17.                     newString += withoutAccent.substr(a, 1);
  18.                     change = true;
  19.                     break;
  20.                 }
  21.             }
  22.  
  23.             for (let b = 0; b < specialCharactere.length; b++) {
  24.                 if (input.substr(i, 1) === specialCharactere.substr(b, 1)) {
  25.                     newString += ' ';
  26.                     change = true;
  27.                     break;
  28.                 }
  29.             }
  30.  
  31.             if (change === false) {
  32.                 newString += input.substr(i, 1);
  33.             }
  34.         }
  35.  
  36.         newString = newString.toLowerCase().split(' ');
  37.  
  38.         newString = newString.filter(el => {
  39.             return el !== '';
  40.         });
  41.  
  42.         const slug = ligature !== null ? ligature : '-';
  43.  
  44.         return newString.join(slug);
  45.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement