Just Talk About Web

Techniques Behind Modern Web

Archive for January, 2008



JavaScript String Functions


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.


Recommended Reading: This site recommends Website Magazine for 'Net Success

Don't forget to subscribe so you don't miss out on future posts!

Finally, after many rumors of a (big) upcoming IPO, MySQL AB has been acquired by Sun for 1$ billion instead. MySQL had raised a total of $39 million from Benchmark, Index, IVP, Intel, and SAP. Sun CEO Jonathan Schwartz wrote about the acquisition on his blog: "But the biggest news of the day is... we're putting a billion dollars behind ...

  • 0 Comments
  • Filed under: MySQL
  • Microsoft Volta was often referred to as a GWT copycat (carelessly). Yes, Volta does generate JavaScript but it does so in a somewhat different way than GWT: while GWT reads Java code and compiles to JavaScript, Volta reads bytecode (.NET IL) and compiles to JavaScript. And Volta uses the existing .Net APIs instead of defining its own UI APIs like GWT ...

    2nd Birthday of JQuery

    John Resig, the creator and lead developer of the jQuery JavaScript library, looks back when JQuery was born: I remember doing the first release at BarCamp NYC (combined with the mention of two other projects of mine that fizzled: Feed Pile and Idea Shrub). While I had released a bunch of open source code in the past, this was the first ...

    Looking for Prototype/script.aculo.us replacement on Rails? jRails project can be an answer if you prefer JQuery. Here is info from project site: Features jRails provides drop-in functionality for these existing Rails methods. Prototype form_remote_for form_remote_tag link_to_remote observe_field observe_form periodically_call_remote remote_form_for submit_to_remote Scriptaculous draggable_element drop_receiving_element sortable_element visual_effect RJS hide insert_html remove replace replace_html show toggle Visual Effects The visual effects in jRails are based on the new jquery-fx library. jRails currently uses a slightly modified version of jquery fx code to get some of the desired effects. ...

    Now Come “The Rails Way”

    Long awaited book stating itself as the "expert guide to building Ruby on Rails applications" has finally come. This book will help you: Increase your productivity as a web developer Realize the overall joy of programming with Ruby on Rails Learn what's new in Rails 2.0 Drive design and protect ...

    Myles Eftos has given 6 things try Rails this year. They are: Install Rails: This is aimed at those of you out there that haven’t tried Ruby on Rails yet. Jump in - have a go, there are plenty of resources out there, and it is fairly easy to install regardless of your platform Upgrade to Rails 2.0: I have covered what’s ...

  • 0 Comments
  • Filed under: Ruby on Rails
  • Hubble Space Telescope 2.0

    One of Hubble's most famous images: pillars of creation where stars are forming in the Eagle Nebula. Credit: NASA/ESA What are new upgrades in Hubble Space Telescope version 2.0? They will make the Hubble telescope 100 times more powerful. And, big news, they will add Web 2.0 functionality on the companion website "hubbl.". You'll ...

  • 8 Comments
  • Filed under: Web 2.0
  • Do you need GWT on Rails?

    Oh, my first thought was "It's silly to add Java to Rails" because normally GWT uses the Java language to generate JavaScript. But I changed my mind after reading Jon Crosby's answers to a short interview from InfoQ: Most of Java's weight comes from JEE, which is not part of GWT on Rails. Client side GWT uses the Java language to ...

  • 0 Comments
  • Filed under: GWT, Ruby on Rails
  • Tim O’Reilly opines on notable tech books from 2007 -- just have a quick look on his list. Mac OS X Leopard: The Missing Manual: this book sold out of its 50,000 copy first printing in a few of days. In the fourth quarter of 2007, the total size of the ...

  • 3 Comments
  • Filed under: Book Reviews
  • Premium Sponsors

    Career Opportunities

    Advertisements

    Website Magazine
    Earn money from your website/blog by, selling text links, banner ads - Advertisers can, buy links, from your blog for SEO. Get paid through PayPal