@@ -442,6 +442,87 @@ loader.paths('f-find-last-%N.html').forEach((path) => {
442442 test ( `find-last - ${ path } ` , ( ) => loader . execute ( path ) ) ;
443443} ) ;
444444
445+ test ( 'find-nth' , ( ) => {
446+ // no filter: return element at index 0
447+ let vars = variables ( [ 'a' , 'b' , 'c' ] ) ;
448+ Core [ 'find-nth' ] . apply ( [ '0' ] , vars , CTX ) ;
449+ expect ( vars [ 0 ] . get ( ) ) . toEqual ( 'a' ) ;
450+
451+ // no filter: return element at index 1
452+ vars = variables ( [ 'a' , 'b' , 'c' ] ) ;
453+ Core [ 'find-nth' ] . apply ( [ '1' ] , vars , CTX ) ;
454+ expect ( vars [ 0 ] . get ( ) ) . toEqual ( 'b' ) ;
455+
456+ // no filter: negative index counts from end
457+ vars = variables ( [ 'a' , 'b' , 'c' ] ) ;
458+ Core [ 'find-nth' ] . apply ( [ '-1' ] , vars , CTX ) ;
459+ expect ( vars [ 0 ] . get ( ) ) . toEqual ( 'c' ) ;
460+
461+ vars = variables ( [ 'a' , 'b' , 'c' ] ) ;
462+ Core [ 'find-nth' ] . apply ( [ '-2' ] , vars , CTX ) ;
463+ expect ( vars [ 0 ] . get ( ) ) . toEqual ( 'b' ) ;
464+
465+ // non-numeric index defaults to 0
466+ vars = variables ( [ 'a' , 'b' , 'c' ] ) ;
467+ Core [ 'find-nth' ] . apply ( [ 'foo' ] , vars , CTX ) ;
468+ expect ( vars [ 0 ] . get ( ) ) . toEqual ( 'a' ) ;
469+
470+ // index out of bounds → missing
471+ vars = variables ( [ 'a' , 'b' , 'c' ] ) ;
472+ Core [ 'find-nth' ] . apply ( [ '5' ] , vars , CTX ) ;
473+ expect ( vars [ 0 ] . node . isMissing ( ) ) . toBe ( true ) ;
474+
475+ // negative index out of bounds → missing
476+ vars = variables ( [ 'a' , 'b' , 'c' ] ) ;
477+ Core [ 'find-nth' ] . apply ( [ '-5' ] , vars , CTX ) ;
478+ expect ( vars [ 0 ] . node . isMissing ( ) ) . toBe ( true ) ;
479+
480+ // empty array → missing
481+ vars = variables ( [ ] ) ;
482+ Core [ 'find-nth' ] . apply ( [ '0' ] , vars , CTX ) ;
483+ expect ( vars [ 0 ] . node . isMissing ( ) ) . toBe ( true ) ;
484+
485+ // non-array → missing
486+ vars = variables ( 'not-an-array' ) ;
487+ Core [ 'find-nth' ] . apply ( [ '0' ] , vars , CTX ) ;
488+ expect ( vars [ 0 ] . node . isMissing ( ) ) . toBe ( true ) ;
489+
490+ // no filter: nth object element
491+ vars = variables ( [ { x : 1 } , { x : 2 } , { x : 3 } ] ) ;
492+ Core [ 'find-nth' ] . apply ( [ '2' ] , vars , CTX ) ;
493+ expect ( vars [ 0 ] . get ( ) ) . toEqual ( { x : 3 } ) ;
494+
495+ // with path: find nth element where path is truthy
496+ vars = variables ( [
497+ { id : 'a' , enabled : false } ,
498+ { id : 'b' , enabled : true } ,
499+ { id : 'c' , enabled : true } ,
500+ { id : 'd' , enabled : true } ,
501+ ] ) ;
502+ Core [ 'find-nth' ] . apply ( [ '1' , 'enabled' ] , vars , CTX ) ;
503+ expect ( vars [ 0 ] . get ( ) ) . toEqual ( { id : 'c' , enabled : true } ) ;
504+
505+ // with path: index 0 of matching elements
506+ vars = variables ( [ { id : 'a' , enabled : false } , { id : 'b' , enabled : true } , { id : 'c' , enabled : true } ] ) ;
507+ Core [ 'find-nth' ] . apply ( [ '0' , 'enabled' ] , vars , CTX ) ;
508+ expect ( vars [ 0 ] . get ( ) ) . toEqual ( { id : 'b' , enabled : true } ) ;
509+
510+ // with path: negative index among matching elements
511+ vars = variables ( [ { id : 'a' , enabled : true } , { id : 'b' , enabled : false } , { id : 'c' , enabled : true } ] ) ;
512+ Core [ 'find-nth' ] . apply ( [ '-1' , 'enabled' ] , vars , CTX ) ;
513+ expect ( vars [ 0 ] . get ( ) ) . toEqual ( { id : 'c' , enabled : true } ) ;
514+
515+ // with path: none match → missing
516+ vars = variables ( [ { id : 'a' , enabled : false } , { id : 'b' } ] ) ;
517+ Core [ 'find-nth' ] . apply ( [ '0' , 'enabled' ] , vars , CTX ) ;
518+ expect ( vars [ 0 ] . node . isMissing ( ) ) . toBe ( true ) ;
519+
520+ // with path: index out of bounds among matches → missing
521+ vars = variables ( [ { id : 'a' , enabled : true } , { id : 'b' , enabled : false } ] ) ;
522+ Core [ 'find-nth' ] . apply ( [ '1' , 'enabled' ] , vars , CTX ) ;
523+ expect ( vars [ 0 ] . node . isMissing ( ) ) . toBe ( true ) ;
524+ } ) ;
525+
445526test ( 'key-by' , ( ) => {
446527 let vars = variables ( [ { id : 1 } ] ) ;
447528 Core [ 'key-by' ] . apply ( [ ] , vars , CTX ) ;
0 commit comments