range-pie 2.0.0 → 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.
@@ -0,0 +1,61 @@
1
+ /**
2
+ * Advanced usage examples for range-pie
3
+ *
4
+ * This file demonstrates more advanced features of the PyRange class
5
+ */
6
+
7
+ const { PyRange } = require('range-pie');
8
+
9
+ console.log('\n--- Advanced PyRange Examples ---');
10
+
11
+ // Example 1: Using the reverse method
12
+ const range1 = new PyRange(1, 6);
13
+ console.log('Original range:', [...range1]); // [1, 2, 3, 4, 5]
14
+
15
+ const reversed = range1.reverse();
16
+ console.log('Reversed range:', [...reversed]); // [5, 4, 3, 2, 1]
17
+
18
+ // Example 2: Using the asProxy method for array-like access
19
+ const range2 = new PyRange(10).asProxy();
20
+ console.log('range2[0]:', range2[0]); // 0
21
+ console.log('range2[5]:', range2[5]); // 5
22
+
23
+ // Create an array using proxy access
24
+ const firstThree = [range2[0], range2[1], range2[2]];
25
+ console.log('firstThree:', firstThree); // [0, 1, 2]
26
+
27
+ // Example 3: Method chaining
28
+ const range3 = new PyRange(1, 10);
29
+ const result = range3
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
+
34
+ console.log('Chained methods result:', result); // [16, 36, 64]
35
+
36
+ // Example 4: Using PyRange with other array methods
37
+ const range4 = new PyRange(5);
38
+ const array = [...range4]; // Convert to array: [0, 1, 2, 3, 4]
39
+
40
+ // Now we can use native array methods
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
+
44
+ // Example 5: Creating a range with custom step
45
+ const range5 = new PyRange(0, 11, 2);
46
+ console.log('Range with step 2:', [...range5]); // [0, 2, 4, 6, 8, 10]
47
+
48
+ // Example 6: Combining multiple ranges
49
+ const range6a = new PyRange(0, 3);
50
+ const range6b = new PyRange(5, 8);
51
+ const combined = [...range6a, ...range6b];
52
+ console.log('Combined ranges:', combined); // [0, 1, 2, 5, 6, 7]
53
+
54
+ // Example 7: Using PyRange for iteration
55
+ console.log('\nIteration example:');
56
+ for (const num of new PyRange(3)) {
57
+ console.log(`Current number: ${num}`);
58
+ }
59
+ // Current number: 0
60
+ // Current number: 1
61
+ // Current number: 2
@@ -0,0 +1,68 @@
1
+ /**
2
+ * Array method examples for range-pie
3
+ *
4
+ * This file demonstrates how to use the array-like methods provided by PyRange
5
+ */
6
+
7
+ const { PyRange } = require('range-pie');
8
+
9
+ console.log('\n--- Array Method Examples ---');
10
+
11
+ // Create a range to work with
12
+ const range = new PyRange(1, 10);
13
+ console.log('Original range:', [...range]); // [1, 2, 3, 4, 5, 6, 7, 8, 9]
14
+
15
+ // Map: Double each value
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
+
19
+ // Filter: Get only even numbers
20
+ const evens = range.filter(x => x % 2 === 0);
21
+ console.log('filter(x => x % 2 === 0):', evens); // [2, 4, 6, 8]
22
+
23
+ // Reduce: Sum all values
24
+ const sum = range.reduce((acc, curr) => acc + curr, 0);
25
+ console.log('reduce((acc, curr) => acc + curr, 0):', sum); // 45
26
+
27
+ // Some: Check if any value meets a condition
28
+ const hasEven = range.some(x => x % 2 === 0);
29
+ console.log('some(x => x % 2 === 0):', hasEven); // true
30
+
31
+ // Every: Check if all values meet a condition
32
+ const allPositive = range.every(x => x > 0);
33
+ console.log('every(x => x > 0):', allPositive); // true
34
+
35
+ // Find: Get the first value that meets a condition
36
+ const firstEven = range.find(x => x % 2 === 0);
37
+ console.log('find(x => x % 2 === 0):', firstEven); // 2
38
+
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('findIndex(x => x % 2 === 0):', firstEvenIndex); // 1
42
+
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('findLastIndex(x => x % 2 === 0):', lastEvenIndex); // 7
46
+
47
+ // Includes: Check if a value exists in the range
48
+ const includesFive = range.includes(5);
49
+ console.log('includes(5):', includesFive); // true
50
+
51
+ // IndexOf: Get the index of a value
52
+ const indexOfFive = range.indexOf(5);
53
+ console.log('indexOf(5):', indexOfFive); // 4
54
+
55
+ // LastIndexOf: Get the last index of a value
56
+ const lastIndexOfFive = range.lastIndexOf(5);
57
+ console.log('lastIndexOf(5):', lastIndexOfFive); // 4
58
+
59
+ // ForEach: Execute a function for each value
60
+ console.log('\nforEach example:');
61
+ range.forEach(x => {
62
+ if (x % 3 === 0) {
63
+ console.log(`${x} is divisible by 3`);
64
+ }
65
+ });
66
+ // 3 is divisible by 3
67
+ // 6 is divisible by 3
68
+ // 9 is divisible by 3
@@ -0,0 +1,42 @@
1
+ /**
2
+ * Basic usage examples for range-pie
3
+ *
4
+ * This file demonstrates the fundamental ways to use the PyRange class
5
+ * with JavaScript.
6
+ */
7
+
8
+ // CommonJS import
9
+ const { PyRange } = require('range-pie');
10
+
11
+ console.log('\n--- Basic PyRange Examples ---');
12
+
13
+ // Example 1: Simple range with just stop value
14
+ const range1 = new PyRange(5);
15
+ console.log('PyRange(5):', [...range1]); // [0, 1, 2, 3, 4]
16
+
17
+ // Example 2: Range with start and stop
18
+ const range2 = new PyRange(2, 8);
19
+ console.log('PyRange(2, 8):', [...range2]); // [2, 3, 4, 5, 6, 7]
20
+
21
+ // Example 3: Range with start, stop, and step
22
+ const range3 = new PyRange(1, 10, 2);
23
+ console.log('PyRange(1, 10, 2):', [...range3]); // [1, 3, 5, 7, 9]
24
+
25
+ // Example 4: Negative step
26
+ const range4 = new PyRange(10, 0, -2);
27
+ console.log('PyRange(10, 0, -2):', [...range4]); // [10, 8, 6, 4, 2]
28
+
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
+
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
+ // console.log(range3.at(10)); // Would throw RangeError: Index out of range
39
+
40
+ console.log('\n--- Converting to Array ---');
41
+ const array = range3.toArray();
42
+ console.log('range3.toArray():', array); // [1, 3, 5, 7, 9]
@@ -0,0 +1,61 @@
1
+ /**
2
+ * TypeScript usage examples for range-pie
3
+ *
4
+ * This file demonstrates how to use the PyRange class with TypeScript,
5
+ * taking advantage of type safety and generics.
6
+ */
7
+
8
+ // ES Module import
9
+ import { PyRange } from "range-pie";
10
+
11
+ console.log("\n--- TypeScript PyRange Examples ---");
12
+
13
+ // Basic usage is the same as JavaScript
14
+ const range = new PyRange(1, 10);
15
+ console.log("Basic range:", [...range]); // [1, 2, 3, 4, 5, 6, 7, 8, 9]
16
+
17
+ // TypeScript provides type safety
18
+ // This would cause a compile-time error:
19
+ // const badRange = new PyRange("not a number");
20
+
21
+ // Type inference with generics
22
+ // TypeScript knows this returns number[]
23
+ const numbers = range.map((x) => x * 2);
24
+ console.log("Doubled numbers:", numbers); // [2, 4, 6, 8, 10, 12, 14, 16, 18]
25
+
26
+ // TypeScript knows this returns string[]
27
+ const strings = range.map((x) => `Number: ${x}`);
28
+ console.log("First string:", strings[0]); // "Number: 1"
29
+
30
+ // TypeScript knows this returns boolean[]
31
+ const booleans = range.map((x) => x % 2 === 0);
32
+ console.log("Is even:", booleans); // [false, true, false, true, false, true, false, true, false]
33
+
34
+ // Type safety with callbacks
35
+ range.forEach((value: number, index: number) => {
36
+ console.log(`Value at index ${index}: ${value}`);
37
+ });
38
+
39
+ // Using reduce with proper typing
40
+ const sum: number = range.reduce((acc, curr) => acc + curr, 0);
41
+ console.log("Sum:", sum); // 45
42
+
43
+ // Creating a custom object with map
44
+ interface NumberInfo {
45
+ value: number;
46
+ isEven: boolean;
47
+ square: number;
48
+ }
49
+
50
+ const infoArray: NumberInfo[] = range.map((x) => ({
51
+ value: x,
52
+ isEven: x % 2 === 0,
53
+ square: x * x,
54
+ }));
55
+
56
+ console.log("First object:", infoArray[0]); // { value: 1, isEven: false, square: 1 }
57
+
58
+ // Chaining methods with type safety
59
+ const evenSquares: number[] = range.filter((x) => x % 2 === 0).map((x) => x * x);
60
+
61
+ console.log("Even squares:", evenSquares); // [4, 16, 36, 64]
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "range-pie",
3
- "version": "2.0.0",
3
+ "version": "2.0.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
  "types": "dist/index.d.ts",
@@ -12,7 +12,8 @@
12
12
  }
13
13
  },
14
14
  "files": [
15
- "dist"
15
+ "dist",
16
+ "examples"
16
17
  ],
17
18
  "scripts": {
18
19
  "build": "tsc",
@@ -41,7 +42,7 @@
41
42
  "license": "MIT",
42
43
  "repository": {
43
44
  "type": "git",
44
- "url": "https://github.com/SkorpionG/range-pie.git"
45
+ "url": "git+https://github.com/SkorpionG/range-pie.git"
45
46
  },
46
47
  "devDependencies": {
47
48
  "@types/jest": "^29.5.0",