@@ -382,11 +382,29 @@ export function injectUrls(
382
382
idToUrlMapping : Record < string , string > ,
383
383
) : void {
384
384
if ( path . length === 0 ) return ;
385
+ const toId = ( value : unknown ) : string | undefined => {
386
+ if ( typeof value === "number" ) {
387
+ return String ( value ) ;
388
+ }
389
+ if ( typeof value === "string" && ID_PATTERN . test ( value ) ) {
390
+ return value ;
391
+ }
392
+ return undefined ;
393
+ } ;
385
394
const [ key , ...rest ] = path ;
386
395
387
396
if ( key === "*" ) {
388
397
if ( Array . isArray ( obj ) ) {
389
- for ( const item of obj ) injectUrls ( item , rest , idToUrlMapping ) ;
398
+ if ( rest . length === 0 ) {
399
+ for ( let i = 0 ; i < obj . length ; i += 1 ) {
400
+ const id = toId ( obj [ i ] ) ;
401
+ if ( id !== undefined ) {
402
+ obj [ i ] = idToUrlMapping [ id ] ?? "" ;
403
+ }
404
+ }
405
+ } else {
406
+ for ( const item of obj ) injectUrls ( item , rest , idToUrlMapping ) ;
407
+ }
390
408
}
391
409
return ;
392
410
}
@@ -395,14 +413,7 @@ export function injectUrls(
395
413
const record = obj as Record < string | number , unknown > ;
396
414
if ( path . length === 1 ) {
397
415
const fieldValue = record [ key ] ;
398
-
399
- const id =
400
- typeof fieldValue === "number"
401
- ? String ( fieldValue )
402
- : typeof fieldValue === "string" && ID_PATTERN . test ( fieldValue )
403
- ? fieldValue
404
- : undefined ;
405
-
416
+ const id = toId ( fieldValue ) ;
406
417
if ( id !== undefined ) {
407
418
record [ key ] = idToUrlMapping [ id ] ?? "" ;
408
419
}
0 commit comments