Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/tools/jsdoc-toolkit/app/frame/String.js
blob: c183c27d561a10410ada398e67feacb9d17a68b3 (plain)
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
/**
	@name String
	@class Additions to the core string object.
*/

/** @author Steven Levithan, released as public domain. */
String.prototype.trim = function() {
	var str = this.replace(/^\s+/, '');
	for (var i = str.length - 1; i >= 0; i--) {
		if (/\S/.test(str.charAt(i))) {
			str = str.substring(0, i + 1);
			break;
		}
	}
	return str;
}
/*t:
	plan(6, "Testing String.prototype.trim.");
	
	var s = "   a bc   ".trim();
	is(s, "a bc", "multiple spaces front and back are trimmed.");

	s = "a bc\n\n".trim();
	is(s, "a bc", "newlines only in back are trimmed.");
	
	s = "\ta bc".trim();
	is(s, "a bc", "tabs only in front are trimmed.");
	
	s = "\n \t".trim();
	is(s, "", "an all-space string is trimmed to empty.");
	
	s = "a b\nc".trim();
	is(s, "a b\nc", "a string with no spaces in front or back is trimmed to itself.");
	
	s = "".trim();
	is(s, "", "an empty string is trimmed to empty.");

*/

String.prototype.balance = function(open, close) {
	var i = 0;
	while (this.charAt(i) != open) {
		if (i == this.length) return [-1, -1];
		i++;
	}
	
	var j = i+1;
	var balance = 1;
	while (j < this.length) {
		if (this.charAt(j) == open) balance++;
		if (this.charAt(j) == close) balance--;
		if (balance == 0) break;
		j++;
		if (j == this.length) return [-1, -1];
	}
	
	return [i, j];
}
/*t:
	plan(16, "Testing String.prototype.balance.");
	
	var s = "{abc}".balance("{","}");
	is(s[0], 0, "opener in first is found.");
	is(s[1], 4, "closer in last is found.");
	
	s = "ab{c}de".balance("{","}");
	is(s[0], 2, "opener in middle is found.");
	is(s[1], 4, "closer in middle is found.");
	
	s = "a{b{c}de}f".balance("{","}");
	is(s[0], 1, "nested opener is found.");
	is(s[1], 8, "nested closer is found.");
	
	s = "{}".balance("{","}");
	is(s[0], 0, "opener with no content is found.");
	is(s[1], 1, "closer with no content is found.");
	
	s = "".balance("{","}");
	is(s[0], -1, "empty string opener is -1.");
	is(s[1], -1, "empty string closer is -1.");
	
	s = "{abc".balance("{","}");
	is(s[0], -1, "opener with no closer returns -1.");
	is(s[1], -1, "no closer returns -1.");
	
	s = "abc".balance("{","}");
	is(s[0], -1, "no opener or closer returns -1 for opener.");
	is(s[1], -1, "no opener or closer returns -1 for closer.");
	
	s = "a<bc}de".balance("<","}");
	is(s[0], 1, "unmatching opener is found.");
	is(s[1], 4, "unmatching closer is found.");
*/