mirror of
https://github.com/arcodange-org/mediabunny.git
synced 2026-09-27 19:03:46 +02:00
Extract insertSorted function
This commit is contained in:
@@ -292,6 +292,12 @@ export const binarySearchLessOrEqual = <T>(arr: T[], key: number, valueGetter: (
|
||||
return ans;
|
||||
};
|
||||
|
||||
/** Assumes the array is already sorted. */
|
||||
export const insertSorted = <T>(arr: T[], item: T, valueGetter: (x: T) => number) => {
|
||||
const insertionIndex = binarySearchLessOrEqual(arr, valueGetter(item), valueGetter);
|
||||
arr.splice(insertionIndex + 1, 0, item); // This even behaves correctly for the -1 case
|
||||
};
|
||||
|
||||
export const promiseWithResolvers = <T = void>() => {
|
||||
let resolve: (value: T) => void;
|
||||
let reject: (reason: unknown) => void;
|
||||
|
||||
Reference in New Issue
Block a user