Skip to content

Commit

Permalink
refactor: cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
orefalo committed Oct 14, 2024
1 parent 75ae4fc commit 5356ad6
Show file tree
Hide file tree
Showing 21 changed files with 90 additions and 702 deletions.
691 changes: 0 additions & 691 deletions src/lib/jshelper.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
type AfterScrollEvent,
type VirtualListModel,
type VirtualRangeEvent
} from '.';
} from '..';
import { binarySearch } from './jshelper';
import clsx from 'clsx';
Expand Down
76 changes: 76 additions & 0 deletions src/lib/new/jshelper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@

type BinarySearchReturn<T> = {
index: number;
value: T;
count: number;
hit: boolean;
greater?: boolean;
} | null;

interface BinarySearchOptions {
start?: number;
end?: number;
returnNearestIfNoHit?: boolean;
maxTimes?: number;
}
/**
* binarySearch, 二分查找
* @param arr
* @param callback return `mid - your_value` for ascending array
* @param opt
* @returns
*/
export function binarySearch<T>(
arr: T[],
callback: (mid: T, index: number, count: number) => number,
opt: BinarySearchOptions = {}
): BinarySearchReturn<T> {
opt = {
start: 0,
end: arr.length - 1,
maxTimes: 1000,
...opt
};
let start = opt.start as number;
let end = opt.end as number;
const returnNearestIfNoHit = opt.returnNearestIfNoHit;
const maxTimes = opt.maxTimes as number;
// let { start, end } = opt;
// const { returnNearestIfNoHit, maxTimes } = opt;
let midNum: number;
let mid: T;
if (!start) {
start = 0;
end = arr.length - 1;
}
let i = 0;
let r = 0;
while (start >= 0 && start <= end) {
if (i >= maxTimes) {
throw Error(`binarySearch: loop times is over ${maxTimes}, you can increase the limit.`);
}
midNum = Math.floor((end - start) / 2 + start);
mid = arr[midNum];
const count = i + 1;
r = callback(mid, midNum, count);
if (r > 0) {
end = midNum - 1;
} else if (r < 0) {
start = midNum + 1;
} else {
return { index: midNum as number, value: mid as T, count, hit: true };
}
i++;
}
return returnNearestIfNoHit
? {
//@ts-expect-error used before being assigned
index: midNum as number,
//@ts-expect-error used before being assigned
value: mid as T,
count: i + 1,
hit: false,
greater: r > 0
}
: null;
}
2 changes: 1 addition & 1 deletion src/routes/+layout.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
{ title: 'Table', path: '/examples/table' },
{ title: 'Variable Sizing', path: '/examples/variablesizing' },
{ title: 'Positioning', path: '/examples/positioning' },
{ title: 'OLDPositioning', path: '/examples/positioning2' },
{ title: 'OLDPositioning', path: '/examples/positioningOLD' },
{ title: 'Partial Loader', path: '/examples/partialloader' },
{ title: 'Events', path: '/examples/events' }
]
Expand Down
2 changes: 1 addition & 1 deletion src/routes/examples/horizontal/code.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script lang="ts">
import VirtualList from '$lib/VirtualList2.svelte';
import VirtualList from 'svelte-virtuallists/new/VirtualList2.svelte';
const myModel = new Array(10000).fill(1).map((v, i) => {
return { text: 'ITEM ' + i + ' - Item ' + i };
Expand Down
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion src/routes/examples/positioning/code.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script lang="ts">
import { ALIGNMENT, SCROLL_BEHAVIOR, type VirtualListModel } from '$lib';

Check warning on line 2 in src/routes/examples/positioning/code.svelte

View workflow job for this annotation

GitHub Actions / tests

'VirtualListModel' is defined but never used. Allowed unused vars must match /^_/u
import VirtualList from '$lib/VirtualList2.svelte';
import VirtualList from 'svelte-virtuallists/new/VirtualList2.svelte';
const myModel = new Array(10000).fill(1).map((v, i) => {
return { text: 'Item ' + i + ' item ' + i, lineHeight: 20 + (i % 30) + 'px' };
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
import {
VirtualList,
ALIGNMENT,
SCROLL_BEHAVIOR,
type VirtualListModel
SCROLL_BEHAVIOR
} from 'svelte-virtuallists';
let virtualList;
Expand Down
2 changes: 1 addition & 1 deletion src/routes/examples/table/code.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script lang="ts">
import VirtualList from '$lib/VirtualList2.svelte';
import VirtualList from 'svelte-virtuallists/new/VirtualList2.svelte';
const myModel = new Array(10000).fill(1).map((v, i) => {
return { text: 'ITEM ' + i + ' - Item ' + i };
Expand Down
10 changes: 7 additions & 3 deletions src/routes/examples/variablesizing/code.svelte
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
<script lang="ts">
import VirtualList from '$lib/VirtualList2.svelte';
import VirtualList from 'svelte-virtuallists/new/VirtualList2.svelte';
const myModel = new Array(10000).fill(1).map((v, i) => {
return { text: 'Item ' + i + ' item ' + i, lineHeight: 20 + (i % 30) + 'px' };
return {
text: 'Item ' + i + ' item ' + i,
lineHeight: 20 + (i % 30) + 'px',
width: 20 + (i % 30) + 'px'
};
});
let rowHeights = (item: any, index: number) => 25;
function randomize() {
Expand Down
2 changes: 1 addition & 1 deletion src/routes/examples/vertical/code.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script lang="ts">
import VirtualList from '$lib/VirtualList2.svelte';
import VirtualList from 'svelte-virtuallists/new/VirtualList2.svelte';
const myModel = new Array(10000).fill(1).map((v, i) => {
return { text: 'ITEM ' + i + ' - Item ' + i };
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 5356ad6

Please sign in to comment.