-
Notifications
You must be signed in to change notification settings - Fork 176
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add tests * Test setInitialState * Add test * Add test * Add test * Typo * Add test * Add tests * Add tests for handleContextmenu * Add tests for handleDblclick * Add tests for touchStart * Add test for touchend * Fix test * Test touchMove * Remove check for current item * binarySearch * Lint * Add test for drag and drop handler * Add jsdom-testing-mocks * Set skipLibCheck option because jsdom-testing-mocks types fail * Don't check multiple times for current element * Extract mockLayout * Use generateHtmlElementsForTree * Add test * Test mouseCapture * Test mouseStart * Wip * Test mouseDrag * Test mouseDrag * Add tests
- Loading branch information
Showing
10 changed files
with
1,330 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
function binarySearch<T>(items: T[], compareFn: (a: T) => number): null | T { | ||
let low = 0; | ||
let high = items.length; | ||
|
||
while (low < high) { | ||
const mid = (low + high) >> 1; | ||
const item = items[mid]; | ||
|
||
if (item === undefined) { | ||
return null; | ||
} | ||
|
||
const compareResult = compareFn(item); | ||
|
||
if (compareResult > 0) { | ||
high = mid; | ||
} else if (compareResult < 0) { | ||
low = mid + 1; | ||
} else { | ||
return item; | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
|
||
export default binarySearch; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import binarySearch from "../../dragAndDropHandler/binarySearch"; | ||
|
||
it("returns null when the array is empty", () => { | ||
const compareFn = (_item: number) => 0; | ||
|
||
const result = binarySearch<number>([], compareFn); | ||
expect(result).toBeNull(); | ||
}); | ||
|
||
it("finds a value", () => { | ||
const compareFn = (item: number) => item - 5; | ||
|
||
const result = binarySearch<number>([1, 5, 7, 9], compareFn); | ||
expect(result).toEqual(5); | ||
}); | ||
|
||
it("returns null when the value doesn't exist", () => { | ||
const compareFn = (item: number) => item - 6; | ||
|
||
const result = binarySearch<number>([1, 5, 7, 9], compareFn); | ||
expect(result).toBeNull(); | ||
}); | ||
|
||
it("handles undefined values in the array", () => { | ||
const compareFn = (item: number) => item - 6; | ||
const array = [1, 5, 7, 9]; | ||
(array as any)[1] = undefined; // eslint-disable-line @typescript-eslint/no-unsafe-member-access | ||
|
||
const result = binarySearch<number>(array, compareFn); | ||
expect(result).toBeNull(); | ||
}); |
Oops, something went wrong.