Skip to content

Commit b963ef0

Browse files
author
Jerry Bruwes
committed
modified: index.ts
1 parent 7a43f8c commit b963ef0

File tree

1 file changed

+43
-58
lines changed

1 file changed

+43
-58
lines changed

index.ts

Lines changed: 43 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,8 @@ export default (
1414
parent: keyParent = "parent",
1515
prev: keyPrev = "prev",
1616
siblings: keySiblings = "siblings",
17-
}: {
18-
branch?: string;
19-
children?: string;
20-
id?: string;
21-
index?: string;
22-
next?: string;
23-
parent?: string;
24-
prev?: string;
25-
siblings?: string;
2617
} = {},
27-
): {
28-
add: (pId: string) => null | string;
29-
down: (pId: string) => void;
30-
leaves: ComputedRef<Record<string, unknown>[]>;
31-
left: (pId: string) => null | string;
32-
remove: (pId: string) => null | string;
33-
right: (pId: string) => null | string;
34-
up: (pId: string) => void;
35-
} => {
18+
) => {
3619
/* -------------------------------------------------------------------------- */
3720
/* Constants */
3821
/* -------------------------------------------------------------------------- */
@@ -61,20 +44,16 @@ export default (
6144
},
6245
[keyNext]: {
6346
get(this: Record<string, unknown>) {
64-
return (
65-
(this[keySiblings] as Record<string, unknown>[])[
66-
(this[keyIndex] as number) + 1
67-
] ?? null
68-
);
47+
return (this[keySiblings] as Record<string, unknown>[])[
48+
(this[keyIndex] as number) + 1
49+
];
6950
},
7051
},
7152
[keyPrev]: {
7253
get(this: Record<string, unknown>) {
73-
return (
74-
(this[keySiblings] as Record<string, unknown>[])[
75-
(this[keyIndex] as number) - 1
76-
] ?? null
77-
);
54+
return (this[keySiblings] as Record<string, unknown>[])[
55+
(this[keyIndex] as number) - 1
56+
];
7857
},
7958
},
8059
};
@@ -87,8 +66,8 @@ export default (
8766
siblings: { configurable?: boolean; value: Record<string, unknown>[] },
8867
parent: {
8968
configurable?: boolean;
90-
value: null | Record<string, unknown>;
91-
} = { value: null },
69+
value?: Record<string, unknown> | undefined;
70+
} = {},
9271
): Record<string, unknown>[] =>
9372
siblings.value.flatMap((value) => {
9473
Object.defineProperties(value, {
@@ -132,13 +111,14 @@ export default (
132111
/* Functions */
133112
/* -------------------------------------------------------------------------- */
134113

135-
const add = (pId: string): null | string => {
136-
const the: null | Record<string, unknown> =
137-
leaves.value.find((leaf) => leaf[keyId] === pId) ?? null;
114+
const add = (pId: string): string | undefined => {
115+
const the: Record<string, unknown> | undefined = leaves.value.find(
116+
(leaf) => leaf[keyId] === pId,
117+
);
138118
if (the) {
139-
const children = (the[keyChildren] ?? null) as
140-
| null
141-
| Record<string, unknown>[];
119+
const children = the[keyChildren] as
120+
| Record<string, unknown>[]
121+
| undefined;
142122
const index = the[keyIndex] as number;
143123
const siblings = the[keySiblings] as Record<string, unknown>[];
144124
const id = v4();
@@ -155,14 +135,15 @@ export default (
155135
}
156136
return id;
157137
}
158-
return null;
138+
return undefined;
159139
};
160140

161141
/* -------------------------------------------------------------------------- */
162142

163143
const down = (pId: string): void => {
164-
const the: null | Record<string, unknown> =
165-
leaves.value.find((leaf) => leaf[keyId] === pId) ?? null;
144+
const the: Record<string, unknown> | undefined = leaves.value.find(
145+
(leaf) => leaf[keyId] === pId,
146+
);
166147
if (the) {
167148
const index: number = the[keyIndex] as number;
168149
const nextIndex: number = index + 1;
@@ -180,11 +161,12 @@ export default (
180161

181162
/* -------------------------------------------------------------------------- */
182163

183-
const left = (pId: string): null | string => {
184-
const the: null | Record<string, unknown> =
185-
leaves.value.find((leaf) => leaf[keyId] === pId) ?? null;
164+
const left = (pId: string): string | undefined => {
165+
const the: Record<string, unknown> | undefined = leaves.value.find(
166+
(leaf) => leaf[keyId] === pId,
167+
);
186168
if (the) {
187-
const parent = (the[keyParent] ?? null) as null | Record<string, unknown>;
169+
const parent = the[keyParent] as Record<string, unknown> | undefined;
188170
if (parent) {
189171
const siblings = parent[keySiblings] as Record<string, unknown>[];
190172
if (parent[keyParent]) {
@@ -199,18 +181,19 @@ export default (
199181
}
200182
}
201183
}
202-
return null;
184+
return undefined;
203185
};
204186

205187
/* -------------------------------------------------------------------------- */
206188

207-
const remove = (pId: string): null | string => {
208-
const the: null | Record<string, unknown> =
209-
leaves.value.find((leaf) => leaf[keyId] === pId) ?? null;
189+
const remove = (pId: string): string | undefined => {
190+
const the: Record<string, unknown> | undefined = leaves.value.find(
191+
(leaf) => leaf[keyId] === pId,
192+
);
210193
if (the) {
211-
const next = (the[keyNext] ?? null) as null | Record<string, unknown>;
212-
const parent = (the[keyParent] ?? null) as null | Record<string, unknown>;
213-
const prev = (the[keyPrev] ?? null) as null | Record<string, unknown>;
194+
const next = the[keyNext] as Record<string, unknown> | undefined;
195+
const parent = the[keyParent] as Record<string, unknown> | undefined;
196+
const prev = the[keyPrev] as Record<string, unknown> | undefined;
214197
if (parent) {
215198
let id: string;
216199
switch (true) {
@@ -231,16 +214,17 @@ export default (
231214
return id;
232215
}
233216
}
234-
return null;
217+
return undefined;
235218
};
236219

237220
/* -------------------------------------------------------------------------- */
238221

239-
const right = (pId: string): null | string => {
240-
const the: null | Record<string, unknown> =
241-
leaves.value.find((leaf) => leaf[keyId] === pId) ?? null;
222+
const right = (pId: string): string | undefined => {
223+
const the: Record<string, unknown> | undefined = leaves.value.find(
224+
(leaf) => leaf[keyId] === pId,
225+
);
242226
if (the) {
243-
const prev = (the[keyPrev] ?? null) as null | Record<string, unknown>;
227+
const prev = the[keyPrev] as Record<string, unknown> | undefined;
244228
if (prev) {
245229
const children = (prev[keyChildren] ?? []) as Record<string, unknown>[];
246230
const id = prev[keyId] as string;
@@ -254,14 +238,15 @@ export default (
254238
return id;
255239
}
256240
}
257-
return null;
241+
return undefined;
258242
};
259243

260244
/* -------------------------------------------------------------------------- */
261245

262246
const up = (pId: string): void => {
263-
const the: null | Record<string, unknown> =
264-
leaves.value.find((leaf) => leaf[keyId] === pId) ?? null;
247+
const the: Record<string, unknown> | undefined = leaves.value.find(
248+
(leaf) => leaf[keyId] === pId,
249+
);
265250
if (the) {
266251
const index: number = the[keyIndex] as number;
267252
const prevIndex: number = index - 1;

0 commit comments

Comments
 (0)