Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 40 additions & 1 deletion src/arrayf/arrayf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,44 @@ export class arrayf {
return output;
}

/**
* Given a set of values and score functions returns the value with the
* lowest score. If two objects tie using the first score function then the
* next given score function (if any) is used to break the tie.
* ```
* const values = [2.2, 2.1, 3.4];
* const fnA = (x) => Math.floor(x);
* const fnB = (x) => x;
* arrayf.min(values, fnA); // Returns 2.2
* arrayf.min(values, fnA, fnB); // Returns 2.1
* ```
*/
static min<T>(values: T[], ...scoreFns: Array<(v: T) => number>): T {
let minValue: T;
let minScore: number = Number.POSITIVE_INFINITY;

const scoreFn = scoreFns[0];

}
values.forEach((value) => {
const score = scoreFn(value);
if (minScore > score) {
minValue = value;
minScore = score;
} else if (minScore === score && scoreFns.length > 1) {
let i = 1;
let tieBreaker = scoreFns[i];
while (
i < scoreFns.length &&
tieBreaker(minValue) === tieBreaker(value)
) {
tieBreaker = scoreFns[i++];
}

if (tieBreaker(minValue) > tieBreaker(value)) {
minValue = value;
}
}
});
return minValue;
}
}