@@ -28,7 +28,7 @@ const useFlatJsonTree: (
28
28
parent,
29
29
prev,
30
30
siblings,
31
- } ? : {
31
+ } : {
32
32
branch ?: string ;
33
33
children ?: string ;
34
34
id ?: string ;
@@ -67,7 +67,7 @@ const useFlatJsonTree: (
67
67
[ keyBranch ] : {
68
68
get ( this : Record < string , unknown > ) {
69
69
const ret = [ this ] ;
70
- while ( ret [ 0 ] [ keyParent ] )
70
+ while ( ret [ 0 ] ?. [ keyParent ] )
71
71
ret . unshift ( ret [ 0 ] [ keyParent ] as Record < string , unknown > ) ;
72
72
return ret ;
73
73
} ,
@@ -146,28 +146,38 @@ const useFlatJsonTree: (
146
146
/* -------------------------------------------------------------------------- */
147
147
148
148
const up : ( pId : string ) => void = ( pId ) => {
149
- const the = leaves . value . find ( ( leaf ) => leaf [ keyId ] === pId ) ;
149
+ const the : null | Record < string , unknown > =
150
+ leaves . value . find ( ( leaf ) => leaf [ keyId ] === pId ) ?? null ;
150
151
if ( the ) {
151
- const index = the [ keyIndex ] as number ;
152
- const siblings = the [ keySiblings ] as Record < string , unknown > [ ] ;
153
- if ( index )
154
- [ siblings [ index - 1 ] , siblings [ index ] ] = [
152
+ const index : number = the [ keyIndex ] as number ;
153
+ const prevIndex : number = index - 1 ;
154
+ const siblings : Record < string , unknown > [ ] = the [ keySiblings ] as Record <
155
+ string ,
156
+ unknown
157
+ > [ ] ;
158
+ if ( index && siblings [ index ] && siblings [ prevIndex ] )
159
+ [ siblings [ prevIndex ] , siblings [ index ] ] = [
155
160
siblings [ index ] ,
156
- siblings [ index - 1 ] ,
161
+ siblings [ prevIndex ] ,
157
162
] ;
158
163
}
159
164
} ;
160
165
161
166
/* -------------------------------------------------------------------------- */
162
167
163
168
const down : ( pId : string ) => void = ( pId ) => {
164
- const the = leaves . value . find ( ( leaf ) => leaf [ keyId ] === pId ) ;
169
+ const the : null | Record < string , unknown > =
170
+ leaves . value . find ( ( leaf ) => leaf [ keyId ] === pId ) ?? null ;
165
171
if ( the ) {
166
- const index = the [ keyIndex ] as number ;
167
- const siblings = the [ keySiblings ] as Record < string , unknown > [ ] ;
168
- if ( index < siblings . length - 1 )
169
- [ siblings [ index ] , siblings [ index + 1 ] ] = [
170
- siblings [ index + 1 ] ,
172
+ const index : number = the [ keyIndex ] as number ;
173
+ const nextIndex : number = index + 1 ;
174
+ const siblings : Record < string , unknown > [ ] = the [ keySiblings ] as Record <
175
+ string ,
176
+ unknown
177
+ > [ ] ;
178
+ if ( index < siblings . length - 1 && siblings [ index ] && siblings [ nextIndex ] )
179
+ [ siblings [ index ] , siblings [ nextIndex ] ] = [
180
+ siblings [ nextIndex ] ,
171
181
siblings [ index ] ,
172
182
] ;
173
183
}
@@ -176,7 +186,8 @@ const useFlatJsonTree: (
176
186
/* -------------------------------------------------------------------------- */
177
187
178
188
const right : ( pId : string ) => null | string = ( pId : string ) => {
179
- const the = leaves . value . find ( ( leaf ) => leaf [ keyId ] === pId ) ?? null ;
189
+ const the : null | Record < string , unknown > =
190
+ leaves . value . find ( ( leaf ) => leaf [ keyId ] === pId ) ?? null ;
180
191
if ( the ) {
181
192
const prev = ( the [ keyPrev ] ?? null ) as null | Record < string , unknown > ;
182
193
if ( prev ) {
@@ -198,7 +209,8 @@ const useFlatJsonTree: (
198
209
/* -------------------------------------------------------------------------- */
199
210
200
211
const left : ( pId : string ) => null | string = ( pId ) => {
201
- const the = leaves . value . find ( ( leaf ) => leaf [ keyId ] === pId ) ;
212
+ const the : null | Record < string , unknown > =
213
+ leaves . value . find ( ( leaf ) => leaf [ keyId ] === pId ) ?? null ;
202
214
if ( the ) {
203
215
const parent = ( the [ keyParent ] ?? null ) as null | Record < string , unknown > ;
204
216
if ( parent ) {
@@ -221,7 +233,8 @@ const useFlatJsonTree: (
221
233
/* -------------------------------------------------------------------------- */
222
234
223
235
const add : ( pId : string ) => null | string = ( pId ) => {
224
- const the = leaves . value . find ( ( leaf ) => leaf [ keyId ] === pId ) ;
236
+ const the : null | Record < string , unknown > =
237
+ leaves . value . find ( ( leaf ) => leaf [ keyId ] === pId ) ?? null ;
225
238
if ( the ) {
226
239
const children = ( the [ keyChildren ] ?? null ) as
227
240
| null
@@ -248,7 +261,8 @@ const useFlatJsonTree: (
248
261
/* -------------------------------------------------------------------------- */
249
262
250
263
const remove : ( pId : string ) => null | string = ( pId ) => {
251
- const the = leaves . value . find ( ( leaf ) => leaf [ keyId ] === pId ) ;
264
+ const the : null | Record < string , unknown > =
265
+ leaves . value . find ( ( leaf ) => leaf [ keyId ] === pId ) ?? null ;
252
266
if ( the ) {
253
267
const next = ( the [ keyNext ] ?? null ) as null | Record < string , unknown > ;
254
268
const parent = ( the [ keyParent ] ?? null ) as null | Record < string , unknown > ;
0 commit comments