range-pie 1.0.1 → 2.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/py-range.js DELETED
@@ -1,376 +0,0 @@
1
- "use strict";
2
-
3
- class PyRange {
4
- /**
5
- * A class that simulate Python's range function, combined with several useful JavaScript array methods.
6
- * @param {...number} args - The arguments of the range. The possible forms are
7
- * - `PyRange(stop)`
8
- * - `PyRange(start, stop)`
9
- * - `PyRange(start, stop, step)`
10
- * @throws {TypeError} If any of the arguments is not a number.
11
- * @throws {TypeError} If any of the arguments is not an integer.
12
- * @throws {Error} If the step is zero.
13
- * @throws {Error} If the arguments count is not between 1 and 3.
14
- * @property {number} start - The start of the range, inclusive.
15
- * @property {number} stop - The stop of the range, exclusive.
16
- * @property {number} step - The step of the range.
17
- * @property {number} length - The length of the range.
18
- */
19
- constructor(...args) {
20
- if (!args.every((arg) => typeof arg === "number")) {
21
- throw new TypeError("All arguments must be numbers");
22
- }
23
-
24
- if (!args.every(Number.isInteger)) {
25
- throw new TypeError("All arguments must be integers");
26
- }
27
-
28
- const [start, stop, step] =
29
- args.length === 1
30
- ? [0, args[0], 1]
31
- : args.length === 2
32
- ? [args[0], args[1], 1]
33
- : args.length === 3
34
- ? [args[0], args[1], args[2]]
35
- : (() => {
36
- throw new Error("Invalid arguments count: must between 1 and 3");
37
- })();
38
-
39
- if (step === 0) {
40
- throw new Error("Step cannot be zero");
41
- }
42
-
43
- this._start = start;
44
- this._stop = stop;
45
- this._step = stop < start && args.length !== 3 ? -step : step;
46
-
47
- const diff = this._step > 0 ? stop - start : start - stop;
48
- this._length = Math.max(Math.ceil(diff / Math.abs(this._step)), 0);
49
- }
50
-
51
- /**
52
- * Gets the length of this range.
53
- *
54
- * @returns {number} the length of this range
55
- * @readonly
56
- */
57
- get length() {
58
- return this._length;
59
- }
60
-
61
- /**
62
- * Gets the starting value of the range.
63
- *
64
- * @returns {number} The starting value of the range.
65
- * @readonly
66
- */
67
- get start() {
68
- return this._start;
69
- }
70
-
71
- /**
72
- * Gets the ending value of the range.
73
- *
74
- * @returns {number} The ending value of the range.
75
- * @readonly
76
- */
77
- get stop() {
78
- return this._stop;
79
- }
80
-
81
- /**
82
- * Gets the step value of the range.
83
- *
84
- * @returns {number} The step value of the range.
85
- * @readonly
86
- */
87
- get step() {
88
- return this._step;
89
- }
90
-
91
- /**
92
- * Gets the value at the specified index in this range.
93
- * @param {number} index - The index of the value to retrieve
94
- * @returns {number} The value at the specified index
95
- * @throws {RangeError} If the index is out of range
96
- */
97
- at(index) {
98
- if (index < 0 || index >= this._length) {
99
- throw new RangeError("Index out of range");
100
- }
101
- return this._start + index * this._step;
102
- }
103
-
104
- /**
105
- * Converts the range to a string.
106
- * @returns {string} A string of the form `Range(start, stop, step)`.
107
- */
108
- toString() {
109
- return `PyRange(${this._start}, ${this._stop}, ${this._step})`;
110
- }
111
-
112
- /**
113
- * Converts the range to an array.
114
- * @returns {number[]} An array of numbers with the same elements as this range.
115
- */
116
- toArray() {
117
- return [...this];
118
- }
119
-
120
- static #validateCb(cb) {
121
- if (typeof cb !== "function") {
122
- throw new TypeError("Callback must be a function");
123
- }
124
- }
125
-
126
- /**
127
- * Creates a new array with the results of applying the given callback
128
- * function to every element in this range.
129
- * @param {function(number, number, PyRange): *} callback - The callback
130
- * function to apply to every element.
131
- * @returns {Array.<*>} A new array of the same length as this range.
132
- */
133
- map(callback) {
134
- PyRange.#validateCb(callback);
135
-
136
- const result = new Array(this._length);
137
-
138
- for (let i = 0; i < this._length; i++) {
139
- result[i] = callback(this.at(i), i, this);
140
- }
141
-
142
- return result;
143
- }
144
-
145
- /**
146
- * Creates a new array with all elements that pass the test implemented by
147
- * the provided function.
148
- * @param {function(number, number, PyRange): boolean} callback - The
149
- * predicate function to apply to every element
150
- * @returns {number[]} A new array of elements that pass the test
151
- */
152
- filter(callback) {
153
- PyRange.#validateCb(callback);
154
-
155
- const result = [];
156
-
157
- for (let i = 0; i < this._length; i++) {
158
- if (callback(this.at(i), i, this)) {
159
- result.push(this.at(i));
160
- }
161
- }
162
-
163
- return result;
164
- }
165
-
166
- /**
167
- * Reduces the range to a single value.
168
- * @param {function(*, number, number, PyRange): *} callback - The callback
169
- * function to apply to every element. The callback should take four
170
- * arguments: the accumulator, the current value, the index of the current
171
- * value, and the range object.
172
- * @param {*} [initialValue] - The initial value of the accumulator.
173
- * @returns {*} The final value of the accumulator.
174
- */
175
- reduce(callback, initialValue) {
176
- PyRange.#validateCb(callback);
177
-
178
- let accumulator = initialValue;
179
-
180
- for (let i = 0; i < this._length; i++) {
181
- accumulator = callback(accumulator, this.at(i), i, this);
182
- }
183
-
184
- return accumulator;
185
- }
186
-
187
- /**
188
- * Determines whether at least one element of the range satisfies the
189
- * provided test.
190
- * @param {function(number, number, PyRange): boolean} callback - The
191
- * predicate function to apply to every element
192
- * @returns {boolean} True if at least one element of the range passes the
193
- * test, false otherwise.
194
- */
195
- some(callback) {
196
- PyRange.#validateCb(callback);
197
-
198
- for (let i = 0; i < this._length; i++) {
199
- if (callback(this.at(i), i, this)) {
200
- return true;
201
- }
202
- }
203
- return false;
204
- }
205
-
206
- /**
207
- * Determines whether all elements of the range satisfy the provided test.
208
- * @param {function(number, number, PyRange): boolean} callback - The
209
- * predicate function to apply to every element
210
- * @returns {boolean} True if all elements of the range pass the test,
211
- * false otherwise.
212
- */
213
- every(callback) {
214
- PyRange.#validateCb(callback);
215
-
216
- for (let i = 0; i < this._length; i++) {
217
- if (!callback(this.at(i), i, this)) {
218
- return false;
219
- }
220
- }
221
- return true;
222
- }
223
-
224
- /**
225
- * Finds the first element in this range that satisfies the provided test.
226
- * @param {function(number, number, PyRange): boolean} callback - The
227
- * predicate function to apply to every element
228
- * @returns {number|undefined} The first element that passes the test,
229
- * or undefined if no element passes the test.
230
- */
231
- find(callback) {
232
- PyRange.#validateCb(callback);
233
-
234
- for (let i = 0; i < this._length; i++) {
235
- if (callback(this.at(i), i, this)) {
236
- return this.at(i);
237
- }
238
- }
239
- return undefined;
240
- }
241
-
242
- /**
243
- * Finds the index of the first element in this range that satisfies the provided test.
244
- * @param {function(number, number, PyRange): boolean} callback - The predicate function to apply to each element.
245
- * @returns {number} The index of the first element that passes the test, or -1 if no element passes the test.
246
- */
247
- findIndex(callback) {
248
- PyRange.#validateCb(callback);
249
-
250
- for (let i = 0; i < this._length; i++) {
251
- if (callback(this.at(i), i, this)) {
252
- return i;
253
- }
254
- }
255
- return -1;
256
- }
257
-
258
- /**
259
- * Finds the index of the last element in this range that satisfies the provided test.
260
- * @param {function(number, number, PyRange): boolean} callback - The predicate function to apply to each element.
261
- * @returns {number} The index of the last element that passes the test, or -1 if no element passes the test.
262
- */
263
- findLastIndex(callback) {
264
- PyRange.#validateCb(callback);
265
-
266
- for (let i = this._length - 1; i >= 0; i--) {
267
- if (callback(this.at(i), i, this)) {
268
- return i;
269
- }
270
- }
271
- return -1;
272
- }
273
-
274
- /**
275
- * Executes a provided function once for each element in this range.
276
- * @param {function(number, number, PyRange): void} callback - The
277
- * function to execute for each element.
278
- */
279
- forEach(callback) {
280
- PyRange.#validateCb(callback);
281
-
282
- for (let i = 0; i < this._length; i++) {
283
- callback(this.at(i), i, this);
284
- }
285
- }
286
-
287
- /**
288
- * Determines whether the given value is present in this range.
289
- * @param {*} value - The value to search for.
290
- * @returns {boolean} True if the value is present, false otherwise.
291
- */
292
- includes(value) {
293
- return this.some((item) => item === value);
294
- }
295
-
296
- /**
297
- * Returns the index of the first occurrence of the specified value, or -1 if it is not present.
298
- * @param {*} value - The value to search for.
299
- * @returns {number} The index of the value, or -1 if it is not present.
300
- */
301
- indexOf(value) {
302
- for (let i = 0; i < this._length; i++) {
303
- if (this.at(i) === value) {
304
- return i;
305
- }
306
- }
307
- return -1;
308
- }
309
-
310
- /**
311
- * Returns the index of the last occurrence of the specified value, or -1 if it is not present.
312
- * @param {*} value - The value to search for.
313
- * @returns {number} The index of the last occurrence of the value, or -1 if it is not present.
314
- */
315
- lastIndexOf(value) {
316
- for (let i = this._length - 1; i >= 0; i--) {
317
- if (this.at(i) === value) {
318
- return i;
319
- }
320
- }
321
- return -1;
322
- }
323
-
324
- /**
325
- * Reverses the order of the elements in this range, returning a new PyRange object.
326
- * @returns {PyRange} A new PyRange object with the elements in reverse order.
327
- */
328
- reverse() {
329
- const result = new PyRange(this._stop, this._start, -this._step);
330
- result._length = this._length;
331
- return result;
332
- }
333
-
334
- [Symbol.iterator]() {
335
- let index = 0;
336
- let current = this._start;
337
- const { _stop: _, _step: step, _length: length } = this;
338
-
339
- return {
340
- next: () => {
341
- if (index >= length) {
342
- return { done: true };
343
- } else {
344
- const value = current;
345
- current += step;
346
- index++;
347
- return { value, done: false };
348
- }
349
- },
350
- };
351
- }
352
-
353
- /**
354
- * Returns a Proxy for this range, allowing indexed access.
355
- * This proxy enables accessing range elements via array-like indexing.
356
- * If the property is a number, it will return the element at that index.
357
- *
358
- * @returns {Proxy} A proxy for the PyRange instance.
359
- */
360
- asProxy() {
361
- return new Proxy(this, {
362
- get(target, prop) {
363
- if (typeof prop === "symbol") {
364
- return target[prop];
365
- }
366
- if (!isNaN(prop)) {
367
- return target.at(parseInt(prop));
368
- }
369
- return target[prop];
370
- },
371
- });
372
- }
373
- }
374
-
375
- module.exports = PyRange;
376
- module.exports.PyRange = PyRange;