Nodejs String Slugify slug(max)

Here you can find the source of slug(max)

Method Source Code

import $removeDiacritics from './internal/removeDiacritics';

String.prototype.slug = function(max) {
    max = max || 60;// ww  w . j  a  v a2 s . c  o m
    var self = $removeDiacritics(this.trim().toLowerCase());
    var b = '';
    var length = self.length;
    for (var i = 0; i < length; i++) {
        var c = self[i];
        var code = self.charCodeAt(i);
        if (b.length >= max) {
            break;
        }
        if (code > 31 && code < 48) {
            if (b.length > 0 && b[b.length - 1] !== '-') {
                b += '-';
            }
            continue;
        }
        if ((code > 47 && code < 58) || (code > 94 && code < 123)) {
            b += c;
        }
    }
    var l = b.length - 1;
    return b[l] === '-' ? b.substring(0, l) : b;
};

Related

  1. slug()
    String.prototype.slug=function(){
       return this.toLowerCase().replace(/\s/g,'-').replace(/[^a-zA-Z0-9]/g,'-').replace(/-{2,}/g,'-').replace(/^-/,'').replace(/-$/,'');
    
  2. slugify()
    String.prototype.slugify = function() {
        var string = this.replace(/[^\w\s-]/g, '').trim().toLowerCase();
        return string.replace(/[_\s]+/g, '_');
    };
    
  3. slugify()
    String.prototype.slugify = function() {
      return this.toString().toLowerCase()
        .replace(/\s+/g, '-')           
        .replace(/[^\w\-]+/g, '')       
        .replace(/\-\-+/g, '-')         
        .replace(/^-+/, '')             
        .replace(/-+$/, '');            
    };
    
  4. slugify()
    String.prototype.slugify = function ()
      return this.toLowerCase()
        .replace(/\s+/g, '-')           
        .replace(/[^\w\-]+/g, '')       
        .replace(/\-\-+/g, '-')         
        .replace(/^-+/, '')             
        .replace(/-+$/, '');            
    
  5. slugify()
    String.prototype.slugify = function () {
        return this.trim().replace(/\s+/g, '-');
    };