Skip to content

Commit dce73c1

Browse files
author
Jerry Bruwes
committed
modified: src/index.ts
1 parent af43469 commit dce73c1

File tree

1 file changed

+41
-51
lines changed

1 file changed

+41
-51
lines changed

src/index.ts

Lines changed: 41 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -6,59 +6,50 @@ import { computed, isReactive, nextTick, reactive, watch } from "vue";
66
const configurable = true;
77
export default (
88
tree: Record<string, unknown>[],
9-
options: Record<string, string> = {},
9+
{
10+
branch: keyBranch = "branch",
11+
children: keyChildren = "children",
12+
id: keyId = "id",
13+
index: keyIndex = "index",
14+
next: keyNext = "next",
15+
parent: keyParent = "parent",
16+
prev: keyPrev = "prev",
17+
siblings: keySiblings = "siblings",
18+
},
1019
) => {
1120
const data = (isReactive(tree) ? tree : reactive(tree)) as Reactive<
1221
Record<string, unknown>[]
1322
>;
14-
const {
15-
branch: keyBranch,
16-
children: keyChildren,
17-
id: keyId,
18-
index: keyIndex,
19-
next: keyNext,
20-
parent: keyParent,
21-
prev: keyPrev,
22-
siblings: keySiblings,
23-
} = {
24-
branch: "branch",
25-
children: "children",
26-
id: "id",
27-
index: "index",
28-
next: "next",
29-
parent: "parent",
30-
prev: "prev",
31-
siblings: "siblings",
32-
...options,
33-
};
3423
{
35-
const index = {
36-
get(this: Record<string, unknown>) {
37-
return (this[keySiblings] as Record<string, unknown>[]).findIndex(
38-
({ id }) => this[keyId] === id,
39-
);
24+
const properties = {
25+
[keyBranch]: {
26+
get(this: Record<string, unknown>) {
27+
const ret = [this];
28+
while (ret[0][keyParent])
29+
ret.unshift(ret[0][keyParent] as Record<string, unknown>);
30+
return ret;
31+
},
4032
},
41-
};
42-
const prev = {
43-
get(this: Record<string, unknown>) {
44-
return (this[keySiblings] as Record<string, unknown>[])[
45-
(this[keyIndex] as number) - 1
46-
];
33+
[keyIndex]: {
34+
get(this: Record<string, unknown>) {
35+
return (this[keySiblings] as Record<string, unknown>[]).findIndex(
36+
({ id }) => this[keyId] === id,
37+
);
38+
},
4739
},
48-
};
49-
const next = {
50-
get(this: Record<string, unknown>) {
51-
return (this[keySiblings] as Record<string, unknown>[])[
52-
(this[keyIndex] as number) + 1
53-
];
40+
[keyNext]: {
41+
get(this: Record<string, unknown>) {
42+
return (this[keySiblings] as Record<string, unknown>[])[
43+
(this[keyIndex] as number) + 1
44+
];
45+
},
5446
},
55-
};
56-
const branch = {
57-
get(this: Record<string, unknown>) {
58-
const ret = [this];
59-
while (ret[0][keyParent])
60-
ret.unshift(ret[0][keyParent] as Record<string, unknown>);
61-
return ret;
47+
[keyPrev]: {
48+
get(this: Record<string, unknown>) {
49+
return (this[keySiblings] as Record<string, unknown>[])[
50+
(this[keyIndex] as number) - 1
51+
];
52+
},
6253
},
6354
};
6455
const defineProperties = (
@@ -68,12 +59,11 @@ export default (
6859
},
6960
) => {
7061
siblings.value.forEach((value) => {
71-
Reflect.defineProperty(value, keyBranch, branch);
72-
Reflect.defineProperty(value, keyIndex, index);
73-
Reflect.defineProperty(value, keyNext, next);
74-
Reflect.defineProperty(value, keyParent, parent);
75-
Reflect.defineProperty(value, keyPrev, prev);
76-
Reflect.defineProperty(value, keySiblings, siblings);
62+
Object.defineProperties(value, {
63+
...properties,
64+
[keyParent]: parent,
65+
[keySiblings]: siblings,
66+
});
7767
defineProperties(
7868
{
7969
configurable,

0 commit comments

Comments
 (0)