String pluralize - Node.js String

Node.js examples for String:Algorithm

Description

String pluralize

Demo Code


String.prototype.pluralize = function (count, suffix) {
    if( (this == 'is') && (count!=1)) {
        return 'are';
    } else if (this.endsWith('s')) {
        suffix = suffix ||?'es';
        return (count==1) ? this : this+suffix;
    } else if (this.endsWith('y')) {
        suffix = suffix ||?'ies';
        return (count==1) ? this : this.substr(0, this.length-1) + suffix;
    } else {//from   w  w  w  .ja v  a  2  s  .c  om
        suffix = suffix ||?'s';
        return (count==1) ? this : this+suffix;
    }
};

Related Tutorials