-
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.
Merge branch 'master' into 25-create-aggregate-operation
- Loading branch information
Showing
24 changed files
with
397 additions
and
49 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,7 +1,7 @@ | ||
{ | ||
"name": "@codibre/fluent-iterable", | ||
"description": "Provides LINQ-like fluent api operations for iterables and async iterables (ES2018+).", | ||
"version": "1.29.2", | ||
"version": "1.31.0", | ||
"private": false, | ||
"author": { | ||
"name": "Thiago O Santos <[email protected]>" | ||
|
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,19 @@ | ||
import { distinctByRecipe } from '../recipes'; | ||
import { filterAsync } from './filter-async'; | ||
import { hasLessOrExactlyAsync } from './has-less-or-exactly-async'; | ||
import { basicAsync } from './basic-ingredients-async'; | ||
import { partitionAsync } from './partition-async'; | ||
import { Ingredient, ResolverIngredient } from '../recipes/ingredients'; | ||
import { reduceAsync } from './reduce-async'; | ||
|
||
function distinctAsyncRecipe(filterOrAll: Ingredient | ResolverIngredient) { | ||
return distinctByRecipe({ | ||
...basicAsync, | ||
partition: partitionAsync, | ||
filterOrAll, | ||
hasLessOrExactly: hasLessOrExactlyAsync, | ||
reduce: reduceAsync, | ||
}); | ||
} | ||
|
||
export const distinctByAsync = distinctAsyncRecipe(filterAsync); |
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 |
---|---|---|
@@ -1,14 +1,14 @@ | ||
import { toObjectChainRecipe } from '../recipes'; | ||
import { basicAsync } from './basic-ingredients-async'; | ||
import { flattenAsync } from './flatten-async'; | ||
import { groupAsync } from './group-async'; | ||
import { reduceAsync } from './reduce-async'; | ||
import { toObjectAsync } from './to-object-async'; | ||
import { unwindAsync } from './unwind-async'; | ||
|
||
export const toObjectChainAsync = toObjectChainRecipe({ | ||
...basicAsync, | ||
group: groupAsync, | ||
toObject: toObjectAsync, | ||
unwind: unwindAsync, | ||
flatten: flattenAsync, | ||
reduce: reduceAsync, | ||
}); |
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 |
---|---|---|
@@ -1,14 +1,14 @@ | ||
import { toObjectChainReduceRecipe } from '../recipes'; | ||
import { basicAsync } from './basic-ingredients-async'; | ||
import { flattenAsync } from './flatten-async'; | ||
import { groupAsync } from './group-async'; | ||
import { reduceAsync } from './reduce-async'; | ||
import { toObjectAsync } from './to-object-async'; | ||
import { unwindAsync } from './unwind-async'; | ||
|
||
export const toObjectChainReduceAsync = toObjectChainReduceRecipe({ | ||
...basicAsync, | ||
group: groupAsync, | ||
toObject: toObjectAsync, | ||
unwind: unwindAsync, | ||
flatten: flattenAsync, | ||
reduce: reduceAsync, | ||
}); |
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
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,51 @@ | ||
import { AnyIterable } from 'augmentative-iterable'; | ||
import { AnyMapper, FunctionAnyMapper } from '../types-internal'; | ||
import { DistinctIngredients } from './ingredients'; | ||
import { prepare } from '../types-internal/prepare'; | ||
|
||
function chooseDistinctByRecipe({ forEach, resolver }: DistinctIngredients) { | ||
return function distinct<T>( | ||
this: AnyIterable<T>, | ||
mappers: FunctionAnyMapper<any>[], | ||
) { | ||
const { length } = mappers; | ||
const setPos = length - 1; | ||
const lastMap = setPos - 1; | ||
const map = setPos === 0 ? new Set() : new Map(); | ||
const result: any[] = []; | ||
|
||
return resolver( | ||
forEach.call(this, (x) => { | ||
let current: any = map; | ||
for (let i = 0; i < setPos; i++) { | ||
const mapper = mappers[i]; | ||
const k = mapper(x); | ||
const prev = current; | ||
current = prev.get(k); | ||
if (!current) { | ||
current = i === lastMap ? new Set() : new Map(); | ||
prev.set(k, current); | ||
} | ||
} | ||
const k = mappers[setPos](x); | ||
if (!current.has(k)) { | ||
current.add(k); | ||
result.push(x); | ||
} | ||
}), | ||
() => result, | ||
); | ||
}; | ||
} | ||
|
||
export function distinctByRecipe(ingredients: DistinctIngredients) { | ||
const choose = chooseDistinctByRecipe(ingredients); | ||
|
||
return function distinctBy<T>( | ||
this: AnyIterable<T>, | ||
...baseMappers: Array<AnyMapper<T>> | ||
) { | ||
const mappers = baseMappers.map(prepare); | ||
return choose.call(this, mappers); | ||
}; | ||
} |
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
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,19 @@ | ||
import { basic } from './basic-ingredients'; | ||
import { distinctByRecipe } from '../recipes'; | ||
import { filter } from './filter'; | ||
import { hasLessOrExactly } from './has-less-or-exactly'; | ||
import { partition } from './partition'; | ||
import { reduce } from './reduce'; | ||
import { Ingredient, ResolverIngredient } from '../recipes/ingredients'; | ||
|
||
function distinctSyncRecipe(filterOrAll: Ingredient | ResolverIngredient) { | ||
return distinctByRecipe({ | ||
...basic, | ||
partition, | ||
filterOrAll, | ||
hasLessOrExactly, | ||
reduce, | ||
}); | ||
} | ||
|
||
export const distinctBy = distinctSyncRecipe(filter); |
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 |
---|---|---|
@@ -1,14 +1,14 @@ | ||
import { toObjectChainReduceRecipe } from '../recipes'; | ||
import { basic } from './basic-ingredients'; | ||
import { flatten } from './flatten'; | ||
import { group } from './group'; | ||
import { reduce } from './reduce'; | ||
import { toObject } from './to-object'; | ||
import { unwind } from './unwind'; | ||
|
||
export const toObjectChainReduce = toObjectChainReduceRecipe({ | ||
...basic, | ||
group, | ||
toObject, | ||
unwind, | ||
flatten, | ||
reduce, | ||
}); |
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 |
---|---|---|
@@ -1,14 +1,14 @@ | ||
import { toObjectChainRecipe } from '../recipes'; | ||
import { basic } from './basic-ingredients'; | ||
import { flatten } from './flatten'; | ||
import { group } from './group'; | ||
import { reduce } from './reduce'; | ||
import { toObject } from './to-object'; | ||
import { unwind } from './unwind'; | ||
|
||
export const toObjectChain = toObjectChainRecipe({ | ||
...basic, | ||
group, | ||
toObject, | ||
unwind, | ||
flatten, | ||
reduce, | ||
}); |
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
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,25 @@ | ||
import { AsyncMapper, Mapper } from 'augmentative-iterable'; | ||
import { FluentAsyncIterable, FluentIterable } from '../base'; | ||
|
||
export interface DistinctByFunction<T> { | ||
/** | ||
* Returns distinct elements from the iterable from a certain list of projections<br> | ||
* Examples:<br> | ||
* * `fluent([{ a: 1, b: 2, c: 1}, { a: 1, b: 2, c: 2}]).distinct()` yields *{ a: 1, b: 2, c: 1 }*<br> | ||
* @typeparam R The type of the data the element equality is based on. | ||
* @param mappers The projections to use to determine element equality. | ||
* @returns The [[FluentIterable]] of the distinct elements. | ||
*/ | ||
<R>(...mappers: Array<Mapper<T, R> | keyof T>): FluentIterable<T>; | ||
} | ||
export interface AsyncDistinctByFunction<T> { | ||
/** | ||
* Returns distinct elements from the iterable from a certain list of projections<br> | ||
* Examples:<br> | ||
* * `fluent([{ a: 1, b: 2, c: 1}, { a: 1, b: 2, c: 2}]).distinct()` yields *{ a: 1, b: 2, c: 1 }*<br> | ||
* @typeparam R The type of the data the element equality is based on. | ||
* @param mappers The projections to use to determine element equality. | ||
* @returns The [[FluentIterable]] of the distinct elements. | ||
*/ | ||
<R>(...mappers: Array<AsyncMapper<T, R> | keyof T>): FluentAsyncIterable<T>; | ||
} |
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
Oops, something went wrong.