Skip to content

Commit 9ada942

Browse files
author
Jerry Bruwes
committed
modified: index.ts
1 parent 48aaa5b commit 9ada942

File tree

1 file changed

+41
-50
lines changed

1 file changed

+41
-50
lines changed

index.ts

Lines changed: 41 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,18 @@ const configurable = true;
1717
/* Functions */
1818
/* -------------------------------------------------------------------------- */
1919

20-
const useFlatJsonTree: (
20+
function useFlatJsonTree(
2121
tree: Reactive<Record<string, unknown>[]> | Record<string, unknown>[],
2222
{
23-
branch,
24-
children,
25-
id,
26-
index,
27-
next,
28-
parent,
29-
prev,
30-
siblings,
31-
}?: {
23+
branch: keyBranch = "branch",
24+
children: keyChildren = "children",
25+
id: keyId = "id",
26+
index: keyIndex = "index",
27+
next: keyNext = "next",
28+
parent: keyParent = "parent",
29+
prev: keyPrev = "prev",
30+
siblings: keySiblings = "siblings",
31+
}: {
3232
branch?: string;
3333
children?: string;
3434
id?: string;
@@ -37,28 +37,16 @@ const useFlatJsonTree: (
3737
parent?: string;
3838
prev?: string;
3939
siblings?: string;
40-
},
41-
) => {
40+
} = {},
41+
): {
4242
add: (pId: string) => null | string;
4343
down: (pId: string) => void;
4444
leaves: ComputedRef<Record<string, unknown>[]>;
4545
left: (pId: string) => null | string;
4646
remove: (pId: string) => null | string;
4747
right: (pId: string) => null | string;
4848
up: (pId: string) => void;
49-
} = (
50-
tree,
51-
{
52-
branch: keyBranch = "branch",
53-
children: keyChildren = "children",
54-
id: keyId = "id",
55-
index: keyIndex = "index",
56-
next: keyNext = "next",
57-
parent: keyParent = "parent",
58-
prev: keyPrev = "prev",
59-
siblings: keySiblings = "siblings",
60-
} = {},
61-
) => {
49+
} {
6250
/* -------------------------------------------------------------------------- */
6351
/* Constants */
6452
/* -------------------------------------------------------------------------- */
@@ -103,11 +91,14 @@ const useFlatJsonTree: (
10391
/* Functions */
10492
/* -------------------------------------------------------------------------- */
10593

106-
const getLeaves: (
94+
function getLeaves(
10795
siblings: { configurable?: boolean; value: Record<string, unknown>[] },
108-
parent?: { configurable?: boolean; value: null | Record<string, unknown> },
109-
) => Record<string, unknown>[] = (siblings, parent = { value: null }) =>
110-
siblings.value.flatMap((value) => {
96+
parent: {
97+
configurable?: boolean;
98+
value: null | Record<string, unknown>;
99+
} = { value: null },
100+
): Record<string, unknown>[] {
101+
return siblings.value.flatMap((value) => {
111102
Object.defineProperties(value, {
112103
...properties,
113104
[keyParent]: parent,
@@ -124,28 +115,28 @@ const useFlatJsonTree: (
124115
),
125116
];
126117
});
118+
}
127119

128120
/* -------------------------------------------------------------------------- */
129-
/* Constants */
130-
/* -------------------------------------------------------------------------- */
131121

132-
const value: Reactive<Record<string, unknown>[]> = isReactive(tree)
133-
? tree
134-
: reactive(tree);
122+
function startLeaves() {
123+
const value: Reactive<Record<string, unknown>[]> = isReactive(tree)
124+
? tree
125+
: reactive(tree);
126+
return getLeaves({ value });
127+
}
135128

136129
/* -------------------------------------------------------------------------- */
137130
/* Computations */
138131
/* -------------------------------------------------------------------------- */
139132

140-
const leaves: ComputedRef<Record<string, unknown>[]> = computed(() =>
141-
getLeaves({ value }),
142-
);
133+
const leaves: ComputedRef<Record<string, unknown>[]> = computed(startLeaves);
143134

144135
/* -------------------------------------------------------------------------- */
145136
/* Functions */
146137
/* -------------------------------------------------------------------------- */
147138

148-
const up: (pId: string) => void = (pId) => {
139+
function up(pId: string): void {
149140
const the: null | Record<string, unknown> =
150141
leaves.value.find((leaf) => leaf[keyId] === pId) ?? null;
151142
if (the) {
@@ -161,11 +152,11 @@ const useFlatJsonTree: (
161152
siblings[prevIndex],
162153
];
163154
}
164-
};
155+
}
165156

166157
/* -------------------------------------------------------------------------- */
167158

168-
const down: (pId: string) => void = (pId) => {
159+
function down(pId: string): void {
169160
const the: null | Record<string, unknown> =
170161
leaves.value.find((leaf) => leaf[keyId] === pId) ?? null;
171162
if (the) {
@@ -181,11 +172,11 @@ const useFlatJsonTree: (
181172
siblings[index],
182173
];
183174
}
184-
};
175+
}
185176

186177
/* -------------------------------------------------------------------------- */
187178

188-
const right: (pId: string) => null | string = (pId: string) => {
179+
function right(pId: string): null | string {
189180
const the: null | Record<string, unknown> =
190181
leaves.value.find((leaf) => leaf[keyId] === pId) ?? null;
191182
if (the) {
@@ -204,11 +195,11 @@ const useFlatJsonTree: (
204195
}
205196
}
206197
return null;
207-
};
198+
}
208199

209200
/* -------------------------------------------------------------------------- */
210201

211-
const left: (pId: string) => null | string = (pId) => {
202+
function left(pId: string): null | string {
212203
const the: null | Record<string, unknown> =
213204
leaves.value.find((leaf) => leaf[keyId] === pId) ?? null;
214205
if (the) {
@@ -228,11 +219,11 @@ const useFlatJsonTree: (
228219
}
229220
}
230221
return null;
231-
};
222+
}
232223

233224
/* -------------------------------------------------------------------------- */
234225

235-
const add: (pId: string) => null | string = (pId) => {
226+
function add(pId: string): null | string {
236227
const the: null | Record<string, unknown> =
237228
leaves.value.find((leaf) => leaf[keyId] === pId) ?? null;
238229
if (the) {
@@ -256,11 +247,11 @@ const useFlatJsonTree: (
256247
return id;
257248
}
258249
return null;
259-
};
250+
}
260251

261252
/* -------------------------------------------------------------------------- */
262253

263-
const remove: (pId: string) => null | string = (pId) => {
254+
function remove(pId: string): null | string {
264255
const the: null | Record<string, unknown> =
265256
leaves.value.find((leaf) => leaf[keyId] === pId) ?? null;
266257
if (the) {
@@ -288,7 +279,7 @@ const useFlatJsonTree: (
288279
}
289280
}
290281
return null;
291-
};
282+
}
292283

293284
/* -------------------------------------------------------------------------- */
294285
/* Main */
@@ -297,7 +288,7 @@ const useFlatJsonTree: (
297288
return { add, down, leaves, left, remove, right, up };
298289

299290
/* -------------------------------------------------------------------------- */
300-
};
291+
}
301292

302293
/* -------------------------------------------------------------------------- */
303294
/* Exports */

0 commit comments

Comments
 (0)