Skip to content

Commit 915ce56

Browse files
committed
Add range() to generate sequences for a given number range
1 parent ac3dbb0 commit 915ce56

File tree

4 files changed

+52
-2
lines changed

4 files changed

+52
-2
lines changed

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,22 @@ Sequency is centered around a single class called `Sequence` to process any kind
3939
Sequences can be created by utilizing one of the following functions:
4040

4141
```js
42-
import {sequenceOf, asSequence, emptySequence, generateSequence} from 'sequency';
42+
import {
43+
asSequence,
44+
sequenceOf,
45+
emptySequence,
46+
range,
47+
generateSequence,
48+
extendSequence
49+
} from 'sequency';
4350
```
4451

4552
- `sequenceOf` accepts one or many values and returns a new sequence.
4653
- `asSequence` accepts an iterable (e.g. an array, set or map) and returns a new sequence.
4754
- `emptySequence` returns a new empty sequence.
55+
- `range` returns as number sequence consisting of all numbers between `startInclusive` and `endExclusive`.
4856
- `generateSequence` returns a sequence generated from the given generator function.
57+
- `extendSequence` allows extending sequences with user-defined operations (see [example](https://github.com/winterbe/sequency/blob/ac3dbb0f212bb08783d970472c7a76dc921b60ba/test/extendSequence.test.ts)).
4958

5059
Each `Sequence` provides a fluent functional API consisting of intermediate and terminal operations. Intermediate functions (e.g. `filter`, `map`, `sorted`) return a new sequence, thus enabling method chaining. Terminal functions (e.g. `toArray`, `groupBy`, `findLast`) return an arbitrary result. Detailed descriptions of all operations are available in the [API docs](https://winterbe.github.io/sequency/).
5160

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "sequency",
3-
"version": "0.15.0",
3+
"version": "0.16.0",
44
"description": "Functional sequences for processing iterable data in JavaScript",
55
"main": "lib/Sequence.js",
66
"umd:main": "lib-umd/sequency.js",

src/Sequence.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,3 +154,22 @@ export function generateSequence<T>(a: any, b?: any): Sequence<T> {
154154
? createSequence<T>(new GeneratorSeedIterator(seed, b))
155155
: emptySequence<T>();
156156
}
157+
158+
export function range(startInclusive: number, endExclusive: number, step: number = 1): Sequence<number> {
159+
if (startInclusive >= endExclusive) {
160+
throw new Error(`startInclusive [${startInclusive}] must be lower then endExclusive [${endExclusive}]`);
161+
}
162+
if (startInclusive === endExclusive - 1) {
163+
return emptySequence();
164+
}
165+
let current = startInclusive;
166+
return generateSequence(() => {
167+
try {
168+
return current < endExclusive
169+
? current
170+
: undefined;
171+
} finally {
172+
current += step;
173+
}
174+
});
175+
}

test/range.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import {range} from "../src/Sequence";
2+
3+
describe("range", () => {
4+
it("should create range of numbers with step = 1", () => {
5+
const numbers = range(0, 5).toArray();
6+
expect(numbers).toEqual([0, 1, 2, 3, 4]);
7+
});
8+
9+
it("should create range of numbers with step = .5", () => {
10+
const numbers = range(0, 4, .5).toArray();
11+
expect(numbers).toEqual([0, .5, 1, 1.5, 2, 2.5, 3, 3.5]);
12+
});
13+
14+
it("should create empty sequence", () => {
15+
const numbers = range(0, 1).toArray();
16+
expect(numbers).toEqual([]);
17+
});
18+
19+
it("should throw on invalid boundaries", () => {
20+
expect(() => range(1, 0)).toThrow();
21+
});
22+
});

0 commit comments

Comments
 (0)