Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@

---

<p align="center">
<strong>★★★ Like this project? <a href="https://github.com/winterbe/sequency/stargazers">Leave a star</a> and <a href="https://twitter.com/winterbe_">follow on Twitter</a>! Thanks. ★★★</strong>
</p>
**★★★ Like this project? [Leave a star](https://github.com/winterbe/sequency/stargazers) and [follow on Twitter](https://twitter.com/winterbe_)! Thanks. ★★★**

## About Sequency

Expand All @@ -18,7 +16,6 @@ Sequency is a lightweight (**5 KB minified**), intensely tested (**200+ tests, 9

## Getting started


```bash
npm install --save sequency
```
Expand Down Expand Up @@ -61,19 +58,19 @@ Sequences are **lazily evaluated** to avoid examining all of the input data when

### Core Components

**Sequence<T>** — The central interface of the library:
**`Sequence<T>`** — The central interface of the library:

- Contains a single `iterator: Iterator<T>` property for lazy iteration
- Extends `SequenceOperators<T>`, which combines all operations
- Sequences can be iterated only once (single-pass)

**SequenceImpl<T>** — The concrete implementation using the **Mixin Pattern**:
**`SequenceImpl<T>`** — The concrete implementation using the **Mixin Pattern**:

- All operations are added dynamically via the `applyMixins()` function
- Each operation is implemented as a separate mixin class
- Enables modular code organization and easy addition of new operations

**AsyncSequence<T>** — Asynchronous version of the sequence:
**`AsyncSequence<T>`** — Asynchronous version of the sequence:

- Uses `AsyncIterator<T>` instead of `Iterator<T>`
- All operations return `Promise`
Expand Down
12 changes: 11 additions & 1 deletion src/operators/async/sortedBy.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {AsyncSequence} from "../../sequency";
import {asAsyncSelector} from "../../internal";

export class SortedBy {

Expand All @@ -9,7 +10,16 @@ export class SortedBy {
* @param {(value: T) => Promise<R> | R} selector
* @returns {AsyncSequence<T>}
*/
sortedBy<T, R>(this: AsyncSequence<T>, selector: (value: T) => Promise<R> | R): AsyncSequence<T> {
sortedBy<T, R>(this: AsyncSequence<T>, selector: (value: T) => Promise<R> | R): AsyncSequence<T>;
/**
* Returns a new sequence with all elements sorted ascending by the value of the given `key`.
*
* @param {keyof T} key
* @returns {AsyncSequence<T>}
*/
sortedBy<T>(this: AsyncSequence<T>, key: keyof NonNullable<T>): AsyncSequence<T>;
sortedBy<T, R>(this: AsyncSequence<T>, keyOrSelector: ((value: T) => Promise<R> | R) | keyof NonNullable<T>): AsyncSequence<T> {
const selector = asAsyncSelector(keyOrSelector);
return this.sorted(it => it.compareBy(selector));
}

Expand Down
12 changes: 11 additions & 1 deletion src/operators/async/sortedByDescending.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {AsyncSequence} from "../../sequency";
import {asAsyncSelector} from "../../internal";

export class SortedByDescending {

Expand All @@ -9,7 +10,16 @@ export class SortedByDescending {
* @param {(value: T) => Promise<R> | R} selector
* @returns {AsyncSequence<T>}
*/
sortedByDescending<T, R>(this: AsyncSequence<T>, selector: (value: T) => Promise<R> | R): AsyncSequence<T> {
sortedByDescending<T, R>(this: AsyncSequence<T>, selector: (value: T) => Promise<R> | R): AsyncSequence<T>;
/**
* Returns a new sequence with all elements sorted descending by the value of the given `key`.
*
* @param {keyof T} key
* @returns {AsyncSequence<T>}
*/
sortedByDescending<T>(this: AsyncSequence<T>, key: keyof NonNullable<T>): AsyncSequence<T>;
sortedByDescending<T, R>(this: AsyncSequence<T>, keyOrSelector: ((value: T) => Promise<R> | R) | keyof NonNullable<T>): AsyncSequence<T> {
const selector = asAsyncSelector(keyOrSelector);
return this.sorted(it => it.compareByDescending(selector));
}

Expand Down
12 changes: 11 additions & 1 deletion src/operators/sync/sortedBy.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {Sequence} from "../../sequency";
import {asSelector} from "../../internal";

export class SortedBy {

Expand All @@ -9,7 +10,16 @@ export class SortedBy {
* @param {(value: T) => R} selector
* @returns {Sequence<T>}
*/
sortedBy<T, R>(this: Sequence<T>, selector: (value: T) => R): Sequence<T> {
sortedBy<T, R>(this: Sequence<T>, selector: (value: T) => R): Sequence<T>;
/**
* Returns a new sequence with all elements sorted ascending by the value of the given `key`.
*
* @param {keyof T} key
* @returns {Sequence<T>}
*/
sortedBy<T>(this: Sequence<T>, key: keyof NonNullable<T>): Sequence<T>;
sortedBy<T, R>(this: Sequence<T>, keyOrSelector: ((value: T) => R) | keyof NonNullable<T>): Sequence<T> {
const selector = asSelector(keyOrSelector);
return this.sorted(it => it.compareBy(selector));
}

Expand Down
12 changes: 11 additions & 1 deletion src/operators/sync/sortedByDescending.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {Sequence} from "../../sequency";
import {asSelector} from "../../internal";

export class SortedByDescending {

Expand All @@ -9,7 +10,16 @@ export class SortedByDescending {
* @param {(value: T) => R} selector
* @returns {Sequence<T>}
*/
sortedByDescending<T, R>(this: Sequence<T>, selector: (value: T) => R): Sequence<T> {
sortedByDescending<T, R>(this: Sequence<T>, selector: (value: T) => R): Sequence<T>;
/**
* Returns a new sequence with all elements sorted descending by the value of the given `key`.
*
* @param {keyof T} key
* @returns {Sequence<T>}
*/
sortedByDescending<T>(this: Sequence<T>, key: keyof NonNullable<T>): Sequence<T>;
sortedByDescending<T, R>(this: Sequence<T>, keyOrSelector: ((value: T) => R) | keyof NonNullable<T>): Sequence<T> {
const selector = asSelector(keyOrSelector);
return this.sorted(it => it.compareByDescending(selector));
}

Expand Down
13 changes: 13 additions & 0 deletions test/operators/async/sortedBy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,17 @@ describe("sortedBy", () => {

expect(array).toEqual([a1, a3, a4, a23]);
});

it("should sort by the given key string ascending", async () => {
const a4 = {a: 4};
const a1 = {a: 1};
const a3 = {a: 3};
const a23 = {a: 23};

const array = await asyncSequenceOf(a4, a1, a3, a23)
.sortedBy("a")
.toArray();

expect(array).toEqual([a1, a3, a4, a23]);
});
});
15 changes: 14 additions & 1 deletion test/operators/async/sortedByDescending.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {asyncSequenceOf} from "../../../src/sequency";

describe("sortedBy", () => {
describe("sortedByDescending", () => {
it("should sort by the given key descending", async () => {
const a4 = {a: 4};
const a1 = {a: 1};
Expand All @@ -13,4 +13,17 @@ describe("sortedBy", () => {

expect(array).toEqual([a23, a4, a3, a1]);
});

it("should sort by the given key string descending", async () => {
const a4 = {a: 4};
const a1 = {a: 1};
const a3 = {a: 3};
const a23 = {a: 23};

const array = await asyncSequenceOf(a4, a1, a3, a23)
.sortedByDescending("a")
.toArray();

expect(array).toEqual([a23, a4, a3, a1]);
});
});
13 changes: 13 additions & 0 deletions test/operators/sync/sortedBy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,17 @@ describe("sortedBy", () => {

expect(array).toEqual([a1, a3, a4, a23]);
});

it("should sort by the given key string ascending", () => {
const a4 = {a: 4};
const a1 = {a: 1};
const a3 = {a: 3};
const a23 = {a: 23};

const array = sequenceOf(a4, a1, a3, a23)
.sortedBy("a")
.toArray();

expect(array).toEqual([a1, a3, a4, a23]);
});
});
15 changes: 14 additions & 1 deletion test/operators/sync/sortedByDescending.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {sequenceOf} from "../../../src/sequency";

describe("sortedBy", () => {
describe("sortedByDescending", () => {
it("should sort by the given key descending", () => {
const a4 = {a: 4};
const a1 = {a: 1};
Expand All @@ -13,4 +13,17 @@ describe("sortedBy", () => {

expect(array).toEqual([a23, a4, a3, a1]);
});

it("should sort by the given key string descending", () => {
const a4 = {a: 4};
const a1 = {a: 1};
const a3 = {a: 3};
const a23 = {a: 23};

const array = sequenceOf(a4, a1, a3, a23)
.sortedByDescending("a")
.toArray();

expect(array).toEqual([a23, a4, a3, a1]);
});
});