Skip to content
Open
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: 7 additions & 4 deletions src/Sequence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ import {Reverse} from "./reverse";
import {Single} from "./single";
import {SingleOrNull} from "./singleOrNull";
import {Sorted} from "./sorted";
import {SortedBy} from "./sortedBy";
import {SortedByDescending} from "./sortedByDescending";
import {SortedBy, SortedByOperator} from "./sortedBy";
import {SortedByDescending, SortedByDescendingOperator} from "./sortedByDescending";
import {SortedDescending} from "./sortedDescending";
import {SortedWith} from "./sortedWith";
import {Sum} from "./sum";
Expand Down Expand Up @@ -89,8 +89,8 @@ export default Sequence;
export interface SequenceOperators<T> extends All, Any, AsIterable, Associate, AssociateBy<T>, Average, Chunk, Contains, Count, Distinct, DistinctBy, Drop,
DropWhile, ElementAt, ElementAtOrElse, ElementAtOrNull, Filter, FilterIndexed, FilterNot, FilterNotNull, First, FirstOrNull, FlatMap, Flatten, Fold, FoldIndexed,
ForEach, ForEachIndexed, GroupBy, IndexOf, IndexOfFirst, IndexOfLast, JoinToString, Last, LastOrNull, Map, MapIndexed, MapNotNull, Max, MaxBy, MaxWith, Merge, Min, MinBy,
Minus, MinWith, None, OnEach, Partition, Plus, Reduce, ReduceIndexed, Reverse, Single, SingleOrNull, Sorted, SortedBy, SortedByDescending, SortedDescending, SortedWith,
Sum, SumBy, Take, TakeWhile, ToArray, ToMap, ToSet, Unzip, WithIndex, Zip {
Minus, MinWith, None, OnEach, Partition, Plus, Reduce, ReduceIndexed, Reverse, Single, SingleOrNull, Sorted, SortedByOperator, SortedByDescendingOperator, SortedDescending,
SortedWith, Sum, SumBy, Take, TakeWhile, ToArray, ToMap, ToSet, Unzip, WithIndex, Zip {
}

class SequenceImpl<T> {
Expand Down Expand Up @@ -174,3 +174,6 @@ export function range(start: number, endInclusive: number, step: number = 1): Se
}
});
}

export { default as Comparator } from "./Comparator";
export { default as ComparatorFactory } from "./ComparatorFactory";
7 changes: 7 additions & 0 deletions src/asSelector.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export function asSelector<T>(
keyOrSelector: keyof T | ((item: T) => any)
) {
return typeof keyOrSelector === "function"
? keyOrSelector
: (item: T) => item[keyOrSelector];
}
7 changes: 1 addition & 6 deletions src/createComparatorFactory.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { asSelector } from "./asSelector";
import ComparatorFactory from "./ComparatorFactory";
import Comparator from "./Comparator";

Expand Down Expand Up @@ -55,12 +56,6 @@ function compareByDescending<T>(keyOrSelector: any): Comparator<T> {
);
}

function asSelector<T>(keyOrSelector: (item: T) => any | string): (item: T) => any {
return typeof keyOrSelector === "function"
? keyOrSelector
: (item: T) => (item as any)[keyOrSelector as string];
}

function naturalCompare<T>(a: T, b: T): number {
return a < b ? -1 : a > b ? 1 : 0;
}
Expand Down
19 changes: 17 additions & 2 deletions src/sortedBy.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { asSelector } from "./asSelector";
import Sequence from "./Sequence";

export class SortedBy {
export interface SortedByOperator {

/**
* Returns a new sequence with all elements sorted ascending by the value specified
Expand All @@ -9,7 +10,21 @@ 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 in ascending order of values for the given `key`.
*
* @param {keyof T} key
* @returns {Sequence<T>}
*/
sortedBy<T>(this: Sequence<T>, key: keyof T): Sequence<T>;
}

export class SortedBy implements SortedByOperator {

sortedBy<T>(this:Sequence<T> , keyOrSelector: keyof T | ((value:T) => any)): Sequence<T> {
const selector = asSelector(keyOrSelector);
return this.sorted(it => it.compareBy(selector));
}

Expand Down
20 changes: 17 additions & 3 deletions src/sortedByDescending.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,29 @@
import { asSelector } from "./asSelector";
import Sequence from "./Sequence";

export class SortedByDescending {

export interface SortedByDescendingOperator {
/**
* Returns a new sequence with all elements sorted descending by the value specified
* by the given `selector` function.
*
* @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 in descending order of values for the given `key`.
*
* @param {keyof T} key
* @returns {Sequence<T>}
*/
sortedByDescending<T>(this: Sequence<T>, key: keyof T): Sequence<T>;
}

export class SortedByDescending implements SortedByDescendingOperator {

sortedByDescending<T>(this: Sequence<T>, keyOrSelector: keyof T | ((value: T) => any)): Sequence<T> {
const selector = asSelector(keyOrSelector);
return this.sorted(it => it.compareByDescending(selector));
}

Expand Down
18 changes: 17 additions & 1 deletion test/sortedBy.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {sequenceOf} from "../src/Sequence";

describe("sortedBy", () => {
it("should sort by the given key ascending", () => {
it("should sort in ascending order using a selector", () => {
const a4 = {a: 4};
const a1 = {a: 1};
const a3 = {a: 3};
Expand All @@ -17,4 +17,20 @@ describe("sortedBy", () => {
expect(array[2]).toBe(a4);
expect(array[3]).toBe(a23);
});
it("should sort in ascending order using a key", () => {
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.length).toBe(4);
expect(array[0]).toBe(a1);
expect(array[1]).toBe(a3);
expect(array[2]).toBe(a4);
expect(array[3]).toBe(a23);
});
});
20 changes: 18 additions & 2 deletions test/sortedByDescending.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {sequenceOf} from "../src/Sequence";

describe("sortedBy", () => {
it("should sort by the given key descending", () => {
describe("sortedByDescending", () => {
it("should sort in descending order using a selector", () => {
const a4 = {a: 4};
const a1 = {a: 1};
const a3 = {a: 3};
Expand All @@ -17,4 +17,20 @@ describe("sortedBy", () => {
expect(array[2]).toBe(a3);
expect(array[3]).toBe(a1);
});
it("should sort in descending order using a key", () => {
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.length).toBe(4);
expect(array[0]).toBe(a23);
expect(array[1]).toBe(a4);
expect(array[2]).toBe(a3);
expect(array[3]).toBe(a1);
});
});