Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
wilgaboury committed Dec 29, 2023
1 parent 3e072e8 commit cdf09e3
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 14 deletions.
11 changes: 3 additions & 8 deletions dev/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
horizontalLayout,
move,
sortableHandle,
splice,
verticalLayout,
} from "../src";

Expand Down Expand Up @@ -71,7 +72,7 @@ const App: Component = () => {
each={data()}
layout={getLayout(layouts[layoutIdx()]!, aligns[alignIdx()]!)}
onMove={(_item, from, to) => {
setData((cur) => move([...cur], from, to));
setData((cur) => move(cur, from, to));
}}
autoscroll={document.documentElement}
>
Expand All @@ -84,13 +85,7 @@ const App: Component = () => {
<div use:sortableHandle class={styles.handle} />
<button
class={styles.button}
onClick={() =>
setData((cur) => {
const tmp = [...cur];
tmp.splice(idx(), 1);
return tmp;
})
}
onClick={() => setData((cur) => splice(cur, idx(), 1))}
>
Remove
</button>
Expand Down
22 changes: 17 additions & 5 deletions src/assorted.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,30 @@ export function difference<T>(set1: Set<T>, set2: Set<T>): Set<T> {
}

export function move<T>(
arr: Array<T>,
arr: ReadonlyArray<T>,
fromIdx: number,
toIdx: number,
): Array<T> {
): ReadonlyArray<T> {
const elem = arr[fromIdx];
if (elem == null) {
console.error("index out of bounds");
return arr;
}
arr.splice(fromIdx, 1);
arr.splice(toIdx, 0, elem);
return arr;
const ret = [...arr];
ret.splice(fromIdx, 1);
ret.splice(toIdx, 0, elem);
return ret;
}

export function splice<T>(
arr: ReadonlyArray<T>,
index: number,
count: number,
...items: T[]
) {
const tmp = [...arr];
tmp.splice(index, count, ...items);
return tmp;
}

export function zip<A, B>(
Expand Down
2 changes: 1 addition & 1 deletion src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Accessor, Component, createComputed, createSignal } from "solid-js";
export * from "./Sortable";
export * from "./layout";
export { move } from "./assorted";
export { move, splice } from "./assorted";

export function createHello(): [Accessor<string>, (to: string) => void] {
const [hello, setHello] = createSignal("Hello World!");
Expand Down

0 comments on commit cdf09e3

Please sign in to comment.