Skip to content

Commit

Permalink
junhao review
Browse files Browse the repository at this point in the history
  • Loading branch information
davemarco committed Oct 2, 2024
1 parent 8cbe208 commit 0e8b80d
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 111 deletions.
79 changes: 79 additions & 0 deletions new-log-viewer/src/utils/data.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,82 @@
import {Nullable} from "../typings/common";

Check failure on line 1 in new-log-viewer/src/utils/data.ts

View workflow job for this annotation

GitHub Actions / lint-check

Expected 2 empty lines after import statement not followed by another import

/**

Check warning on line 3 in new-log-viewer/src/utils/data.ts

View workflow job for this annotation

GitHub Actions / lint-check

Missing JSDoc @param "length" declaration
* Performs binary search to find the smallest index `i` in the range [0, length) where the
* `conditionFn` is true. Assumes that the `conditionFn` is false for some prefix of the
* input range and true for the remainder. The most common use is find the index `i` of
* a value x in a sorted array.
*
* @param n The length of the range to search.

Check warning on line 9 in new-log-viewer/src/utils/data.ts

View workflow job for this annotation

GitHub Actions / lint-check

Expected @param names to be "length, conditionFn". Got "n, conditionFn"
* @param conditionFn A function that takes an index and returns `true` or `false`.
* @return The smallest index where `conditionFn(i)` is true. If no such index exists, returns `length`.

Check warning on line 11 in new-log-viewer/src/utils/data.ts

View workflow job for this annotation

GitHub Actions / lint-check

This line has a comment length of 104. Maximum allowed is 100
* @example
* const arr = [1, 3, 5, 7, 10, 15, 20];
* const result = binarySearch(arr.length, (i) => arr[i] >= 10);
* console.log(result); // Output: 4 (since arr[4] is 10).
*/
const binarySearch = (length: number, conditionFn: (index: number) => boolean): number => {
// Generic implementation based on Go standard library implementation.
// Reference: https://pkg.go.dev/sort#Search
let i = 0;
let j = length;

while (i < j) {
const mid = Math.floor((i + j) / 2);

if (false === conditionFn(mid)) {
i = mid + 1;
} else {
j = mid;
}
}

return i;
};

/**
* Finds the largest index `i` in a sorted array `data` such that `data[i] <= x`.
* Uses binary search for efficiency. Returns 0 if `x` is less than `data[0]`.
*
* @param data Sorted array.
* @param x Target value.
* @return The largest index where `data[i] <= x`. There are 2 edge cases where returns:
* - 0 if `x` is less than `data[0]`.
* - `null` if array is empty.
* @example
* const arr = [2, 3, 5, 7, 10, 15, 20];
* const result = findLargestIdxLte(arr, 8);
* console.log(result); // Output: 3 (since arr[3] is 7).
*/
const findLargestIdxLte = (data: number[], x: number): Nullable<number> => {
const {length} = data;

if (0 === length) {
return null;
}

// Binary search to find the first index where data[i] > x.
const firstGreaterIdx: number = binarySearch(length, (i) => data[i] as number > x);

if (0 === firstGreaterIdx) {
return 0;
}

return firstGreaterIdx - 1;
};

/** Checks if 'x' is bounded by the first and last value in a sorted array of numbers.

Check warning on line 67 in new-log-viewer/src/utils/data.ts

View workflow job for this annotation

GitHub Actions / lint-check

Should have no text on the "0th" line (after the `/**`)
*
* @param data Sorted array.
* @param x Target value.
* @return True if is `x` is within bounds and false is outside of bounds or array is empty.
*/

Check warning on line 72 in new-log-viewer/src/utils/data.ts

View workflow job for this annotation

GitHub Actions / lint-check

Expected JSDoc block to be aligned
const isWithinBounds = (data: number[], x: number): boolean => {
const {length} = data;
if (length === 0) {

Check failure on line 75 in new-log-viewer/src/utils/data.ts

View workflow job for this annotation

GitHub Actions / lint-check

Expected literal to be on the left side of ===
return false;
}
return (x >= (data[0] as number)) && (x <= (data[length - 1] as number));

Check warning on line 78 in new-log-viewer/src/utils/data.ts

View workflow job for this annotation

GitHub Actions / lint-check

Expected blank line before this statement
}

Check failure on line 79 in new-log-viewer/src/utils/data.ts

View workflow job for this annotation

GitHub Actions / lint-check

Missing semicolon

/**
* Finds the key in a map based on the provided value.
Expand Down Expand Up @@ -39,6 +116,8 @@ const getMapValueWithNearestLessThanOrEqualKey = <T>(
};

export {

Check warning on line 118 in new-log-viewer/src/utils/data.ts

View workflow job for this annotation

GitHub Actions / lint-check

Run autofix to sort these exports!
findLargestIdxLte,
isWithinBounds,
getMapKeyByValue,
getMapValueWithNearestLessThanOrEqualKey,
};
111 changes: 0 additions & 111 deletions new-log-viewer/src/utils/search.ts

This file was deleted.

0 comments on commit 0e8b80d

Please sign in to comment.