-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tuple.js
185 lines (147 loc) · 5.21 KB
/
Tuple.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
if (! (globalThis.Tuple instanceof Function)) {
/**
* Creates an unfrozen Tuple, such as for `Tuple.of()`
*/
const createTuple = items => {
const tuple = Array.apply(Object.create(Tuple.prototype), items);
Object.setPrototypeOf(tuple, Tuple.prototype);
return tuple;
};
function Tuple(...items) {
if (new.target === Tuple) {
throw new TypeError('Tuple is not a constructor');
}
const tuple = createTuple(items);
Object.freeze(tuple);
return tuple;
}
Tuple.prototype.forEach = function forEach(callbackFn, thisArg) {
Array.prototype.forEach.call(this, callbackFn, thisArg);
};
Tuple.prototype.join = function join(separator = ',') {
return Array.prototype.join.call(this, separator);
};
Tuple.prototype.concat = function concat(...values) {
return Tuple(...this, ...values);
};
Tuple.prototype.slice = function slice(start, end) {
return Tuple.from(Array.prototype.slice.call(this, start, end));
};
Tuple.prototype.indexOf = function indexOf(searchElement, fromIndex) {
return Array.prototype.indexOf.call(this, searchElement, fromIndex);
};
Tuple.prototype.lastIndexOf = function lastIndexOf(searchElement, fromIndex) {
return Array.prototype.lastIndexOf.call(this, searchElement, fromIndex);
};
Tuple.prototype.findIndex = function findIndex(callbackFn, thisArg) {
return Array.prototype.findIndex.call(this, callbackFn, thisArg);
};
Tuple.prototype.find = function find(callbackFn, thisArg) {
return Array.prototype.find.call(this, callbackFn, thisArg);
};
Tuple.prototype.findLast = function findLast(callbackFn, thisArg) {
return Array.prototype.findLast.call(this, callbackFn, thisArg);
};
Tuple.prototype.findLastIndex = function findLastIndex(callbackFn, thisArg) {
return Array.prototype.findLastIndex.call(this, callbackFn, thisArg);
};
Tuple.prototype.every = function every(callbackFn, thisArg) {
return Array.prototype.every.call(this, callbackFn, thisArg);
};
Tuple.prototype.some = function some(callbackFn, thisArg) {
return Array.prototype.some.call(this, callbackFn, thisArg);
};
Tuple.prototype.includes = function includes(searchElement, fromIndex) {
return Array.prototype.includes.call(this, searchElement, fromIndex);
};
Tuple.prototype.map = function map(callbackFn, thisArg) {
return Tuple.from(Array.prototype.map.call(this, callbackFn, thisArg));
};
Tuple.prototype.filter = function filter(callbackFn, thisArg) {
return Tuple.from(Array.prototype.filter.call(this, callbackFn, thisArg));
};
Tuple.prototype.flat = function flat(depth) {
return Tuple.from(Array.prototype.flat.call(this, depth));
};
Tuple.prototype.flatMap = function flatMap(callbackFn, thisArg) {
return Tuple.from(Array.prototype.flatMap.call(this, callbackFn, thisArg));
};
Tuple.prototype.at = function at(index) {
return Array.prototype.at.call(this, index);
};
Tuple.prototype.reduce = function reduce(callbackFn, initialValue) {
return Tuple.from(Array.prototype.reduce.call(this, callbackFn, initialValue));
};
Tuple.prototype.reduceRight = function reduceRight(callbackFn, initialValue) {
return Tuple.from(Array.prototype.reduceRight.call(this, callbackFn, initialValue));
};
Tuple.prototype.toSorted = function toSorted(toStringTag) {
return Tuple.from(Array.prototype.toSorted.call(this, toStringTag));
};
Tuple.prototype.toSpliced = function toSpliced(start, deleteCount, ...items) {
return Tuple.from(Array.prototype.toSpliced.call(this, start, deleteCount, ...items));
};
Tuple.prototype.toReversed = function toReversed() {
return Tuple.from(Array.prototype.toReversed.apply(this));
};
Tuple.prototype.with = function(index, value) {
return Tuple.from(Array.prototype.with.call(this, index, value));
};
Tuple.prototype.toLocaleString = function toLocaleString(locales, options) {
return Array.prototype.toLocaleString.call(this, locales, options);
};
Tuple.prototype.toJSON = function toJSON() {
return [...this];
};
Tuple.prototype.keys = function keys() {
return Array.prototype.keys.apply(this);
};
Tuple.prototype.values = function values(){
return Array.prototype.values.apply(this);
};
Tuple.prototype.entries = function entries(){
return Array.prototype.entries.apply(this);
};
Tuple.prototype[Symbol.iterator] = function() {
return Array.prototype[Symbol.iterator].apply(this);
};
Tuple.from = function from(items, mapFn, thisArg) {
const tuple = createTuple([]);
Array.prototype.push.apply(tuple, Array.from(items, mapFn, thisArg));
Object.freeze(tuple);
return tuple;
};
Tuple.of = function(...items) {
return Tuple.from(items);
};
Object.defineProperties(Tuple.prototype, {
'length': {
get: function() {
return Object.getOwnPropertyDescriptor(Array.prototype, 'length').get.apply(this);
},
enumerable: true,
configurable: false,
},
'lastItem': {
get: function() {
return this[this.lastIndex];
},
enumerable: true,
configurable: false,
},
'lastIndex': {
get: function() {
return Object.getOwnPropertyDescriptor(Array.prototype, 'lastIndex').get.apply(this);
},
enumerable: true,
configurable: false,
},
[Symbol.toStringTag]: {
value: 'Tuple',
enumerable: true,
configurable: false,
writable: false,
},
});
globalThis.Tuple = Tuple;
}