author | Lennard de Rijk <ljvderijk@gmail.com> |
Sun, 25 Oct 2009 18:45:58 -0700 | |
changeset 3052 | 6907a33ca0a2 |
parent 3041 | c8f47f0b6697 |
permissions | -rw-r--r-- |
3041
c8f47f0b6697
JSDocToolkit 2.3.0 added to create documentation for JS files.
Mario Ferraro <fadinlight@gmail.com>
parents:
diff
changeset
|
1 |
/** |
c8f47f0b6697
JSDocToolkit 2.3.0 added to create documentation for JS files.
Mario Ferraro <fadinlight@gmail.com>
parents:
diff
changeset
|
2 |
@name String |
c8f47f0b6697
JSDocToolkit 2.3.0 added to create documentation for JS files.
Mario Ferraro <fadinlight@gmail.com>
parents:
diff
changeset
|
3 |
@class Additions to the core string object. |
c8f47f0b6697
JSDocToolkit 2.3.0 added to create documentation for JS files.
Mario Ferraro <fadinlight@gmail.com>
parents:
diff
changeset
|
4 |
*/ |
c8f47f0b6697
JSDocToolkit 2.3.0 added to create documentation for JS files.
Mario Ferraro <fadinlight@gmail.com>
parents:
diff
changeset
|
5 |
|
c8f47f0b6697
JSDocToolkit 2.3.0 added to create documentation for JS files.
Mario Ferraro <fadinlight@gmail.com>
parents:
diff
changeset
|
6 |
/** @author Steven Levithan, released as public domain. */ |
c8f47f0b6697
JSDocToolkit 2.3.0 added to create documentation for JS files.
Mario Ferraro <fadinlight@gmail.com>
parents:
diff
changeset
|
7 |
String.prototype.trim = function() { |
c8f47f0b6697
JSDocToolkit 2.3.0 added to create documentation for JS files.
Mario Ferraro <fadinlight@gmail.com>
parents:
diff
changeset
|
8 |
var str = this.replace(/^\s+/, ''); |
c8f47f0b6697
JSDocToolkit 2.3.0 added to create documentation for JS files.
Mario Ferraro <fadinlight@gmail.com>
parents:
diff
changeset
|
9 |
for (var i = str.length - 1; i >= 0; i--) { |
c8f47f0b6697
JSDocToolkit 2.3.0 added to create documentation for JS files.
Mario Ferraro <fadinlight@gmail.com>
parents:
diff
changeset
|
10 |
if (/\S/.test(str.charAt(i))) { |
c8f47f0b6697
JSDocToolkit 2.3.0 added to create documentation for JS files.
Mario Ferraro <fadinlight@gmail.com>
parents:
diff
changeset
|
11 |
str = str.substring(0, i + 1); |
c8f47f0b6697
JSDocToolkit 2.3.0 added to create documentation for JS files.
Mario Ferraro <fadinlight@gmail.com>
parents:
diff
changeset
|
12 |
break; |
c8f47f0b6697
JSDocToolkit 2.3.0 added to create documentation for JS files.
Mario Ferraro <fadinlight@gmail.com>
parents:
diff
changeset
|
13 |
} |
c8f47f0b6697
JSDocToolkit 2.3.0 added to create documentation for JS files.
Mario Ferraro <fadinlight@gmail.com>
parents:
diff
changeset
|
14 |
} |
c8f47f0b6697
JSDocToolkit 2.3.0 added to create documentation for JS files.
Mario Ferraro <fadinlight@gmail.com>
parents:
diff
changeset
|
15 |
return str; |
c8f47f0b6697
JSDocToolkit 2.3.0 added to create documentation for JS files.
Mario Ferraro <fadinlight@gmail.com>
parents:
diff
changeset
|
16 |
} |
c8f47f0b6697
JSDocToolkit 2.3.0 added to create documentation for JS files.
Mario Ferraro <fadinlight@gmail.com>
parents:
diff
changeset
|
17 |
/*t: |
c8f47f0b6697
JSDocToolkit 2.3.0 added to create documentation for JS files.
Mario Ferraro <fadinlight@gmail.com>
parents:
diff
changeset
|
18 |
plan(6, "Testing String.prototype.trim."); |
c8f47f0b6697
JSDocToolkit 2.3.0 added to create documentation for JS files.
Mario Ferraro <fadinlight@gmail.com>
parents:
diff
changeset
|
19 |
|
c8f47f0b6697
JSDocToolkit 2.3.0 added to create documentation for JS files.
Mario Ferraro <fadinlight@gmail.com>
parents:
diff
changeset
|
20 |
var s = " a bc ".trim(); |
c8f47f0b6697
JSDocToolkit 2.3.0 added to create documentation for JS files.
Mario Ferraro <fadinlight@gmail.com>
parents:
diff
changeset
|
21 |
is(s, "a bc", "multiple spaces front and back are trimmed."); |
c8f47f0b6697
JSDocToolkit 2.3.0 added to create documentation for JS files.
Mario Ferraro <fadinlight@gmail.com>
parents:
diff
changeset
|
22 |
|
c8f47f0b6697
JSDocToolkit 2.3.0 added to create documentation for JS files.
Mario Ferraro <fadinlight@gmail.com>
parents:
diff
changeset
|
23 |
s = "a bc\n\n".trim(); |
c8f47f0b6697
JSDocToolkit 2.3.0 added to create documentation for JS files.
Mario Ferraro <fadinlight@gmail.com>
parents:
diff
changeset
|
24 |
is(s, "a bc", "newlines only in back are trimmed."); |
c8f47f0b6697
JSDocToolkit 2.3.0 added to create documentation for JS files.
Mario Ferraro <fadinlight@gmail.com>
parents:
diff
changeset
|
25 |
|
c8f47f0b6697
JSDocToolkit 2.3.0 added to create documentation for JS files.
Mario Ferraro <fadinlight@gmail.com>
parents:
diff
changeset
|
26 |
s = "\ta bc".trim(); |
c8f47f0b6697
JSDocToolkit 2.3.0 added to create documentation for JS files.
Mario Ferraro <fadinlight@gmail.com>
parents:
diff
changeset
|
27 |
is(s, "a bc", "tabs only in front are trimmed."); |
c8f47f0b6697
JSDocToolkit 2.3.0 added to create documentation for JS files.
Mario Ferraro <fadinlight@gmail.com>
parents:
diff
changeset
|
28 |
|
c8f47f0b6697
JSDocToolkit 2.3.0 added to create documentation for JS files.
Mario Ferraro <fadinlight@gmail.com>
parents:
diff
changeset
|
29 |
s = "\n \t".trim(); |
c8f47f0b6697
JSDocToolkit 2.3.0 added to create documentation for JS files.
Mario Ferraro <fadinlight@gmail.com>
parents:
diff
changeset
|
30 |
is(s, "", "an all-space string is trimmed to empty."); |
c8f47f0b6697
JSDocToolkit 2.3.0 added to create documentation for JS files.
Mario Ferraro <fadinlight@gmail.com>
parents:
diff
changeset
|
31 |
|
c8f47f0b6697
JSDocToolkit 2.3.0 added to create documentation for JS files.
Mario Ferraro <fadinlight@gmail.com>
parents:
diff
changeset
|
32 |
s = "a b\nc".trim(); |
c8f47f0b6697
JSDocToolkit 2.3.0 added to create documentation for JS files.
Mario Ferraro <fadinlight@gmail.com>
parents:
diff
changeset
|
33 |
is(s, "a b\nc", "a string with no spaces in front or back is trimmed to itself."); |
c8f47f0b6697
JSDocToolkit 2.3.0 added to create documentation for JS files.
Mario Ferraro <fadinlight@gmail.com>
parents:
diff
changeset
|
34 |
|
c8f47f0b6697
JSDocToolkit 2.3.0 added to create documentation for JS files.
Mario Ferraro <fadinlight@gmail.com>
parents:
diff
changeset
|
35 |
s = "".trim(); |
c8f47f0b6697
JSDocToolkit 2.3.0 added to create documentation for JS files.
Mario Ferraro <fadinlight@gmail.com>
parents:
diff
changeset
|
36 |
is(s, "", "an empty string is trimmed to empty."); |
c8f47f0b6697
JSDocToolkit 2.3.0 added to create documentation for JS files.
Mario Ferraro <fadinlight@gmail.com>
parents:
diff
changeset
|
37 |
|
c8f47f0b6697
JSDocToolkit 2.3.0 added to create documentation for JS files.
Mario Ferraro <fadinlight@gmail.com>
parents:
diff
changeset
|
38 |
*/ |
c8f47f0b6697
JSDocToolkit 2.3.0 added to create documentation for JS files.
Mario Ferraro <fadinlight@gmail.com>
parents:
diff
changeset
|
39 |
|
c8f47f0b6697
JSDocToolkit 2.3.0 added to create documentation for JS files.
Mario Ferraro <fadinlight@gmail.com>
parents:
diff
changeset
|
40 |
String.prototype.balance = function(open, close) { |
c8f47f0b6697
JSDocToolkit 2.3.0 added to create documentation for JS files.
Mario Ferraro <fadinlight@gmail.com>
parents:
diff
changeset
|
41 |
var i = 0; |
c8f47f0b6697
JSDocToolkit 2.3.0 added to create documentation for JS files.
Mario Ferraro <fadinlight@gmail.com>
parents:
diff
changeset
|
42 |
while (this.charAt(i) != open) { |
c8f47f0b6697
JSDocToolkit 2.3.0 added to create documentation for JS files.
Mario Ferraro <fadinlight@gmail.com>
parents:
diff
changeset
|
43 |
if (i == this.length) return [-1, -1]; |
c8f47f0b6697
JSDocToolkit 2.3.0 added to create documentation for JS files.
Mario Ferraro <fadinlight@gmail.com>
parents:
diff
changeset
|
44 |
i++; |
c8f47f0b6697
JSDocToolkit 2.3.0 added to create documentation for JS files.
Mario Ferraro <fadinlight@gmail.com>
parents:
diff
changeset
|
45 |
} |
c8f47f0b6697
JSDocToolkit 2.3.0 added to create documentation for JS files.
Mario Ferraro <fadinlight@gmail.com>
parents:
diff
changeset
|
46 |
|
c8f47f0b6697
JSDocToolkit 2.3.0 added to create documentation for JS files.
Mario Ferraro <fadinlight@gmail.com>
parents:
diff
changeset
|
47 |
var j = i+1; |
c8f47f0b6697
JSDocToolkit 2.3.0 added to create documentation for JS files.
Mario Ferraro <fadinlight@gmail.com>
parents:
diff
changeset
|
48 |
var balance = 1; |
c8f47f0b6697
JSDocToolkit 2.3.0 added to create documentation for JS files.
Mario Ferraro <fadinlight@gmail.com>
parents:
diff
changeset
|
49 |
while (j < this.length) { |
c8f47f0b6697
JSDocToolkit 2.3.0 added to create documentation for JS files.
Mario Ferraro <fadinlight@gmail.com>
parents:
diff
changeset
|
50 |
if (this.charAt(j) == open) balance++; |
c8f47f0b6697
JSDocToolkit 2.3.0 added to create documentation for JS files.
Mario Ferraro <fadinlight@gmail.com>
parents:
diff
changeset
|
51 |
if (this.charAt(j) == close) balance--; |
c8f47f0b6697
JSDocToolkit 2.3.0 added to create documentation for JS files.
Mario Ferraro <fadinlight@gmail.com>
parents:
diff
changeset
|
52 |
if (balance == 0) break; |
c8f47f0b6697
JSDocToolkit 2.3.0 added to create documentation for JS files.
Mario Ferraro <fadinlight@gmail.com>
parents:
diff
changeset
|
53 |
j++; |
c8f47f0b6697
JSDocToolkit 2.3.0 added to create documentation for JS files.
Mario Ferraro <fadinlight@gmail.com>
parents:
diff
changeset
|
54 |
if (j == this.length) return [-1, -1]; |
c8f47f0b6697
JSDocToolkit 2.3.0 added to create documentation for JS files.
Mario Ferraro <fadinlight@gmail.com>
parents:
diff
changeset
|
55 |
} |
c8f47f0b6697
JSDocToolkit 2.3.0 added to create documentation for JS files.
Mario Ferraro <fadinlight@gmail.com>
parents:
diff
changeset
|
56 |
|
c8f47f0b6697
JSDocToolkit 2.3.0 added to create documentation for JS files.
Mario Ferraro <fadinlight@gmail.com>
parents:
diff
changeset
|
57 |
return [i, j]; |
c8f47f0b6697
JSDocToolkit 2.3.0 added to create documentation for JS files.
Mario Ferraro <fadinlight@gmail.com>
parents:
diff
changeset
|
58 |
} |
c8f47f0b6697
JSDocToolkit 2.3.0 added to create documentation for JS files.
Mario Ferraro <fadinlight@gmail.com>
parents:
diff
changeset
|
59 |
/*t: |
c8f47f0b6697
JSDocToolkit 2.3.0 added to create documentation for JS files.
Mario Ferraro <fadinlight@gmail.com>
parents:
diff
changeset
|
60 |
plan(16, "Testing String.prototype.balance."); |
c8f47f0b6697
JSDocToolkit 2.3.0 added to create documentation for JS files.
Mario Ferraro <fadinlight@gmail.com>
parents:
diff
changeset
|
61 |
|
c8f47f0b6697
JSDocToolkit 2.3.0 added to create documentation for JS files.
Mario Ferraro <fadinlight@gmail.com>
parents:
diff
changeset
|
62 |
var s = "{abc}".balance("{","}"); |
c8f47f0b6697
JSDocToolkit 2.3.0 added to create documentation for JS files.
Mario Ferraro <fadinlight@gmail.com>
parents:
diff
changeset
|
63 |
is(s[0], 0, "opener in first is found."); |
c8f47f0b6697
JSDocToolkit 2.3.0 added to create documentation for JS files.
Mario Ferraro <fadinlight@gmail.com>
parents:
diff
changeset
|
64 |
is(s[1], 4, "closer in last is found."); |
c8f47f0b6697
JSDocToolkit 2.3.0 added to create documentation for JS files.
Mario Ferraro <fadinlight@gmail.com>
parents:
diff
changeset
|
65 |
|
c8f47f0b6697
JSDocToolkit 2.3.0 added to create documentation for JS files.
Mario Ferraro <fadinlight@gmail.com>
parents:
diff
changeset
|
66 |
s = "ab{c}de".balance("{","}"); |
c8f47f0b6697
JSDocToolkit 2.3.0 added to create documentation for JS files.
Mario Ferraro <fadinlight@gmail.com>
parents:
diff
changeset
|
67 |
is(s[0], 2, "opener in middle is found."); |
c8f47f0b6697
JSDocToolkit 2.3.0 added to create documentation for JS files.
Mario Ferraro <fadinlight@gmail.com>
parents:
diff
changeset
|
68 |
is(s[1], 4, "closer in middle is found."); |
c8f47f0b6697
JSDocToolkit 2.3.0 added to create documentation for JS files.
Mario Ferraro <fadinlight@gmail.com>
parents:
diff
changeset
|
69 |
|
c8f47f0b6697
JSDocToolkit 2.3.0 added to create documentation for JS files.
Mario Ferraro <fadinlight@gmail.com>
parents:
diff
changeset
|
70 |
s = "a{b{c}de}f".balance("{","}"); |
c8f47f0b6697
JSDocToolkit 2.3.0 added to create documentation for JS files.
Mario Ferraro <fadinlight@gmail.com>
parents:
diff
changeset
|
71 |
is(s[0], 1, "nested opener is found."); |
c8f47f0b6697
JSDocToolkit 2.3.0 added to create documentation for JS files.
Mario Ferraro <fadinlight@gmail.com>
parents:
diff
changeset
|
72 |
is(s[1], 8, "nested closer is found."); |
c8f47f0b6697
JSDocToolkit 2.3.0 added to create documentation for JS files.
Mario Ferraro <fadinlight@gmail.com>
parents:
diff
changeset
|
73 |
|
c8f47f0b6697
JSDocToolkit 2.3.0 added to create documentation for JS files.
Mario Ferraro <fadinlight@gmail.com>
parents:
diff
changeset
|
74 |
s = "{}".balance("{","}"); |
c8f47f0b6697
JSDocToolkit 2.3.0 added to create documentation for JS files.
Mario Ferraro <fadinlight@gmail.com>
parents:
diff
changeset
|
75 |
is(s[0], 0, "opener with no content is found."); |
c8f47f0b6697
JSDocToolkit 2.3.0 added to create documentation for JS files.
Mario Ferraro <fadinlight@gmail.com>
parents:
diff
changeset
|
76 |
is(s[1], 1, "closer with no content is found."); |
c8f47f0b6697
JSDocToolkit 2.3.0 added to create documentation for JS files.
Mario Ferraro <fadinlight@gmail.com>
parents:
diff
changeset
|
77 |
|
c8f47f0b6697
JSDocToolkit 2.3.0 added to create documentation for JS files.
Mario Ferraro <fadinlight@gmail.com>
parents:
diff
changeset
|
78 |
s = "".balance("{","}"); |
c8f47f0b6697
JSDocToolkit 2.3.0 added to create documentation for JS files.
Mario Ferraro <fadinlight@gmail.com>
parents:
diff
changeset
|
79 |
is(s[0], -1, "empty string opener is -1."); |
c8f47f0b6697
JSDocToolkit 2.3.0 added to create documentation for JS files.
Mario Ferraro <fadinlight@gmail.com>
parents:
diff
changeset
|
80 |
is(s[1], -1, "empty string closer is -1."); |
c8f47f0b6697
JSDocToolkit 2.3.0 added to create documentation for JS files.
Mario Ferraro <fadinlight@gmail.com>
parents:
diff
changeset
|
81 |
|
c8f47f0b6697
JSDocToolkit 2.3.0 added to create documentation for JS files.
Mario Ferraro <fadinlight@gmail.com>
parents:
diff
changeset
|
82 |
s = "{abc".balance("{","}"); |
c8f47f0b6697
JSDocToolkit 2.3.0 added to create documentation for JS files.
Mario Ferraro <fadinlight@gmail.com>
parents:
diff
changeset
|
83 |
is(s[0], -1, "opener with no closer returns -1."); |
c8f47f0b6697
JSDocToolkit 2.3.0 added to create documentation for JS files.
Mario Ferraro <fadinlight@gmail.com>
parents:
diff
changeset
|
84 |
is(s[1], -1, "no closer returns -1."); |
c8f47f0b6697
JSDocToolkit 2.3.0 added to create documentation for JS files.
Mario Ferraro <fadinlight@gmail.com>
parents:
diff
changeset
|
85 |
|
c8f47f0b6697
JSDocToolkit 2.3.0 added to create documentation for JS files.
Mario Ferraro <fadinlight@gmail.com>
parents:
diff
changeset
|
86 |
s = "abc".balance("{","}"); |
c8f47f0b6697
JSDocToolkit 2.3.0 added to create documentation for JS files.
Mario Ferraro <fadinlight@gmail.com>
parents:
diff
changeset
|
87 |
is(s[0], -1, "no opener or closer returns -1 for opener."); |
c8f47f0b6697
JSDocToolkit 2.3.0 added to create documentation for JS files.
Mario Ferraro <fadinlight@gmail.com>
parents:
diff
changeset
|
88 |
is(s[1], -1, "no opener or closer returns -1 for closer."); |
c8f47f0b6697
JSDocToolkit 2.3.0 added to create documentation for JS files.
Mario Ferraro <fadinlight@gmail.com>
parents:
diff
changeset
|
89 |
|
c8f47f0b6697
JSDocToolkit 2.3.0 added to create documentation for JS files.
Mario Ferraro <fadinlight@gmail.com>
parents:
diff
changeset
|
90 |
s = "a<bc}de".balance("<","}"); |
c8f47f0b6697
JSDocToolkit 2.3.0 added to create documentation for JS files.
Mario Ferraro <fadinlight@gmail.com>
parents:
diff
changeset
|
91 |
is(s[0], 1, "unmatching opener is found."); |
c8f47f0b6697
JSDocToolkit 2.3.0 added to create documentation for JS files.
Mario Ferraro <fadinlight@gmail.com>
parents:
diff
changeset
|
92 |
is(s[1], 4, "unmatching closer is found."); |
c8f47f0b6697
JSDocToolkit 2.3.0 added to create documentation for JS files.
Mario Ferraro <fadinlight@gmail.com>
parents:
diff
changeset
|
93 |
*/ |