Comparing version 0.0.1 to 0.0.2
@@ -5,2 +5,3 @@ declare global { | ||
* Converts the sequence into an array. | ||
* @returns The sequence as a type safe array | ||
*/ | ||
@@ -12,2 +13,3 @@ array(): T[]; | ||
* Converts the sequence into an array. | ||
* @returns A promise of the sequence as a type safe array | ||
*/ | ||
@@ -14,0 +16,0 @@ array(): Promise<T[]>; |
@@ -5,2 +5,3 @@ declare global { | ||
* Converts the sequence into an AsyncGenerator for asynchrones processing. | ||
* @returns A type safe async generator | ||
*/ | ||
@@ -12,2 +13,4 @@ async(): AsyncGenerator<T, TReturn, TNext>; | ||
* Converts the sequence into an AsyncGenerator for asynchrones processing. | ||
* Present for when you do not know if you sequence is async or not. | ||
* @returns The original subject. | ||
*/ | ||
@@ -14,0 +17,0 @@ async(): AsyncGenerator<T, TReturn, TNext>; |
@@ -14,9 +14,2 @@ "use strict"; | ||
}; | ||
var __asyncValues = (this && this.__asyncValues) || function (o) { | ||
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined."); | ||
var m = o[Symbol.asyncIterator], i; | ||
return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i); | ||
function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; } | ||
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); } | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
@@ -31,18 +24,3 @@ const utils_1 = require("./utils"); | ||
}, function () { | ||
return __asyncGenerator(this, arguments, function* () { | ||
var e_1, _a; | ||
try { | ||
for (var _b = __asyncValues(this), _c; _c = yield __await(_b.next()), !_c.done;) { | ||
const item = _c.value; | ||
yield yield __await(item); | ||
} | ||
} | ||
catch (e_1_1) { e_1 = { error: e_1_1 }; } | ||
finally { | ||
try { | ||
if (_c && !_c.done && (_a = _b.return)) yield __await(_a.call(_b)); | ||
} | ||
finally { if (e_1) throw e_1.error; } | ||
} | ||
}); | ||
return this; | ||
}); |
import { Promised } from "./utils"; | ||
declare global { | ||
interface Generator<T = unknown, TReturn = any, TNext = unknown> { | ||
/** | ||
* Creates a dictionary from a generator according to a | ||
* specified key selector function. | ||
* @param key_selector A function to extract a key from each element. | ||
* @returns A dictionary that contains values of type selected from the input sequence. | ||
*/ | ||
dictionary<TKey extends number | string>(key_selector: (item: T) => TKey): Record<TKey, T>; | ||
/** | ||
* Creates a dictionary from a generator according to | ||
* specified key selector and element selector functions. | ||
* @param key_selector A function to extract a key from each element. | ||
* @param value_selector A transform function to produce a result element value from each element. | ||
* @returns A dictionary that contains values of type selected from the input sequence. | ||
*/ | ||
dictionary<TKey extends number | string, TResult>(key_selector: (item: T) => TKey, value_selector: (item: T) => TResult): Record<TKey, TResult>; | ||
} | ||
interface AsyncGenerator<T = unknown, TReturn = any, TNext = unknown> { | ||
/** | ||
* Creates a dictionary from a generator according to a | ||
* specified key selector function. | ||
* @param key_selector A function to extract a key from each element. | ||
* @returns A dictionary that contains values of type selected from the input sequence. | ||
*/ | ||
dictionary<TKey extends number | string>(key_selector: (item: T) => Promised<TKey>): Promise<Record<TKey, T>>; | ||
/** | ||
* Creates a dictionary from a generator according to | ||
* specified key selector and element selector functions. | ||
* @param key_selector A function to extract a key from each element. | ||
* @param value_selector A transform function to produce a result element value from each element. | ||
* @returns A dictionary that contains values of type selected from the input sequence. | ||
*/ | ||
dictionary<TKey extends number | string, TResult>(key_selector: (item: T) => Promised<TKey>, value_selector: (item: T) => Promised<TResult>): Promise<Record<TKey, TResult>>; | ||
} | ||
} |
import { Promised } from "./utils"; | ||
declare global { | ||
interface Generator<T = unknown, TReturn = any, TNext = unknown> { | ||
/** | ||
* Returns distinct elements from a sequence by using the default equality comparer to compare values. | ||
* @returns A generator that contains distinct elements from the source sequence. | ||
*/ | ||
distinct(this: Generator<number, TReturn, TNext>): Generator<number, TReturn, TNext>; | ||
/** | ||
* Returns distinct elements from a sequence by using the default equality comparer to compare values. | ||
* @returns A generator that contains distinct elements from the source sequence. | ||
*/ | ||
distinct(this: Generator<string, TReturn, TNext>): Generator<string, TReturn, TNext>; | ||
/** | ||
* Returns distinct elements from a sequence by using the default equality comparer to compare values. | ||
* @returns A generator that contains distinct elements from the source sequence. | ||
*/ | ||
distinct(this: Generator<boolean, TReturn, TNext>): Generator<boolean, TReturn, TNext>; | ||
/** | ||
* Returns distinct elements from a sequence by using a specified comparitor to compare values. | ||
* @param comparitor A comparitor to compare values. | ||
* @returns A generator that contains distinct elements from the source sequence. | ||
*/ | ||
distinct(comparitor: (a: T, b: T) => boolean): Generator<T, TReturn, TNext>; | ||
} | ||
interface AsyncGenerator<T = unknown, TReturn = any, TNext = unknown> { | ||
/** | ||
* Returns distinct elements from a sequence by using the default equality comparer to compare values. | ||
* @returns A generator that contains distinct elements from the source sequence. | ||
*/ | ||
distinct(this: AsyncGenerator<number, TReturn, TNext>): AsyncGenerator<number, TReturn, TNext>; | ||
/** | ||
* Returns distinct elements from a sequence by using the default equality comparer to compare values. | ||
* @returns A generator that contains distinct elements from the source sequence. | ||
*/ | ||
distinct(this: AsyncGenerator<string, TReturn, TNext>): AsyncGenerator<string, TReturn, TNext>; | ||
/** | ||
* Returns distinct elements from a sequence by using the default equality comparer to compare values. | ||
* @returns A generator that contains distinct elements from the source sequence. | ||
*/ | ||
distinct(this: AsyncGenerator<boolean, TReturn, TNext>): AsyncGenerator<boolean, TReturn, TNext>; | ||
/** | ||
* Returns distinct elements from a sequence by using a specified comparitor to compare values. | ||
* @param comparitor A comparitor to compare values. | ||
* @returns A generator that contains distinct elements from the source sequence. | ||
*/ | ||
distinct(comparitor: (a: T, b: T) => Promised<boolean>): AsyncGenerator<T, TReturn, TNext>; | ||
} | ||
} |
@@ -0,1 +1,5 @@ | ||
/** | ||
* Builds an empty generator with the specified type. | ||
* @returns An empty generator or specified type. | ||
*/ | ||
export declare function Empty<T>(): Generator<T>; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.Empty = void 0; | ||
/** | ||
* Builds an empty generator with the specified type. | ||
* @returns An empty generator or specified type. | ||
*/ | ||
function* Empty() { } | ||
exports.Empty = Empty; |
declare global { | ||
interface Generator<T = unknown, TReturn = any, TNext = unknown> { | ||
/** | ||
* Produces the set difference of two sequences by using the default equality comparer to compare values. | ||
* @param that A sequence whose elements that also occur in the first sequence will cause those elements to be removed from the returned sequence. | ||
* @returns A sequence that contains the set difference of the elements of two sequences. | ||
*/ | ||
except(this: Generator<number, TReturn, TNext>, that: Generator<number, TReturn, TNext> | number[]): Generator<number, TReturn, TNext>; | ||
/** | ||
* Produces the set difference of two sequences by using the default equality comparer to compare values. | ||
* @param that A sequence whose elements that also occur in the first sequence will cause those elements to be removed from the returned sequence. | ||
* @returns A sequence that contains the set difference of the elements of two sequences. | ||
*/ | ||
except(this: Generator<string, TReturn, TNext>, that: Generator<string, TReturn, TNext> | string[]): Generator<string, TReturn, TNext>; | ||
/** | ||
* Produces the set difference of two sequences by using the default equality comparer to compare values. | ||
* @param that A sequence whose elements that also occur in the first sequence will cause those elements to be removed from the returned sequence. | ||
* @returns A sequence that contains the set difference of the elements of two sequences. | ||
*/ | ||
except(this: Generator<boolean, TReturn, TNext>, that: Generator<boolean, TReturn, TNext> | boolean[]): Generator<boolean, TReturn, TNext>; | ||
/** | ||
* Produces the set difference of two sequences by using the default equality comparer to compare values. | ||
* @param that A sequence whose elements that also occur in the first sequence will cause those elements to be removed from the returned sequence. | ||
* @param comparitor A comparitor function to compare values. | ||
* @returns A sequence that contains the set difference of the elements of two sequences. | ||
*/ | ||
except(that: Generator<T, TReturn, TNext> | T[], comparitor: (a: T, b: T) => boolean): Generator<T, TReturn, TNext>; | ||
} | ||
interface AsyncGenerator<T = unknown, TReturn = any, TNext = unknown> { | ||
/** | ||
* Produces the set difference of two sequences by using the default equality comparer to compare values. | ||
* @param that A sequence whose elements that also occur in the first sequence will cause those elements to be removed from the returned sequence. | ||
* @returns A sequence that contains the set difference of the elements of two sequences. | ||
*/ | ||
except(this: AsyncGenerator<number, TReturn, TNext>, that: AsyncGenerator<number, TReturn, TNext> | Generator<number, TReturn, TNext> | number[]): AsyncGenerator<number, TReturn, TNext>; | ||
/** | ||
* Produces the set difference of two sequences by using the default equality comparer to compare values. | ||
* @param that A sequence whose elements that also occur in the first sequence will cause those elements to be removed from the returned sequence. | ||
* @returns A sequence that contains the set difference of the elements of two sequences. | ||
*/ | ||
except(this: AsyncGenerator<string, TReturn, TNext>, that: AsyncGenerator<string, TReturn, TNext> | Generator<string, TReturn, TNext> | string[]): AsyncGenerator<string, TReturn, TNext>; | ||
/** | ||
* Produces the set difference of two sequences by using the default equality comparer to compare values. | ||
* @param that A sequence whose elements that also occur in the first sequence will cause those elements to be removed from the returned sequence. | ||
* @returns A sequence that contains the set difference of the elements of two sequences. | ||
*/ | ||
except(this: AsyncGenerator<boolean, TReturn, TNext>, that: AsyncGenerator<boolean, TReturn, TNext> | Generator<boolean, TReturn, TNext> | boolean[]): AsyncGenerator<boolean, TReturn, TNext>; | ||
/** | ||
* Produces the set difference of two sequences by using the default equality comparer to compare values. | ||
* @param that A sequence whose elements that also occur in the first sequence will cause those elements to be removed from the returned sequence. | ||
* @param comparitor A comparitor function to compare values. | ||
* @returns A sequence that contains the set difference of the elements of two sequences. | ||
*/ | ||
except(that: AsyncGenerator<T, TReturn, TNext> | Generator<T, TReturn, TNext> | T[], comparitor: (a: T, b: T) => boolean): AsyncGenerator<T, TReturn, TNext>; | ||
@@ -13,0 +55,0 @@ } |
import { Promised } from "./utils"; | ||
declare global { | ||
interface Generator<T = unknown, TReturn = any, TNext = unknown> { | ||
/** | ||
* Returns the first element of a sequence. | ||
* @returns The first element in the specified sequence. | ||
*/ | ||
first(): T; | ||
/** | ||
* Returns the first element of a sequence, or undefined if the sequence contains no elements. | ||
* @param allow_empty Specify that the sequence may be empty. | ||
* @returns The first element in the specified sequence, or undefined if the sequence is empty. | ||
*/ | ||
first(allow_empty: "or-default"): T | undefined; | ||
/** | ||
* Returns the first element in a sequence that satisfies a specified condition. | ||
* @param predicate A function to test each element for a condition. | ||
* @returns The first element in the sequence that passes the test in the specified predicate function. | ||
*/ | ||
first(predicate: (item: T) => boolean): T; | ||
/** | ||
* Returns the first element of the sequence that satisfies a condition or a default value if no such element is found. | ||
* @param predicate A function to test each element for a condition. | ||
* @param allow_empty Specify that the sequence may be empty. | ||
* @returns The first element in the sequence that passes the test in the specified predicate function. | ||
*/ | ||
first(predicate: (item: T) => boolean, allow_empty: "or-default"): T | undefined; | ||
/** | ||
* Returns the first element in a sequence that matches the type specified in the type checker. | ||
* @param predicate A function to test each element for a type. | ||
* @returns The first element in the sequence that passes the test in the specified predicate function. | ||
*/ | ||
first<TRes extends T>(predicate: (item: T) => item is TRes): TRes; | ||
/** | ||
* Returns the first element of the sequence that matches the type specified in the type checker or a default value if no such element is found. | ||
* @param predicate A function to test each element for a type. | ||
* @param allow_empty Specify that the sequence may be empty. | ||
* @returns The first element in the sequence that passes the test in the specified predicate function. | ||
*/ | ||
first<TRes extends T>(predicate: (item: T) => item is TRes, allow_empty: "or-default"): TRes | undefined; | ||
} | ||
interface AsyncGenerator<T = unknown, TReturn = any, TNext = unknown> { | ||
/** | ||
* Returns the first element of a sequence. | ||
* @returns The first element in the specified sequence. | ||
*/ | ||
first(): Promise<T>; | ||
first(allow_empty: true): Promise<T | undefined>; | ||
/** | ||
* Returns the first element of a sequence, or undefined if the sequence contains no elements. | ||
* @param allow_empty Specify that the sequence may be empty. | ||
* @returns The first element in the specified sequence, or undefined if the sequence is empty. | ||
*/ | ||
first(allow_empty: "or-default"): Promise<T | undefined>; | ||
/** | ||
* Returns the first element in a sequence that satisfies a specified condition. | ||
* @param predicate A function to test each element for a condition. | ||
* @returns The first element in the sequence that passes the test in the specified predicate function. | ||
*/ | ||
first(predicate: (item: T) => Promised<boolean>): Promise<T>; | ||
first(predicate: (item: T) => Promised<boolean>, allow_empty: true): Promise<T | undefined>; | ||
/** | ||
* Returns the first element of the sequence that satisfies a condition or a default value if no such element is found. | ||
* @param predicate A function to test each element for a condition. | ||
* @param allow_empty Specify that the sequence may be empty. | ||
* @returns The first element in the sequence that passes the test in the specified predicate function. | ||
*/ | ||
first(predicate: (item: T) => Promised<boolean>, allow_empty: "or-default"): Promise<T | undefined>; | ||
/** | ||
* Returns the first element in a sequence that matches the type specified in the type checker. | ||
* @param predicate A function to test each element for a type. | ||
* @returns The first element in the sequence that passes the test in the specified predicate function. | ||
*/ | ||
first<TRes extends T>(predicate: (item: T) => item is TRes): Promise<TRes>; | ||
/** | ||
* Returns the first element of the sequence that matches the type specified in the type checker or a default value if no such element is found. | ||
* @param predicate A function to test each element for a type. | ||
* @param allow_empty Specify that the sequence may be empty. | ||
* @returns The first element in the sequence that passes the test in the specified predicate function. | ||
*/ | ||
first<TRes extends T>(predicate: (item: T) => item is TRes, allow_empty: "or-default"): Promise<TRes | undefined>; | ||
} | ||
} |
declare global { | ||
/** | ||
* Builds a generator from an array. | ||
* @returns A generator with the same type as the array. | ||
*/ | ||
interface Array<T> { | ||
@@ -3,0 +7,0 @@ geninq(): Generator<T, void, unknown>; |
import { Promised } from "./utils"; | ||
declare global { | ||
interface Generator<T = unknown, TReturn = any, TNext = unknown> { | ||
/** | ||
* Groups the elements of a sequence according to a specified key selector function. | ||
* @param builder A function to extract the key for each element. | ||
* @returns A collection of elements of the type specified where each element represents a projection over a group and its key. | ||
*/ | ||
group_by<TKey extends string | number | boolean, TElement = T, TResult = [TKey, Generator<TElement>]>(builder: (item: T) => TKey): Generator<TResult, TReturn, TNext>; | ||
/** | ||
* Groups the elements of a sequence according to a specified key selector function | ||
* and projects the elements for each group by using a specified function. | ||
* @param builder.key A function to extract the key for each element. | ||
* @param builder.element A function to map each source element to an element in an grouping. | ||
* @returns A collection of elements of the type specified where each element represents a projection over a group and its key. | ||
*/ | ||
group_by<TKey extends string | number | boolean, TElement = T, TResult = [TKey, Generator<TElement>]>(builder: { | ||
@@ -9,2 +21,11 @@ key: (item: T) => TKey; | ||
}): Generator<TResult, TReturn, TNext>; | ||
/** | ||
* Groups the elements of a sequence according to a key selector function. | ||
* The keys are compared by using a comparer and each group's elements are | ||
* projected by using a specified function. | ||
* @param builder.key A function to extract the key for each element. | ||
* @param builder.element A function to map each source element to an element in an grouping. | ||
* @param comparitor A comparitor function to compare keys with. | ||
* @returns A collection of elements of the type specified where each element represents a projection over a group and its key. | ||
*/ | ||
group_by<TKey, TElement = T, TResult = [TKey, Generator<TElement>]>(builder: { | ||
@@ -14,6 +35,25 @@ key: (item: T) => TKey; | ||
}, comparitor: (a: TKey, b: TKey) => boolean): Generator<TResult, TReturn, TNext>; | ||
/** | ||
* Groups the elements of a sequence according to a specified key selector | ||
* function and compares the keys by using a specified comparer. | ||
* @param builder A function to extract the key for each element. | ||
* @param comparitor A comparitor function to compare keys with. | ||
* @returns A collection of elements of the type specified where each element represents a projection over a group and its key. | ||
*/ | ||
group_by<TKey, TElement = T, TResult = [TKey, Generator<TElement>]>(builder: (item: T) => TKey, comparitor: (a: TKey, b: TKey) => boolean): Generator<TResult, TReturn, TNext>; | ||
} | ||
interface AsyncGenerator<T = unknown, TReturn = any, TNext = unknown> { | ||
/** | ||
* Groups the elements of a sequence according to a specified key selector function. | ||
* @param builder A function to extract the key for each element. | ||
* @returns A collection of elements of the type specified where each element represents a projection over a group and its key. | ||
*/ | ||
group_by<TKey extends string | number | boolean, TElement = T, TResult = [TKey, AsyncGenerator<TElement>]>(builder: (item: T) => Promised<TKey>): AsyncGenerator<TResult, TReturn, TNext>; | ||
/** | ||
* Groups the elements of a sequence according to a specified key selector function | ||
* and projects the elements for each group by using a specified function. | ||
* @param builder.key A function to extract the key for each element. | ||
* @param builder.element A function to map each source element to an element in an grouping. | ||
* @returns A collection of elements of the type specified where each element represents a projection over a group and its key. | ||
*/ | ||
group_by<TKey extends string | number | boolean, TElement = T, TResult = [TKey, AsyncGenerator<TElement>]>(builder: { | ||
@@ -23,3 +63,19 @@ key: (item: T) => Promised<TKey>; | ||
}): AsyncGenerator<TResult, TReturn, TNext>; | ||
/** | ||
* Groups the elements of a sequence according to a key selector function. | ||
* The keys are compared by using a comparer and each group's elements are | ||
* projected by using a specified function. | ||
* @param builder.key A function to extract the key for each element. | ||
* @param builder.element A function to map each source element to an element in an grouping. | ||
* @param comparitor A comparitor function to compare keys with. | ||
* @returns A collection of elements of the type specified where each element represents a projection over a group and its key. | ||
*/ | ||
group_by<TKey, TElement = T, TResult = [TKey, AsyncGenerator<TElement>]>(builder: (item: T) => Promised<TKey>, comparitor: (a: TKey, b: TKey) => boolean): AsyncGenerator<TResult, TReturn, TNext>; | ||
/** | ||
* Groups the elements of a sequence according to a specified key selector | ||
* function and compares the keys by using a specified comparer. | ||
* @param builder A function to extract the key for each element. | ||
* @param comparitor A comparitor function to compare keys with. | ||
* @returns A collection of elements of the type specified where each element represents a projection over a group and its key. | ||
*/ | ||
group_by<TKey, TElement = T, TResult = [TKey, AsyncGenerator<TElement>]>(builder: { | ||
@@ -26,0 +82,0 @@ key: (item: T) => Promised<TKey>; |
@@ -5,9 +5,47 @@ import "./array"; | ||
interface Generator<T = unknown, TReturn = any, TNext = unknown> { | ||
group_join<TKey extends string | number | boolean, TInner, TResult>(inner: Generator<TInner>, outer_key_selector: (item: T) => TKey, inner_key_selector: (item: TInner) => TKey, result_selector: (item: T, attached: Generator<TInner>) => TResult): Generator<TResult>; | ||
group_join<TKey, TInner, TResult>(inner: Generator<TInner>, outer_key_selector: (item: T) => TKey, inner_key_selector: (item: TInner) => TKey, result_selector: (item: T, attached: Generator<TInner>) => TResult, comparitor: (a: TKey, b: TKey) => boolean): Generator<TResult>; | ||
/** | ||
* Correlates the elements of two sequences based on equality of keys | ||
* and groups the results. The default equality comparer is used to compare keys. | ||
* @param inner The sequence to join to the first sequence. | ||
* @param outer_key_selector A function to extract the join key from each element of the first sequence. | ||
* @param inner_key_selector A function to extract the join key from each element of the second sequence. | ||
* @param result_selector A function to create a result element from an element from the first sequence and a collection of matching elements from the second sequence. | ||
* @returns A generator that contains elements of type TResult that are obtained by performing a grouped join on two sequences. | ||
*/ | ||
group_join<TKey extends string | number | boolean, TInner, TResult>(inner: Generator<TInner> | TInner[], outer_key_selector: (item: T) => TKey, inner_key_selector: (item: TInner) => TKey, result_selector: (item: T, attached: Generator<TInner>) => TResult): Generator<TResult>; | ||
/** | ||
* Correlates the elements of two sequences based on key equality and groups the results. | ||
* A specified comparitor is used to compare keys. | ||
* @param inner The sequence to join to the first sequence. | ||
* @param outer_key_selector A function to extract the join key from each element of the first sequence. | ||
* @param inner_key_selector A function to extract the join key from each element of the second sequence. | ||
* @param result_selector A function to create a result element from an element from the first sequence and a collection of matching elements from the second sequence. | ||
* @param comparitor A comparitor function to hash and compare keys. | ||
* @returns A generator that contains elements of type TResult that are obtained by performing a grouped join on two sequences. | ||
*/ | ||
group_join<TKey, TInner, TResult>(inner: Generator<TInner> | TInner[], outer_key_selector: (item: T) => TKey, inner_key_selector: (item: TInner) => TKey, result_selector: (item: T, attached: Generator<TInner>) => TResult, comparitor: (a: TKey, b: TKey) => boolean): Generator<TResult>; | ||
} | ||
interface AsyncGenerator<T = unknown, TReturn = any, TNext = unknown> { | ||
group_join<TKey extends string | number | boolean, TInner, TResult>(inner: AsyncGenerator<TInner>, outer_key_selector: (item: T) => Promised<TKey>, inner_key_selector: (item: TInner) => Promised<TKey>, result_selector: (item: T, attached: AsyncGenerator<TInner>) => Promised<TResult>): AsyncGenerator<TResult>; | ||
group_join<TKey, TInner, TResult>(inner: AsyncGenerator<TInner>, outer_key_selector: (item: T) => Promised<TKey>, inner_key_selector: (item: TInner) => Promised<TKey>, result_selector: (item: T, attached: AsyncGenerator<TInner>) => Promised<TResult>, comparitor: (a: TKey, b: TKey) => boolean): AsyncGenerator<TResult>; | ||
/** | ||
* Correlates the elements of two sequences based on equality of keys | ||
* and groups the results. The default equality comparer is used to compare keys. | ||
* @param inner The sequence to join to the first sequence. | ||
* @param outer_key_selector A function to extract the join key from each element of the first sequence. | ||
* @param inner_key_selector A function to extract the join key from each element of the second sequence. | ||
* @param result_selector A function to create a result element from an element from the first sequence and a collection of matching elements from the second sequence. | ||
* @returns A generator that contains elements of type TResult that are obtained by performing a grouped join on two sequences. | ||
*/ | ||
group_join<TKey extends string | number | boolean, TInner, TResult>(inner: AsyncGenerator<TInner> | Generator<TInner> | TInner[], outer_key_selector: (item: T) => Promised<TKey>, inner_key_selector: (item: TInner) => Promised<TKey>, result_selector: (item: T, attached: AsyncGenerator<TInner>) => Promised<TResult>): AsyncGenerator<TResult>; | ||
/** | ||
* Correlates the elements of two sequences based on key equality and groups the results. | ||
* A specified comparitor is used to compare keys. | ||
* @param inner The sequence to join to the first sequence. | ||
* @param outer_key_selector A function to extract the join key from each element of the first sequence. | ||
* @param inner_key_selector A function to extract the join key from each element of the second sequence. | ||
* @param result_selector A function to create a result element from an element from the first sequence and a collection of matching elements from the second sequence. | ||
* @param comparitor A comparitor function to hash and compare keys. | ||
* @returns A generator that contains elements of type TResult that are obtained by performing a grouped join on two sequences. | ||
*/ | ||
group_join<TKey, TInner, TResult>(inner: AsyncGenerator<TInner> | Generator<TInner> | TInner[], outer_key_selector: (item: T) => Promised<TKey>, inner_key_selector: (item: TInner) => Promised<TKey>, result_selector: (item: T, attached: AsyncGenerator<TInner>) => Promised<TResult>, comparitor: (a: TKey, b: TKey) => boolean): AsyncGenerator<TResult>; | ||
} | ||
} |
@@ -25,3 +25,3 @@ "use strict"; | ||
(0, utils_1.Build)("group_join", function* (inner, outer_key_selector, inner_key_selector, result_selector, comparitor = (a, b) => a === b) { | ||
const inner_array = inner.array(); | ||
const inner_array = Array.isArray(inner) ? inner : inner.array(); | ||
for (const item of this) { | ||
@@ -41,3 +41,3 @@ const key = outer_key_selector(item); | ||
var e_1, _a; | ||
const inner_array = yield __await(inner.array()); | ||
const inner_array = Array.isArray(inner) ? inner : yield __await(inner.array()); | ||
try { | ||
@@ -44,0 +44,0 @@ for (var _b = __asyncValues(this), _c; _c = yield __await(_b.next()), !_c.done;) { |
@@ -5,9 +5,9 @@ import "./array"; | ||
interface Generator<T = unknown, TReturn = any, TNext = unknown> { | ||
join<TKey extends string | number | boolean, TInner, TResult>(inner: Generator<TInner>, outer_key_selector: (item: T) => TKey, inner_key_selector: (item: TInner) => TKey, result_selector: (out: T, inn: TInner) => TResult): Generator<TResult>; | ||
join<TKey, TInner, TResult>(inner: Generator<TInner>, outer_key_selector: (item: T) => TKey, inner_key_selector: (item: TInner) => TKey, result_selector: (out: T, inn: TInner) => TResult, comparitor: (a: TKey, b: TKey) => boolean): Generator<TResult>; | ||
join<TKey extends string | number | boolean, TInner, TResult>(inner: Generator<TInner> | TInner[], outer_key_selector: (item: T) => TKey, inner_key_selector: (item: TInner) => TKey, result_selector: (out: T, inn: TInner) => TResult): Generator<TResult>; | ||
join<TKey, TInner, TResult>(inner: Generator<TInner> | TInner[], outer_key_selector: (item: T) => TKey, inner_key_selector: (item: TInner) => TKey, result_selector: (out: T, inn: TInner) => TResult, comparitor: (a: TKey, b: TKey) => boolean): Generator<TResult>; | ||
} | ||
interface AsyncGenerator<T = unknown, TReturn = any, TNext = unknown> { | ||
join<TKey extends string | number | boolean, TInner, TResult>(inner: AsyncGenerator<TInner>, outer_key_selector: (item: T) => Promised<TKey>, inner_key_selector: (item: TInner) => Promised<TKey>, result_selector: (out: T, inn: TInner) => Promised<TResult>): AsyncGenerator<TResult>; | ||
join<TKey, TInner, TResult>(inner: AsyncGenerator<TInner>, outer_key_selector: (item: T) => Promised<TKey>, inner_key_selector: (item: TInner) => Promised<TKey>, result_selector: (out: T, inn: TInner) => Promised<TResult>, comparitor: (a: TKey, b: TKey) => boolean): AsyncGenerator<TResult>; | ||
join<TKey extends string | number | boolean, TInner, TResult>(inner: AsyncGenerator<TInner> | Generator<TInner> | TInner[], outer_key_selector: (item: T) => Promised<TKey>, inner_key_selector: (item: TInner) => Promised<TKey>, result_selector: (out: T, inn: TInner) => Promised<TResult>): AsyncGenerator<TResult>; | ||
join<TKey, TInner, TResult>(inner: AsyncGenerator<TInner> | Generator<TInner> | TInner[], outer_key_selector: (item: T) => Promised<TKey>, inner_key_selector: (item: TInner) => Promised<TKey>, result_selector: (out: T, inn: TInner) => Promised<TResult>, comparitor: (a: TKey, b: TKey) => boolean): AsyncGenerator<TResult>; | ||
} | ||
} |
@@ -25,3 +25,3 @@ "use strict"; | ||
(0, utils_1.Build)("join", function* (inner, outer_key_selector, inner_key_selector, result_selector, comparitor = (a, b) => a === b) { | ||
const inner_array = inner.array(); | ||
const inner_array = Array.isArray(inner) ? inner : inner.array(); | ||
for (const item of this) { | ||
@@ -39,3 +39,3 @@ const key = outer_key_selector(item); | ||
var e_1, _a; | ||
const inner_array = yield __await(inner.array()); | ||
const inner_array = Array.isArray(inner) ? inner : yield __await(inner.array()); | ||
try { | ||
@@ -42,0 +42,0 @@ for (var _b = __asyncValues(this), _c; _c = yield __await(_b.next()), !_c.done;) { |
{ | ||
"name": "geninq", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "A JavaScript version of the Linq library for Generators", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -7,2 +7,3 @@ import { Build } from "./utils"; | ||
* Converts the sequence into an array. | ||
* @returns The sequence as a type safe array | ||
*/ | ||
@@ -15,2 +16,3 @@ array(): T[]; | ||
* Converts the sequence into an array. | ||
* @returns A promise of the sequence as a type safe array | ||
*/ | ||
@@ -17,0 +19,0 @@ array(): Promise<T[]>; |
@@ -7,2 +7,3 @@ import { Build } from "./utils"; | ||
* Converts the sequence into an AsyncGenerator for asynchrones processing. | ||
* @returns A type safe async generator | ||
*/ | ||
@@ -15,2 +16,4 @@ async(): AsyncGenerator<T, TReturn, TNext>; | ||
* Converts the sequence into an AsyncGenerator for asynchrones processing. | ||
* Present for when you do not know if you sequence is async or not. | ||
* @returns The original subject. | ||
*/ | ||
@@ -28,7 +31,5 @@ async(): AsyncGenerator<T, TReturn, TNext>; | ||
}, | ||
async function* () { | ||
for await (const item of this) { | ||
yield item; | ||
} | ||
function () { | ||
return this; | ||
} | ||
); |
@@ -5,5 +5,18 @@ import { Build, Promised } from "./utils"; | ||
interface Generator<T = unknown, TReturn = any, TNext = unknown> { | ||
/** | ||
* Creates a dictionary from a generator according to a | ||
* specified key selector function. | ||
* @param key_selector A function to extract a key from each element. | ||
* @returns A dictionary that contains values of type selected from the input sequence. | ||
*/ | ||
dictionary<TKey extends number | string>( | ||
key_selector: (item: T) => TKey | ||
): Record<TKey, T>; | ||
/** | ||
* Creates a dictionary from a generator according to | ||
* specified key selector and element selector functions. | ||
* @param key_selector A function to extract a key from each element. | ||
* @param value_selector A transform function to produce a result element value from each element. | ||
* @returns A dictionary that contains values of type selected from the input sequence. | ||
*/ | ||
dictionary<TKey extends number | string, TResult>( | ||
@@ -16,5 +29,18 @@ key_selector: (item: T) => TKey, | ||
interface AsyncGenerator<T = unknown, TReturn = any, TNext = unknown> { | ||
/** | ||
* Creates a dictionary from a generator according to a | ||
* specified key selector function. | ||
* @param key_selector A function to extract a key from each element. | ||
* @returns A dictionary that contains values of type selected from the input sequence. | ||
*/ | ||
dictionary<TKey extends number | string>( | ||
key_selector: (item: T) => Promised<TKey> | ||
): Promise<Record<TKey, T>>; | ||
/** | ||
* Creates a dictionary from a generator according to | ||
* specified key selector and element selector functions. | ||
* @param key_selector A function to extract a key from each element. | ||
* @param value_selector A transform function to produce a result element value from each element. | ||
* @returns A dictionary that contains values of type selected from the input sequence. | ||
*/ | ||
dictionary<TKey extends number | string, TResult>( | ||
@@ -21,0 +47,0 @@ key_selector: (item: T) => Promised<TKey>, |
@@ -5,11 +5,28 @@ import { Build, Promised } from "./utils"; | ||
interface Generator<T = unknown, TReturn = any, TNext = unknown> { | ||
/** | ||
* Returns distinct elements from a sequence by using the default equality comparer to compare values. | ||
* @returns A generator that contains distinct elements from the source sequence. | ||
*/ | ||
distinct( | ||
this: Generator<number, TReturn, TNext> | ||
): Generator<number, TReturn, TNext>; | ||
/** | ||
* Returns distinct elements from a sequence by using the default equality comparer to compare values. | ||
* @returns A generator that contains distinct elements from the source sequence. | ||
*/ | ||
distinct( | ||
this: Generator<string, TReturn, TNext> | ||
): Generator<string, TReturn, TNext>; | ||
/** | ||
* Returns distinct elements from a sequence by using the default equality comparer to compare values. | ||
* @returns A generator that contains distinct elements from the source sequence. | ||
*/ | ||
distinct( | ||
this: Generator<boolean, TReturn, TNext> | ||
): Generator<boolean, TReturn, TNext>; | ||
/** | ||
* Returns distinct elements from a sequence by using a specified comparitor to compare values. | ||
* @param comparitor A comparitor to compare values. | ||
* @returns A generator that contains distinct elements from the source sequence. | ||
*/ | ||
distinct(comparitor: (a: T, b: T) => boolean): Generator<T, TReturn, TNext>; | ||
@@ -19,11 +36,28 @@ } | ||
interface AsyncGenerator<T = unknown, TReturn = any, TNext = unknown> { | ||
/** | ||
* Returns distinct elements from a sequence by using the default equality comparer to compare values. | ||
* @returns A generator that contains distinct elements from the source sequence. | ||
*/ | ||
distinct( | ||
this: AsyncGenerator<number, TReturn, TNext> | ||
): AsyncGenerator<number, TReturn, TNext>; | ||
/** | ||
* Returns distinct elements from a sequence by using the default equality comparer to compare values. | ||
* @returns A generator that contains distinct elements from the source sequence. | ||
*/ | ||
distinct( | ||
this: AsyncGenerator<string, TReturn, TNext> | ||
): AsyncGenerator<string, TReturn, TNext>; | ||
/** | ||
* Returns distinct elements from a sequence by using the default equality comparer to compare values. | ||
* @returns A generator that contains distinct elements from the source sequence. | ||
*/ | ||
distinct( | ||
this: AsyncGenerator<boolean, TReturn, TNext> | ||
): AsyncGenerator<boolean, TReturn, TNext>; | ||
/** | ||
* Returns distinct elements from a sequence by using a specified comparitor to compare values. | ||
* @param comparitor A comparitor to compare values. | ||
* @returns A generator that contains distinct elements from the source sequence. | ||
*/ | ||
distinct( | ||
@@ -30,0 +64,0 @@ comparitor: (a: T, b: T) => Promised<boolean> |
@@ -0,1 +1,5 @@ | ||
/** | ||
* Builds an empty generator with the specified type. | ||
* @returns An empty generator or specified type. | ||
*/ | ||
export function* Empty<T>(): Generator<T> {} |
@@ -5,2 +5,7 @@ import { Build } from "./utils"; | ||
interface Generator<T = unknown, TReturn = any, TNext = unknown> { | ||
/** | ||
* Produces the set difference of two sequences by using the default equality comparer to compare values. | ||
* @param that A sequence whose elements that also occur in the first sequence will cause those elements to be removed from the returned sequence. | ||
* @returns A sequence that contains the set difference of the elements of two sequences. | ||
*/ | ||
except( | ||
@@ -10,2 +15,7 @@ this: Generator<number, TReturn, TNext>, | ||
): Generator<number, TReturn, TNext>; | ||
/** | ||
* Produces the set difference of two sequences by using the default equality comparer to compare values. | ||
* @param that A sequence whose elements that also occur in the first sequence will cause those elements to be removed from the returned sequence. | ||
* @returns A sequence that contains the set difference of the elements of two sequences. | ||
*/ | ||
except( | ||
@@ -15,2 +25,7 @@ this: Generator<string, TReturn, TNext>, | ||
): Generator<string, TReturn, TNext>; | ||
/** | ||
* Produces the set difference of two sequences by using the default equality comparer to compare values. | ||
* @param that A sequence whose elements that also occur in the first sequence will cause those elements to be removed from the returned sequence. | ||
* @returns A sequence that contains the set difference of the elements of two sequences. | ||
*/ | ||
except( | ||
@@ -20,2 +35,8 @@ this: Generator<boolean, TReturn, TNext>, | ||
): Generator<boolean, TReturn, TNext>; | ||
/** | ||
* Produces the set difference of two sequences by using the default equality comparer to compare values. | ||
* @param that A sequence whose elements that also occur in the first sequence will cause those elements to be removed from the returned sequence. | ||
* @param comparitor A comparitor function to compare values. | ||
* @returns A sequence that contains the set difference of the elements of two sequences. | ||
*/ | ||
except( | ||
@@ -28,2 +49,7 @@ that: Generator<T, TReturn, TNext> | T[], | ||
interface AsyncGenerator<T = unknown, TReturn = any, TNext = unknown> { | ||
/** | ||
* Produces the set difference of two sequences by using the default equality comparer to compare values. | ||
* @param that A sequence whose elements that also occur in the first sequence will cause those elements to be removed from the returned sequence. | ||
* @returns A sequence that contains the set difference of the elements of two sequences. | ||
*/ | ||
except( | ||
@@ -36,2 +62,7 @@ this: AsyncGenerator<number, TReturn, TNext>, | ||
): AsyncGenerator<number, TReturn, TNext>; | ||
/** | ||
* Produces the set difference of two sequences by using the default equality comparer to compare values. | ||
* @param that A sequence whose elements that also occur in the first sequence will cause those elements to be removed from the returned sequence. | ||
* @returns A sequence that contains the set difference of the elements of two sequences. | ||
*/ | ||
except( | ||
@@ -44,2 +75,7 @@ this: AsyncGenerator<string, TReturn, TNext>, | ||
): AsyncGenerator<string, TReturn, TNext>; | ||
/** | ||
* Produces the set difference of two sequences by using the default equality comparer to compare values. | ||
* @param that A sequence whose elements that also occur in the first sequence will cause those elements to be removed from the returned sequence. | ||
* @returns A sequence that contains the set difference of the elements of two sequences. | ||
*/ | ||
except( | ||
@@ -52,2 +88,8 @@ this: AsyncGenerator<boolean, TReturn, TNext>, | ||
): AsyncGenerator<boolean, TReturn, TNext>; | ||
/** | ||
* Produces the set difference of two sequences by using the default equality comparer to compare values. | ||
* @param that A sequence whose elements that also occur in the first sequence will cause those elements to be removed from the returned sequence. | ||
* @param comparitor A comparitor function to compare values. | ||
* @returns A sequence that contains the set difference of the elements of two sequences. | ||
*/ | ||
except( | ||
@@ -54,0 +96,0 @@ that: |
@@ -6,5 +6,25 @@ import { IsString } from "@paulpopat/safe-type"; | ||
interface Generator<T = unknown, TReturn = any, TNext = unknown> { | ||
/** | ||
* Returns the first element of a sequence. | ||
* @returns The first element in the specified sequence. | ||
*/ | ||
first(): T; | ||
/** | ||
* Returns the first element of a sequence, or undefined if the sequence contains no elements. | ||
* @param allow_empty Specify that the sequence may be empty. | ||
* @returns The first element in the specified sequence, or undefined if the sequence is empty. | ||
*/ | ||
first(allow_empty: "or-default"): T | undefined; | ||
/** | ||
* Returns the first element in a sequence that satisfies a specified condition. | ||
* @param predicate A function to test each element for a condition. | ||
* @returns The first element in the sequence that passes the test in the specified predicate function. | ||
*/ | ||
first(predicate: (item: T) => boolean): T; | ||
/** | ||
* Returns the first element of the sequence that satisfies a condition or a default value if no such element is found. | ||
* @param predicate A function to test each element for a condition. | ||
* @param allow_empty Specify that the sequence may be empty. | ||
* @returns The first element in the sequence that passes the test in the specified predicate function. | ||
*/ | ||
first( | ||
@@ -14,3 +34,14 @@ predicate: (item: T) => boolean, | ||
): T | undefined; | ||
/** | ||
* Returns the first element in a sequence that matches the type specified in the type checker. | ||
* @param predicate A function to test each element for a type. | ||
* @returns The first element in the sequence that passes the test in the specified predicate function. | ||
*/ | ||
first<TRes extends T>(predicate: (item: T) => item is TRes): TRes; | ||
/** | ||
* Returns the first element of the sequence that matches the type specified in the type checker or a default value if no such element is found. | ||
* @param predicate A function to test each element for a type. | ||
* @param allow_empty Specify that the sequence may be empty. | ||
* @returns The first element in the sequence that passes the test in the specified predicate function. | ||
*/ | ||
first<TRes extends T>( | ||
@@ -23,10 +54,41 @@ predicate: (item: T) => item is TRes, | ||
interface AsyncGenerator<T = unknown, TReturn = any, TNext = unknown> { | ||
/** | ||
* Returns the first element of a sequence. | ||
* @returns The first element in the specified sequence. | ||
*/ | ||
first(): Promise<T>; | ||
first(allow_empty: true): Promise<T | undefined>; | ||
/** | ||
* Returns the first element of a sequence, or undefined if the sequence contains no elements. | ||
* @param allow_empty Specify that the sequence may be empty. | ||
* @returns The first element in the specified sequence, or undefined if the sequence is empty. | ||
*/ | ||
first(allow_empty: "or-default"): Promise<T | undefined>; | ||
/** | ||
* Returns the first element in a sequence that satisfies a specified condition. | ||
* @param predicate A function to test each element for a condition. | ||
* @returns The first element in the sequence that passes the test in the specified predicate function. | ||
*/ | ||
first(predicate: (item: T) => Promised<boolean>): Promise<T>; | ||
/** | ||
* Returns the first element of the sequence that satisfies a condition or a default value if no such element is found. | ||
* @param predicate A function to test each element for a condition. | ||
* @param allow_empty Specify that the sequence may be empty. | ||
* @returns The first element in the sequence that passes the test in the specified predicate function. | ||
*/ | ||
first( | ||
predicate: (item: T) => Promised<boolean>, | ||
allow_empty: true | ||
allow_empty: "or-default" | ||
): Promise<T | undefined>; | ||
/** | ||
* Returns the first element in a sequence that matches the type specified in the type checker. | ||
* @param predicate A function to test each element for a type. | ||
* @returns The first element in the sequence that passes the test in the specified predicate function. | ||
*/ | ||
first<TRes extends T>(predicate: (item: T) => item is TRes): Promise<TRes>; | ||
/** | ||
* Returns the first element of the sequence that matches the type specified in the type checker or a default value if no such element is found. | ||
* @param predicate A function to test each element for a type. | ||
* @param allow_empty Specify that the sequence may be empty. | ||
* @returns The first element in the sequence that passes the test in the specified predicate function. | ||
*/ | ||
first<TRes extends T>( | ||
@@ -33,0 +95,0 @@ predicate: (item: T) => item is TRes, |
import { Build } from "./utils"; | ||
declare global { | ||
/** | ||
* Builds a generator from an array. | ||
* @returns A generator with the same type as the array. | ||
*/ | ||
interface Array<T> { | ||
@@ -5,0 +9,0 @@ geninq(): Generator<T, void, unknown>; |
@@ -7,2 +7,7 @@ import { IsFunction } from "@paulpopat/safe-type"; | ||
interface Generator<T = unknown, TReturn = any, TNext = unknown> { | ||
/** | ||
* Groups the elements of a sequence according to a specified key selector function. | ||
* @param builder A function to extract the key for each element. | ||
* @returns A collection of elements of the type specified where each element represents a projection over a group and its key. | ||
*/ | ||
group_by< | ||
@@ -15,2 +20,9 @@ TKey extends string | number | boolean, | ||
): Generator<TResult, TReturn, TNext>; | ||
/** | ||
* Groups the elements of a sequence according to a specified key selector function | ||
* and projects the elements for each group by using a specified function. | ||
* @param builder.key A function to extract the key for each element. | ||
* @param builder.element A function to map each source element to an element in an grouping. | ||
* @returns A collection of elements of the type specified where each element represents a projection over a group and its key. | ||
*/ | ||
group_by< | ||
@@ -24,2 +36,11 @@ TKey extends string | number | boolean, | ||
}): Generator<TResult, TReturn, TNext>; | ||
/** | ||
* Groups the elements of a sequence according to a key selector function. | ||
* The keys are compared by using a comparer and each group's elements are | ||
* projected by using a specified function. | ||
* @param builder.key A function to extract the key for each element. | ||
* @param builder.element A function to map each source element to an element in an grouping. | ||
* @param comparitor A comparitor function to compare keys with. | ||
* @returns A collection of elements of the type specified where each element represents a projection over a group and its key. | ||
*/ | ||
group_by<TKey, TElement = T, TResult = [TKey, Generator<TElement>]>( | ||
@@ -32,2 +53,9 @@ builder: { | ||
): Generator<TResult, TReturn, TNext>; | ||
/** | ||
* Groups the elements of a sequence according to a specified key selector | ||
* function and compares the keys by using a specified comparer. | ||
* @param builder A function to extract the key for each element. | ||
* @param comparitor A comparitor function to compare keys with. | ||
* @returns A collection of elements of the type specified where each element represents a projection over a group and its key. | ||
*/ | ||
group_by<TKey, TElement = T, TResult = [TKey, Generator<TElement>]>( | ||
@@ -40,2 +68,7 @@ builder: (item: T) => TKey, | ||
interface AsyncGenerator<T = unknown, TReturn = any, TNext = unknown> { | ||
/** | ||
* Groups the elements of a sequence according to a specified key selector function. | ||
* @param builder A function to extract the key for each element. | ||
* @returns A collection of elements of the type specified where each element represents a projection over a group and its key. | ||
*/ | ||
group_by< | ||
@@ -48,2 +81,9 @@ TKey extends string | number | boolean, | ||
): AsyncGenerator<TResult, TReturn, TNext>; | ||
/** | ||
* Groups the elements of a sequence according to a specified key selector function | ||
* and projects the elements for each group by using a specified function. | ||
* @param builder.key A function to extract the key for each element. | ||
* @param builder.element A function to map each source element to an element in an grouping. | ||
* @returns A collection of elements of the type specified where each element represents a projection over a group and its key. | ||
*/ | ||
group_by< | ||
@@ -57,2 +97,11 @@ TKey extends string | number | boolean, | ||
}): AsyncGenerator<TResult, TReturn, TNext>; | ||
/** | ||
* Groups the elements of a sequence according to a key selector function. | ||
* The keys are compared by using a comparer and each group's elements are | ||
* projected by using a specified function. | ||
* @param builder.key A function to extract the key for each element. | ||
* @param builder.element A function to map each source element to an element in an grouping. | ||
* @param comparitor A comparitor function to compare keys with. | ||
* @returns A collection of elements of the type specified where each element represents a projection over a group and its key. | ||
*/ | ||
group_by<TKey, TElement = T, TResult = [TKey, AsyncGenerator<TElement>]>( | ||
@@ -62,2 +111,9 @@ builder: (item: T) => Promised<TKey>, | ||
): AsyncGenerator<TResult, TReturn, TNext>; | ||
/** | ||
* Groups the elements of a sequence according to a specified key selector | ||
* function and compares the keys by using a specified comparer. | ||
* @param builder A function to extract the key for each element. | ||
* @param comparitor A comparitor function to compare keys with. | ||
* @returns A collection of elements of the type specified where each element represents a projection over a group and its key. | ||
*/ | ||
group_by<TKey, TElement = T, TResult = [TKey, AsyncGenerator<TElement>]>( | ||
@@ -64,0 +120,0 @@ builder: { |
@@ -159,1 +159,72 @@ import { SIt, AIt } from "../test-utils"; | ||
); | ||
SIt( | ||
"Performs a basic join on an array", | ||
(s) => | ||
s | ||
.group_join( | ||
[ | ||
{ name: "Barley", owner: people[1] }, | ||
{ name: "Boots", owner: people[1] }, | ||
{ name: "Whiskers", owner: people[2] }, | ||
{ name: "Daisy", owner: people[0] }, | ||
], | ||
(person) => person.name, | ||
(pet) => pet.owner.name, | ||
(a, b) => ({ | ||
owner: a.name, | ||
pets: b.select((p) => p.name).array(), | ||
}) | ||
) | ||
.array(), | ||
[ | ||
{ | ||
owner: "Hedlund, Magnus", | ||
pets: ["Daisy"], | ||
}, | ||
{ | ||
owner: "Adams, Terry", | ||
pets: ["Barley", "Boots"], | ||
}, | ||
{ | ||
owner: "Weiss, Charlotte", | ||
pets: ["Whiskers"], | ||
}, | ||
], | ||
people | ||
); | ||
AIt( | ||
"Performs a basic join on an array", | ||
(s) => | ||
s | ||
.group_join( | ||
[ | ||
{ name: "Barley", owner: people[1] }, | ||
{ name: "Boots", owner: people[1] }, | ||
{ name: "Whiskers", owner: people[2] }, | ||
{ name: "Daisy", owner: people[0] }, | ||
], | ||
(person) => person.name, | ||
(pet) => pet.owner.name, | ||
async (a, b) => ({ | ||
owner: a.name, | ||
pets: await b.select((p) => p.name).array(), | ||
}) | ||
) | ||
.array(), | ||
[ | ||
{ | ||
owner: "Hedlund, Magnus", | ||
pets: ["Daisy"], | ||
}, | ||
{ | ||
owner: "Adams, Terry", | ||
pets: ["Barley", "Boots"], | ||
}, | ||
{ | ||
owner: "Weiss, Charlotte", | ||
pets: ["Whiskers"], | ||
}, | ||
], | ||
people | ||
); |
@@ -6,4 +6,13 @@ import "./array"; | ||
interface Generator<T = unknown, TReturn = any, TNext = unknown> { | ||
/** | ||
* Correlates the elements of two sequences based on equality of keys | ||
* and groups the results. The default equality comparer is used to compare keys. | ||
* @param inner The sequence to join to the first sequence. | ||
* @param outer_key_selector A function to extract the join key from each element of the first sequence. | ||
* @param inner_key_selector A function to extract the join key from each element of the second sequence. | ||
* @param result_selector A function to create a result element from an element from the first sequence and a collection of matching elements from the second sequence. | ||
* @returns A generator that contains elements of type TResult that are obtained by performing a grouped join on two sequences. | ||
*/ | ||
group_join<TKey extends string | number | boolean, TInner, TResult>( | ||
inner: Generator<TInner>, | ||
inner: Generator<TInner> | TInner[], | ||
outer_key_selector: (item: T) => TKey, | ||
@@ -13,4 +22,14 @@ inner_key_selector: (item: TInner) => TKey, | ||
): Generator<TResult>; | ||
/** | ||
* Correlates the elements of two sequences based on key equality and groups the results. | ||
* A specified comparitor is used to compare keys. | ||
* @param inner The sequence to join to the first sequence. | ||
* @param outer_key_selector A function to extract the join key from each element of the first sequence. | ||
* @param inner_key_selector A function to extract the join key from each element of the second sequence. | ||
* @param result_selector A function to create a result element from an element from the first sequence and a collection of matching elements from the second sequence. | ||
* @param comparitor A comparitor function to hash and compare keys. | ||
* @returns A generator that contains elements of type TResult that are obtained by performing a grouped join on two sequences. | ||
*/ | ||
group_join<TKey, TInner, TResult>( | ||
inner: Generator<TInner>, | ||
inner: Generator<TInner> | TInner[], | ||
outer_key_selector: (item: T) => TKey, | ||
@@ -24,4 +43,13 @@ inner_key_selector: (item: TInner) => TKey, | ||
interface AsyncGenerator<T = unknown, TReturn = any, TNext = unknown> { | ||
/** | ||
* Correlates the elements of two sequences based on equality of keys | ||
* and groups the results. The default equality comparer is used to compare keys. | ||
* @param inner The sequence to join to the first sequence. | ||
* @param outer_key_selector A function to extract the join key from each element of the first sequence. | ||
* @param inner_key_selector A function to extract the join key from each element of the second sequence. | ||
* @param result_selector A function to create a result element from an element from the first sequence and a collection of matching elements from the second sequence. | ||
* @returns A generator that contains elements of type TResult that are obtained by performing a grouped join on two sequences. | ||
*/ | ||
group_join<TKey extends string | number | boolean, TInner, TResult>( | ||
inner: AsyncGenerator<TInner>, | ||
inner: AsyncGenerator<TInner> | Generator<TInner> | TInner[], | ||
outer_key_selector: (item: T) => Promised<TKey>, | ||
@@ -34,4 +62,14 @@ inner_key_selector: (item: TInner) => Promised<TKey>, | ||
): AsyncGenerator<TResult>; | ||
/** | ||
* Correlates the elements of two sequences based on key equality and groups the results. | ||
* A specified comparitor is used to compare keys. | ||
* @param inner The sequence to join to the first sequence. | ||
* @param outer_key_selector A function to extract the join key from each element of the first sequence. | ||
* @param inner_key_selector A function to extract the join key from each element of the second sequence. | ||
* @param result_selector A function to create a result element from an element from the first sequence and a collection of matching elements from the second sequence. | ||
* @param comparitor A comparitor function to hash and compare keys. | ||
* @returns A generator that contains elements of type TResult that are obtained by performing a grouped join on two sequences. | ||
*/ | ||
group_join<TKey, TInner, TResult>( | ||
inner: AsyncGenerator<TInner>, | ||
inner: AsyncGenerator<TInner> | Generator<TInner> | TInner[], | ||
outer_key_selector: (item: T) => Promised<TKey>, | ||
@@ -57,3 +95,3 @@ inner_key_selector: (item: TInner) => Promised<TKey>, | ||
) { | ||
const inner_array = inner.array(); | ||
const inner_array = Array.isArray(inner) ? inner : inner.array(); | ||
for (const item of this) { | ||
@@ -80,3 +118,3 @@ const key = outer_key_selector(item); | ||
) { | ||
const inner_array = await inner.array(); | ||
const inner_array = Array.isArray(inner) ? inner : await inner.array(); | ||
for await (const item of this) { | ||
@@ -83,0 +121,0 @@ const key = await Promise.resolve(outer_key_selector(item)); |
@@ -43,1 +43,40 @@ import { It } from "../test-utils"; | ||
); | ||
It( | ||
"Performs a basic join on an array", | ||
(s) => | ||
s | ||
.join( | ||
[ | ||
{ name: "Test Pet 1", owner: "Test1" }, | ||
{ name: "Test Pet 2", owner: "Test2" }, | ||
{ name: "Test Pet 3", owner: "Paul Popat" }, | ||
{ name: "Test Pet 4", owner: "Test1" }, | ||
{ name: "Test Pet 5", owner: "Test2" }, | ||
{ name: "Test Pet 6", owner: "Paul Popat" }, | ||
{ name: "Test Pet 7", owner: "Test1" }, | ||
], | ||
(i) => i.name, | ||
(i) => i.owner, | ||
(owner, pet) => ({ | ||
name: pet.name, | ||
owner: owner.name, | ||
type: owner.type, | ||
}) | ||
) | ||
.array(), | ||
[ | ||
{ owner: "Test1", type: 2, name: "Test Pet 1" }, | ||
{ owner: "Test1", type: 2, name: "Test Pet 4" }, | ||
{ owner: "Test1", type: 2, name: "Test Pet 7" }, | ||
{ owner: "Test2", type: 1, name: "Test Pet 2" }, | ||
{ owner: "Test2", type: 1, name: "Test Pet 5" }, | ||
{ owner: "Paul Popat", type: 2, name: "Test Pet 3" }, | ||
{ owner: "Paul Popat", type: 2, name: "Test Pet 6" }, | ||
], | ||
[ | ||
{ name: "Test1", type: 2 }, | ||
{ name: "Test2", type: 1 }, | ||
{ name: "Paul Popat", type: 2 }, | ||
] | ||
); |
@@ -7,3 +7,3 @@ import "./array"; | ||
join<TKey extends string | number | boolean, TInner, TResult>( | ||
inner: Generator<TInner>, | ||
inner: Generator<TInner> | TInner[], | ||
outer_key_selector: (item: T) => TKey, | ||
@@ -14,3 +14,3 @@ inner_key_selector: (item: TInner) => TKey, | ||
join<TKey, TInner, TResult>( | ||
inner: Generator<TInner>, | ||
inner: Generator<TInner> | TInner[], | ||
outer_key_selector: (item: T) => TKey, | ||
@@ -25,3 +25,3 @@ inner_key_selector: (item: TInner) => TKey, | ||
join<TKey extends string | number | boolean, TInner, TResult>( | ||
inner: AsyncGenerator<TInner>, | ||
inner: AsyncGenerator<TInner> | Generator<TInner> | TInner[], | ||
outer_key_selector: (item: T) => Promised<TKey>, | ||
@@ -32,3 +32,3 @@ inner_key_selector: (item: TInner) => Promised<TKey>, | ||
join<TKey, TInner, TResult>( | ||
inner: AsyncGenerator<TInner>, | ||
inner: AsyncGenerator<TInner> | Generator<TInner> | TInner[], | ||
outer_key_selector: (item: T) => Promised<TKey>, | ||
@@ -51,3 +51,3 @@ inner_key_selector: (item: TInner) => Promised<TKey>, | ||
) { | ||
const inner_array = inner.array(); | ||
const inner_array = Array.isArray(inner) ? inner : inner.array(); | ||
for (const item of this) { | ||
@@ -69,3 +69,3 @@ const key = outer_key_selector(item); | ||
) { | ||
const inner_array = await inner.array(); | ||
const inner_array = Array.isArray(inner) ? inner : await inner.array(); | ||
for await (const item of this) { | ||
@@ -72,0 +72,0 @@ const key = await Promise.resolve(outer_key_selector(item)); |
284342
6905