This repository has been archived by the owner on May 8, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
List.js
122 lines (112 loc) · 3.4 KB
/
List.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
/**
* Enumerate the array with the pair [index, value].
*
* @param {array} array the array to enumerate
* @returns {generator} an array with the index of the element and the element
* itself for each element of the original array.
*/
const enumerate = function*(array) {
for (let i = 0; i < array.length; i++) {
yield [i, array[i]];
}
};
/**
* Removes the given indexes from the array.
*
* @param {array} array the array to remove elements from
* @param {int} start the first index to remove
* @param {int} end the last index to remove
* @returns {array} the array without the elements (note, the actual array
* reference is modified)
*/
const remove = function(array, start, end) {
const stop = end || start;
array.splice(start, (stop - start) + 1);
return array;
};
/**
* Returns a generator that iterates through the list backwards.
*
* @param {array} array the array to iterate on
* @returns {generator} a generator which outputs the elements of the array in
* a reversed order
*/
const reversed = function*(array) {
for (let i = array.length - 1; i >= 0; i--) {
yield array[i];
}
};
/**
* Sort the array according to the given evaluator function.
*
* @param {array} array the array to sort
* @param {function} evaluator the function to use to evaluate the value of
* each element of the array
* @returns {array} the sorted array
*/
const sortBy = function(array, evaluator) {
array.sort((a, b) => {
const aVal = evaluator(a);
const bVal = evaluator(b);
return aVal - bVal;
});
return array;
};
// Add the functions to the Array prototype.
/**
* Enumerate the array with the pair [index, value].
*
* @param {array} array the array to enumerate
* @returns {generator} an array with the index of the element and the element
* itself for each element of the original array.
*/
Array.prototype.enumerate = Array.prototype.enumerate || function*() {
yield* enumerate(this);
};
/**
* Removes the given indexes from the array.
*
* @param {int} start the first index to remove
* @param {int} stop the first index to remove
* @returns {array} the array without the elements (note, the actual array
* reference is modified)
*/
Array.prototype.remove = Array.prototype.remove || function(start, stop) {
return remove(this, start, stop);
};
/**
* Returns a generator that iterates through the list backwards.
*
* @returns {generator} a generator which outputs the elements of the array in
* a reversed order
*/
Array.prototype.reversed = Array.prototype.reversed || function*() {
yield* reversed(this);
};
/**
* Sort the array according to the given evaluator function.
*
* @param {function} evaluator the function to use to evaluate the value of
* each element of the array.
* @returns {*} the sorted array
*/
Array.prototype.sortBy = Array.prototype.sortBy || function(evaluator) {
return sortBy(this, evaluator);
};
/**
* Convert a generator or string to an array.
*
* @param {iterable} source an iterable object
* @returns {array} the array from the generator function
*/
const list = function(source) {
if (!source) {
return [];
}
return Array.from(source);
};
list.enumerate = enumerate;
list.remove = remove;
list.reversed = reversed;
list.sortBy = sortBy;
module.exports = list;