Skip to content

Commit 72ebbc9

Browse files
committed
modified: index.ts
1 parent b14aed4 commit 72ebbc9

File tree

1 file changed

+35
-30
lines changed

1 file changed

+35
-30
lines changed

index.ts

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -14,50 +14,55 @@ export default (
1414
siblings: keySiblings = "siblings",
1515
} = {},
1616
) => {
17-
const configurable: PropertyDescriptor["configurable"] = true,
18-
getLeaves = (
17+
const properties: PropertyDescriptorMap = {
18+
[keyBranch]: {
19+
get(this: Record<string, unknown>) {
20+
const ret = [this];
21+
while (ret[0]?.[keyParent])
22+
ret.unshift(ret[0][keyParent] as Record<string, unknown>);
23+
return ret;
24+
},
25+
},
26+
[keyIndex]: {
27+
get(this: Record<string, unknown>) {
28+
return (this[keySiblings] as Record<string, unknown>[]).findIndex(
29+
(sibling) => this[keyId] === sibling[keyId],
30+
);
31+
},
32+
},
33+
[keyNext]: {
34+
get(this: Record<string, unknown>) {
35+
return (this[keySiblings] as Record<string, unknown>[])[
36+
(this[keyIndex] as number) + 1
37+
];
38+
},
39+
},
40+
[keyPrev]: {
41+
get(this: Record<string, unknown>) {
42+
return (this[keySiblings] as Record<string, unknown>[])[
43+
(this[keyIndex] as number) - 1
44+
];
45+
},
46+
},
47+
};
48+
const getLeaves = (
1949
siblings: { configurable?: boolean; value: Record<string, unknown>[] },
2050
parent = {},
2151
) =>
2252
siblings.value.flatMap((value): Record<string, unknown>[] => {
2353
Object.defineProperties(value, {
24-
[keyBranch]: {
25-
get: () => {
26-
const ret = [value];
27-
while (ret[0]?.[keyParent])
28-
ret.unshift(ret[0][keyParent] as Record<string, unknown>);
29-
return ret;
30-
},
31-
},
32-
[keyIndex]: {
33-
get: () =>
34-
(value[keySiblings] as Record<string, unknown>[]).findIndex(
35-
(sibling) => value[keyId] === sibling[keyId],
36-
),
37-
},
38-
[keyNext]: {
39-
get: () =>
40-
(value[keySiblings] as Record<string, unknown>[])[
41-
(value[keyIndex] as number) + 1
42-
],
43-
},
54+
...properties,
4455
[keyParent]: parent,
45-
[keyPrev]: {
46-
get: () =>
47-
(value[keySiblings] as Record<string, unknown>[])[
48-
(value[keyIndex] as number) - 1
49-
],
50-
},
5156
[keySiblings]: siblings,
5257
});
5358
return [
5459
value,
5560
...getLeaves(
5661
{
57-
configurable,
62+
configurable: true,
5863
value: (value[keyChildren] ?? []) as Record<string, unknown>[],
5964
},
60-
{ configurable, value },
65+
{ configurable: true, value },
6166
),
6267
];
6368
}),

0 commit comments

Comments
 (0)