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