Skip to content

Commit

Permalink
Merge d8259f3 into 6c4a8ee
Browse files Browse the repository at this point in the history
  • Loading branch information
sea1jxr committed Apr 23, 2017
2 parents 6c4a8ee + d8259f3 commit 488c19f
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ const setOf4LetterWords = new Set(genSequence(setOfWords).filter(a => a.length =
- `.any()` -- true if any value in the sequence exists where *fn(value)* returns true.
- `.first()` -- return the next value in the sequence.
- `.first(fn)` -- return the next value in the sequence where *fn(value)* return true.
- `.max()` -- return the largest value in the sequence.
- `.max(fn)` -- return the largest value of *fn(value)* in the sequence.

### Misc
- `toIterable()` -- Casts a Sequence into an IterableIterator - used in cases where type checking is too strict.
95 changes: 95 additions & 0 deletions src/GenSequence.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,4 +297,99 @@ describe('GenSequence Tests', function() {
});
expect(count).to.be.equal(3);
});

it('test max on single value', () => {
const values = [2];
expect(genSequence(values).max()).to.equal(2);
});

it('test max returns max on start', () => {
const values = [4, 3, 2, 1];
expect(genSequence(values).max()).to.equal(4);
});

it('test max returns max in middle', () => {
const values = [1, 3, 1];
expect(genSequence(values).max()).to.equal(3);
});

it('test max returns max on end', () => {
const values = [1, 2, 3, 4];
expect(genSequence(values).max()).to.equal(4);
});

it('test max on empty set returns undefined', () => {
const values = [];
expect(genSequence(values).max()).to.be.undefined
});

it('test max on string values', () => {
const values = ["one", "two"];
expect(genSequence(values).max()).to.equal("two");
});

it('test max on object values', () => {
const smaller: any = {
valueOf: function() { return 1; }
};
const bigger: any = {
valueOf: function() { return 2; }
};
const values = [smaller, bigger];
expect(genSequence(values).max()).to.equal(bigger);
});

it('test max starts with undefined always undefined', () => {
const values = [undefined, 1, undefined, 2];
expect(genSequence(values).max()).to.be.undefined;
});

it('test max undefined value', () => {
const values = [1, undefined, 2, undefined];
expect(genSequence(values).max()).to.equal(2);
});

it('test max null value', () => {
const values = [null, 1, null, 2];
expect(genSequence(values).max()).to.equal(2);
});

it('test max starts with NaN always NaN', () => {
const values = [NaN, 1, NaN, 2];
expect(genSequence(values).max()).to.be.NaN;
});

it('test max NaN value', () => {
const values = [1, NaN, 2];
expect(genSequence(values).max()).to.equal(2);
});

it('test max all undefined value', () => {
const values = [undefined, undefined];
expect(genSequence(values).max()).to.be.undefined;
});

it('test max all null value', () => {
const values = [null, null];
expect(genSequence(values).max()).to.be.null;
});

it('test max all NaN value', () => {
const values = [NaN, NaN];
expect(genSequence(values).max()).to.be.NaN;
});

it('test max with selector', () => {
const one: any = {
age: 1,
animal: "zebra"
};
const two: any = {
age: 2,
animal: "alligator"
};
const values = [one, two];
expect(genSequence(values).max((v) => v.age)).to.equal(two);
expect(genSequence(values).max((v) => v.animal)).to.equal(one);
});
});
12 changes: 11 additions & 1 deletion src/GenSequence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ export interface Sequence<T> extends IterableLike<T> {
any(fnFilter: (t: T)=> boolean): boolean;
first(fnFilter?: (t: T)=> boolean, defaultValue?: T): Maybe<T>;
first(fnFilter: (t: T)=> boolean, defaultValue: T): T;
max(fnSelector?: (t: T) => T): Maybe<T>;
max<U>(fnSelector: (t: T) => U): Maybe<T>;
toArray(): T[];
toIterable(): IterableIterator<T>;
}
Expand Down Expand Up @@ -87,6 +89,9 @@ export function genSequence<T>(i: GenIterable<T>): Sequence<T> {
first: (fnFilter: (t: T) => boolean, defaultValue: T): T => {
return first(fnFilter, defaultValue, i) as T;
},
max: <U>(fnSelector: (t: T) => U): Maybe<T> => {
return max<T, U>(fnSelector, i);
},
toArray: () => [...i],
toIterable: () => {
return toIterator(i);
Expand Down Expand Up @@ -282,11 +287,16 @@ export function first<T>(fn: (t: T) => boolean, defaultValue: T, i: Iterable<T>)
return defaultValue;
}

export function max<T, U>(fn: (t: T) => U, i: Iterable<T>): Maybe<T>;
export function max<T>(fn: Maybe<(t: T) => T>, i: Iterable<T>): Maybe<T> {
const selector = fn || ((v) => v);
return reduce((p: T, c: T) => selector(c) > selector(p) ? c : p, undefined, i);
}

export function* toIterator<T>(i: Iterable<T>) {
yield* i;
}


export type KeyValuePair<T> = [keyof T, T[keyof T]];

export function* objectIterator<T>(t: T): IterableIterator<KeyValuePair<T>> {
Expand Down

0 comments on commit 488c19f

Please sign in to comment.