Skip to content

Commit 5356ad6

Browse files
committed
refactor: cleanup
1 parent 75ae4fc commit 5356ad6

File tree

21 files changed

+90
-702
lines changed

21 files changed

+90
-702
lines changed

src/lib/jshelper.ts

Lines changed: 0 additions & 691 deletions
This file was deleted.

src/lib/VirtualList2.svelte renamed to src/lib/new/VirtualList2.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
type AfterScrollEvent,
3939
type VirtualListModel,
4040
type VirtualRangeEvent
41-
} from '.';
41+
} from '..';
4242
4343
import { binarySearch } from './jshelper';
4444
import clsx from 'clsx';

src/lib/new/jshelper.ts

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
2+
type BinarySearchReturn<T> = {
3+
index: number;
4+
value: T;
5+
count: number;
6+
hit: boolean;
7+
greater?: boolean;
8+
} | null;
9+
10+
interface BinarySearchOptions {
11+
start?: number;
12+
end?: number;
13+
returnNearestIfNoHit?: boolean;
14+
maxTimes?: number;
15+
}
16+
/**
17+
* binarySearch, 二分查找
18+
* @param arr
19+
* @param callback return `mid - your_value` for ascending array
20+
* @param opt
21+
* @returns
22+
*/
23+
export function binarySearch<T>(
24+
arr: T[],
25+
callback: (mid: T, index: number, count: number) => number,
26+
opt: BinarySearchOptions = {}
27+
): BinarySearchReturn<T> {
28+
opt = {
29+
start: 0,
30+
end: arr.length - 1,
31+
maxTimes: 1000,
32+
...opt
33+
};
34+
let start = opt.start as number;
35+
let end = opt.end as number;
36+
const returnNearestIfNoHit = opt.returnNearestIfNoHit;
37+
const maxTimes = opt.maxTimes as number;
38+
// let { start, end } = opt;
39+
// const { returnNearestIfNoHit, maxTimes } = opt;
40+
let midNum: number;
41+
let mid: T;
42+
if (!start) {
43+
start = 0;
44+
end = arr.length - 1;
45+
}
46+
let i = 0;
47+
let r = 0;
48+
while (start >= 0 && start <= end) {
49+
if (i >= maxTimes) {
50+
throw Error(`binarySearch: loop times is over ${maxTimes}, you can increase the limit.`);
51+
}
52+
midNum = Math.floor((end - start) / 2 + start);
53+
mid = arr[midNum];
54+
const count = i + 1;
55+
r = callback(mid, midNum, count);
56+
if (r > 0) {
57+
end = midNum - 1;
58+
} else if (r < 0) {
59+
start = midNum + 1;
60+
} else {
61+
return { index: midNum as number, value: mid as T, count, hit: true };
62+
}
63+
i++;
64+
}
65+
return returnNearestIfNoHit
66+
? {
67+
//@ts-expect-error used before being assigned
68+
index: midNum as number,
69+
//@ts-expect-error used before being assigned
70+
value: mid as T,
71+
count: i + 1,
72+
hit: false,
73+
greater: r > 0
74+
}
75+
: null;
76+
}

src/routes/+layout.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
{ title: 'Table', path: '/examples/table' },
3030
{ title: 'Variable Sizing', path: '/examples/variablesizing' },
3131
{ title: 'Positioning', path: '/examples/positioning' },
32-
{ title: 'OLDPositioning', path: '/examples/positioning2' },
32+
{ title: 'OLDPositioning', path: '/examples/positioningOLD' },
3333
{ title: 'Partial Loader', path: '/examples/partialloader' },
3434
{ title: 'Events', path: '/examples/events' }
3535
]

src/routes/examples/horizontal/code.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<script lang="ts">
2-
import VirtualList from '$lib/VirtualList2.svelte';
2+
import VirtualList from 'svelte-virtuallists/new/VirtualList2.svelte';
33
44
const myModel = new Array(10000).fill(1).map((v, i) => {
55
return { text: 'ITEM ' + i + ' - Item ' + i };

src/routes/examples/positioning/code.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<script lang="ts">
22
import { ALIGNMENT, SCROLL_BEHAVIOR, type VirtualListModel } from '$lib';
3-
import VirtualList from '$lib/VirtualList2.svelte';
3+
import VirtualList from 'svelte-virtuallists/new/VirtualList2.svelte';
44
55
const myModel = new Array(10000).fill(1).map((v, i) => {
66
return { text: 'Item ' + i + ' item ' + i, lineHeight: 20 + (i % 30) + 'px' };

0 commit comments

Comments
 (0)