range-pie 2.3.0 → 2.4.0
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 +42 -0
- package/dist/py-range.d.ts +25 -0
- package/dist/py-range.js +35 -0
- package/examples/array-methods.js +12 -0
- package/package.json +6 -3
package/README.md
CHANGED
|
@@ -32,6 +32,9 @@ A TypeScript/JavaScript library that brings Python's range functionality to Java
|
|
|
32
32
|
- [pop()](#pop)
|
|
33
33
|
- [slice()](#slice)
|
|
34
34
|
- [reverse()](#reverse)
|
|
35
|
+
- [entries()](#entries)
|
|
36
|
+
- [keys()](#keys)
|
|
37
|
+
- [values()](#values)
|
|
35
38
|
|
|
36
39
|
- [Advanced Usage](#advanced-usage)
|
|
37
40
|
|
|
@@ -74,6 +77,10 @@ console.log([...range2]); // [2, 3, 4, 5, 6, 7]
|
|
|
74
77
|
// Create a range with step
|
|
75
78
|
const range3 = new PyRange(0, 10, 2);
|
|
76
79
|
console.log([...range3]); // [0, 2, 4, 6, 8]
|
|
80
|
+
|
|
81
|
+
// Create a reverse range
|
|
82
|
+
const range4 = new PyRange(5, 2);
|
|
83
|
+
console.log([...range4]); // [5, 4, 3]
|
|
77
84
|
```
|
|
78
85
|
|
|
79
86
|
### TypeScript
|
|
@@ -341,6 +348,41 @@ const reversedStep = rangeWithStep.reverse(); // [9, 7, 5, 3, 1]
|
|
|
341
348
|
console.log([...reversedStep]);
|
|
342
349
|
```
|
|
343
350
|
|
|
351
|
+
### entries()
|
|
352
|
+
|
|
353
|
+
It works the same as [**`Array.prototype.entries`**](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/entries).
|
|
354
|
+
It returns an iterator of `[index, value]` pairs.
|
|
355
|
+
|
|
356
|
+
```javascript
|
|
357
|
+
const range = new PyRange(1, 4); // [1, 2, 3]
|
|
358
|
+
for (const [index, value] of range.entries()) {
|
|
359
|
+
console.log(index, value);
|
|
360
|
+
}
|
|
361
|
+
// 0 1
|
|
362
|
+
// 1 2
|
|
363
|
+
// 2 3
|
|
364
|
+
```
|
|
365
|
+
|
|
366
|
+
### keys()
|
|
367
|
+
|
|
368
|
+
It works the same as [**`Array.prototype.keys`**](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/keys).
|
|
369
|
+
It returns an iterator of the indices of the range.
|
|
370
|
+
|
|
371
|
+
```javascript
|
|
372
|
+
const range = new PyRange(3); // [0, 1, 2]
|
|
373
|
+
console.log([...range.keys()]); // [0, 1, 2]
|
|
374
|
+
```
|
|
375
|
+
|
|
376
|
+
### values()
|
|
377
|
+
|
|
378
|
+
It works the same as [**`Array.prototype.values`**](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/values).
|
|
379
|
+
It returns an iterator of the values in the range.
|
|
380
|
+
|
|
381
|
+
```javascript
|
|
382
|
+
const range = new PyRange(3); // [0, 1, 2]
|
|
383
|
+
console.log([...range.values()]); // [0, 1, 2]
|
|
384
|
+
```
|
|
385
|
+
|
|
344
386
|
## Advanced Usage
|
|
345
387
|
|
|
346
388
|
### Iteration
|
package/dist/py-range.d.ts
CHANGED
|
@@ -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.
|
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.
|
|
@@ -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.0",
|
|
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",
|
|
@@ -36,7 +36,8 @@
|
|
|
36
36
|
"example:array": "node examples/array-methods.js",
|
|
37
37
|
"example:advanced": "node examples/advanced-usage.js",
|
|
38
38
|
"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"
|
|
39
|
+
"example:methods": "for file in examples/methods/*.ts; do echo \"\\n=== Running $file ===\"; ts-node \"$file\"; done",
|
|
40
|
+
"knip": "knip"
|
|
40
41
|
},
|
|
41
42
|
"keywords": [
|
|
42
43
|
"range",
|
|
@@ -58,14 +59,16 @@
|
|
|
58
59
|
"devDependencies": {
|
|
59
60
|
"@eslint/js": "^9.28.0",
|
|
60
61
|
"@types/jest": "^29.5.0",
|
|
62
|
+
"@types/node": "^24.0.3",
|
|
61
63
|
"eslint": "^9.28.0",
|
|
62
64
|
"globals": "^16.2.0",
|
|
63
65
|
"jest": "^29.7.0",
|
|
66
|
+
"knip": "^5.61.2",
|
|
64
67
|
"prettier": "^3.5.3",
|
|
65
68
|
"rimraf": "^5.0.0",
|
|
66
69
|
"ts-jest": "^29.1.0",
|
|
67
70
|
"ts-node": "^10.9.1",
|
|
68
|
-
"typescript": "^5.
|
|
71
|
+
"typescript": "^5.8.3",
|
|
69
72
|
"typescript-eslint": "^8.33.1"
|
|
70
73
|
}
|
|
71
74
|
}
|