range-pie 2.0.2 → 2.1.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 +8 -4
- package/dist/index.js +10 -0
- package/examples/advanced-usage.js +18 -18
- package/examples/array-methods.js +24 -24
- package/examples/basic-usage.js +17 -17
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -55,8 +55,10 @@ npm install range-pie
|
|
|
55
55
|
### JavaScript
|
|
56
56
|
|
|
57
57
|
```javascript
|
|
58
|
-
// CommonJS
|
|
59
|
-
const { PyRange } = require('range-pie');
|
|
58
|
+
// CommonJS - Both import styles are supported
|
|
59
|
+
const { PyRange } = require('range-pie'); // Named import style
|
|
60
|
+
// OR
|
|
61
|
+
const PyRange = require('range-pie'); // Default import style
|
|
60
62
|
|
|
61
63
|
// Create a range from 0 to 5
|
|
62
64
|
const range = new PyRange(5);
|
|
@@ -74,8 +76,10 @@ console.log([...range3]); // [0, 2, 4, 6, 8]
|
|
|
74
76
|
### TypeScript
|
|
75
77
|
|
|
76
78
|
```typescript
|
|
77
|
-
// ES Modules
|
|
78
|
-
import { PyRange } from 'range-pie';
|
|
79
|
+
// ES Modules - Both import styles are supported
|
|
80
|
+
import { PyRange } from 'range-pie'; // Named import style
|
|
81
|
+
// OR
|
|
82
|
+
import PyRange from 'range-pie'; // Default import style
|
|
79
83
|
|
|
80
84
|
// Create a range from 0 to 5
|
|
81
85
|
const range = new PyRange(5);
|
package/dist/index.js
CHANGED
|
@@ -37,3 +37,13 @@ exports.PyRange = void 0;
|
|
|
37
37
|
const py_range_1 = __importStar(require("./py-range"));
|
|
38
38
|
Object.defineProperty(exports, "PyRange", { enumerable: true, get: function () { return py_range_1.PyRange; } });
|
|
39
39
|
exports.default = py_range_1.default;
|
|
40
|
+
// For CommonJS compatibility with both import styles
|
|
41
|
+
// This ensures `const PyRange = require('range-pie')` works
|
|
42
|
+
// as well as `const { PyRange } = require('range-pie')`
|
|
43
|
+
if (typeof module !== "undefined" && typeof module.exports !== "undefined") {
|
|
44
|
+
Object.defineProperty(module.exports, "__esModule", { value: true });
|
|
45
|
+
// Assign PyRange to both default export and named export
|
|
46
|
+
module.exports = Object.assign(py_range_1.default, module.exports);
|
|
47
|
+
// Also ensure PyRange is available as a property
|
|
48
|
+
module.exports.PyRange = py_range_1.default;
|
|
49
|
+
}
|
|
@@ -1,58 +1,58 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Advanced usage examples for range-pie
|
|
3
|
-
*
|
|
3
|
+
*
|
|
4
4
|
* This file demonstrates more advanced features of the PyRange class
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
|
-
const { PyRange } = require(
|
|
7
|
+
const { PyRange } = require("range-pie");
|
|
8
8
|
|
|
9
|
-
console.log(
|
|
9
|
+
console.log("\n--- Advanced PyRange Examples ---");
|
|
10
10
|
|
|
11
11
|
// Example 1: Using the reverse method
|
|
12
12
|
const range1 = new PyRange(1, 6);
|
|
13
|
-
console.log(
|
|
13
|
+
console.log("Original range:", [...range1]); // [1, 2, 3, 4, 5]
|
|
14
14
|
|
|
15
15
|
const reversed = range1.reverse();
|
|
16
|
-
console.log(
|
|
16
|
+
console.log("Reversed range:", [...reversed]); // [5, 4, 3, 2, 1]
|
|
17
17
|
|
|
18
18
|
// Example 2: Using the asProxy method for array-like access
|
|
19
19
|
const range2 = new PyRange(10).asProxy();
|
|
20
|
-
console.log(
|
|
21
|
-
console.log(
|
|
20
|
+
console.log("range2[0]:", range2[0]); // 0
|
|
21
|
+
console.log("range2[5]:", range2[5]); // 5
|
|
22
22
|
|
|
23
23
|
// Create an array using proxy access
|
|
24
24
|
const firstThree = [range2[0], range2[1], range2[2]];
|
|
25
|
-
console.log(
|
|
25
|
+
console.log("firstThree:", firstThree); // [0, 1, 2]
|
|
26
26
|
|
|
27
27
|
// Example 3: Method chaining
|
|
28
28
|
const range3 = new PyRange(1, 10);
|
|
29
29
|
const result = range3
|
|
30
|
-
.filter(x => x % 2 === 0)
|
|
31
|
-
.map(x => x * x)
|
|
32
|
-
.filter(x => x > 10);
|
|
30
|
+
.filter((x) => x % 2 === 0) // Get even numbers
|
|
31
|
+
.map((x) => x * x) // Square them
|
|
32
|
+
.filter((x) => x > 10); // Filter values greater than 10
|
|
33
33
|
|
|
34
|
-
console.log(
|
|
34
|
+
console.log("Chained methods result:", result); // [16, 36, 64]
|
|
35
35
|
|
|
36
36
|
// Example 4: Using PyRange with other array methods
|
|
37
37
|
const range4 = new PyRange(5);
|
|
38
|
-
const array = [...range4];
|
|
38
|
+
const array = [...range4]; // Convert to array: [0, 1, 2, 3, 4]
|
|
39
39
|
|
|
40
40
|
// Now we can use native array methods
|
|
41
|
-
const sorted = array.sort((a, b) => b - a);
|
|
42
|
-
console.log(
|
|
41
|
+
const sorted = array.sort((a, b) => b - a); // Sort in descending order
|
|
42
|
+
console.log("Sorted array:", sorted); // [4, 3, 2, 1, 0]
|
|
43
43
|
|
|
44
44
|
// Example 5: Creating a range with custom step
|
|
45
45
|
const range5 = new PyRange(0, 11, 2);
|
|
46
|
-
console.log(
|
|
46
|
+
console.log("Range with step 2:", [...range5]); // [0, 2, 4, 6, 8, 10]
|
|
47
47
|
|
|
48
48
|
// Example 6: Combining multiple ranges
|
|
49
49
|
const range6a = new PyRange(0, 3);
|
|
50
50
|
const range6b = new PyRange(5, 8);
|
|
51
51
|
const combined = [...range6a, ...range6b];
|
|
52
|
-
console.log(
|
|
52
|
+
console.log("Combined ranges:", combined); // [0, 1, 2, 5, 6, 7]
|
|
53
53
|
|
|
54
54
|
// Example 7: Using PyRange for iteration
|
|
55
|
-
console.log(
|
|
55
|
+
console.log("\nIteration example:");
|
|
56
56
|
for (const num of new PyRange(3)) {
|
|
57
57
|
console.log(`Current number: ${num}`);
|
|
58
58
|
}
|
|
@@ -1,64 +1,64 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Array method examples for range-pie
|
|
3
|
-
*
|
|
3
|
+
*
|
|
4
4
|
* This file demonstrates how to use the array-like methods provided by PyRange
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
|
-
const { PyRange } = require(
|
|
7
|
+
const { PyRange } = require("range-pie");
|
|
8
8
|
|
|
9
|
-
console.log(
|
|
9
|
+
console.log("\n--- Array Method Examples ---");
|
|
10
10
|
|
|
11
11
|
// Create a range to work with
|
|
12
12
|
const range = new PyRange(1, 10);
|
|
13
|
-
console.log(
|
|
13
|
+
console.log("Original range:", [...range]); // [1, 2, 3, 4, 5, 6, 7, 8, 9]
|
|
14
14
|
|
|
15
15
|
// Map: Double each value
|
|
16
|
-
const doubled = range.map(x => x * 2);
|
|
17
|
-
console.log(
|
|
16
|
+
const doubled = range.map((x) => x * 2);
|
|
17
|
+
console.log("map(x => x * 2):", doubled); // [2, 4, 6, 8, 10, 12, 14, 16, 18]
|
|
18
18
|
|
|
19
19
|
// Filter: Get only even numbers
|
|
20
|
-
const evens = range.filter(x => x % 2 === 0);
|
|
21
|
-
console.log(
|
|
20
|
+
const evens = range.filter((x) => x % 2 === 0);
|
|
21
|
+
console.log("filter(x => x % 2 === 0):", evens); // [2, 4, 6, 8]
|
|
22
22
|
|
|
23
23
|
// Reduce: Sum all values
|
|
24
24
|
const sum = range.reduce((acc, curr) => acc + curr, 0);
|
|
25
|
-
console.log(
|
|
25
|
+
console.log("reduce((acc, curr) => acc + curr, 0):", sum); // 45
|
|
26
26
|
|
|
27
27
|
// Some: Check if any value meets a condition
|
|
28
|
-
const hasEven = range.some(x => x % 2 === 0);
|
|
29
|
-
console.log(
|
|
28
|
+
const hasEven = range.some((x) => x % 2 === 0);
|
|
29
|
+
console.log("some(x => x % 2 === 0):", hasEven); // true
|
|
30
30
|
|
|
31
31
|
// Every: Check if all values meet a condition
|
|
32
|
-
const allPositive = range.every(x => x > 0);
|
|
33
|
-
console.log(
|
|
32
|
+
const allPositive = range.every((x) => x > 0);
|
|
33
|
+
console.log("every(x => x > 0):", allPositive); // true
|
|
34
34
|
|
|
35
35
|
// Find: Get the first value that meets a condition
|
|
36
|
-
const firstEven = range.find(x => x % 2 === 0);
|
|
37
|
-
console.log(
|
|
36
|
+
const firstEven = range.find((x) => x % 2 === 0);
|
|
37
|
+
console.log("find(x => x % 2 === 0):", firstEven); // 2
|
|
38
38
|
|
|
39
39
|
// FindIndex: Get the index of the first value that meets a condition
|
|
40
|
-
const firstEvenIndex = range.findIndex(x => x % 2 === 0);
|
|
41
|
-
console.log(
|
|
40
|
+
const firstEvenIndex = range.findIndex((x) => x % 2 === 0);
|
|
41
|
+
console.log("findIndex(x => x % 2 === 0):", firstEvenIndex); // 1
|
|
42
42
|
|
|
43
43
|
// FindLastIndex: Get the index of the last value that meets a condition
|
|
44
|
-
const lastEvenIndex = range.findLastIndex(x => x % 2 === 0);
|
|
45
|
-
console.log(
|
|
44
|
+
const lastEvenIndex = range.findLastIndex((x) => x % 2 === 0);
|
|
45
|
+
console.log("findLastIndex(x => x % 2 === 0):", lastEvenIndex); // 7
|
|
46
46
|
|
|
47
47
|
// Includes: Check if a value exists in the range
|
|
48
48
|
const includesFive = range.includes(5);
|
|
49
|
-
console.log(
|
|
49
|
+
console.log("includes(5):", includesFive); // true
|
|
50
50
|
|
|
51
51
|
// IndexOf: Get the index of a value
|
|
52
52
|
const indexOfFive = range.indexOf(5);
|
|
53
|
-
console.log(
|
|
53
|
+
console.log("indexOf(5):", indexOfFive); // 4
|
|
54
54
|
|
|
55
55
|
// LastIndexOf: Get the last index of a value
|
|
56
56
|
const lastIndexOfFive = range.lastIndexOf(5);
|
|
57
|
-
console.log(
|
|
57
|
+
console.log("lastIndexOf(5):", lastIndexOfFive); // 4
|
|
58
58
|
|
|
59
59
|
// ForEach: Execute a function for each value
|
|
60
|
-
console.log(
|
|
61
|
-
range.forEach(x => {
|
|
60
|
+
console.log("\nforEach example:");
|
|
61
|
+
range.forEach((x) => {
|
|
62
62
|
if (x % 3 === 0) {
|
|
63
63
|
console.log(`${x} is divisible by 3`);
|
|
64
64
|
}
|
package/examples/basic-usage.js
CHANGED
|
@@ -1,42 +1,42 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Basic usage examples for range-pie
|
|
3
|
-
*
|
|
3
|
+
*
|
|
4
4
|
* This file demonstrates the fundamental ways to use the PyRange class
|
|
5
5
|
* with JavaScript.
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
8
|
// CommonJS import
|
|
9
|
-
const { PyRange } = require(
|
|
9
|
+
const { PyRange } = require("range-pie");
|
|
10
10
|
|
|
11
|
-
console.log(
|
|
11
|
+
console.log("\n--- Basic PyRange Examples ---");
|
|
12
12
|
|
|
13
13
|
// Example 1: Simple range with just stop value
|
|
14
14
|
const range1 = new PyRange(5);
|
|
15
|
-
console.log(
|
|
15
|
+
console.log("PyRange(5):", [...range1]); // [0, 1, 2, 3, 4]
|
|
16
16
|
|
|
17
17
|
// Example 2: Range with start and stop
|
|
18
18
|
const range2 = new PyRange(2, 8);
|
|
19
|
-
console.log(
|
|
19
|
+
console.log("PyRange(2, 8):", [...range2]); // [2, 3, 4, 5, 6, 7]
|
|
20
20
|
|
|
21
21
|
// Example 3: Range with start, stop, and step
|
|
22
22
|
const range3 = new PyRange(1, 10, 2);
|
|
23
|
-
console.log(
|
|
23
|
+
console.log("PyRange(1, 10, 2):", [...range3]); // [1, 3, 5, 7, 9]
|
|
24
24
|
|
|
25
25
|
// Example 4: Negative step
|
|
26
26
|
const range4 = new PyRange(10, 0, -2);
|
|
27
|
-
console.log(
|
|
27
|
+
console.log("PyRange(10, 0, -2):", [...range4]); // [10, 8, 6, 4, 2]
|
|
28
28
|
|
|
29
|
-
console.log(
|
|
30
|
-
console.log(
|
|
31
|
-
console.log(
|
|
32
|
-
console.log(
|
|
33
|
-
console.log(
|
|
29
|
+
console.log("\n--- Accessing Properties ---");
|
|
30
|
+
console.log("range3.start:", range3.start); // 1
|
|
31
|
+
console.log("range3.stop:", range3.stop); // 10
|
|
32
|
+
console.log("range3.step:", range3.step); // 2
|
|
33
|
+
console.log("range3.length:", range3.length); // 5
|
|
34
34
|
|
|
35
|
-
console.log(
|
|
36
|
-
console.log(
|
|
37
|
-
console.log(
|
|
35
|
+
console.log("\n--- Using at() Method ---");
|
|
36
|
+
console.log("range3.at(0):", range3.at(0)); // 1
|
|
37
|
+
console.log("range3.at(2):", range3.at(2)); // 5
|
|
38
38
|
// console.log(range3.at(10)); // Would throw RangeError: Index out of range
|
|
39
39
|
|
|
40
|
-
console.log(
|
|
40
|
+
console.log("\n--- Converting to Array ---");
|
|
41
41
|
const array = range3.toArray();
|
|
42
|
-
console.log(
|
|
42
|
+
console.log("range3.toArray():", array); // [1, 3, 5, 7, 9]
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "range-pie",
|
|
3
|
-
"version": "2.0
|
|
3
|
+
"version": "2.1.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
|
"types": "dist/index.d.ts",
|
|
@@ -21,8 +21,8 @@
|
|
|
21
21
|
"prepublishOnly": "npm run build:prod",
|
|
22
22
|
"test": "jest",
|
|
23
23
|
"clean": "rimraf dist",
|
|
24
|
-
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\" \"examples/**/*.ts\"",
|
|
25
|
-
"check-format": "prettier --check \"src/**/*.ts\" \"test/**/*.ts\" \"examples/**/*.ts\"",
|
|
24
|
+
"format": "prettier --write \"src/**/*.{ts,js}\" \"test/**/*.{ts,js}\" \"examples/**/*.{ts,js}\" \"types/**/*.{ts,js,json}\" \"*.{js,json}\"",
|
|
25
|
+
"check-format": "prettier --check \"src/**/*.{ts,js}\" \"test/**/*.{ts,js}\" \"examples/**/*.{ts,js}\" \"types/**/*.{ts,js,json}\" \"*.{js,json}\"",
|
|
26
26
|
"example:basic": "node examples/basic-usage.js",
|
|
27
27
|
"example:array": "node examples/array-methods.js",
|
|
28
28
|
"example:advanced": "node examples/advanced-usage.js",
|