range-pie 2.3.0 → 2.4.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/README.md +106 -67
- package/dist/py-range.d.ts +32 -5
- package/dist/py-range.js +39 -4
- package/examples/array-methods.js +12 -0
- package/package.json +22 -14
package/README.md
CHANGED
|
@@ -2,16 +2,17 @@
|
|
|
2
2
|
|
|
3
3
|
A TypeScript/JavaScript library that brings Python's range functionality to JavaScript, enhanced with familiar array methods. This lightweight utility provides a seamless way to work with numeric sequences while maintaining JavaScript's functional programming paradigm. Fully typed for TypeScript users while remaining compatible with JavaScript projects.
|
|
4
4
|
|
|
5
|
+
[](https://www.npmjs.com/package/range-pie)
|
|
6
|
+
[](https://opensource.org/licenses/MIT)
|
|
7
|
+
|
|
5
8
|
## Table of Contents
|
|
6
9
|
|
|
7
10
|
- [Installation](#installation)
|
|
8
11
|
- [Basic Usage](#basic-usage)
|
|
9
|
-
|
|
10
12
|
- [JavaScript](#javascript)
|
|
11
13
|
- [TypeScript](#typescript)
|
|
12
14
|
|
|
13
15
|
- [API Reference](#api-reference)
|
|
14
|
-
|
|
15
16
|
- [Constructor Options](#constructor-options)
|
|
16
17
|
- [Properties](#properties)
|
|
17
18
|
- [at()](#at)
|
|
@@ -32,14 +33,15 @@ A TypeScript/JavaScript library that brings Python's range functionality to Java
|
|
|
32
33
|
- [pop()](#pop)
|
|
33
34
|
- [slice()](#slice)
|
|
34
35
|
- [reverse()](#reverse)
|
|
36
|
+
- [entries()](#entries)
|
|
37
|
+
- [keys()](#keys)
|
|
38
|
+
- [values()](#values)
|
|
35
39
|
|
|
36
40
|
- [Advanced Usage](#advanced-usage)
|
|
37
|
-
|
|
38
41
|
- [Iteration](#iteration)
|
|
39
42
|
- [Proxy Access](#proxy-access)
|
|
40
43
|
|
|
41
44
|
- [Examples](#examples)
|
|
42
|
-
|
|
43
45
|
- [Methods chaining](#methods-chaining)
|
|
44
46
|
- [Using as Array-like Object](#using-as-array-like-object)
|
|
45
47
|
- [Example Files](#example-files)
|
|
@@ -59,9 +61,9 @@ npm install range-pie
|
|
|
59
61
|
|
|
60
62
|
```javascript
|
|
61
63
|
// CommonJS - Both import styles are supported
|
|
62
|
-
const { PyRange } = require(
|
|
64
|
+
const { PyRange } = require("range-pie"); // Named import style
|
|
63
65
|
// OR
|
|
64
|
-
const PyRange = require(
|
|
66
|
+
const PyRange = require("range-pie"); // Default import style
|
|
65
67
|
|
|
66
68
|
// Create a range from 0 to 5
|
|
67
69
|
const range = new PyRange(5);
|
|
@@ -74,26 +76,30 @@ console.log([...range2]); // [2, 3, 4, 5, 6, 7]
|
|
|
74
76
|
// Create a range with step
|
|
75
77
|
const range3 = new PyRange(0, 10, 2);
|
|
76
78
|
console.log([...range3]); // [0, 2, 4, 6, 8]
|
|
79
|
+
|
|
80
|
+
// Create a reverse range
|
|
81
|
+
const range4 = new PyRange(5, 2);
|
|
82
|
+
console.log([...range4]); // [5, 4, 3]
|
|
77
83
|
```
|
|
78
84
|
|
|
79
85
|
### TypeScript
|
|
80
86
|
|
|
81
87
|
```typescript
|
|
82
88
|
// ES Modules - Both import styles are supported
|
|
83
|
-
import { PyRange } from
|
|
89
|
+
import { PyRange } from "range-pie"; // Named import style
|
|
84
90
|
// OR
|
|
85
|
-
import PyRange from
|
|
91
|
+
import PyRange from "range-pie"; // Default import style
|
|
86
92
|
|
|
87
93
|
// Create a range from 0 to 5
|
|
88
94
|
const range = new PyRange(5);
|
|
89
95
|
console.log([...range]); // [0, 1, 2, 3, 4]
|
|
90
96
|
|
|
91
97
|
// Type-safe operations
|
|
92
|
-
const doubledValues: number[] = range.map(x => x * 2);
|
|
98
|
+
const doubledValues: number[] = range.map((x) => x * 2);
|
|
93
99
|
console.log(doubledValues); // [0, 2, 4, 6, 8]
|
|
94
100
|
|
|
95
101
|
// Type inference works with generics
|
|
96
|
-
const stringValues: string[] = range.map(x => `Value: ${x}`);
|
|
102
|
+
const stringValues: string[] = range.map((x) => `Value: ${x}`);
|
|
97
103
|
console.log(stringValues); // ['Value: 0', 'Value: 1', 'Value: 2', 'Value: 3', 'Value: 4']
|
|
98
104
|
```
|
|
99
105
|
|
|
@@ -102,9 +108,9 @@ console.log(stringValues); // ['Value: 0', 'Value: 1', 'Value: 2', 'Value: 3', '
|
|
|
102
108
|
### Constructor Options
|
|
103
109
|
|
|
104
110
|
```javascript
|
|
105
|
-
new PyRange(stop)
|
|
106
|
-
new PyRange(start, stop)
|
|
107
|
-
new PyRange(start, stop, step)
|
|
111
|
+
new PyRange(stop); // 0 to stop-1
|
|
112
|
+
new PyRange(start, stop); // start to stop-1
|
|
113
|
+
new PyRange(start, stop, step); // start to stop-1 with step
|
|
108
114
|
```
|
|
109
115
|
|
|
110
116
|
- **PyRange(stop:number)**
|
|
@@ -141,9 +147,9 @@ console.log(PyRange(2, -10, -1));
|
|
|
141
147
|
|
|
142
148
|
```javascript
|
|
143
149
|
const range = new PyRange(1, 10, 2);
|
|
144
|
-
console.log(range.start);
|
|
145
|
-
console.log(range.stop);
|
|
146
|
-
console.log(range.step);
|
|
150
|
+
console.log(range.start); // 1
|
|
151
|
+
console.log(range.stop); // 10
|
|
152
|
+
console.log(range.step); // 2
|
|
147
153
|
console.log(range.length); // 5
|
|
148
154
|
```
|
|
149
155
|
|
|
@@ -152,9 +158,9 @@ console.log(range.length); // 5
|
|
|
152
158
|
The 'at' method accepts a number as argument to gets the value at the specified index in a range. Generate a RangeError if the index is out of range.
|
|
153
159
|
|
|
154
160
|
```javascript
|
|
155
|
-
const range = new PyRange(1, 5);
|
|
156
|
-
console.log(range.at(0));
|
|
157
|
-
console.log(range.at(2));
|
|
161
|
+
const range = new PyRange(1, 5); // [1, 2, 3, 4]
|
|
162
|
+
console.log(range.at(0)); // 1
|
|
163
|
+
console.log(range.at(2)); // 3
|
|
158
164
|
console.log(range.at(-1)); // RangeError
|
|
159
165
|
```
|
|
160
166
|
|
|
@@ -181,8 +187,8 @@ console.log(range.toArray()); // [1, 2, 3]
|
|
|
181
187
|
It works the same as [**`Array.prototype.map`**](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map)
|
|
182
188
|
|
|
183
189
|
```javascript
|
|
184
|
-
const range = new PyRange(1, 4);
|
|
185
|
-
console.log(range.map(x => x * 2));
|
|
190
|
+
const range = new PyRange(1, 4); // [1, 2, 3]
|
|
191
|
+
console.log(range.map((x) => x * 2)); // [2, 4, 6]
|
|
186
192
|
```
|
|
187
193
|
|
|
188
194
|
### filter()
|
|
@@ -190,8 +196,8 @@ console.log(range.map(x => x * 2)); // [2, 4, 6]
|
|
|
190
196
|
It works the same as [**`Array.prototype.filter`**](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter)
|
|
191
197
|
|
|
192
198
|
```javascript
|
|
193
|
-
const range = new PyRange(1, 6);
|
|
194
|
-
console.log(range.filter(x => x % 2 === 0));
|
|
199
|
+
const range = new PyRange(1, 6); // [1, 2, 3, 4, 5]
|
|
200
|
+
console.log(range.filter((x) => x % 2 === 0)); // [2, 4]
|
|
195
201
|
```
|
|
196
202
|
|
|
197
203
|
### reduce()
|
|
@@ -199,9 +205,9 @@ console.log(range.filter(x => x % 2 === 0)); // [2, 4]
|
|
|
199
205
|
It works the same as [**`Array.prototype.reduce`**](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce)
|
|
200
206
|
|
|
201
207
|
```javascript
|
|
202
|
-
const range = new PyRange(1, 4);
|
|
208
|
+
const range = new PyRange(1, 4); // [1, 2, 3]
|
|
203
209
|
const sum = range.reduce((acc, curr) => acc + curr, 0);
|
|
204
|
-
console.log(sum);
|
|
210
|
+
console.log(sum); // 6
|
|
205
211
|
```
|
|
206
212
|
|
|
207
213
|
### some()
|
|
@@ -209,9 +215,9 @@ console.log(sum); // 6
|
|
|
209
215
|
It works the same as [**`Array.prototype.some`**](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some)
|
|
210
216
|
|
|
211
217
|
```javascript
|
|
212
|
-
const range = new PyRange(1, 5);
|
|
213
|
-
console.log(range.some(x => x > 3));
|
|
214
|
-
console.log(range.some(x => x < 0));
|
|
218
|
+
const range = new PyRange(1, 5); // [1, 2, 3, 4]
|
|
219
|
+
console.log(range.some((x) => x > 3)); // true
|
|
220
|
+
console.log(range.some((x) => x < 0)); // false
|
|
215
221
|
```
|
|
216
222
|
|
|
217
223
|
### every()
|
|
@@ -219,9 +225,9 @@ console.log(range.some(x => x < 0)); // false
|
|
|
219
225
|
It works the same as [**`Array.prototype.every`**](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/every)
|
|
220
226
|
|
|
221
227
|
```javascript
|
|
222
|
-
const range = new PyRange(1, 5);
|
|
223
|
-
console.log(range.every(x => x > 0));
|
|
224
|
-
console.log(range.every(x => x > 2));
|
|
228
|
+
const range = new PyRange(1, 5); // [1, 2, 3, 4]
|
|
229
|
+
console.log(range.every((x) => x > 0)); // true
|
|
230
|
+
console.log(range.every((x) => x > 2)); // false
|
|
225
231
|
```
|
|
226
232
|
|
|
227
233
|
### find()
|
|
@@ -229,9 +235,9 @@ console.log(range.every(x => x > 2)); // false
|
|
|
229
235
|
It works the same as [**`Array.prototype.find`**](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find)
|
|
230
236
|
|
|
231
237
|
```javascript
|
|
232
|
-
const range = new PyRange(1, 5);
|
|
233
|
-
console.log(range.find(x => x > 2));
|
|
234
|
-
console.log(range.find(x => x > 5));
|
|
238
|
+
const range = new PyRange(1, 5); // [1, 2, 3, 4]
|
|
239
|
+
console.log(range.find((x) => x > 2)); // 3
|
|
240
|
+
console.log(range.find((x) => x > 5)); // undefined
|
|
235
241
|
```
|
|
236
242
|
|
|
237
243
|
### findIndex()
|
|
@@ -239,9 +245,9 @@ console.log(range.find(x => x > 5)); // undefined
|
|
|
239
245
|
It works the same as [**`Array.prototype.findIndex`**](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex)
|
|
240
246
|
|
|
241
247
|
```javascript
|
|
242
|
-
const range = new PyRange(1, 5);
|
|
243
|
-
console.log(range.findIndex(x => x > 2));
|
|
244
|
-
console.log(range.findIndex(x => x > 5));
|
|
248
|
+
const range = new PyRange(1, 5); // [1, 2, 3, 4]
|
|
249
|
+
console.log(range.findIndex((x) => x > 2)); // 2
|
|
250
|
+
console.log(range.findIndex((x) => x > 5)); // -1
|
|
245
251
|
```
|
|
246
252
|
|
|
247
253
|
### findLastIndex()
|
|
@@ -249,9 +255,9 @@ console.log(range.findIndex(x => x > 5)); // -1
|
|
|
249
255
|
It works the same as [**`Array.prototype.findLastIndex`**](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findLastIndex)
|
|
250
256
|
|
|
251
257
|
```javascript
|
|
252
|
-
const range = new PyRange(1, 5);
|
|
253
|
-
console.log(range.findLastIndex(x => x > 2));
|
|
254
|
-
console.log(range.findLastIndex(x => x > 5));
|
|
258
|
+
const range = new PyRange(1, 5); // [1, 2, 3, 4]
|
|
259
|
+
console.log(range.findLastIndex((x) => x > 2)); // 3
|
|
260
|
+
console.log(range.findLastIndex((x) => x > 5)); // -1
|
|
255
261
|
```
|
|
256
262
|
|
|
257
263
|
### forEach()
|
|
@@ -259,8 +265,8 @@ console.log(range.findLastIndex(x => x > 5)); // -1
|
|
|
259
265
|
It works the same as [**`Array.prototype.forEach`**](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach)
|
|
260
266
|
|
|
261
267
|
```javascript
|
|
262
|
-
const range = new PyRange(1, 4);
|
|
263
|
-
range.forEach(x => console.log(x));
|
|
268
|
+
const range = new PyRange(1, 4); // [1, 2, 3]
|
|
269
|
+
range.forEach((x) => console.log(x));
|
|
264
270
|
// 1
|
|
265
271
|
// 2
|
|
266
272
|
// 3
|
|
@@ -271,9 +277,9 @@ range.forEach(x => console.log(x));
|
|
|
271
277
|
It works the same as [**`Array.prototype.includes`**](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes)
|
|
272
278
|
|
|
273
279
|
```javascript
|
|
274
|
-
const range = new PyRange(1, 5);
|
|
275
|
-
console.log(range.includes(3));
|
|
276
|
-
console.log(range.includes(5));
|
|
280
|
+
const range = new PyRange(1, 5); // [1, 2, 3, 4]
|
|
281
|
+
console.log(range.includes(3)); // true
|
|
282
|
+
console.log(range.includes(5)); // false
|
|
277
283
|
```
|
|
278
284
|
|
|
279
285
|
### indexOf()
|
|
@@ -281,9 +287,9 @@ console.log(range.includes(5)); // false
|
|
|
281
287
|
It works the same as [**`Array.prototype.indexOf`**](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf)
|
|
282
288
|
|
|
283
289
|
```javascript
|
|
284
|
-
const range = new PyRange(1, 5);
|
|
285
|
-
console.log(range.indexOf(3));
|
|
286
|
-
console.log(range.indexOf(5));
|
|
290
|
+
const range = new PyRange(1, 5); // [1, 2, 3, 4]
|
|
291
|
+
console.log(range.indexOf(3)); // 2
|
|
292
|
+
console.log(range.indexOf(5)); // -1
|
|
287
293
|
```
|
|
288
294
|
|
|
289
295
|
### lastIndexOf()
|
|
@@ -291,9 +297,9 @@ console.log(range.indexOf(5)); // -1
|
|
|
291
297
|
It works the same as [**`Array.prototype.lastIndexOf`**](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/lastIndexOf)
|
|
292
298
|
|
|
293
299
|
```javascript
|
|
294
|
-
const range = new PyRange(1, 5, 1);
|
|
295
|
-
console.log(range.lastIndexOf(3));
|
|
296
|
-
console.log(range.lastIndexOf(5));
|
|
300
|
+
const range = new PyRange(1, 5, 1); // [1, 2, 3, 4]
|
|
301
|
+
console.log(range.lastIndexOf(3)); // 2
|
|
302
|
+
console.log(range.lastIndexOf(5)); // -1
|
|
297
303
|
```
|
|
298
304
|
|
|
299
305
|
### pop()
|
|
@@ -303,9 +309,9 @@ this method removes the last value from the range, shortens the range and
|
|
|
303
309
|
returns that value.
|
|
304
310
|
|
|
305
311
|
```javascript
|
|
306
|
-
const range = new PyRange(1, 5);
|
|
307
|
-
console.log(range.pop());
|
|
308
|
-
console.log([...range]);
|
|
312
|
+
const range = new PyRange(1, 5); // [1, 2, 3, 4]
|
|
313
|
+
console.log(range.pop()); // 4
|
|
314
|
+
console.log([...range]); // [1, 2, 3]
|
|
309
315
|
```
|
|
310
316
|
|
|
311
317
|
### slice()
|
|
@@ -315,10 +321,10 @@ It returns a new PyRange instance containing elements from the specified indices
|
|
|
315
321
|
The original range is not modified.
|
|
316
322
|
|
|
317
323
|
```javascript
|
|
318
|
-
const range = new PyRange(0, 10);
|
|
324
|
+
const range = new PyRange(0, 10); // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
|
|
319
325
|
const sliced = range.slice(2, 5);
|
|
320
326
|
console.log([...sliced]); // [2, 3, 4]
|
|
321
|
-
console.log([...range]);
|
|
327
|
+
console.log([...range]); // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] (unchanged)
|
|
322
328
|
|
|
323
329
|
// Negative indices are supported
|
|
324
330
|
const lastThree = range.slice(-3);
|
|
@@ -330,17 +336,52 @@ console.log([...lastThree]); // [7, 8, 9]
|
|
|
330
336
|
It works the same as [**`Array.prototype.reverse`**](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reverse)
|
|
331
337
|
|
|
332
338
|
```javascript
|
|
333
|
-
const range = new PyRange(1, 5);
|
|
334
|
-
console.log([...range]);
|
|
339
|
+
const range = new PyRange(1, 5); // [1, 2, 3, 4]
|
|
340
|
+
console.log([...range]); // [1, 2, 3, 4]
|
|
335
341
|
|
|
336
342
|
const reversed = range.reverse(); // [4, 3, 2, 1]
|
|
337
|
-
console.log([...reversed]);
|
|
343
|
+
console.log([...reversed]); // [4, 3, 2, 1]
|
|
338
344
|
|
|
339
|
-
const rangeWithStep = new PyRange(1, 10, 2);
|
|
345
|
+
const rangeWithStep = new PyRange(1, 10, 2); // [1, 3, 5, 7, 9]
|
|
340
346
|
const reversedStep = rangeWithStep.reverse(); // [9, 7, 5, 3, 1]
|
|
341
347
|
console.log([...reversedStep]);
|
|
342
348
|
```
|
|
343
349
|
|
|
350
|
+
### entries()
|
|
351
|
+
|
|
352
|
+
It works the same as [**`Array.prototype.entries`**](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/entries).
|
|
353
|
+
It returns an iterator of `[index, value]` pairs.
|
|
354
|
+
|
|
355
|
+
```javascript
|
|
356
|
+
const range = new PyRange(1, 4); // [1, 2, 3]
|
|
357
|
+
for (const [index, value] of range.entries()) {
|
|
358
|
+
console.log(index, value);
|
|
359
|
+
}
|
|
360
|
+
// 0 1
|
|
361
|
+
// 1 2
|
|
362
|
+
// 2 3
|
|
363
|
+
```
|
|
364
|
+
|
|
365
|
+
### keys()
|
|
366
|
+
|
|
367
|
+
It works the same as [**`Array.prototype.keys`**](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/keys).
|
|
368
|
+
It returns an iterator of the indices of the range.
|
|
369
|
+
|
|
370
|
+
```javascript
|
|
371
|
+
const range = new PyRange(3); // [0, 1, 2]
|
|
372
|
+
console.log([...range.keys()]); // [0, 1, 2]
|
|
373
|
+
```
|
|
374
|
+
|
|
375
|
+
### values()
|
|
376
|
+
|
|
377
|
+
It works the same as [**`Array.prototype.values`**](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/values).
|
|
378
|
+
It returns an iterator of the values in the range.
|
|
379
|
+
|
|
380
|
+
```javascript
|
|
381
|
+
const range = new PyRange(3); // [0, 1, 2]
|
|
382
|
+
console.log([...range.values()]); // [0, 1, 2]
|
|
383
|
+
```
|
|
384
|
+
|
|
344
385
|
## Advanced Usage
|
|
345
386
|
|
|
346
387
|
### Iteration
|
|
@@ -350,7 +391,7 @@ const range = new PyRange(3);
|
|
|
350
391
|
|
|
351
392
|
// For...of loop
|
|
352
393
|
for (const num of range) {
|
|
353
|
-
|
|
394
|
+
console.log(num); // 0, 1, 2
|
|
354
395
|
}
|
|
355
396
|
|
|
356
397
|
// Spread operator
|
|
@@ -383,13 +424,13 @@ This package is written in TypeScript and provides full type definitions for all
|
|
|
383
424
|
const range = new PyRange(5);
|
|
384
425
|
|
|
385
426
|
// TypeScript knows this is a number[]
|
|
386
|
-
const numbers = range.map(x => x * 2);
|
|
427
|
+
const numbers = range.map((x) => x * 2);
|
|
387
428
|
|
|
388
429
|
// TypeScript knows this is a string[]
|
|
389
|
-
const strings = range.map(x => `Number: ${x}`);
|
|
430
|
+
const strings = range.map((x) => `Number: ${x}`);
|
|
390
431
|
|
|
391
432
|
// TypeScript knows this is a boolean[]
|
|
392
|
-
const booleans = range.map(x => x % 2 === 0);
|
|
433
|
+
const booleans = range.map((x) => x % 2 === 0);
|
|
393
434
|
```
|
|
394
435
|
|
|
395
436
|
## Examples
|
|
@@ -398,9 +439,7 @@ const booleans = range.map(x => x % 2 === 0);
|
|
|
398
439
|
|
|
399
440
|
```javascript
|
|
400
441
|
const range = new PyRange(1, 6);
|
|
401
|
-
const squares = range
|
|
402
|
-
.filter(x => x % 2 === 0)
|
|
403
|
-
.map(x => x * x);
|
|
442
|
+
const squares = range.filter((x) => x % 2 === 0).map((x) => x * x);
|
|
404
443
|
console.log(squares); // [4, 16]
|
|
405
444
|
```
|
|
406
445
|
|
package/dist/py-range.d.ts
CHANGED
|
@@ -149,19 +149,19 @@ declare class PyRange implements Iterable<number> {
|
|
|
149
149
|
* @param {any} value - The value to search for.
|
|
150
150
|
* @returns {boolean} True if the value is present, false otherwise.
|
|
151
151
|
*/
|
|
152
|
-
includes(value:
|
|
152
|
+
includes(value: unknown): boolean;
|
|
153
153
|
/**
|
|
154
154
|
* Returns the index of the first occurrence of the specified value, or -1 if it is not present.
|
|
155
155
|
* @param {any} value - The value to search for.
|
|
156
156
|
* @returns {number} The index of the value, or -1 if it is not present.
|
|
157
157
|
*/
|
|
158
|
-
indexOf(value:
|
|
158
|
+
indexOf(value: unknown): number;
|
|
159
159
|
/**
|
|
160
160
|
* Returns the index of the last occurrence of the specified value, or -1 if it is not present.
|
|
161
161
|
* @param {any} value - The value to search for.
|
|
162
162
|
* @returns {number} The index of the last occurrence of the value, or -1 if it is not present.
|
|
163
163
|
*/
|
|
164
|
-
lastIndexOf(value:
|
|
164
|
+
lastIndexOf(value: unknown): number;
|
|
165
165
|
/**
|
|
166
166
|
* Removes the last element from the range and returns it.
|
|
167
167
|
*
|
|
@@ -192,6 +192,31 @@ declare class PyRange implements Iterable<number> {
|
|
|
192
192
|
* @returns {PyRange} A new PyRange object with the elements in reverse order.
|
|
193
193
|
*/
|
|
194
194
|
reverse(): PyRange;
|
|
195
|
+
/**
|
|
196
|
+
* Returns an iterator of `[index, value]` pairs for each element in the range.
|
|
197
|
+
*
|
|
198
|
+
* This method behaves like `Array.prototype.entries()` and is useful for
|
|
199
|
+
* iterating over both the index and value of each item.
|
|
200
|
+
*
|
|
201
|
+
* @returns {IterableIterator<[number, number]>} Iterator of index/value pairs.
|
|
202
|
+
*/
|
|
203
|
+
entries(): IterableIterator<[number, number]>;
|
|
204
|
+
/**
|
|
205
|
+
* Returns an iterator of the indices for each element in the range.
|
|
206
|
+
*
|
|
207
|
+
* Works the same as `Array.prototype.keys()`.
|
|
208
|
+
*
|
|
209
|
+
* @returns {IterableIterator<number>} Iterator of indices.
|
|
210
|
+
*/
|
|
211
|
+
keys(): IterableIterator<number>;
|
|
212
|
+
/**
|
|
213
|
+
* Returns an iterator of the values in the range.
|
|
214
|
+
*
|
|
215
|
+
* Equivalent to `Array.prototype.values()`.
|
|
216
|
+
*
|
|
217
|
+
* @returns {IterableIterator<number>} Iterator of values.
|
|
218
|
+
*/
|
|
219
|
+
values(): IterableIterator<number>;
|
|
195
220
|
/**
|
|
196
221
|
* Implements the iterable protocol for this range.
|
|
197
222
|
* @returns {Iterator<number>} An iterator for this range.
|
|
@@ -202,9 +227,11 @@ declare class PyRange implements Iterable<number> {
|
|
|
202
227
|
* This proxy enables accessing range elements via array-like indexing.
|
|
203
228
|
* If the property is a number, it will return the element at that index.
|
|
204
229
|
*
|
|
205
|
-
* @returns {
|
|
230
|
+
* @returns {PyRange & { [key: number]: number }} A proxy for the PyRange instance.
|
|
206
231
|
*/
|
|
207
|
-
asProxy():
|
|
232
|
+
asProxy(): PyRange & {
|
|
233
|
+
[key: number]: number;
|
|
234
|
+
};
|
|
208
235
|
}
|
|
209
236
|
export { PyRange };
|
|
210
237
|
export default PyRange;
|
package/dist/py-range.js
CHANGED
|
@@ -381,6 +381,41 @@ class PyRange {
|
|
|
381
381
|
result._length = this._length;
|
|
382
382
|
return result;
|
|
383
383
|
}
|
|
384
|
+
/**
|
|
385
|
+
* Returns an iterator of `[index, value]` pairs for each element in the range.
|
|
386
|
+
*
|
|
387
|
+
* This method behaves like `Array.prototype.entries()` and is useful for
|
|
388
|
+
* iterating over both the index and value of each item.
|
|
389
|
+
*
|
|
390
|
+
* @returns {IterableIterator<[number, number]>} Iterator of index/value pairs.
|
|
391
|
+
*/
|
|
392
|
+
*entries() {
|
|
393
|
+
for (let i = 0; i < this._length; i++) {
|
|
394
|
+
yield [i, this.at(i)];
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
/**
|
|
398
|
+
* Returns an iterator of the indices for each element in the range.
|
|
399
|
+
*
|
|
400
|
+
* Works the same as `Array.prototype.keys()`.
|
|
401
|
+
*
|
|
402
|
+
* @returns {IterableIterator<number>} Iterator of indices.
|
|
403
|
+
*/
|
|
404
|
+
*keys() {
|
|
405
|
+
for (let i = 0; i < this._length; i++) {
|
|
406
|
+
yield i;
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
/**
|
|
410
|
+
* Returns an iterator of the values in the range.
|
|
411
|
+
*
|
|
412
|
+
* Equivalent to `Array.prototype.values()`.
|
|
413
|
+
*
|
|
414
|
+
* @returns {IterableIterator<number>} Iterator of values.
|
|
415
|
+
*/
|
|
416
|
+
*values() {
|
|
417
|
+
yield* this;
|
|
418
|
+
}
|
|
384
419
|
/**
|
|
385
420
|
* Implements the iterable protocol for this range.
|
|
386
421
|
* @returns {Iterator<number>} An iterator for this range.
|
|
@@ -408,18 +443,18 @@ class PyRange {
|
|
|
408
443
|
* This proxy enables accessing range elements via array-like indexing.
|
|
409
444
|
* If the property is a number, it will return the element at that index.
|
|
410
445
|
*
|
|
411
|
-
* @returns {
|
|
446
|
+
* @returns {PyRange & { [key: number]: number }} A proxy for the PyRange instance.
|
|
412
447
|
*/
|
|
413
448
|
asProxy() {
|
|
414
449
|
return new Proxy(this, {
|
|
415
|
-
get(target, prop) {
|
|
450
|
+
get(target, prop, receiver) {
|
|
416
451
|
if (typeof prop === "symbol") {
|
|
417
|
-
return target
|
|
452
|
+
return Reflect.get(target, prop, receiver);
|
|
418
453
|
}
|
|
419
454
|
if (!isNaN(Number(prop))) {
|
|
420
455
|
return target.at(parseInt(String(prop), 10));
|
|
421
456
|
}
|
|
422
|
-
return target
|
|
457
|
+
return Reflect.get(target, prop, receiver);
|
|
423
458
|
},
|
|
424
459
|
});
|
|
425
460
|
}
|
|
@@ -76,3 +76,15 @@ console.log("After pop:", [...range]); // [1, 2, 3, 4, 5, 6, 7, 8]
|
|
|
76
76
|
const sliced = range.slice(2, 5);
|
|
77
77
|
console.log("slice(2, 5):", [...sliced]); // [3, 4, 5]
|
|
78
78
|
console.log("Original after slice:", [...range]); // [1, 2, 3, 4, 5, 6, 7, 8] (unchanged)
|
|
79
|
+
|
|
80
|
+
// Entries: Iterate over index/value pairs
|
|
81
|
+
console.log("\nentries() example:");
|
|
82
|
+
for (const [i, v] of range.entries()) {
|
|
83
|
+
if (i < 3) {
|
|
84
|
+
console.log(i, v);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// Keys and values helpers
|
|
89
|
+
console.log("keys():", [...range.keys()].slice(0, 3)); // [0,1,2]
|
|
90
|
+
console.log("values():", [...range.values()].slice(0, 3)); // [1,2,3]
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "range-pie",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.4.1",
|
|
4
4
|
"description": "A TypeScript class that simulates Python's range function, combined with several useful JavaScript array methods.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.js",
|
|
@@ -22,21 +22,25 @@
|
|
|
22
22
|
],
|
|
23
23
|
"scripts": {
|
|
24
24
|
"build": "tsc",
|
|
25
|
+
"typecheck": "tsc --noEmit",
|
|
25
26
|
"build:prod": "tsc -p tsconfig.prod.json",
|
|
26
27
|
"build:watch": "tsc --watch",
|
|
28
|
+
"prepare": "husky",
|
|
27
29
|
"prepublishOnly": "npm run build:prod && npm run test",
|
|
28
30
|
"test": "jest",
|
|
29
31
|
"lint": "eslint . --ext .ts,.js",
|
|
30
32
|
"lint:fix": "eslint . --ext .ts,.js --fix",
|
|
31
33
|
"test:watch": "jest --watch",
|
|
32
34
|
"clean": "rimraf dist",
|
|
33
|
-
"format": "prettier --write \"src/**/*.{ts,js}\" \"test/**/*.{ts,js}\" \"examples/**/*.{ts,js}\" \"
|
|
34
|
-
"format:check": "prettier --check \"src/**/*.{ts,js}\" \"test/**/*.{ts,js}\" \"examples/**/*.{ts,js}\" \"
|
|
35
|
+
"format": "prettier --write \"src/**/*.{ts,js}\" \"test/**/*.{ts,js}\" \"examples/**/*.{ts,js}\" \"*.{js,json}\"",
|
|
36
|
+
"format:check": "prettier --check \"src/**/*.{ts,js}\" \"test/**/*.{ts,js}\" \"examples/**/*.{ts,js}\" \"*.{js,json}\"",
|
|
35
37
|
"example:basic": "node examples/basic-usage.js",
|
|
36
38
|
"example:array": "node examples/array-methods.js",
|
|
37
39
|
"example:advanced": "node examples/advanced-usage.js",
|
|
38
40
|
"example:ts": "ts-node examples/typescript-usage.ts",
|
|
39
|
-
"example:methods": "for file in examples/methods/*.ts; do echo \"\\n=== Running $file ===\"; ts-node \"$file\"; done"
|
|
41
|
+
"example:methods": "for file in examples/methods/*.ts; do echo \"\\n=== Running $file ===\"; ts-node \"$file\"; done",
|
|
42
|
+
"knip": "knip",
|
|
43
|
+
"precommit": "npm run typecheck && npx --no-install lint-staged"
|
|
40
44
|
},
|
|
41
45
|
"keywords": [
|
|
42
46
|
"range",
|
|
@@ -56,16 +60,20 @@
|
|
|
56
60
|
"url": "git+https://github.com/SkorpionG/range-pie.git"
|
|
57
61
|
},
|
|
58
62
|
"devDependencies": {
|
|
59
|
-
"@eslint/js": "^9.
|
|
60
|
-
"@types/jest": "^
|
|
61
|
-
"
|
|
62
|
-
"
|
|
63
|
-
"
|
|
64
|
-
"
|
|
65
|
-
"
|
|
66
|
-
"
|
|
63
|
+
"@eslint/js": "^9.39.4",
|
|
64
|
+
"@types/jest": "^30.0.0",
|
|
65
|
+
"@types/node": "^25.4.0",
|
|
66
|
+
"eslint": "^9.39.4",
|
|
67
|
+
"globals": "^17.4.0",
|
|
68
|
+
"husky": "^9.1.7",
|
|
69
|
+
"jest": "^30.3.0",
|
|
70
|
+
"knip": "^5.86.0",
|
|
71
|
+
"lint-staged": "^16.3.3",
|
|
72
|
+
"prettier": "^3.8.1",
|
|
73
|
+
"rimraf": "^6.1.3",
|
|
74
|
+
"ts-jest": "^29.4.6",
|
|
67
75
|
"ts-node": "^10.9.1",
|
|
68
|
-
"typescript": "^5.
|
|
69
|
-
"typescript-eslint": "^8.
|
|
76
|
+
"typescript": "^5.9.3",
|
|
77
|
+
"typescript-eslint": "^8.57.0"
|
|
70
78
|
}
|
|
71
79
|
}
|