forked from vkolgi/tuneup_js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lang-ext.js
78 lines (68 loc) · 1.6 KB
/
lang-ext.js
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
/**
* Extend +destination+ with +source+
*/
function extend(destination, source) {
for (var property in source) {
destination[property] = source[property];
}
return destination;
}
extend(Object.prototype, {
/**
* Dump the properties out a String returned by the function.
*/
dumpProperties: function() {
var dumpStr = "";
for (var propName in this) {
if (this.hasOwnProperty(propName)) {
if (dumpStr !== "") {
dumpStr += ", ";
}
dumpStr += (propName + "=" + this[propName]);
}
}
return dumpStr;
},
getMethods: function() {
var methods = [];
for (var m in this) {
//if (typeof this[m] == "function" && this.hasOwnProperty(m)) {
if (typeof this[m] == "function") {
methods.push(m);
}
}
return methods;
}
});
extend(Array.prototype, {
/**
* Applies the given function to each element in the array until a
* match is made. Otherwise returns null.
* */
contains: function(f) {
for (i = 0; i < this.length; i++) {
if (f(this[i])) {
return this[i];
}
}
return null;
},
unique: function() {
function onlyUnique(value, index, self) {
return self.indexOf(value) === index;
}
return this.filter(onlyUnique);
}
});
String.prototype.trim = function() {
return this.replace(/^\s+|\s+$/g,"");
};
String.prototype.ltrim = function() {
return this.replace(/^\s+/,"");
};
String.prototype.rtrim = function() {
return this.replace(/\s+$/,"");
};
String.prototype.lcfirst = function() {
return this.charAt(0).toLowerCase() + this.substr(1);
};