Techniques Behind Modern Web
19 Jan
Matt Snider has shown us JavaScript String helper functions that recalls the ones I used in my project recently — a bundle of functions collected from many sources. Frankly, some of them are not very efficient.
1 2 3 4 5 6 7 8 9 10 | String.prototype.ellipse = function(maxLength){ if(this.length > maxLength){ return this.substr(0, maxLength-3) + '...'; } return this; }; String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, ''); }; |
He used YAHOO.lang.augmentObject to augment the String.prototype providing with most needed and missing functionality: word capitalization, stripping characters (alpha, numbers, etc…), stripping tags (script, or all html tags), and trimming white spaces.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 | YAHOO.lang.augmentObject(String.prototype, { /** * Capitolize the first letter of every word; ucfirst, ensures that all non-first letters are lower-case * * @method capitalize * @param ucfirst {boolean} OPTIONAL: when truthy, converts non-first letters to lower-case * @return {string} the converted string * @static */ capitalize: function(ucfirst) { var words = this.split(/b/g), rs = []; Core.batch(words, function(w, i) { if (w.trim()) { rs[i] = w.charAt(0).toUpperCase() + (ucfirst? w.substring(1).toLowerCase(): w.substring(1)); } }); return rs.join(’ ‘); }, /** * Checks if a string contains any of the strings in the arguement set * * @method contains * @param argument {string} as many strings you want to test * @return {boolean} true, if string contains any of the arguements * @static */ contains: function() { var hasValue = false; Core.batch(arguments, function(arg) { hasValue = -1 < str.indexOf(arg); // terminates iteration if this becomes true return hasValue; }); return hasValue; }, /** * Removes the rx pattern from the string * * @method remove * @param rx {regex} a regex to find characters to remove * @public */ remove: function(rx) { return this.replace(rx, ''); }, /** * Remove all non-alpha characters;space ok * * @method stripNonAlpha * @public */ stripNonAlpha: function() { return this.remove(/[^A-Za-z ]+/g); }, /** * Remove all non-alpha-numeric characters; space ok * * @method stripNonAlphaNumeric * @public */ stripNonAlphaNumeric: function() { return this.remove(/[^A-Za-z0-9 ]+/g); }, /** * Removes non-numeric characters, except minus and decimal * * @method stripNonNumeric * @public */ stripNonNumeric: function() { return this.remove(/[^0-9-.]/g); }, /** * Remove all characters that are 0-9 * * @method stripNumeric * @public */ stripNumeric: function() { return this.remove(/[0-9]/g); }, /** * HTML script tags from the string * * @method stripScripts * @public */ stripScripts: function() { return this.remove(new RegExp(‘(?: )((n|r|.)*?)(?:)’, ‘img’)); }, /** * HTML tags from the string * * @method stripTags * @public */ stripTags: function() { return this.remove(/]+>/gi); }, /** * Replaces the white spaces at the front and end of the string * OPTIMIZED: http://blog.stevenlevithan.com/archives/faster-trim-javascript * * @method trim * @public */ trim: function() { return this.remove(/^ss*/).remove(/ss*$/); } }); |
He said he had put a lot of thought into some of these methods, such as trim, to achieve the most efficient regex.
I like the way he put them all together and surely I’ll use them in my projects in future.
Sponsored by Free Website Trade Publication >> Website Magazine
Don't forget to subscribe
so you don't miss out on future posts!
1 Trackbacks/Pingbacks
Leave a reply