Bug Tracker

Ticket #2279: trim[6001].diff

File trim[6001].diff, 2.3 KB (added by flesler, 3 years ago)

My trim function along with tests.

  • src/core.js

     
    11171117                return elem[ name ]; 
    11181118        }, 
    11191119 
     1120        // Trim function by Ariel Flesler 
     1121        // http://flesler.blogspot.com/2008/11/fast-trim-function-for-javascript.html 
    11201122        trim: function( text ) { 
    1121                 return (text || "").replace( /^\s+|\s+$/g, "" ); 
     1123                if( !text ) 
     1124                        return ""; 
     1125 
     1126                var start = -1, 
     1127                        end = text.length; 
     1128                while( text.charCodeAt(--end) < 33 ); 
     1129                while( text.charCodeAt(++start) < 33 ); 
     1130                return text.slice( start, end + 1 ); 
    11221131        }, 
    11231132 
    11241133        makeArray: function( array ) { 
     
    11261135 
    11271136                if( array != null ){ 
    11281137                        var i = array.length; 
    1129                         // The window, strings (and functions) also have 'length' 
     1138                        // The window, strings and functions also have 'length' 
    11301139                        if( i == null || typeof array === "string" || jQuery.isFunction(array) || array.setInterval ) 
    11311140                                ret[0] = array; 
    11321141                        else 
  • test/unit/core.js

     
    17951795        equals( c[0].nodeValue, "hi", "Check node,textnode,comment contents is just the one from span" ); 
    17961796}); 
    17971797 
    1798 test("jQuery.makeArray", function(){ 
     1798test("makeArray()", function(){ 
    17991799        expect(15); 
    18001800 
    18011801        equals( jQuery.makeArray(jQuery('html>*'))[0].nodeName, "HEAD", "Pass makeArray a jQuery object" ); 
     
    18301830 
    18311831        ok( jQuery.makeArray(document.getElementById('form')).length >= 13, "Pass makeArray a form (treat as elements)" ); 
    18321832}); 
     1833 
     1834test("trim()", function(){ 
     1835        expect( 10 ); 
     1836         
     1837        var parse = jQuery.trim; 
     1838         
     1839        equals( parse(""), "", "Empty String" ); 
     1840        equals( parse("  "), "", "Empty, with spaces" ); 
     1841        equals( parse(" \n\t\r  "), "", "Empty, with new lines, tabs, etc" ); 
     1842         
     1843        equals( parse("abc"), "abc", "No spaces" ); 
     1844         
     1845        equals( parse("abc  "), "abc", "Trailing spaces" ); 
     1846        equals( parse("abc \n\t\r  "), "abc", "Trailing new lines, tabs, etc" ); 
     1847         
     1848        equals( parse("  abc"), "abc", "Leading spaces" ); 
     1849        equals( parse(" \n\t\r abc"), "abc", "Leading new lines, tabs, etc" ); 
     1850         
     1851        equals( parse("  abc  "), "abc", "Both leading and trailing spaces" ); 
     1852        equals( parse(" \n\t\r abc \n\t\r "), "abc", "Both leading and trailing new lines, tabs, etc" ); 
     1853}); 
     1854 No newline at end of file