FIX#64
[euphorik.git] / js / betterjs.js
1 // tout un tas d'améliorations de JavaScript ;)
2
3
4 /**
5 * Pour chaque propriété de l'objet execute f(p, v) ou p est le nom de la propriété et v sa valeur.
6 * Ne parcours pas les propriétés des prototypes.
7 * FIXME : Normalement : Object.prototype.each = function(f) mais non supporté par jquery
8 */
9 //Object.prototype.each = function(f) {
10 var objectEach = function(o, f) {
11 for (var k in o) {
12 if (o.hasOwnProperty(k)) {
13 f(k, o[k]);
14 }
15 }
16 };
17
18 var objectMemberCount = function(o) {
19 var nb = 0;
20 for (var k in o) {
21 if (o.hasOwnProperty(k)) {
22 nb += 1;
23 }
24 }
25 return nb;
26 };
27
28 Array.prototype.each = function(f) {
29 for (var i = 0; i < this.length; i++) {
30 f(i, this[i]);
31 }
32 };
33
34 Array.prototype.map = function(f) {
35 for (var i = 0; i < this.length; i++) {
36 this[i] = f(this[i]);
37 }
38 };
39
40 String.prototype.trim = function() {
41 return jQuery.trim(this); // anciennement : this.replace(/^\s+|\s+$/g, "");
42 };
43
44 String.prototype.ltrim = function() {
45 return this.replace(/^\s+/, "");
46 };
47
48 String.prototype.rtrim = function() {
49 return this.replace(/\s+$/, "");
50 };
51
52 /**
53 * Voir : http://www.coolpage.com/developer/javascript/Correct%20OOP%20for%20Javascript.html
54 *
55 * Exemple :
56 *
57 * function Mammal(name) {
58 * this.name = name;
59 * }
60 *
61 * Cat.Inherits(Mammal);
62 * function Cat(name) {
63 * this.Super(Mammal, name);
64 * }
65 */
66 /*Object.prototype.Super = function(parent) {
67 if(arguments.length > 1) {
68 parent.apply( this, Array.prototype.slice.call( arguments, 1 ) );
69 } else {
70 parent.call( this );
71 }
72 }
73 Function.prototype.Inherits = function(parent) {
74 this.prototype = new parent();
75 this.prototype.constructor = this;
76 }*/