Skip to content

Commit 696af61

Browse files
author
Jerry Bruwes
committed
modified: index.ts
1 parent 44d9ab0 commit 696af61

File tree

1 file changed

+32
-18
lines changed

1 file changed

+32
-18
lines changed

index.ts

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ const useFlatJsonTree: (
2828
parent,
2929
prev,
3030
siblings,
31-
}?: {
31+
}: {
3232
branch?: string;
3333
children?: string;
3434
id?: string;
@@ -67,7 +67,7 @@ const useFlatJsonTree: (
6767
[keyBranch]: {
6868
get(this: Record<string, unknown>) {
6969
const ret = [this];
70-
while (ret[0][keyParent])
70+
while (ret[0]?.[keyParent])
7171
ret.unshift(ret[0][keyParent] as Record<string, unknown>);
7272
return ret;
7373
},
@@ -146,28 +146,38 @@ const useFlatJsonTree: (
146146
/* -------------------------------------------------------------------------- */
147147

148148
const up: (pId: string) => void = (pId) => {
149-
const the = leaves.value.find((leaf) => leaf[keyId] === pId);
149+
const the: null | Record<string, unknown> =
150+
leaves.value.find((leaf) => leaf[keyId] === pId) ?? null;
150151
if (the) {
151-
const index = the[keyIndex] as number;
152-
const siblings = the[keySiblings] as Record<string, unknown>[];
153-
if (index)
154-
[siblings[index - 1], siblings[index]] = [
152+
const index: number = the[keyIndex] as number;
153+
const prevIndex: number = index - 1;
154+
const siblings: Record<string, unknown>[] = the[keySiblings] as Record<
155+
string,
156+
unknown
157+
>[];
158+
if (index && siblings[index] && siblings[prevIndex])
159+
[siblings[prevIndex], siblings[index]] = [
155160
siblings[index],
156-
siblings[index - 1],
161+
siblings[prevIndex],
157162
];
158163
}
159164
};
160165

161166
/* -------------------------------------------------------------------------- */
162167

163168
const down: (pId: string) => void = (pId) => {
164-
const the = leaves.value.find((leaf) => leaf[keyId] === pId);
169+
const the: null | Record<string, unknown> =
170+
leaves.value.find((leaf) => leaf[keyId] === pId) ?? null;
165171
if (the) {
166-
const index = the[keyIndex] as number;
167-
const siblings = the[keySiblings] as Record<string, unknown>[];
168-
if (index < siblings.length - 1)
169-
[siblings[index], siblings[index + 1]] = [
170-
siblings[index + 1],
172+
const index: number = the[keyIndex] as number;
173+
const nextIndex: number = index + 1;
174+
const siblings: Record<string, unknown>[] = the[keySiblings] as Record<
175+
string,
176+
unknown
177+
>[];
178+
if (index < siblings.length - 1 && siblings[index] && siblings[nextIndex])
179+
[siblings[index], siblings[nextIndex]] = [
180+
siblings[nextIndex],
171181
siblings[index],
172182
];
173183
}
@@ -176,7 +186,8 @@ const useFlatJsonTree: (
176186
/* -------------------------------------------------------------------------- */
177187

178188
const right: (pId: string) => null | string = (pId: string) => {
179-
const the = leaves.value.find((leaf) => leaf[keyId] === pId) ?? null;
189+
const the: null | Record<string, unknown> =
190+
leaves.value.find((leaf) => leaf[keyId] === pId) ?? null;
180191
if (the) {
181192
const prev = (the[keyPrev] ?? null) as null | Record<string, unknown>;
182193
if (prev) {
@@ -198,7 +209,8 @@ const useFlatJsonTree: (
198209
/* -------------------------------------------------------------------------- */
199210

200211
const left: (pId: string) => null | string = (pId) => {
201-
const the = leaves.value.find((leaf) => leaf[keyId] === pId);
212+
const the: null | Record<string, unknown> =
213+
leaves.value.find((leaf) => leaf[keyId] === pId) ?? null;
202214
if (the) {
203215
const parent = (the[keyParent] ?? null) as null | Record<string, unknown>;
204216
if (parent) {
@@ -221,7 +233,8 @@ const useFlatJsonTree: (
221233
/* -------------------------------------------------------------------------- */
222234

223235
const add: (pId: string) => null | string = (pId) => {
224-
const the = leaves.value.find((leaf) => leaf[keyId] === pId);
236+
const the: null | Record<string, unknown> =
237+
leaves.value.find((leaf) => leaf[keyId] === pId) ?? null;
225238
if (the) {
226239
const children = (the[keyChildren] ?? null) as
227240
| null
@@ -248,7 +261,8 @@ const useFlatJsonTree: (
248261
/* -------------------------------------------------------------------------- */
249262

250263
const remove: (pId: string) => null | string = (pId) => {
251-
const the = leaves.value.find((leaf) => leaf[keyId] === pId);
264+
const the: null | Record<string, unknown> =
265+
leaves.value.find((leaf) => leaf[keyId] === pId) ?? null;
252266
if (the) {
253267
const next = (the[keyNext] ?? null) as null | Record<string, unknown>;
254268
const parent = (the[keyParent] ?? null) as null | Record<string, unknown>;

0 commit comments

Comments
 (0)