-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
333 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { Enumerable, ofType, TypeOfMember } from '..'; | ||
|
||
describe('ofType', () => { | ||
it.each([[1, 2, 3], new Set([1, 2, 3]), '123', new Map()])('should return an Enumerable', src => { | ||
const result = ofType<unknown, string>(src, 'string'); | ||
|
||
expect(result).toBeInstanceOf(Enumerable); | ||
}); | ||
|
||
it.each([ | ||
[[1, 'b', 'c', false], 'string', ['b', 'c']], | ||
[[1, 'b', 'c', false, 2, true], 'number', [1, 2]], | ||
[[1, 'b', 'c', false, 2, true], 'boolean', [false, true]] | ||
])('should only return the specified type', (src, type, expected) => { | ||
const result = ofType(src, type as TypeOfMember).toArray(); | ||
|
||
expect(result).toEqual(expected); | ||
}); | ||
|
||
it('should only return instances of passed class', () => { | ||
class Test {} | ||
|
||
const a = new Test(); | ||
const b = new Test(); | ||
|
||
const items = [1, 3, 'sadf', a, false, null, undefined, b, NaN]; | ||
|
||
const result = ofType(items, Test).toArray(); | ||
|
||
expect(result).toEqual([a, b]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { Enumerable, pipe } from '..'; | ||
|
||
describe('pipe', () => { | ||
it.each([[1, 2, 3], new Set([1, 2, 3]), '123', new Map()])('should return an Enumerable', src => { | ||
const result = pipe<unknown>(src, () => {}); | ||
|
||
expect(result).toBeInstanceOf(Enumerable); | ||
}); | ||
|
||
it('should call action for each item', () => { | ||
const action = jest.fn(); | ||
|
||
const items = [1, 2, 3]; | ||
|
||
const _ = pipe(items, action).toArray(); | ||
|
||
expect(action).toHaveBeenCalledTimes(items.length); | ||
}); | ||
|
||
it('should have deferred execution', () => { | ||
const action = jest.fn(); | ||
|
||
const items = [1, 2, 3]; | ||
|
||
const result = pipe(items, action); | ||
|
||
expect(action).toHaveBeenCalledTimes(0); | ||
|
||
items.push(4); | ||
|
||
const _ = result.toArray(); | ||
|
||
expect(action).toHaveBeenCalledTimes(items.length); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { Enumerable } from '../Enumerable'; | ||
|
||
export function asEnumerable<TSource>(src: Iterable<TSource>): Enumerable<TSource> { | ||
return new Enumerable(src); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { count } from './count'; | ||
import { sum } from './sum'; | ||
|
||
export function average<TSource>(src: Iterable<TSource>, selector?: (item: TSource) => number): number { | ||
return sum(src, selector) / count(src); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { Enumerable } from '../Enumerable'; | ||
import { select } from './select'; | ||
|
||
export function chunk<TSource>(src: Iterable<TSource>, chunkSize: number): Enumerable<Enumerable<TSource>> { | ||
return select(src, (x, i) => ({ index: i, value: x })) | ||
.groupBy(x => Math.floor(x.index / chunkSize)) | ||
.select(x => x.select(v => v.value)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
export * from './aggregate'; | ||
export * from './all'; | ||
export * from './any'; | ||
export * from './append'; | ||
export * from './asEnumerable'; | ||
export * from './atLeast'; | ||
export * from './atMost'; | ||
export * from './average'; | ||
export * from './chunk'; | ||
export * from './concat'; | ||
export * from './contains'; | ||
export * from './count'; | ||
export * from './defaultIfEmpty'; | ||
export * from './distinct'; | ||
export * from './elementAt'; | ||
export * from './empty'; | ||
export * from './endsWith'; | ||
export * from './except'; | ||
export * from './first'; | ||
export * from './forEach'; | ||
export * from './from'; | ||
export * from './groupBy'; | ||
export * from './intersect'; | ||
export * from './last'; | ||
export * from './max'; | ||
export * from './min'; | ||
export * from './ofType'; | ||
export * from './orderBy'; | ||
export * from './pipe'; | ||
export * from './prepend'; | ||
export * from './quantile'; | ||
export * from './range'; | ||
export * from './repeat'; | ||
export * from './reverse'; | ||
export * from './select'; | ||
export * from './sequenceEqual'; | ||
export * from './shuffle'; | ||
export * from './skip'; | ||
export * from './startsWith'; | ||
export * from './sum'; | ||
export * from './take'; | ||
export * from './thenBy'; | ||
export * from './toArray'; | ||
export * from './toMap'; | ||
export * from './toObject'; | ||
export * from './toSet'; | ||
export * from './union'; | ||
export * from './where'; | ||
export * from './zip'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { aggregate } from './aggregate'; | ||
import { select } from './select'; | ||
|
||
export function min<TSource>(src: Iterable<TSource>): TSource; | ||
export function min<TSource, TResult>(src: Iterable<TSource>, selector: (item: TSource) => TResult): TResult; | ||
export function min<TSource, TResult>(src: Iterable<TSource>, selector?: (item: TSource) => TResult): TSource | TResult; | ||
export function min<TSource, TResult>( | ||
src: Iterable<TSource>, | ||
selector?: (item: TSource) => TResult | ||
): TSource | TResult { | ||
if (!selector) { | ||
return aggregate(src, (prev, curr) => (prev < curr ? prev : curr)); | ||
} | ||
|
||
return select(src, selector).aggregate((prev, curr) => (prev < curr ? prev : curr)); | ||
} | ||
|
||
export function minBy<TSource, TKey>(src: Iterable<TSource>, keySelector: (item: TSource) => TKey): TSource { | ||
return aggregate(src, (prev, curr) => (keySelector(prev) <= keySelector(curr) ? prev : curr)); | ||
} |
Oops, something went wrong.