Ext.util.Inflector

Hierarchy

Ext.Base
Ext.util.Inflector

Files

General purpose inflector class that pluralizes, singularizes and ordinalizes words. Sample usage:

// turning singular words into plurals
Ext.util.Inflector.pluralize('word'); // 'words'
Ext.util.Inflector.pluralize('person'); // 'people'
Ext.util.Inflector.pluralize('sheep'); // 'sheep'

// turning plurals into singulars
Ext.util.Inflector.singularize('words'); // 'word'
Ext.util.Inflector.singularize('people'); // 'person'
Ext.util.Inflector.singularize('sheep'); // 'sheep'

// ordinalizing numbers
Ext.util.Inflector.ordinalize(11); // "11th"
Ext.util.Inflector.ordinalize(21); // "21st"
Ext.util.Inflector.ordinalize(1043); // "1043rd"

Customization

The Inflector comes with a default set of US English pluralization rules. These can be augmented with additional rules if the default rules do not meet your application's requirements, or swapped out entirely for other languages. Here is how we might add a rule that pluralizes "ox" to "oxen":

Ext.util.Inflector.plural(/^(ox)$/i, "$1en");

Each rule consists of two items - a regular expression that matches one or more rules, and a replacement string. In this case, the regular expression will only match the string "ox", and will replace that match with "oxen". Here's how we could add the inverse rule:

Ext.util.Inflector.singular(/^(ox)en$/i, "$1");

Note: The ox/oxen rules are present by default.

Properties

Defined By

Instance properties

Ext.util.Inflector
view source
: Arrayprivate
The registered plural tuples. ...

The registered plural tuples. Each item in the array should contain two items - the first must be a regular expression that matchers the singular form of a word, the second must be a String that replaces the matched part of the regular expression. This is managed by the plural method.

Defaults to: [[/(quiz)$/i, "$1zes"], [/^(ox)$/i, "$1en"], [/([m|l])ouse$/i, "$1ice"], [/(matr|vert|ind)ix|ex$/i, "$1ices"], [/(x|ch|ss|sh)$/i, "$1es"], [/([^aeiouy]|qu)y$/i, "$1ies"], [/(hive)$/i, "$1s"], [/(?:([^f])fe|([lr])f)$/i, "$1$2ves"], [/sis$/i, "ses"], [/([ti])um$/i, "$1a"], [/(buffal|tomat|potat)o$/i, "$1oes"], [/(bu)s$/i, "$1ses"], [/(alias|status|sex)$/i, "$1es"], [/(octop|vir)us$/i, "$1i"], [/(ax|test)is$/i, "$1es"], [/^person$/, "people"], [/^man$/, "men"], [/^(child)$/, "$1ren"], [/s$/i, "s"], [/$/, "s"]]

Get the reference to the current class from which this object was instantiated. ...

Get the reference to the current class from which this object was instantiated. Unlike statics, this.self is scope-dependent and it's meant to be used for dynamic inheritance. See statics for a detailed comparison

Ext.define('My.Cat', {
    statics: {
        speciesName: 'Cat' // My.Cat.speciesName = 'Cat'
    },

    constructor: function() {
        alert(this.self.speciesName); // dependent on 'this'
    },

    clone: function() {
        return new this.self();
    }
});


Ext.define('My.SnowLeopard', {
    extend: 'My.Cat',
    statics: {
        speciesName: 'Snow Leopard'         // My.SnowLeopard.speciesName = 'Snow Leopard'
    }
});

var cat = new My.Cat();                     // alerts 'Cat'
var snowLeopard = new My.SnowLeopard();     // alerts 'Snow Leopard'

var clone = snowLeopard.clone();
alert(Ext.getClassName(clone));             // alerts 'My.SnowLeopard'
Ext.util.Inflector
view source
: Arrayprivate
The set of registered singular matchers. ...

The set of registered singular matchers. Each item in the array should contain two items - the first must be a regular expression that matches the plural form of a word, the second must be a String that replaces the matched part of the regular expression. This is managed by the singular method.

Defaults to: [[/(quiz)zes$/i, "$1"], [/(matr)ices$/i, "$1ix"], [/(vert|ind)ices$/i, "$1ex"], [/^(ox)en/i, "$1"], [/(alias|status)es$/i, "$1"], [/(octop|vir)i$/i, "$1us"], [/(cris|ax|test)es$/i, "$1is"], [/(shoe)s$/i, "$1"], [/(o)es$/i, "$1"], [/(bus)es$/i, "$1"], [/([m|l])ice$/i, "$1ouse"], [/(x|ch|ss|sh)es$/i, "$1"], [/(m)ovies$/i, "$1ovie"], [/(s)eries$/i, "$1eries"], [/([^aeiouy]|qu)ies$/i, "$1y"], [/([lr])ves$/i, "$1f"], [/(tive)s$/i, "$1"], [/(hive)s$/i, "$1"], [/([^f])ves$/i, "$1fe"], [/(^analy)ses$/i, "$1sis"], [/((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$/i, "$1$2sis"], [/([ti])a$/i, "$1um"], [/(n)ews$/i, "$1ews"], [/people$/i, "person"], [/s$/i, ""]]

Ext.util.Inflector
view source
: Arrayprivate
The registered uncountable words ...

The registered uncountable words

Defaults to: ["sheep", "fish", "series", "species", "money", "rice", "information", "equipment", "grass", "mud", "offspring", "deer", "means"]

Defined By

Static properties

...

Defaults to: []

Methods

Defined By

Instance methods

Call the original method that was previously overridden with override, This method is deprecated as callParent does ...

Call the original method that was previously overridden with override,

This method is deprecated as callParent does the same thing.

Ext.define('My.Cat', {
    constructor: function() {
        alert("I'm a cat!");
    }
});

My.Cat.override({
    constructor: function() {
        alert("I'm going to be a cat!");

        var instance = this.callOverridden();

        alert("Meeeeoooowwww");

        return instance;
    }
});

var kitty = new My.Cat(); // alerts "I'm going to be a cat!"
                          // alerts "I'm a cat!"
                          // alerts "Meeeeoooowwww"

Parameters

  • args : Array/Arguments

    The arguments, either an array or the arguments object from the current method, for example: this.callOverridden(arguments)

Returns

  • Object

    Returns the result of calling the overridden method

Call the "parent" method of the current method. ...

Call the "parent" method of the current method. That is the method previously overridden by derivation or by an override (see Ext.define).

 Ext.define('My.Base', {
     constructor: function (x) {
         this.x = x;
     },

     statics: {
         method: function (x) {
             return x;
         }
     }
 });

 Ext.define('My.Derived', {
     extend: 'My.Base',

     constructor: function () {
         this.callParent([21]);
     }
 });

 var obj = new My.Derived();

 alert(obj.x);  // alerts 21

This can be used with an override as follows:

 Ext.define('My.DerivedOverride', {
     override: 'My.Derived',

     constructor: function (x) {
         this.callParent([x*2]); // calls original My.Derived constructor
     }
 });

 var obj = new My.Derived();

 alert(obj.x);  // now alerts 42

This also works with static methods.

 Ext.define('My.Derived2', {
     extend: 'My.Base',

     statics: {
         method: function (x) {
             return this.callParent([x*2]); // calls My.Base.method
         }
     }
 });

 alert(My.Base.method(10));     // alerts 10
 alert(My.Derived2.method(10)); // alerts 20

Lastly, it also works with overridden static methods.

 Ext.define('My.Derived2Override', {
     override: 'My.Derived2',

     statics: {
         method: function (x) {
             return this.callParent([x*2]); // calls My.Derived2.method
         }
     }
 });

 alert(My.Derived2.method(10)); // now alerts 40

To override a method and replace it and also call the superclass method, use callSuper. This is often done to patch a method to fix a bug.

Parameters

  • args : Array/Arguments

    The arguments, either an array or the arguments object from the current method, for example: this.callParent(arguments)

Returns

  • Object

    Returns the result of calling the parent method

This method is used by an override to call the superclass method but bypass any overridden method. ...

This method is used by an override to call the superclass method but bypass any overridden method. This is often done to "patch" a method that contains a bug but for whatever reason cannot be fixed directly.

Consider:

 Ext.define('Ext.some.Class', {
     method: function () {
         console.log('Good');
     }
 });

 Ext.define('Ext.some.DerivedClass', {
     method: function () {
         console.log('Bad');

         // ... logic but with a bug ...

         this.callParent();
     }
 });

To patch the bug in DerivedClass.method, the typical solution is to create an override:

 Ext.define('App.paches.DerivedClass', {
     override: 'Ext.some.DerivedClass',

     method: function () {
         console.log('Fixed');

         // ... logic but with bug fixed ...

         this.callSuper();
     }
 });

The patch method cannot use callParent to call the superclass method since that would call the overridden method containing the bug. In other words, the above patch would only produce "Fixed" then "Good" in the console log, whereas, using callParent would produce "Fixed" then "Bad" then "Good".

Parameters

  • args : Array/Arguments

    The arguments, either an array or the arguments object from the current method, for example: this.callSuper(arguments)

Returns

  • Object

    Returns the result of calling the superclass method

Ext.util.Inflector
view source
( word ) : String
Returns the correct Model name for a given string. ...

Returns the correct Model name for a given string. Mostly used internally by the data package

Parameters

  • word : String

    The word to classify

Returns

  • String

    The classified version of the word

Fires

    Ext.util.Inflector
    view source
    ( )
    Removes all registered pluralization rules ...

    Removes all registered pluralization rules

    Ext.util.Inflector
    view source
    ( )
    Removes all registered singularization rules ...

    Removes all registered singularization rules

    ...

    Parameters

    Returns the initial configuration passed to constructor. ...

    Returns the initial configuration passed to constructor.

    Parameters

    • name : String (optional)

      When supplied, value for particular configuration option is returned, otherwise the full config object is returned.

    Returns

    ...

    Parameters

    ( instanceConfig ) : Objectchainableprotected
    Initialize configuration for this class. ...

    Initialize configuration for this class. a typical example:

    Ext.define('My.awesome.Class', {
        // The default config
        config: {
            name: 'Awesome',
            isAwesome: true
        },
    
        constructor: function(config) {
            this.initConfig(config);
        }
    });
    
    var awesome = new My.awesome.Class({
        name: 'Super Awesome'
    });
    
    alert(awesome.getName()); // 'Super Awesome'
    

    Parameters

    Returns

    • Object

      mixins The mixin prototypes as key - value pairs

    Fires

      Ext.util.Inflector
      view source
      ( word ) : Boolean
      Returns true if the given word is transnumeral (the word is its own singular and plural form - e.g. ...

      Returns true if the given word is transnumeral (the word is its own singular and plural form - e.g. sheep, fish)

      Parameters

      • word : String

        The word to test

      Returns

      • Boolean

        True if the word is transnumeral

      ( names, callback, scope )private
      ...

      Parameters

      Ext.util.Inflector
      view source
      ( number ) : String
      Ordinalizes a given number by adding a prefix such as 'st', 'nd', 'rd' or 'th' based on the last digit of the number. ...

      Ordinalizes a given number by adding a prefix such as 'st', 'nd', 'rd' or 'th' based on the last digit of the number. 21 -> 21st, 22 -> 22nd, 23 -> 23rd, 24 -> 24th etc

      Parameters

      • number : Number

        The number to ordinalize

      Returns

      Ext.util.Inflector
      view source
      ( matcher, replacer )
      Adds a new pluralization rule to the Inflector. ...

      Adds a new pluralization rule to the Inflector. See the intro docs for more information

      Parameters

      • matcher : RegExp

        The matcher regex

      • replacer : String

        The replacement string, which can reference matches from the matcher argument

      Ext.util.Inflector
      view source
      ( word ) : String
      Returns the pluralized form of a word (e.g. ...

      Returns the pluralized form of a word (e.g. Ext.util.Inflector.pluralize('word') returns 'words')

      Parameters

      • word : String

        The word to pluralize

      Returns

      • String

        The pluralized form of the word

      Fires

        ( config, applyIfNotSet ) : Ext.Basechainableprivate
        ...

        Parameters

        Returns

        Ext.util.Inflector
        view source
        ( matcher, replacer )
        Adds a new singularization rule to the Inflector. ...

        Adds a new singularization rule to the Inflector. See the intro docs for more information

        Parameters

        • matcher : RegExp

          The matcher regex

        • replacer : String

          The replacement string, which can reference matches from the matcher argument

        Ext.util.Inflector
        view source
        ( word ) : String
        Returns the singularized form of a word (e.g. ...

        Returns the singularized form of a word (e.g. Ext.util.Inflector.singularize('words') returns 'word')

        Parameters

        • word : String

          The word to singularize

        Returns

        • String

          The singularized form of the word

        Fires

          Get the reference to the class from which this object was instantiated. ...

          Get the reference to the class from which this object was instantiated. Note that unlike self, this.statics() is scope-independent and it always returns the class from which it was called, regardless of what this points to during run-time

          Ext.define('My.Cat', {
              statics: {
                  totalCreated: 0,
                  speciesName: 'Cat' // My.Cat.speciesName = 'Cat'
              },
          
              constructor: function() {
                  var statics = this.statics();
          
                  alert(statics.speciesName);     // always equals to 'Cat' no matter what 'this' refers to
                                                  // equivalent to: My.Cat.speciesName
          
                  alert(this.self.speciesName);   // dependent on 'this'
          
                  statics.totalCreated++;
              },
          
              clone: function() {
                  var cloned = new this.self();                    // dependent on 'this'
          
                  cloned.groupName = this.statics().speciesName;   // equivalent to: My.Cat.speciesName
          
                  return cloned;
              }
          });
          
          
          Ext.define('My.SnowLeopard', {
              extend: 'My.Cat',
          
              statics: {
                  speciesName: 'Snow Leopard'     // My.SnowLeopard.speciesName = 'Snow Leopard'
              },
          
              constructor: function() {
                  this.callParent();
              }
          });
          
          var cat = new My.Cat();                 // alerts 'Cat', then alerts 'Cat'
          
          var snowLeopard = new My.SnowLeopard(); // alerts 'Cat', then alerts 'Snow Leopard'
          
          var clone = snowLeopard.clone();
          alert(Ext.getClassName(clone));         // alerts 'My.SnowLeopard'
          alert(clone.groupName);                 // alerts 'Cat'
          
          alert(My.Cat.totalCreated);             // alerts 3
          

          Returns

          Defined By

          Static methods

          ( config, fullMerge )privatestatic
          ...

          Parameters

          ( members )chainableprivatestatic
          ...

          Parameters

          ( name, member )chainableprivatestatic
          ...

          Parameters

          ( members )chainablestatic
          Add methods / properties to the prototype of this class. ...

          Add methods / properties to the prototype of this class.

          Ext.define('My.awesome.Cat', {
              constructor: function() {
                  // ...
              }
          });
          
           My.awesome.Cat.addMembers({
               meow: function() {
                  alert('Meowww...');
               }
           });
          
           var kitty = new My.awesome.Cat();
           kitty.meow();
          

          Parameters

          ( members ) : Ext.Basechainablestatic
          Add / override static properties of this class. ...

          Add / override static properties of this class.

          Ext.define('My.cool.Class', {
              // this.se
          });
          
          My.cool.Class.addStatics({
              someProperty: 'someValue',      // My.cool.Class.someProperty = 'someValue'
              method1: function() {  },    // My.cool.Class.method1 = function() { ... };
              method2: function() {  }     // My.cool.Class.method2 = function() { ... };
          });
          

          Parameters

          Returns

          ( xtype )chainableprivatestatic
          ...

          Parameters

          ( fromClass, members ) : Ext.Basechainableprivatestatic
          Borrow another class' members to the prototype of this class. ...

          Borrow another class' members to the prototype of this class.

          Ext.define('Bank', {
              money: '$$$',
              printMoney: function() {
                  alert('$$$$$$$');
              }
          });
          
          Ext.define('Thief', {
              // ...
          });
          
          Thief.borrow(Bank, ['money', 'printMoney']);
          
          var steve = new Thief();
          
          alert(steve.money); // alerts '$$$'
          steve.printMoney(); // alerts '$$$$$$$'
          

          Parameters

          • fromClass : Ext.Base

            The class to borrow members from

          • members : Array/String

            The names of the members to borrow

          Returns

          ( args )protectedstatic
          ...

          Parameters

          Create a new instance of this Class. ...

          Create a new instance of this Class.

          Ext.define('My.cool.Class', {
              // ...
          });
          
          My.cool.Class.create({
              someConfig: true
          });
          

          All parameters are passed to the constructor of the class.

          Returns

          ( alias, origin )static
          Create aliases for existing prototype methods. ...

          Create aliases for existing prototype methods. Example:

          Ext.define('My.cool.Class', {
              method1: function() {  },
              method2: function() {  }
          });
          
          var test = new My.cool.Class();
          
          My.cool.Class.createAlias({
              method3: 'method1',
              method4: 'method2'
          });
          
          test.method3(); // test.method1()
          
          My.cool.Class.createAlias('method5', 'method3');
          
          test.method5(); // test.method3() -> test.method1()
          

          Parameters

          ( parent )privatestatic
          ...

          Parameters

          Get the current class' name in string format. ...

          Get the current class' name in string format.

          Ext.define('My.cool.Class', {
              constructor: function() {
                  alert(this.self.getName()); // alerts 'My.cool.Class'
              }
          });
          
          My.cool.Class.getName(); // 'My.cool.Class'
          

          Returns

          ...
          ( name, mixinClass )privatestatic
          Used internally by the mixins pre-processor ...

          Used internally by the mixins pre-processor

          Parameters

          ( fn, scope )chainableprivatestatic
          ...

          Parameters

          ( members ) : Ext.Basechainabledeprecatedstatic
          Override members of this class. ...

          Override members of this class. Overridden methods can be invoked via callParent.

          Ext.define('My.Cat', {
              constructor: function() {
                  alert("I'm a cat!");
              }
          });
          
          My.Cat.override({
              constructor: function() {
                  alert("I'm going to be a cat!");
          
                  var instance = this.callParent(arguments);
          
                  alert("Meeeeoooowwww");
          
                  return instance;
              }
          });
          
          var kitty = new My.Cat(); // alerts "I'm going to be a cat!"
                                    // alerts "I'm a cat!"
                                    // alerts "Meeeeoooowwww"
          

          As of 2.1, direct use of this method is deprecated. Use Ext.define instead:

          Ext.define('My.CatOverride', {
              override: 'My.Cat',
          
              constructor: function() {
                  alert("I'm going to be a cat!");
          
                  var instance = this.callParent(arguments);
          
                  alert("Meeeeoooowwww");
          
                  return instance;
              }
          });
          

          The above accomplishes the same result but can be managed by the Ext.Loader which can properly order the override and its target class and the build process can determine whether the override is needed based on the required state of the target class (My.Cat).

          This method has been deprecated since 2.1.0

          Please use Ext.define instead

          Parameters

          • members : Object

            The properties to add to this class. This should be specified as an object literal containing one or more properties.

          Returns