Nodejs String Pluralize plural()

Here you can find the source of plural()

Method Source Code

String.prototype.plural = function() {
   var s = this.trim().toLowerCase();
   end = s.substr(-1);//  w w  w  .ja v a  2 s. co m
   if(end == 'y') {
      var vowels = ['a', 'e', 'i', 'o', 'u'];
      s = s.substr(-2, 1) in vowels ? s + 's' : s.substr(0, s.length-1) + 'ies';
   } else if(end == 'h') {
       s += s.substr(-2) == 'ch' || s.substr(-2) == 'sh' ? 'es' : 's';
   } else if(end == 's') {
      s += 'es';
   } else {
      s += 's';
   }
   return s;
}

Related

  1. pluralize()
    String.prototype.pluralize = function () {
      return inflector.pluralize(this);
    };
    
  2. pluralize()
    String.prototype.pluralize = function() {
      const plurals = {
        person: "people",
        Person: "People"
      };
      if (plurals.hasOwnProperty(this)) {
        return plurals[this];
      return `${this}s`;
    ...
    
  3. pluralize()
    String.prototype.pluralize = function () {
      if (this.lastChar() === 'y') {
        if ((this.charAt(this.length - 2)).isVowel()) {
          return this + 's';
        else {
          return this.slice(0, -1) + 'ies';
      else if (this.substring(this.length - 2) === 'us') {
        return this.slice(0, -2) + 'i';
      else if (
        [
          'ch',
          'sh'
        ].indexOf(this.substring(this.length - 2)) !== -1 ||
          [
            'x',
            's'
          ].indexOf(this.lastChar()) !== -1) {
        return this + 'es';
      else {
        return this + 's';
    };
    
  4. pluralize()
    String.prototype.pluralize = function () {
        return Inflector.applyRules(this, Inflector.pluralRules);
    };