Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix randomly selected rows #164 #231

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/lib/plugins/addSubRows.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,17 @@ export const addSubRows =
NewTablePropSet<never>
> =>
() => {
const getChildren: ValidChildrenFn<Item> =
children instanceof Function ? children : (item) => item[children] as unknown as Item[];
let getChildren: ValidChildrenFn<Item>;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that ValidChildrenFn has an optional return value, the original intention is that getChildren should return undefined if invalid.

Furtherfore, there is a semantic difference between returning [] and undefined, so the added unknownVal.length > 0 check is an incorrect addition.

if (children instanceof Function) {
getChildren = children;
} else {
getChildren = (item) => {
const unknownVal = item[children];
return unknownVal instanceof Array && unknownVal.length > 0
? (unknownVal as unknown as Item[])
: undefined;
};
}

const deriveRows: DeriveRowsFn<Item> = (rows) => {
return derived(rows, ($rows) => {
Expand Down
2 changes: 1 addition & 1 deletion src/routes/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import { getDistinct } from '../lib/utils/array.js';
import SelectIndicator from './_SelectIndicator.svelte';

const data = readable(createSamples(2, 2));
const data = readable(createSamples(5, 2));

let serverSide = false;

Expand Down
4 changes: 3 additions & 1 deletion src/routes/_createSamples.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ export const createSamples = (...lengths: number[]) => {
return [...Array(length)].map(() => {
return {
...getSample(),
...(lengths[depth + 1] !== undefined ? { children: createSamplesLevel(depth + 1) } : {}),
...(lengths[depth + 1] !== undefined && Math.random() >= 0.25
? { children: createSamplesLevel(depth + 1) }
: { children: [] }),
};
});
};
Expand Down