Skip to content

Commit

Permalink
feat: add min and max
Browse files Browse the repository at this point in the history
  • Loading branch information
tomi committed Jan 19, 2022
1 parent 7a130e4 commit 8e9a579
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/Sequence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,38 @@ export class SequenceImpl<TItem> implements Iterable<TItem>, Sequence<TItem> {
return items.length === 0 ? undefined : items[items.length - 1];
}

max<TItem>(): TItem | undefined {
let result: any | undefined = undefined;

for (const item of this._iterable) {
if (result === undefined) {
result = item as any;
} else if (item > result) {
result = item;
}
}

return result;
}

map<TResultItem>(mapFn: MapFn<TItem, TResultItem>): Sequence<TResultItem> {
return this._sequenceFromGenerator<TResultItem>(map, [mapFn]);
}

min<TItem>(): TItem | undefined {
let result: any | undefined = undefined;

for (const item of this._iterable) {
if (result === undefined) {
result = item as any;
} else if (item < result) {
result = item;
}
}

return result;
}

pick<TKeys extends keyof TItem>(
...keys: TKeys[]
): Sequence<{ [P in TKeys]: TItem[P] }> {
Expand Down
20 changes: 20 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,16 @@ export interface Sequence<TItem> extends Iterable<TItem> {
* ```
*/
last(): TItem | undefined;
/**
* Returns the maximum value in the sequence. `undefined` is returned if the squence is empty.
*
* @example
* ```typescript
* // Returns 3
* from([1, 2, 3]).max();
* ```
*/
max<TItem>(): TItem | undefined;
/**
* Maps the sequence to a new sequence where each item is converted
* to a new value using the given mapper function.
Expand All @@ -295,6 +305,16 @@ export interface Sequence<TItem> extends Iterable<TItem> {
* ```
*/
map<TResultItem>(mapFn: MapFn<TItem, TResultItem>): Sequence<TResultItem>;
/**
* Returns the minimum value in the sequence. `undefined` is returned if the squence is empty.
*
* @example
* ```typescript
* // Returns 1
* from([1, 2, 3]).min();
* ```
*/
min<TItem>(): TItem | undefined;
/**
* Maps each item in the sequence to an object composed of the picked
* object properties.
Expand Down
30 changes: 30 additions & 0 deletions test/fromfrom.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,21 @@ describe("fromfrom", () => {
});
});

describe("max", () => {
it("returns the maximum value for numbers", () => {
expect(from([1, 2, 3]).max()).toEqual(3);
expect(from([3, 2, 1]).max()).toEqual(3);
});

it("returns maximum value for string", () => {
expect(from(["1", "2", "3"]).max()).toEqual("3");
});

it("returns undefined if sequence is empty", () => {
expect(from([]).max()).toBeUndefined();
});
});

describe("map", () => {
let mapper: jest.Mock;

Expand Down Expand Up @@ -486,6 +501,21 @@ describe("fromfrom", () => {
});
});

describe("min", () => {
it("returns the minimum value for numbers", () => {
expect(from([1, 2, 3]).min()).toEqual(1);
expect(from([3, 2, 1]).min()).toEqual(1);
});

it("returns minimum value for string", () => {
expect(from(["1", "2", "3"]).min()).toEqual("1");
});

it("returns undefined if sequence is empty", () => {
expect(from([]).min()).toBeUndefined();
});
});

describe("pick", () => {
const user = Object.freeze({
name: {
Expand Down

0 comments on commit 8e9a579

Please sign in to comment.