Skip to content

Commit bf0b7b1

Browse files
author
Jerry Bruwes
committed
modified: index.ts
1 parent fa0e7d7 commit bf0b7b1

File tree

1 file changed

+126
-28
lines changed

1 file changed

+126
-28
lines changed

index.ts

Lines changed: 126 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,53 @@
1-
import type { Reactive } from "vue";
1+
/* -------------------------------------------------------------------------- */
2+
/* Imports */
3+
/* -------------------------------------------------------------------------- */
4+
5+
import type { ComputedRef, Reactive } from "vue";
26

37
import { v4 } from "uuid";
48
import { computed, isReactive, reactive } from "vue";
59

10+
/* -------------------------------------------------------------------------- */
11+
/* Constants */
12+
/* -------------------------------------------------------------------------- */
13+
614
const configurable = true;
7-
export default (
15+
16+
/* -------------------------------------------------------------------------- */
17+
/* Functions */
18+
/* -------------------------------------------------------------------------- */
19+
20+
const useFlatJsonTree: (
821
tree: Reactive<Record<string, unknown>[]> | Record<string, unknown>[],
22+
{
23+
branch,
24+
children,
25+
id,
26+
index,
27+
next,
28+
parent,
29+
prev,
30+
siblings,
31+
}?: {
32+
branch?: string;
33+
children?: string;
34+
id?: string;
35+
index?: string;
36+
next?: string;
37+
parent?: string;
38+
prev?: string;
39+
siblings?: string;
40+
},
41+
) => {
42+
add: (pId: string) => null | string;
43+
down: (pId: string) => void;
44+
leaves: ComputedRef<Record<string, unknown>[]>;
45+
left: (pId: string) => null | string;
46+
remove: (pId: string) => null | string;
47+
right: (pId: string) => null | string;
48+
up: (pId: string) => void;
49+
} = (
50+
tree,
951
{
1052
branch: keyBranch = "branch",
1153
children: keyChildren = "children",
@@ -17,7 +59,11 @@ export default (
1759
siblings: keySiblings = "siblings",
1860
} = {},
1961
) => {
20-
const properties = {
62+
/* -------------------------------------------------------------------------- */
63+
/* Constants */
64+
/* -------------------------------------------------------------------------- */
65+
66+
const properties: PropertyDescriptorMap = {
2167
[keyBranch]: {
2268
get(this: Record<string, unknown>) {
2369
const ret = [this];
@@ -48,10 +94,15 @@ export default (
4894
},
4995
},
5096
};
97+
98+
/* -------------------------------------------------------------------------- */
99+
/* Functions */
100+
/* -------------------------------------------------------------------------- */
101+
51102
const getLeaves: (
52103
siblings: { configurable?: boolean; value: Record<string, unknown>[] },
53-
parent?: { configurable?: boolean; value?: Record<string, unknown> },
54-
) => Record<string, unknown>[] = (siblings, parent = { value: undefined }) =>
104+
parent?: { configurable?: boolean; value: null | Record<string, unknown> },
105+
) => Record<string, unknown>[] = (siblings, parent = { value: null }) =>
55106
siblings.value.flatMap((value) => {
56107
Object.defineProperties(value, {
57108
...properties,
@@ -69,11 +120,28 @@ export default (
69120
),
70121
];
71122
});
72-
const value = (isReactive(tree) ? tree : reactive(tree)) as Reactive<
73-
Record<string, unknown>[]
74-
>;
75-
const leaves = computed(() => getLeaves({ value }));
76-
const up = (pId: string) => {
123+
124+
/* -------------------------------------------------------------------------- */
125+
/* Constants */
126+
/* -------------------------------------------------------------------------- */
127+
128+
const value: Reactive<Record<string, unknown>[]> = isReactive(tree)
129+
? tree
130+
: reactive(tree);
131+
132+
/* -------------------------------------------------------------------------- */
133+
/* Computations */
134+
/* -------------------------------------------------------------------------- */
135+
136+
const leaves: ComputedRef<Record<string, unknown>[]> = computed(() =>
137+
getLeaves({ value }),
138+
);
139+
140+
/* -------------------------------------------------------------------------- */
141+
/* Functions */
142+
/* -------------------------------------------------------------------------- */
143+
144+
const up: (pId: string) => void = (pId) => {
77145
const the = leaves.value.find((leaf) => leaf[keyId] === pId);
78146
if (the) {
79147
const index = the[keyIndex] as number;
@@ -85,7 +153,10 @@ export default (
85153
];
86154
}
87155
};
88-
const down = (pId: string) => {
156+
157+
/* -------------------------------------------------------------------------- */
158+
159+
const down: (pId: string) => void = (pId) => {
89160
const the = leaves.value.find((leaf) => leaf[keyId] === pId);
90161
if (the) {
91162
const index = the[keyIndex] as number;
@@ -97,10 +168,13 @@ export default (
97168
];
98169
}
99170
};
100-
const right = (pId: string) => {
101-
const the = leaves.value.find((leaf) => leaf[keyId] === pId);
171+
172+
/* -------------------------------------------------------------------------- */
173+
174+
const right: (pId: string) => null | string = (pId: string) => {
175+
const the = leaves.value.find((leaf) => leaf[keyId] === pId) ?? null;
102176
if (the) {
103-
const prev = the[keyPrev] as Record<string, unknown> | undefined;
177+
const prev = (the[keyPrev] ?? null) as null | Record<string, unknown>;
104178
if (prev) {
105179
const children = (prev[keyChildren] ?? []) as Record<string, unknown>[];
106180
const id = prev[keyId] as string;
@@ -114,12 +188,15 @@ export default (
114188
return id;
115189
}
116190
}
117-
return undefined;
191+
return null;
118192
};
119-
const left = (pId: string) => {
193+
194+
/* -------------------------------------------------------------------------- */
195+
196+
const left: (pId: string) => null | string = (pId) => {
120197
const the = leaves.value.find((leaf) => leaf[keyId] === pId);
121198
if (the) {
122-
const parent = the[keyParent] as Record<string, unknown> | undefined;
199+
const parent = (the[keyParent] ?? null) as null | Record<string, unknown>;
123200
if (parent) {
124201
const siblings = parent[keySiblings] as Record<string, unknown>[];
125202
if (parent[keyParent]) {
@@ -134,14 +211,17 @@ export default (
134211
}
135212
}
136213
}
137-
return undefined;
214+
return null;
138215
};
139-
const add = (pId: string) => {
216+
217+
/* -------------------------------------------------------------------------- */
218+
219+
const add: (pId: string) => null | string = (pId) => {
140220
const the = leaves.value.find((leaf) => leaf[keyId] === pId);
141221
if (the) {
142-
const children = the[keyChildren] as
143-
| Record<string, unknown>[]
144-
| undefined;
222+
const children = (the[keyChildren] ?? null) as
223+
| null
224+
| Record<string, unknown>[];
145225
const index = the[keyIndex] as number;
146226
const siblings = the[keySiblings] as Record<string, unknown>[];
147227
const id = v4();
@@ -158,14 +238,17 @@ export default (
158238
}
159239
return id;
160240
}
161-
return undefined;
241+
return null;
162242
};
163-
const remove = (pId: string) => {
243+
244+
/* -------------------------------------------------------------------------- */
245+
246+
const remove: (pId: string) => null | string = (pId) => {
164247
const the = leaves.value.find((leaf) => leaf[keyId] === pId);
165248
if (the) {
166-
const next = the[keyNext] as Record<string, unknown> | undefined;
167-
const parent = the[keyParent] as Record<string, unknown> | undefined;
168-
const prev = the[keyPrev] as Record<string, unknown> | undefined;
249+
const next = (the[keyNext] ?? null) as null | Record<string, unknown>;
250+
const parent = (the[keyParent] ?? null) as null | Record<string, unknown>;
251+
const prev = (the[keyPrev] ?? null) as null | Record<string, unknown>;
169252
if (parent) {
170253
let id: string;
171254
switch (true) {
@@ -186,7 +269,22 @@ export default (
186269
return id;
187270
}
188271
}
189-
return undefined;
272+
return null;
190273
};
274+
275+
/* -------------------------------------------------------------------------- */
276+
/* Main */
277+
/* -------------------------------------------------------------------------- */
278+
191279
return { add, down, leaves, left, remove, right, up };
280+
281+
/* -------------------------------------------------------------------------- */
192282
};
283+
284+
/* -------------------------------------------------------------------------- */
285+
/* Exports */
286+
/* -------------------------------------------------------------------------- */
287+
288+
export default useFlatJsonTree;
289+
290+
/* -------------------------------------------------------------------------- */

0 commit comments

Comments
 (0)