-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherray.js
217 lines (198 loc) · 5.74 KB
/
erray.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
class Erray {
/** Initialization of an object. */
constructor(array = []) {
this.array = array;
}
/** Get length of specified array.*/
length() {
let length = 0;
for (let element of this.array) {
length++;
}
return length;
}
/**
* Appends new elements to an array, and returns the new length of the array.
* @param items New elements of the Array.
*/
push(...items) {
let length = this.array.length;
for (let [key, value] of Object.entries(items)) {
this.array[length] = value;
length++;
}
return this.array.length;
}
/**
* Remove a elements to an array, and returns the array.
* @param items elements of the Array.
*/
remove(items) {
let counter = 0;
let result = [];
for (let element of this.array) {
if (element != items) {
result[counter] = element;
counter++;
}
}
return this.array = result;
}
/**
* Returns the position of the first occurrence of an element.
* @param item Element of an array
*/
indexOf(item) {
for (let [key, element] of Object.entries(this.array)) {
if (item == element) {
return parseInt(key);
}
}
return null;
}
/**
* Returns the value of the first occurrence of an element.
* @param item Element of an array
*/
indexAt(index) {
return this.array[index];
}
/**
* Adds all the elements of an array separated by the specified separator string.
* @param string A string used to separate one element of an array from the next in the resulting String.
*/
join(string) {
let result = '';
for (let [key, value] of Object.entries(this.array)) {
if (key != 0) {
result += string + value;
} else {
result += value;
}
}
return result;
}
/**
* Combines two or more arrays.
* @param items Additional items to add to the end of arrayx.
*/
concat(...items) {
let lengthOfCaller = this.array.length;
for (let array of items) {
for (const [key, value] of Object.entries(array)) {
this.array.push(value);
}
}
return this.array;
}
/**
* Calls dynamic function.
* @param item Name of function.
*/
forEach(item) {
for (let element of this.array) {
item(element);
}
}
/** Removes the last element from an array and returns it. */
pop() {
let length = this.array.length;
return this.remove(this.array[--length]);
}
/**
* Returns the this object after filling the section identified by start and end with value
* @param value value to fill array section with.
* @param start index to start filling the array at.
* @param end index to stop filling the array at.
*/
fill(value, start = 0, end = this.array.length) {
for (let [key, element] of Object.entries(this.array)) {
if (key >= start && key < end) {
this.array[key] = value;
}
}
return this.array;
}
/**
* Checking in given array an element is exists.
* @param value value to fill array section with.
* @param start index to start filling the array at.
*/
includes(value, start = 0) {
for (start; start <= this.array.length; start++) {
if (value == this.array[start]) {
return true;
}
}
return false;
}
/** Returns the names of the enumerable properties and methods of an object. */
keys() {
let keys = [];
for (let [key, element] of Object.entries(this.array)) {
keys[key] = key;
}
return keys;
}
/** Returns the values of the specified array. */
valueOf() {
let values = [];
for (let [key, element] of Object.entries(this.array)) {
values[key] = element;
}
return values;
}
/** Returns a string representation of an object. */
toString() {
let result = '';
for (let [key, element] of Object.entries(this.array)) {
result += element;
if (key != this.array.length - 1) {
result += ',';
}
}
return result;
}
/** Reverses the elements in an Array. */
reverse() {
let length = this.array.length - 1;
let result = [];
for (let [key, element] of Object.entries(this.array)) {
result[length] = element;
length--;
}
return this.array = result;
}
/**
* Returns a array.
* @param start index to start filling the array at.
* @param end index to stop filling the array at.
*/
slice(start = 0, end = this.array.length) {
let result = [];
let counter = 0;
for (let [key, element] of Object.entries(this.array)) {
if (key >= start && key < end) {
result[counter] = element;
counter++;
}
}
return result;
}
/** Removes the first element from an array and returns it. */
shift() {
let shiftedValed = this.indexAt(0);
this.remove(this.array[0]);
return shiftedValed;
}
/**
* Inserts new elements at the start of an array.
* @param items Elements to insert at the start of the Array.
*/
unshift(...items) {
for (let element of items) {
this.remove(element);
}
return this.array.length;
}
}