-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack.js
142 lines (130 loc) · 2.41 KB
/
stack.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
var starr=exports;
var sys=require('sys');
var stack=starr.stack=function(){
this.array=[];
};
stack.prototype.each=function(fnc){
for (var i = 0; i < this.array.length; i++) {
var r = fnc(this.array[i], i);
if (r === false) {
return;
}
}
};
stack.prototype.add=function(obj){
this.prune();
var id=this.array.length;
if(obj.name)
{
}
else
{
obj.name=this.array.length;
}
if(this.exists(obj.name))
{
this.array[this.getByName(obj.name).id]=obj;
return this.exists(obj.name);
}
else
{
this.array[id]=obj;
return id;
}
return false;
};
stack.prototype.remove=function(name){
console.log(this.exists(name))
if(this.exists(name))
{
this.array[this.getByName(name).id]=null;
}
this.prune();
};
stack.prototype.size=function(){
return this.array.length;
};
stack.prototype.exists=function(name){
var result=false;
this.each(function(obj, id){
if(obj && obj.name==name)
result=true;
return result;
});
return result;
};
stack.prototype.get=function(id){
return this.array[id];
};
stack.prototype.getByName=function(name){
if(this.exists(name))
{
var result=false;
this.each(function(obj, id){
if(obj && obj.name==name)
result=obj;
result.id=id;
return result;
});
return result;
};
};
stack.prototype.addProperty=function(id,name,value)
{
try{
var item=this.get(id);
item[name]=value;
}catch(e)
{
console.log(e);
}
};
stack.prototype.getProperty=function(id,name)
{
try{
var item=this.get(id);
return item[name];
}catch(e){
console.log(e);
}
};
stack.prototype.getPropertyByName=function(name,item)
{
var id=this.getByName(name).id;
return this.getProperty(id,item);
};
stack.prototype.addPropertyByName=function(name,item,value)
{
var id=this.getByName(name).id;
this.addProperty(id,item,value);
};
stack.prototype.prune=function(){
var temparr=[];
this.each(function(arr){
if(arr && arr.name)
{
temparr[temparr.length]=arr;
}
});
this.array=temparr;
return true;
};
//Todo: Finish These Functions. They are just Skeletal right now.
stack.prototype.getByPropertyName=function(name){
};
stack.prototype.toJSON=function(){
return JSON.parse(JSON.stringify(this.array));
};
stack.prototype.toString=function(){
};
stack.prototype.toArray=function(){
};
stack.prototype.join=function(delimiter)
{
};
stack.prototype.first=function(){
};
stack.prototype.last=function(){
};
stack.prototype.without=function(values){
};