gensequence
Advanced tools
Comparing version 1.1.0 to 1.2.0
# Release Notes | ||
# 1.2.0 | ||
* Added `min` and `max` - thanks to @sea1jxr | ||
# 1.1.0 | ||
@@ -4,0 +7,0 @@ * Added `all` and `any` - thanks to @sea1jxr |
@@ -27,2 +27,6 @@ export declare type Maybe<T> = T | undefined; | ||
first(fnFilter: (t: T) => boolean, defaultValue: T): T; | ||
max(fnSelector?: (t: T) => T): Maybe<T>; | ||
max<U>(fnSelector: (t: T) => U): Maybe<T>; | ||
min(fnSelector?: (t: T) => T): Maybe<T>; | ||
min<U>(fnSelector: (t: T) => U): Maybe<T>; | ||
toArray(): T[]; | ||
@@ -74,2 +78,4 @@ toIterable(): IterableIterator<T>; | ||
export declare function first<T>(fn: Maybe<(t: T) => boolean>, defaultValue: Maybe<T>, i: Iterable<T>): Maybe<T>; | ||
export declare function max<T, U>(selector: (t: T) => U, i: Iterable<T>): Maybe<T>; | ||
export declare function min<T, U>(selector: (t: T) => U, i: Iterable<T>): Maybe<T>; | ||
export declare function toIterator<T>(i: Iterable<T>): IterableIterator<T>; | ||
@@ -76,0 +82,0 @@ export declare type KeyValuePair<T> = [keyof T, T[keyof T]]; |
@@ -50,2 +50,8 @@ "use strict"; | ||
}, | ||
max: (fnSelector) => { | ||
return max(fnSelector, i); | ||
}, | ||
min: (fnSelector) => { | ||
return min(fnSelector, i); | ||
}, | ||
toArray: () => [...i], | ||
@@ -228,2 +234,10 @@ toIterable: () => { | ||
exports.first = first; | ||
function max(selector = (t => t), i) { | ||
return reduce((p, c) => selector(c) > selector(p) ? c : p, undefined, i); | ||
} | ||
exports.max = max; | ||
function min(selector = (t => t), i) { | ||
return reduce((p, c) => selector(c) < selector(p) ? c : p, undefined, i); | ||
} | ||
exports.min = min; | ||
function* toIterator(i) { | ||
@@ -230,0 +244,0 @@ yield* i; |
{ | ||
"name": "gensequence", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"description": "Small library to simplify working with Generators and Iterators in Javascript / Typescript", | ||
@@ -57,3 +57,6 @@ "main": "lib/index.js", | ||
], | ||
"exclude": [], | ||
"exclude": [ | ||
"src/**/*.test.ts", | ||
"src/**/*.perf.ts" | ||
], | ||
"extension": [ | ||
@@ -60,0 +63,0 @@ ".ts" |
@@ -128,4 +128,8 @@ # GenSequence | ||
- `.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. | ||
- `.min()` -- return the smallest value in the sequence. | ||
- `.min(fn)` -- return the smallest value of *fn(value)* in the sequence. | ||
### Misc | ||
- `toIterable()` -- Casts a Sequence into an IterableIterator - used in cases where type checking is too strict. |
@@ -300,2 +300,192 @@ import { genSequence, sequenceFromObject, sequenceFromRegExpMatch } from './GenSequence'; | ||
}); | ||
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); | ||
}); | ||
it('test min on single value', () => { | ||
const values = [2]; | ||
expect(genSequence(values).min()).to.equal(2); | ||
}); | ||
it('test min returns min on start', () => { | ||
const values = [1, 2, 3, 4]; | ||
expect(genSequence(values).min()).to.equal(1); | ||
}); | ||
it('test min returns min in middle', () => { | ||
const values = [3, 1, 3]; | ||
expect(genSequence(values).min()).to.equal(1); | ||
}); | ||
it('test min returns min on end', () => { | ||
const values = [4, 3, 2, 1]; | ||
expect(genSequence(values).min()).to.equal(1); | ||
}); | ||
it('test min on empty set returns undefined', () => { | ||
const values = []; | ||
expect(genSequence(values).min()).to.be.undefined | ||
}); | ||
it('test min on string values', () => { | ||
const values = ["one", "two"]; | ||
expect(genSequence(values).min()).to.equal("one"); | ||
}); | ||
it('test min on object values', () => { | ||
const smaller: any = { | ||
valueOf: function() { return 1; } | ||
}; | ||
const bigger: any = { | ||
valueOf: function() { return 2; } | ||
}; | ||
const values = [smaller, bigger]; | ||
expect(genSequence(values).min()).to.equal(smaller); | ||
}); | ||
it('test min starts with undefined always undefined', () => { | ||
const values = [undefined, 1, undefined, 2]; | ||
expect(genSequence(values).min()).to.be.undefined; | ||
}); | ||
it('test min undefined value', () => { | ||
const values = [2, undefined, 1, undefined]; | ||
expect(genSequence(values).min()).to.equal(1); | ||
}); | ||
it('test min null value', () => { | ||
const values = [null, 1, null, 2]; | ||
expect(genSequence(values).min()).to.be.null; | ||
}); | ||
it('test min starts with NaN always NaN', () => { | ||
const values = [NaN, 1, NaN, 2]; | ||
expect(genSequence(values).min()).to.be.NaN; | ||
}); | ||
it('test min NaN value', () => { | ||
const values = [1, NaN, 2]; | ||
expect(genSequence(values).min()).to.equal(1); | ||
}); | ||
it('test min all undefined value', () => { | ||
const values = [undefined, undefined]; | ||
expect(genSequence(values).min()).to.be.undefined; | ||
}); | ||
it('test min all null value', () => { | ||
const values = [null, null]; | ||
expect(genSequence(values).min()).to.be.null; | ||
}); | ||
it('test min all NaN value', () => { | ||
const values = [NaN, NaN]; | ||
expect(genSequence(values).min()).to.be.NaN; | ||
}); | ||
it('test min with selector', () => { | ||
const one: any = { | ||
age: 1, | ||
animal: "zebra" | ||
}; | ||
const two: any = { | ||
age: 2, | ||
animal: "alligator" | ||
}; | ||
const values = [one, two]; | ||
expect(genSequence(values).min((v) => v.age)).to.equal(one); | ||
expect(genSequence(values).min((v) => v.animal)).to.equal(two); | ||
}); | ||
}); |
@@ -30,2 +30,6 @@ | ||
first(fnFilter: (t: T)=> boolean, defaultValue: T): T; | ||
max(fnSelector?: (t: T) => T): Maybe<T>; | ||
max<U>(fnSelector: (t: T) => U): Maybe<T>; | ||
min(fnSelector?: (t: T) => T): Maybe<T>; | ||
min<U>(fnSelector: (t: T) => U): Maybe<T>; | ||
toArray(): T[]; | ||
@@ -91,2 +95,8 @@ toIterable(): IterableIterator<T>; | ||
}, | ||
max: <U>(fnSelector: (t: T) => U): Maybe<T> => { | ||
return max<T, U>(fnSelector, i); | ||
}, | ||
min: <U>(fnSelector: (t: T) => U): Maybe<T> => { | ||
return min<T, U>(fnSelector, i); | ||
}, | ||
toArray: () => [...i], | ||
@@ -287,2 +297,12 @@ toIterable: () => { | ||
export function max<T, U>(selector: (t: T) => U, i: Iterable<T>): Maybe<T>; | ||
export function max<T>(selector: (t: T) => T = (t => t), i: Iterable<T>): Maybe<T> { | ||
return reduce((p: T, c: T) => selector(c) > selector(p) ? c : p, undefined, i); | ||
} | ||
export function min<T, U>(selector: (t: T) => U, i: Iterable<T>): Maybe<T>; | ||
export function min<T>(selector: (t: T) => T = (t => t), i: Iterable<T>): Maybe<T> { | ||
return reduce((p: T, c: T) => selector(c) < selector(p) ? c : p, undefined, i); | ||
} | ||
export function* toIterator<T>(i: Iterable<T>) { | ||
@@ -292,3 +312,2 @@ yield* i; | ||
export type KeyValuePair<T> = [keyof T, T[keyof T]]; | ||
@@ -295,0 +314,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1368
135
125578