range-pie 1.0.1 → 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.
- package/LICENSE +1 -1
- package/README.md +93 -1
- package/dist/index.d.ts +3 -0
- package/dist/index.js +40 -0
- package/dist/index.js.map +1 -0
- package/dist/py-range.d.ts +185 -0
- package/dist/py-range.js +361 -0
- package/dist/py-range.js.map +1 -0
- package/examples/advanced-usage.js +61 -0
- package/examples/array-methods.js +68 -0
- package/examples/basic-usage.js +42 -0
- package/examples/typescript-usage.ts +61 -0
- package/package.json +34 -8
- package/src/index.js +0 -5
- package/src/py-range.js +0 -376
package/LICENSE
CHANGED
package/README.md
CHANGED
|
@@ -1,11 +1,15 @@
|
|
|
1
1
|
# range-pie
|
|
2
2
|
|
|
3
|
-
A 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.
|
|
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
5
|
## Table of Contents
|
|
6
6
|
|
|
7
7
|
- [Installation](#installation)
|
|
8
8
|
- [Basic Usage](#basic-usage)
|
|
9
|
+
|
|
10
|
+
- [JavaScript](#javascript)
|
|
11
|
+
- [TypeScript](#typescript)
|
|
12
|
+
|
|
9
13
|
- [API Reference](#api-reference)
|
|
10
14
|
|
|
11
15
|
- [Constructor Options](#constructor-options)
|
|
@@ -36,6 +40,9 @@ A JavaScript library that brings Python's range functionality to JavaScript, enh
|
|
|
36
40
|
|
|
37
41
|
- [Methods chaining](#methods-chaining)
|
|
38
42
|
- [Using as Array-like Object](#using-as-array-like-object)
|
|
43
|
+
- [Example Files](#example-files)
|
|
44
|
+
- [Running the Examples](#running-the-examples)
|
|
45
|
+
- [Available Examples](#available-examples)
|
|
39
46
|
|
|
40
47
|
## Installation
|
|
41
48
|
|
|
@@ -45,7 +52,10 @@ npm install range-pie
|
|
|
45
52
|
|
|
46
53
|
## Basic Usage
|
|
47
54
|
|
|
55
|
+
### JavaScript
|
|
56
|
+
|
|
48
57
|
```javascript
|
|
58
|
+
// CommonJS
|
|
49
59
|
const { PyRange } = require('range-pie');
|
|
50
60
|
|
|
51
61
|
// Create a range from 0 to 5
|
|
@@ -61,6 +71,25 @@ const range3 = new PyRange(0, 10, 2);
|
|
|
61
71
|
console.log([...range3]); // [0, 2, 4, 6, 8]
|
|
62
72
|
```
|
|
63
73
|
|
|
74
|
+
### TypeScript
|
|
75
|
+
|
|
76
|
+
```typescript
|
|
77
|
+
// ES Modules
|
|
78
|
+
import { PyRange } from 'range-pie';
|
|
79
|
+
|
|
80
|
+
// Create a range from 0 to 5
|
|
81
|
+
const range = new PyRange(5);
|
|
82
|
+
console.log([...range]); // [0, 1, 2, 3, 4]
|
|
83
|
+
|
|
84
|
+
// Type-safe operations
|
|
85
|
+
const doubledValues: number[] = range.map(x => x * 2);
|
|
86
|
+
console.log(doubledValues); // [0, 2, 4, 6, 8]
|
|
87
|
+
|
|
88
|
+
// Type inference works with generics
|
|
89
|
+
const stringValues: string[] = range.map(x => `Value: ${x}`);
|
|
90
|
+
console.log(stringValues); // ['Value: 0', 'Value: 1', 'Value: 2', 'Value: 3', 'Value: 4']
|
|
91
|
+
```
|
|
92
|
+
|
|
64
93
|
## API Reference
|
|
65
94
|
|
|
66
95
|
### Constructor Options
|
|
@@ -304,6 +333,29 @@ console.log(proxy[0]); // 0
|
|
|
304
333
|
console.log(proxy[3]); // 3
|
|
305
334
|
```
|
|
306
335
|
|
|
336
|
+
### TypeScript Support
|
|
337
|
+
|
|
338
|
+
This package is written in TypeScript and provides full type definitions for all methods and properties. TypeScript users get the following benefits:
|
|
339
|
+
|
|
340
|
+
- Type checking for all method parameters and return values
|
|
341
|
+
- Autocompletion in IDEs
|
|
342
|
+
- Generic type support for methods like `map()` and `reduce()`
|
|
343
|
+
- Better documentation through type annotations
|
|
344
|
+
|
|
345
|
+
```typescript
|
|
346
|
+
// Type inference with generics
|
|
347
|
+
const range = new PyRange(5);
|
|
348
|
+
|
|
349
|
+
// TypeScript knows this is a number[]
|
|
350
|
+
const numbers = range.map(x => x * 2);
|
|
351
|
+
|
|
352
|
+
// TypeScript knows this is a string[]
|
|
353
|
+
const strings = range.map(x => `Number: ${x}`);
|
|
354
|
+
|
|
355
|
+
// TypeScript knows this is a boolean[]
|
|
356
|
+
const booleans = range.map(x => x % 2 === 0);
|
|
357
|
+
```
|
|
358
|
+
|
|
307
359
|
## Examples
|
|
308
360
|
|
|
309
361
|
### Methods chaining
|
|
@@ -323,3 +375,43 @@ const range = new PyRange(5).asProxy();
|
|
|
323
375
|
const firstThree = [range[0], range[1], range[2]];
|
|
324
376
|
console.log(firstThree); // [0, 1, 2]
|
|
325
377
|
```
|
|
378
|
+
|
|
379
|
+
### Example Files
|
|
380
|
+
|
|
381
|
+
This package includes several example files to help you get started. You can find them in the `examples` directory after installing the package.
|
|
382
|
+
|
|
383
|
+
#### Running the Examples
|
|
384
|
+
|
|
385
|
+
If you've cloned the repository, you can run the examples using npm scripts:
|
|
386
|
+
|
|
387
|
+
```bash
|
|
388
|
+
# Run basic usage examples
|
|
389
|
+
npm run example:basic
|
|
390
|
+
|
|
391
|
+
# Run array methods examples
|
|
392
|
+
npm run example:array
|
|
393
|
+
|
|
394
|
+
# Run advanced usage examples
|
|
395
|
+
npm run example:advanced
|
|
396
|
+
|
|
397
|
+
# Run TypeScript examples (requires ts-node)
|
|
398
|
+
npm run example:ts
|
|
399
|
+
```
|
|
400
|
+
|
|
401
|
+
Or you can run them directly with Node.js:
|
|
402
|
+
|
|
403
|
+
```bash
|
|
404
|
+
node node_modules/range-pie/examples/basic-usage.js
|
|
405
|
+
node node_modules/range-pie/examples/array-methods.js
|
|
406
|
+
node node_modules/range-pie/examples/advanced-usage.js
|
|
407
|
+
|
|
408
|
+
# For TypeScript examples
|
|
409
|
+
ts-node node_modules/range-pie/examples/typescript-usage.ts
|
|
410
|
+
```
|
|
411
|
+
|
|
412
|
+
#### Available Examples
|
|
413
|
+
|
|
414
|
+
- **basic-usage.js**: Shows fundamental PyRange operations
|
|
415
|
+
- **array-methods.js**: Demonstrates all array-like methods
|
|
416
|
+
- **advanced-usage.js**: Covers advanced features like method chaining and proxy usage
|
|
417
|
+
- **typescript-usage.ts**: Shows TypeScript-specific features and type safety
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.PyRange = void 0;
|
|
37
|
+
const py_range_1 = __importStar(require("./py-range"));
|
|
38
|
+
Object.defineProperty(exports, "PyRange", { enumerable: true, get: function () { return py_range_1.PyRange; } });
|
|
39
|
+
exports.default = py_range_1.default;
|
|
40
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,uDAA8D;AAGrC,wFAHI,kBAAY,OAGT;AAChC,kBAAe,kBAAO,CAAC"}
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A class that simulates Python's range function, combined with several useful JavaScript array methods.
|
|
3
|
+
*/
|
|
4
|
+
declare class PyRange implements Iterable<number> {
|
|
5
|
+
private _start;
|
|
6
|
+
private _stop;
|
|
7
|
+
private _step;
|
|
8
|
+
private _length;
|
|
9
|
+
/**
|
|
10
|
+
* Creates a new PyRange instance.
|
|
11
|
+
* @param {...number} args - The arguments of the range. The possible forms are
|
|
12
|
+
* - `PyRange(stop)`
|
|
13
|
+
* - `PyRange(start, stop)`
|
|
14
|
+
* - `PyRange(start, stop, step)`
|
|
15
|
+
* @throws {TypeError} If any of the arguments is not a number.
|
|
16
|
+
* @throws {TypeError} If any of the arguments is not an integer.
|
|
17
|
+
* @throws {Error} If the step is zero.
|
|
18
|
+
* @throws {Error} If the arguments count is not between 1 and 3.
|
|
19
|
+
* @property {number} start - The start of the range, inclusive.
|
|
20
|
+
* @property {number} stop - The stop of the range, exclusive.
|
|
21
|
+
* @property {number} step - The step of the range.
|
|
22
|
+
* @property {number} length - The length of the range.
|
|
23
|
+
*/
|
|
24
|
+
constructor(...args: number[]);
|
|
25
|
+
/**
|
|
26
|
+
* Gets the length of this range.
|
|
27
|
+
*
|
|
28
|
+
* @returns {number} the length of this range
|
|
29
|
+
* @readonly
|
|
30
|
+
*/
|
|
31
|
+
get length(): number;
|
|
32
|
+
/**
|
|
33
|
+
* Gets the starting value of the range.
|
|
34
|
+
*
|
|
35
|
+
* @returns {number} The starting value of the range.
|
|
36
|
+
* @readonly
|
|
37
|
+
*/
|
|
38
|
+
get start(): number;
|
|
39
|
+
/**
|
|
40
|
+
* Gets the ending value of the range.
|
|
41
|
+
*
|
|
42
|
+
* @returns {number} The ending value of the range.
|
|
43
|
+
* @readonly
|
|
44
|
+
*/
|
|
45
|
+
get stop(): number;
|
|
46
|
+
/**
|
|
47
|
+
* Gets the step value of the range.
|
|
48
|
+
*
|
|
49
|
+
* @returns {number} The step value of the range.
|
|
50
|
+
* @readonly
|
|
51
|
+
*/
|
|
52
|
+
get step(): number;
|
|
53
|
+
/**
|
|
54
|
+
* Gets the value at the specified index in this range.
|
|
55
|
+
* @param {number} index - The index of the value to retrieve
|
|
56
|
+
* @returns {number} The value at the specified index
|
|
57
|
+
* @throws {RangeError} If the index is out of range
|
|
58
|
+
*/
|
|
59
|
+
at(index: number): number;
|
|
60
|
+
/**
|
|
61
|
+
* Converts the range to a string.
|
|
62
|
+
* @returns {string} A string of the form `Range(start, stop, step)`.
|
|
63
|
+
*/
|
|
64
|
+
toString(): string;
|
|
65
|
+
/**
|
|
66
|
+
* Converts the range to an array.
|
|
67
|
+
* @returns {number[]} An array of numbers with the same elements as this range.
|
|
68
|
+
*/
|
|
69
|
+
toArray(): number[];
|
|
70
|
+
/**
|
|
71
|
+
* Validates that the callback is a function.
|
|
72
|
+
* @param {Function} cb - The callback to validate.
|
|
73
|
+
* @throws {TypeError} If the callback is not a function.
|
|
74
|
+
*/
|
|
75
|
+
private static validateCb;
|
|
76
|
+
/**
|
|
77
|
+
* Creates a new array with the results of applying the given callback
|
|
78
|
+
* function to every element in this range.
|
|
79
|
+
* @param {function(number, number, PyRange): T} callback - The callback
|
|
80
|
+
* function to apply to every element.
|
|
81
|
+
* @returns {T[]} A new array of the same length as this range.
|
|
82
|
+
* @template T
|
|
83
|
+
*/
|
|
84
|
+
map<T>(callback: (value: number, index: number, range: PyRange) => T): T[];
|
|
85
|
+
/**
|
|
86
|
+
* Creates a new array with all elements that pass the test implemented by
|
|
87
|
+
* the provided function.
|
|
88
|
+
* @param {function(number, number, PyRange): boolean} callback - The
|
|
89
|
+
* predicate function to apply to every element
|
|
90
|
+
* @returns {number[]} A new array of elements that pass the test
|
|
91
|
+
*/
|
|
92
|
+
filter(callback: (value: number, index: number, range: PyRange) => boolean): number[];
|
|
93
|
+
/**
|
|
94
|
+
* Reduces the range to a single value.
|
|
95
|
+
* @param {function(T, number, number, PyRange): T} callback - The callback
|
|
96
|
+
* function to apply to every element. The callback should take four
|
|
97
|
+
* arguments: the accumulator, the current value, the index of the current
|
|
98
|
+
* value, and the range object.
|
|
99
|
+
* @param {T} [initialValue] - The initial value of the accumulator.
|
|
100
|
+
* @returns {T} The final value of the accumulator.
|
|
101
|
+
* @template T
|
|
102
|
+
*/
|
|
103
|
+
reduce<T>(callback: (accumulator: T, value: number, index: number, range: PyRange) => T, initialValue?: T): T;
|
|
104
|
+
/**
|
|
105
|
+
* Determines whether at least one element of the range satisfies the
|
|
106
|
+
* provided test.
|
|
107
|
+
* @param {function(number, number, PyRange): boolean} callback - The
|
|
108
|
+
* predicate function to apply to every element
|
|
109
|
+
* @returns {boolean} True if at least one element of the range passes the
|
|
110
|
+
* test, false otherwise.
|
|
111
|
+
*/
|
|
112
|
+
some(callback: (value: number, index: number, range: PyRange) => boolean): boolean;
|
|
113
|
+
/**
|
|
114
|
+
* Determines whether all elements of the range satisfy the provided test.
|
|
115
|
+
* @param {function(number, number, PyRange): boolean} callback - The
|
|
116
|
+
* predicate function to apply to every element
|
|
117
|
+
* @returns {boolean} True if all elements of the range pass the test,
|
|
118
|
+
* false otherwise.
|
|
119
|
+
*/
|
|
120
|
+
every(callback: (value: number, index: number, range: PyRange) => boolean): boolean;
|
|
121
|
+
/**
|
|
122
|
+
* Finds the first element in this range that satisfies the provided test.
|
|
123
|
+
* @param {function(number, number, PyRange): boolean} callback - The
|
|
124
|
+
* predicate function to apply to every element
|
|
125
|
+
* @returns {number|undefined} The first element that passes the test,
|
|
126
|
+
* or undefined if no element passes the test.
|
|
127
|
+
*/
|
|
128
|
+
find(callback: (value: number, index: number, range: PyRange) => boolean): number | undefined;
|
|
129
|
+
/**
|
|
130
|
+
* Finds the index of the first element in this range that satisfies the provided test.
|
|
131
|
+
* @param {function(number, number, PyRange): boolean} callback - The predicate function to apply to each element.
|
|
132
|
+
* @returns {number} The index of the first element that passes the test, or -1 if no element passes the test.
|
|
133
|
+
*/
|
|
134
|
+
findIndex(callback: (value: number, index: number, range: PyRange) => boolean): number;
|
|
135
|
+
/**
|
|
136
|
+
* Finds the index of the last element in this range that satisfies the provided test.
|
|
137
|
+
* @param {function(number, number, PyRange): boolean} callback - The predicate function to apply to each element.
|
|
138
|
+
* @returns {number} The index of the last element that passes the test, or -1 if no element passes the test.
|
|
139
|
+
*/
|
|
140
|
+
findLastIndex(callback: (value: number, index: number, range: PyRange) => boolean): number;
|
|
141
|
+
/**
|
|
142
|
+
* Executes a provided function once for each element in this range.
|
|
143
|
+
* @param {function(number, number, PyRange): void} callback - The
|
|
144
|
+
* function to execute for each element.
|
|
145
|
+
*/
|
|
146
|
+
forEach(callback: (value: number, index: number, range: PyRange) => void): void;
|
|
147
|
+
/**
|
|
148
|
+
* Determines whether the given value is present in this range.
|
|
149
|
+
* @param {any} value - The value to search for.
|
|
150
|
+
* @returns {boolean} True if the value is present, false otherwise.
|
|
151
|
+
*/
|
|
152
|
+
includes(value: any): boolean;
|
|
153
|
+
/**
|
|
154
|
+
* Returns the index of the first occurrence of the specified value, or -1 if it is not present.
|
|
155
|
+
* @param {any} value - The value to search for.
|
|
156
|
+
* @returns {number} The index of the value, or -1 if it is not present.
|
|
157
|
+
*/
|
|
158
|
+
indexOf(value: any): number;
|
|
159
|
+
/**
|
|
160
|
+
* Returns the index of the last occurrence of the specified value, or -1 if it is not present.
|
|
161
|
+
* @param {any} value - The value to search for.
|
|
162
|
+
* @returns {number} The index of the last occurrence of the value, or -1 if it is not present.
|
|
163
|
+
*/
|
|
164
|
+
lastIndexOf(value: any): number;
|
|
165
|
+
/**
|
|
166
|
+
* Reverses the order of the elements in this range, returning a new PyRange object.
|
|
167
|
+
* @returns {PyRange} A new PyRange object with the elements in reverse order.
|
|
168
|
+
*/
|
|
169
|
+
reverse(): PyRange;
|
|
170
|
+
/**
|
|
171
|
+
* Implements the iterable protocol for this range.
|
|
172
|
+
* @returns {Iterator<number>} An iterator for this range.
|
|
173
|
+
*/
|
|
174
|
+
[Symbol.iterator](): Iterator<number>;
|
|
175
|
+
/**
|
|
176
|
+
* Returns a Proxy for this range, allowing indexed access.
|
|
177
|
+
* This proxy enables accessing range elements via array-like indexing.
|
|
178
|
+
* If the property is a number, it will return the element at that index.
|
|
179
|
+
*
|
|
180
|
+
* @returns {any} A proxy for the PyRange instance.
|
|
181
|
+
*/
|
|
182
|
+
asProxy(): any;
|
|
183
|
+
}
|
|
184
|
+
export { PyRange };
|
|
185
|
+
export default PyRange;
|