Add case-changing magic words to mw.jqueryMsg
authorScimonster <tehalmightyscimonster@gmail.com>
Mon, 14 Dec 2015 16:56:26 +0000 (18:56 +0200)
committerScimonster <tehalmightyscimonster@gmail.com>
Mon, 14 Dec 2015 17:00:43 +0000 (19:00 +0200)
Add the lc, uc, lcfirst, and ucfirst magic words.
Task done as part of GCI15.

Bug: T46495
Change-Id: I246b878ff4e8ef4cf49fe1859ee6c5d33d373b30

resources/src/mediawiki/mediawiki.jqueryMsg.js
tests/qunit/suites/resources/mediawiki/mediawiki.jqueryMsg.test.js

index 620dd5e..e905f69 100644 (file)
                                number = nodes[ 0 ];
 
                        return this.language.convertNumber( number, isInteger );
+               },
+
+               /**
+                * Lowercase text
+                *
+                * @param {Array} nodes List of nodes
+                * @return {string} The given text, all in lowercase
+                */
+               lc: function ( nodes ) {
+                       return textify( nodes[ 0 ] ).toLowerCase();
+               },
+
+               /**
+                * Uppercase text
+                *
+                * @param {Array} nodes List of nodes
+                * @return {string} The given text, all in uppercase
+                */
+               uc: function ( nodes ) {
+                       return textify( nodes[ 0 ] ).toUpperCase();
+               },
+
+               /**
+                * Lowercase first letter of input, leaving the rest unchanged
+                *
+                * @param {Array} nodes List of nodes
+                * @return {string} The given text, with the first character in lowercase
+                */
+               lcfirst: function ( nodes ) {
+                       var text = textify( nodes[ 0 ] );
+                       return text.charAt( 0 ).toLowerCase() + text.slice( 1 );
+               },
+
+               /**
+                * Uppercase first letter of input, leaving the rest unchanged
+                *
+                * @param {Array} nodes List of nodes
+                * @return {string} The given text, with the first character in uppercase
+                */
+               ucfirst: function ( nodes ) {
+                       var text = textify( nodes[ 0 ] );
+                       return text.charAt( 0 ).toUpperCase() + text.slice( 1 );
                }
        };
 
index 53a714f..43b324e 100644 (file)
                mw.user.options.set( 'gender', originalGender );
        } );
 
+       QUnit.test( 'Case changing', 8, function ( assert ) {
+               mw.messages.set( 'to-lowercase', '{{lc:thIS hAS MEsSed uP CapItaliZatiON}}' );
+               assert.equal( formatParse( 'to-lowercase' ), 'this has messed up capitalization', 'To lowercase' );
+
+               mw.messages.set( 'to-caps', '{{uc:thIS hAS MEsSed uP CapItaliZatiON}}' );
+               assert.equal( formatParse( 'to-caps' ), 'THIS HAS MESSED UP CAPITALIZATION', 'To caps' );
+
+               mw.messages.set( 'uc-to-lcfirst', '{{lcfirst:THis hAS MEsSed uP CapItaliZatiON}}' );
+               mw.messages.set( 'lc-to-lcfirst', '{{lcfirst:thIS hAS MEsSed uP CapItaliZatiON}}' );
+               assert.equal( formatParse( 'uc-to-lcfirst' ), 'tHis hAS MEsSed uP CapItaliZatiON', 'Lcfirst caps' );
+               assert.equal( formatParse( 'lc-to-lcfirst' ), 'thIS hAS MEsSed uP CapItaliZatiON', 'Lcfirst lowercase' );
+
+               mw.messages.set( 'uc-to-ucfirst', '{{ucfirst:THis hAS MEsSed uP CapItaliZatiON}}' );
+               mw.messages.set( 'lc-to-ucfirst', '{{ucfirst:thIS hAS MEsSed uP CapItaliZatiON}}' );
+               assert.equal( formatParse( 'uc-to-ucfirst' ), 'THis hAS MEsSed uP CapItaliZatiON', 'Ucfirst caps' );
+               assert.equal( formatParse( 'lc-to-ucfirst' ), 'ThIS hAS MEsSed uP CapItaliZatiON', 'Ucfirst lowercase' );
+
+               mw.messages.set( 'mixed-to-sentence', '{{ucfirst:{{lc:thIS hAS MEsSed uP CapItaliZatiON}}}}' );
+               assert.equal( formatParse( 'mixed-to-sentence' ), 'This has messed up capitalization', 'To sentence case' );
+               mw.messages.set( 'all-caps-except-first', '{{lcfirst:{{uc:thIS hAS MEsSed uP CapItaliZatiON}}}}' );
+               assert.equal( formatParse( 'all-caps-except-first' ), 'tHIS HAS MESSED UP CAPITALIZATION', 'To opposite sentence case' );
+       } );
+
        QUnit.test( 'Grammar', 2, function ( assert ) {
                assert.equal( formatParse( 'grammar-msg' ), 'Przeszukaj Wiki', 'Grammar Test with sitename' );