-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
123 lines (108 loc) · 3.47 KB
/
index.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
/* jslint node: true */
"use strict";
global.modulesCache = global.modulesCache || {};
if(global.modulesCache['objectarray']){
module.exports = global.modulesCache['objectarray'];
return;
}
module.exports = global.modulesCache['objectarray'] = ObjectArray;
function ObjectArray(){
var args = Array.prototype.slice.call(arguments);
this.splice.apply(this,[0,0].concat(args));
this.pid = 'id';
}
ObjectArray.prototype = [];
ObjectArray.prototype.constructor = Array;
ObjectArray.prototype.insert = function(element,index,pid,after){
index = typeof index === 'string' ? this.search(index,pid) : index;
if(index<0){index = this.length + index;}
index += after ? 1 : 0;
if(Math.floor(index) !== parseFloat(index,10) || index<0 || index>this.length){
this.push(element);
} else {
this.splice(index,0,element);
}
return this;
};
ObjectArray.prototype.insertAfter = function(element,index,pid){
return this.insert(element,index,pid,true);
};
ObjectArray.prototype.add = ObjectArray.prototype.insert;
ObjectArray.prototype.addAfter = ObjectArray.prototype.insertAfter;
ObjectArray.prototype.get = function(index,pid){
index = typeof index === 'string' ? this.search(index,pid) : index;
if(index<0){index = this.length + index;}
if(Math.floor(index) !== parseFloat(index,10) || index<0 || index>this.length){
return;
} else {
return this[index];
}
};
ObjectArray.prototype.getAll = function(index,pid){
pid = pid || this.pid || 'id';
var results = [];
for(var i=0,l=this.length;i<l;i++){
if(this[i][pid] === index){
results.push(this[i]);
}
}
return results;
};
ObjectArray.prototype.search = function(index,pid){
pid = pid || this.pid || 'id';
for(var i=0,l=this.length;i<l;i++){
if(this[i][pid] === index){
return i;
}
}
};
ObjectArray.prototype.searchAll = function(index,pid){
pid = pid || this.pid || 'id';
var results = [];
for(var i=0,l=this.length;i<l;i++){
if(this[i][pid] === index){
results.push(i);
}
}
return results;
};
ObjectArray.prototype.move = function(from,index,pid,after){
from = typeof from === 'string' ? this.search(from,pid) : from;
if(from<0){from = this.length + from;}
if(Math.floor(from) !== parseFloat(from,10) || from<0 || from>=this.length){
return false;
}
index = typeof index === 'string' ? this.search(index,pid) : index;
if(index<0){index = this.length + index;}
index += after ? 1 : 0;
if(Math.floor(index) !== parseFloat(index,10) || index<0 || index>this.length){
return false;
}
if(from == index || from==index-1){
return true;
}
var copy = this[from];
this.splice(index, 0, copy);
this.splice(from > index ? from+1 : from, 1);
return true;
};
ObjectArray.prototype.moveAfter = function(index,indexTo,pid){
return this.move(index,indexTo,pid,true);
};
ObjectArray.prototype.remove = function(index,pid){
index = typeof index === 'string' ? this.search(index,pid) : index;
if(index<0){index = this.length + index;}
if(Math.floor(index) !== parseFloat(index,10) || index<0 || index>=this.length){
return false;
}
this.splice(index, 1);
return true;
};
ObjectArray.prototype.keys = function(pid){
pid = pid || this.pid || 'id';
var keys = [];
for(var i=0,l=this.length;i<l;i++){
keys.push(this[i][pid]);
}
return keys;
};