range-pie 2.1.0 → 2.3.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 +43 -0
- package/dist/py-range.d.ts +25 -0
- package/dist/py-range.js +68 -0
- package/examples/array-methods.js +10 -0
- package/package.json +21 -7
package/README.md
CHANGED
|
@@ -29,6 +29,8 @@ A TypeScript/JavaScript library that brings Python's range functionality to Java
|
|
|
29
29
|
- [includes()](#includes)
|
|
30
30
|
- [indexOf()](#indexof)
|
|
31
31
|
- [lastIndexOf()](#lastindexof)
|
|
32
|
+
- [pop()](#pop)
|
|
33
|
+
- [slice()](#slice)
|
|
32
34
|
- [reverse()](#reverse)
|
|
33
35
|
|
|
34
36
|
- [Advanced Usage](#advanced-usage)
|
|
@@ -43,6 +45,7 @@ A TypeScript/JavaScript library that brings Python's range functionality to Java
|
|
|
43
45
|
- [Example Files](#example-files)
|
|
44
46
|
- [Running the Examples](#running-the-examples)
|
|
45
47
|
- [Available Examples](#available-examples)
|
|
48
|
+
- [Method-Specific Demonstrations](#method-specific-demonstrations)
|
|
46
49
|
|
|
47
50
|
## Installation
|
|
48
51
|
|
|
@@ -293,6 +296,35 @@ console.log(range.lastIndexOf(3)); // 2
|
|
|
293
296
|
console.log(range.lastIndexOf(5)); // -1
|
|
294
297
|
```
|
|
295
298
|
|
|
299
|
+
### pop()
|
|
300
|
+
|
|
301
|
+
Similar to [**`Array.prototype.pop`**](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/pop),
|
|
302
|
+
this method removes the last value from the range, shortens the range and
|
|
303
|
+
returns that value.
|
|
304
|
+
|
|
305
|
+
```javascript
|
|
306
|
+
const range = new PyRange(1, 5); // [1, 2, 3, 4]
|
|
307
|
+
console.log(range.pop()); // 4
|
|
308
|
+
console.log([...range]); // [1, 2, 3]
|
|
309
|
+
```
|
|
310
|
+
|
|
311
|
+
### slice()
|
|
312
|
+
|
|
313
|
+
This method behaves similarly to [**`Array.prototype.slice`**](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice).
|
|
314
|
+
It returns a new PyRange instance containing elements from the specified indices.
|
|
315
|
+
The original range is not modified.
|
|
316
|
+
|
|
317
|
+
```javascript
|
|
318
|
+
const range = new PyRange(0, 10); // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
|
|
319
|
+
const sliced = range.slice(2, 5);
|
|
320
|
+
console.log([...sliced]); // [2, 3, 4]
|
|
321
|
+
console.log([...range]); // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] (unchanged)
|
|
322
|
+
|
|
323
|
+
// Negative indices are supported
|
|
324
|
+
const lastThree = range.slice(-3);
|
|
325
|
+
console.log([...lastThree]); // [7, 8, 9]
|
|
326
|
+
```
|
|
327
|
+
|
|
296
328
|
### reverse()
|
|
297
329
|
|
|
298
330
|
It works the same as [**`Array.prototype.reverse`**](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reverse)
|
|
@@ -419,3 +451,14 @@ ts-node node_modules/range-pie/examples/typescript-usage.ts
|
|
|
419
451
|
- **array-methods.js**: Demonstrates all array-like methods
|
|
420
452
|
- **advanced-usage.js**: Covers advanced features like method chaining and proxy usage
|
|
421
453
|
- **typescript-usage.ts**: Shows TypeScript-specific features and type safety
|
|
454
|
+
|
|
455
|
+
#### Method-Specific Demonstrations
|
|
456
|
+
|
|
457
|
+
For detailed demonstrations of individual methods, see the `examples/methods/` folder. Each method has its own comprehensive demo file with detailed explanations and use cases.
|
|
458
|
+
|
|
459
|
+
```bash
|
|
460
|
+
# Run all method demonstrations
|
|
461
|
+
npm run example:methods
|
|
462
|
+
```
|
|
463
|
+
|
|
464
|
+
For instructions on running individual method demos and more details, see the [README in the examples/methods folder](examples/methods/README.md).
|
package/dist/py-range.d.ts
CHANGED
|
@@ -162,6 +162,31 @@ declare class PyRange implements Iterable<number> {
|
|
|
162
162
|
* @returns {number} The index of the last occurrence of the value, or -1 if it is not present.
|
|
163
163
|
*/
|
|
164
164
|
lastIndexOf(value: any): number;
|
|
165
|
+
/**
|
|
166
|
+
* Removes the last element from the range and returns it.
|
|
167
|
+
*
|
|
168
|
+
* This method behaves similarly to `Array.prototype.pop`. It decreases the
|
|
169
|
+
* `stop` value by the step size, shortens the range length by one, and returns
|
|
170
|
+
* the removed value. If the range is empty, `undefined` is returned and the
|
|
171
|
+
* range remains unchanged.
|
|
172
|
+
*
|
|
173
|
+
* @returns {number|undefined} The removed last value, or `undefined` if the
|
|
174
|
+
* range is empty.
|
|
175
|
+
*/
|
|
176
|
+
pop(): number | undefined;
|
|
177
|
+
/**
|
|
178
|
+
* Returns a new PyRange instance containing elements from the specified indices.
|
|
179
|
+
*
|
|
180
|
+
* This method behaves similarly to `Array.prototype.slice`. It creates a new
|
|
181
|
+
* PyRange with elements from `begin` to `end` (exclusive) based on the given
|
|
182
|
+
* parameters. Negative indices are supported. The original range is not modified.
|
|
183
|
+
*
|
|
184
|
+
* @param {number} [begin=0] - Zero-based index at which to begin slicing.
|
|
185
|
+
* @param {number} [end=this.length] - Zero-based index at which to end slicing
|
|
186
|
+
* (exclusive).
|
|
187
|
+
* @returns {PyRange} A new PyRange instance containing the sliced elements.
|
|
188
|
+
*/
|
|
189
|
+
slice(begin?: number, end?: number): PyRange;
|
|
165
190
|
/**
|
|
166
191
|
* Reverses the order of the elements in this range, returning a new PyRange object.
|
|
167
192
|
* @returns {PyRange} A new PyRange object with the elements in reverse order.
|
package/dist/py-range.js
CHANGED
|
@@ -303,6 +303,74 @@ class PyRange {
|
|
|
303
303
|
}
|
|
304
304
|
return -1;
|
|
305
305
|
}
|
|
306
|
+
/**
|
|
307
|
+
* Removes the last element from the range and returns it.
|
|
308
|
+
*
|
|
309
|
+
* This method behaves similarly to `Array.prototype.pop`. It decreases the
|
|
310
|
+
* `stop` value by the step size, shortens the range length by one, and returns
|
|
311
|
+
* the removed value. If the range is empty, `undefined` is returned and the
|
|
312
|
+
* range remains unchanged.
|
|
313
|
+
*
|
|
314
|
+
* @returns {number|undefined} The removed last value, or `undefined` if the
|
|
315
|
+
* range is empty.
|
|
316
|
+
*/
|
|
317
|
+
pop() {
|
|
318
|
+
if (this._length === 0) {
|
|
319
|
+
return undefined;
|
|
320
|
+
}
|
|
321
|
+
const lastValue = this.at(this._length - 1);
|
|
322
|
+
this._stop -= this._step;
|
|
323
|
+
this._length--;
|
|
324
|
+
return lastValue;
|
|
325
|
+
}
|
|
326
|
+
/**
|
|
327
|
+
* Returns a new PyRange instance containing elements from the specified indices.
|
|
328
|
+
*
|
|
329
|
+
* This method behaves similarly to `Array.prototype.slice`. It creates a new
|
|
330
|
+
* PyRange with elements from `begin` to `end` (exclusive) based on the given
|
|
331
|
+
* parameters. Negative indices are supported. The original range is not modified.
|
|
332
|
+
*
|
|
333
|
+
* @param {number} [begin=0] - Zero-based index at which to begin slicing.
|
|
334
|
+
* @param {number} [end=this.length] - Zero-based index at which to end slicing
|
|
335
|
+
* (exclusive).
|
|
336
|
+
* @returns {PyRange} A new PyRange instance containing the sliced elements.
|
|
337
|
+
*/
|
|
338
|
+
slice(begin = 0, end = this._length) {
|
|
339
|
+
if (typeof begin !== "number" || typeof end !== "number") {
|
|
340
|
+
throw new TypeError("Indices must be numbers");
|
|
341
|
+
}
|
|
342
|
+
if (!Number.isInteger(begin) || !Number.isInteger(end)) {
|
|
343
|
+
throw new TypeError("Indices must be integers");
|
|
344
|
+
}
|
|
345
|
+
const len = this._length;
|
|
346
|
+
let startIdx = begin;
|
|
347
|
+
let endIdx = end;
|
|
348
|
+
if (startIdx < 0) {
|
|
349
|
+
startIdx = Math.max(len + startIdx, 0);
|
|
350
|
+
}
|
|
351
|
+
else {
|
|
352
|
+
startIdx = Math.min(startIdx, len);
|
|
353
|
+
}
|
|
354
|
+
if (endIdx < 0) {
|
|
355
|
+
endIdx = Math.max(len + endIdx, 0);
|
|
356
|
+
}
|
|
357
|
+
else {
|
|
358
|
+
endIdx = Math.min(endIdx, len);
|
|
359
|
+
}
|
|
360
|
+
const origStart = this._start;
|
|
361
|
+
const step = this._step;
|
|
362
|
+
if (startIdx >= endIdx) {
|
|
363
|
+
// Return an empty range
|
|
364
|
+
const emptyStart = origStart + startIdx * step;
|
|
365
|
+
const result = new PyRange(emptyStart, emptyStart, step);
|
|
366
|
+
return result;
|
|
367
|
+
}
|
|
368
|
+
const newStart = origStart + startIdx * step;
|
|
369
|
+
const newStop = origStart + endIdx * step;
|
|
370
|
+
// Create a new PyRange instance with the calculated bounds
|
|
371
|
+
const result = new PyRange(newStart, newStop, step);
|
|
372
|
+
return result;
|
|
373
|
+
}
|
|
306
374
|
/**
|
|
307
375
|
* Reverses the order of the elements in this range, returning a new PyRange object.
|
|
308
376
|
* @returns {PyRange} A new PyRange object with the elements in reverse order.
|
|
@@ -66,3 +66,13 @@ range.forEach((x) => {
|
|
|
66
66
|
// 3 is divisible by 3
|
|
67
67
|
// 6 is divisible by 3
|
|
68
68
|
// 9 is divisible by 3
|
|
69
|
+
|
|
70
|
+
// Pop: Remove the last value
|
|
71
|
+
const popped = range.pop();
|
|
72
|
+
console.log("pop():", popped); // 9
|
|
73
|
+
console.log("After pop:", [...range]); // [1, 2, 3, 4, 5, 6, 7, 8]
|
|
74
|
+
|
|
75
|
+
// Slice: Get a subset of the range (returns new instance)
|
|
76
|
+
const sliced = range.slice(2, 5);
|
|
77
|
+
console.log("slice(2, 5):", [...sliced]); // [3, 4, 5]
|
|
78
|
+
console.log("Original after slice:", [...range]); // [1, 2, 3, 4, 5, 6, 7, 8] (unchanged)
|
package/package.json
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "range-pie",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.3.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
|
+
"module": "dist/index.js",
|
|
6
7
|
"types": "dist/index.d.ts",
|
|
7
8
|
"exports": {
|
|
8
9
|
".": {
|
|
@@ -12,21 +13,30 @@
|
|
|
12
13
|
}
|
|
13
14
|
},
|
|
14
15
|
"files": [
|
|
15
|
-
"dist",
|
|
16
|
-
"
|
|
16
|
+
"dist/**/*.js",
|
|
17
|
+
"dist/**/*.d.ts",
|
|
18
|
+
"!dist/**/*.map",
|
|
19
|
+
"examples/*.js",
|
|
20
|
+
"examples/*.ts",
|
|
21
|
+
"!examples/methods"
|
|
17
22
|
],
|
|
18
23
|
"scripts": {
|
|
19
24
|
"build": "tsc",
|
|
20
25
|
"build:prod": "tsc -p tsconfig.prod.json",
|
|
21
|
-
"
|
|
26
|
+
"build:watch": "tsc --watch",
|
|
27
|
+
"prepublishOnly": "npm run build:prod && npm run test",
|
|
22
28
|
"test": "jest",
|
|
29
|
+
"lint": "eslint . --ext .ts,.js",
|
|
30
|
+
"lint:fix": "eslint . --ext .ts,.js --fix",
|
|
31
|
+
"test:watch": "jest --watch",
|
|
23
32
|
"clean": "rimraf dist",
|
|
24
33
|
"format": "prettier --write \"src/**/*.{ts,js}\" \"test/**/*.{ts,js}\" \"examples/**/*.{ts,js}\" \"types/**/*.{ts,js,json}\" \"*.{js,json}\"",
|
|
25
|
-
"check
|
|
34
|
+
"format:check": "prettier --check \"src/**/*.{ts,js}\" \"test/**/*.{ts,js}\" \"examples/**/*.{ts,js}\" \"types/**/*.{ts,js,json}\" \"*.{js,json}\"",
|
|
26
35
|
"example:basic": "node examples/basic-usage.js",
|
|
27
36
|
"example:array": "node examples/array-methods.js",
|
|
28
37
|
"example:advanced": "node examples/advanced-usage.js",
|
|
29
|
-
"example:ts": "ts-node examples/typescript-usage.ts"
|
|
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"
|
|
30
40
|
},
|
|
31
41
|
"keywords": [
|
|
32
42
|
"range",
|
|
@@ -46,12 +56,16 @@
|
|
|
46
56
|
"url": "git+https://github.com/SkorpionG/range-pie.git"
|
|
47
57
|
},
|
|
48
58
|
"devDependencies": {
|
|
59
|
+
"@eslint/js": "^9.28.0",
|
|
49
60
|
"@types/jest": "^29.5.0",
|
|
61
|
+
"eslint": "^9.28.0",
|
|
62
|
+
"globals": "^16.2.0",
|
|
50
63
|
"jest": "^29.7.0",
|
|
51
64
|
"prettier": "^3.5.3",
|
|
52
65
|
"rimraf": "^5.0.0",
|
|
53
66
|
"ts-jest": "^29.1.0",
|
|
54
67
|
"ts-node": "^10.9.1",
|
|
55
|
-
"typescript": "^5.0.4"
|
|
68
|
+
"typescript": "^5.0.4",
|
|
69
|
+
"typescript-eslint": "^8.33.1"
|
|
56
70
|
}
|
|
57
71
|
}
|