Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
export function maxBy(source: Queryable, keySelector: (value: T) => K, keyComparer: Comparison | Comparer = Comparer.defaultComparer): T | undefined {
if (typeof keyComparer === "function") keyComparer = Comparer.create(keyComparer);
assert.mustBeQueryable(source, "source");
assert.mustBeFunction(keySelector, "keySelector");
assert.mustBeComparer(keyComparer, "keyComparer");
let hasResult = false;
let result: T | undefined;
let resultKey: K | undefined;
for (const element of ToIterable(source)) {
const key = keySelector(element);
if (!hasResult) {
result = element;
resultKey = key;
hasResult = true;
}
else if (keyComparer.compare(key, resultKey!) > 0) {
result = element;
export async function minByAsync(source: AsyncQueryable, keySelector: (value: T) => K, keyComparer: Comparison | Comparer = Comparer.defaultComparer): Promise {
if (typeof keyComparer === "function") keyComparer = Comparer.create(keyComparer);
assert.mustBeAsyncQueryable(source, "source");
assert.mustBeFunction(keySelector, "keySelector");
assert.mustBeComparer(keyComparer, "keyComparer");
let hasResult = false;
let result: T | undefined;
let resultKey: K | undefined;
for await (const element of ToPossiblyAsyncIterable(source)) {
const key = keySelector(element);
if (!hasResult) {
result = element;
resultKey = key;
hasResult = true;
}
else if (keyComparer.compare(key, resultKey!) < 0) {
result = element;
export async function minAsync(source: AsyncQueryable, comparer: Comparison | Comparer = Comparer.defaultComparer): Promise {
if (typeof comparer === "function") comparer = Comparer.create(comparer);
assert.mustBeAsyncQueryable(source, "source");
assert.mustBeComparer(comparer, "comparer");
return minByAsync(source, identity, comparer);
}
export async function maxByAsync(source: AsyncQueryable, keySelector: (value: T) => K, keyComparer: Comparison | Comparer = Comparer.defaultComparer): Promise {
if (typeof keyComparer === "function") keyComparer = Comparer.create(keyComparer);
assert.mustBeAsyncQueryable(source, "source");
assert.mustBeFunction(keySelector, "keySelector");
assert.mustBeComparer(keyComparer, "keyComparer");
let hasResult = false;
let result: T | undefined;
let resultKey: K | undefined;
for await (const element of ToPossiblyAsyncIterable(source)) {
const key = keySelector(element);
if (!hasResult) {
result = element;
resultKey = key;
hasResult = true;
}
else if (keyComparer.compare(key, resultKey!) > 0) {
result = element;
export function compare(x: T, y: T): number {
return Comparer.defaultComparer.compare(x, y);
}
export function thenByDescending(source: OrderedIterable, keySelector: (element: T) => K, keyComparer: Comparison | Comparer = Comparer.defaultComparer): OrderedIterable {
if (typeof keyComparer === "function") keyComparer = Comparer.create(keyComparer);
assert.mustBeOrderedIterable(source, "source");
assert.mustBeFunction(keySelector, "keySelector");
assert.mustBeComparer(keyComparer, "keyComparer");
return FlowHierarchy(ThenBy(source, keySelector, keyComparer, /*descending*/ true), source);
}
export function minBy(source: Queryable, keySelector: (value: T) => K, keyComparer: Comparison | Comparer = Comparer.defaultComparer): T | undefined {
if (typeof keyComparer === "function") keyComparer = Comparer.create(keyComparer);
assert.mustBeQueryable(source, "source");
assert.mustBeFunction(keySelector, "keySelector");
assert.mustBeComparer(keyComparer, "keyComparer");
let hasResult = false;
let result: T | undefined;
let resultKey: K | undefined;
for (const element of ToIterable(source)) {
const key = keySelector(element);
if (!hasResult) {
result = element;
resultKey = key;
hasResult = true;
}
else if (keyComparer.compare(key, resultKey!) < 0) {
result = element;
export function max(source: Queryable, comparer: Comparison | Comparer = Comparer.defaultComparer): T | undefined {
if (typeof comparer === "function") comparer = Comparer.create(comparer);
assert.mustBeQueryable(source, "source");
assert.mustBeComparer(comparer, "comparer");
return maxBy(source, identity, comparer);
}
export function thenByDescendingAsync(source: PossiblyAsyncOrderedIterable, keySelector: (element: T) => K, keyComparer: Comparison | Comparer = Comparer.defaultComparer): AsyncOrderedIterable {
if (typeof keyComparer === "function") keyComparer = Comparer.create(keyComparer);
assert.mustBePossiblyAsyncOrderedIterable(source, "source");
assert.mustBeFunction(keySelector, "keySelector");
assert.mustBeComparer(keyComparer, "keyComparer");
return FlowHierarchy(ThenByAsync(ToAsyncOrderedIterable(source), keySelector, keyComparer, /*descending*/ true), source);
}
export function min(source: Queryable, comparer: Comparison | Comparer = Comparer.defaultComparer): T | undefined {
if (typeof comparer === "function") comparer = Comparer.create(comparer);
assert.mustBeQueryable(source, "source");
assert.mustBeComparer(comparer, "comparer");
return minBy(source, identity, comparer);
}