Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/tools/jsdoc-toolkit/app/frame/Chain.js
blob: 506469d18595273a4897a0e821078b10cb4201ea (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
94
95
96
97
98
99
100
101
102
/**@constructor*/
function ChainNode(object, link) {
	this.value = object;
	this.link = link; // describes this node's relationship to the previous node
}

/**@constructor*/
function Chain(valueLinks) {
	this.nodes = [];
	this.cursor = -1;
	
	if (valueLinks && valueLinks.length > 0) {
		this.push(valueLinks[0], "//");
		for (var i = 1, l = valueLinks.length; i < l; i+=2) {
			this.push(valueLinks[i+1], valueLinks[i]);
		}
	}
}

Chain.prototype.push = function(o, link) {
	if (this.nodes.length > 0 && link) this.nodes.push(new ChainNode(o, link));
	else this.nodes.push(new ChainNode(o));
}

Chain.prototype.unshift = function(o, link) {
	if (this.nodes.length > 0 && link) this.nodes[0].link = link;
	this.nodes.unshift(new ChainNode(o));
	this.cursor++;
}

Chain.prototype.get = function() {
	if (this.cursor < 0 || this.cursor > this.nodes.length-1) return null;
	return this.nodes[this.cursor];
}

Chain.prototype.first = function() {
	this.cursor = 0;
	return this.get();
}

Chain.prototype.last = function() {
	this.cursor = this.nodes.length-1;
	return this.get();
}

Chain.prototype.next = function() {
	this.cursor++;
	return this.get();
}

Chain.prototype.prev = function() {
	this.cursor--;
	return this.get();
}

Chain.prototype.toString = function() {
	var string = "";
	for (var i = 0, l = this.nodes.length; i < l; i++) {
		if (this.nodes[i].link) string += " -("+this.nodes[i].link+")-> ";
		string += this.nodes[i].value.toString();
	}
	return string;
}

Chain.prototype.joinLeft = function() {
	var result = "";
	for (var i = 0, l = this.cursor; i < l; i++) {
		if (result && this.nodes[i].link) result += this.nodes[i].link;
		result += this.nodes[i].value.toString();
	}
	return result;
}


/* USAGE:

var path = "one/two/three.four/five-six";
var pathChain = new Chain(path.split(/([\/.-])/));
print(pathChain);

var lineage = new Chain();
lineage.push("Port");
lineage.push("Les", "son");
lineage.push("Dawn", "daughter");
lineage.unshift("Purdie", "son");

print(lineage);

// walk left
for (var node = lineage.last(); node !== null; node = lineage.prev()) {
	print("< "+node.value);
}

// walk right
var node = lineage.first()
while (node !== null) {
	print(node.value);
	node = lineage.next();
	if (node && node.link) print("had a "+node.link+" named");
}

*/